###These are the necessary functions: ###Libraries #require (foreign) require (ggplot2) ### 1.Function to make and format numbers. It takes a character vector or a factor and it ## outputs a number. The digits argument specifies the number of digits. make.number<-function (x, y) {x<-round(as.numeric(as.character(x)), digits=y) return(x)} ### 2. Function to compute the mean government position for any variable ## The function takes as input the name of the Manifesto variable (party position). ## The list is available here https://manifesto-project.wzb.eu/data/MPDataset_full_codebook.pdf ## By default, the function computes the weighted average position (each party position is weighted by the share of the party from the total of the government coalition) ## If weighted is switched off, a simple average of the governing parties' positions is computed party.position<-function (x, weighted=TRUE) { t<-unique(cabinets$cabinet_id) #get the list for all cabinets m<-matrix (NA, length(t), 3+length(x)) #construct a matrix that has as many columns as unique cabinets and four columns: three indicators for coutnry, start and end date, and the position itslef for (i in 1:length(t)){ #for all cabinets j<-t[i] #get the cabinet id for ease temp<-subset(cabinets, cabinets$cabinet_id==j & cabinets$cabinet_party==1) #filter only the records relevant for this cabinet and the parties in the cabinet temp2<-subset(cabinets, cabinets$previous_cabinet_id==j) #filter only the records for the previous cabinet m[i,1]<-temp[1,colnames(temp)=='countryname'] #country name m[i,2]<-temp[1,colnames(temp)=='start_date'] #start date m[i,3]<-temp2[1,colnames(temp)=='start_date'] #end date: search for the record that has the current cabinet number as the previous cabinet, and put its starting date if (weighted==FALSE) { m[i,4]<-mean(temp[,colnames(temp)==x], na.rm=T)} #unweighted mean party position else { m[i,4]<-weighted.mean(temp[,colnames(temp)==x], temp$seats_share)} #weighted mean party position } m<-as.data.frame(m) colnames(m)<-c('Country','start.date','end.date',x) m<-subset(m, is.na(m$Country)==F) m[,4]<-round(as.numeric(as.character((m[,4]))), digits=4) ###Format the dates of the data m$startY<-format(as.Date(m$start.date, format='%Y-%m-%d'), format='%Y') #cabinet start year m$startM<-format(as.Date(m$start.date, format='%Y-%m-%d'), format='%m') #cabinet start month m$startF<-format(as.Date(m$start.date, format='%Y-%m-%d'), format='%Y.%m') #cabinet start year/month m$endY<-format(as.Date(m$end.date, format='%Y-%m-%d'), format='%Y') #cabinet end year m$endM<-format(as.Date(m$end.date, format='%Y-%m-%d'), format='%m') #cabinet end month m$endF<-format(as.Date(m$end.date, format='%Y-%m-%d'), format='%Y.%m') #cabinte end year/month return(m) } ### 3. Function to prepare a list of year with the associated government positions ## The function takes as input the data frame returned from the function party.position(). ## If long=True a long data frame (county/year as the row) is returned. If long=F a wide data frame is returned with the coutnries as rows and the years as columns ## the start and end argumetns specify the start and end years of the extraction party.position.yearly <-function (m, long=TRUE, start=1945, end=2010) { u.c<-unique(m$Country)[-c(5,17,27,32,33)] #exclude non-Euroepan countries v<-seq(start,end, 1) #start and end years year.table<-matrix(NA, length(u.c),length(v)+1 ) #prepare the matrix with rows the number of countries and columns the nubmer of years plus 1 for (i in 1:length(u.c)) { #for each country m.temp<-subset(m, m$Country==u.c[i]) #subset only the relevant observations year.table[i,1]<-as.character(u.c[i]) #record the name of the country for ( j in 1:length(v)) { #for each year m.temp2<-subset(m.temp, m.temp$startY<=v[j] & m.temp$endY>=v[j] ) #subset only the cabinets that have started before and have no yet ended if (nrow(m.temp2)==0) next #if not applicabel move one else if (nrow(m.temp2)==1) #if only one such cabinet year.table[i, j+1]<-m.temp2[1,4] #get its position and record it else if (nrow(m.temp2)==2) { m.temp2<-m.temp2[order(m.temp2$endF) , ] z.v<-vector(mode='numeric',2) z.v[2]<-12-make.number(m.temp2$startM[2]) z.v[1]<-12-z.v[2] year.table[i, j+1]<-sum(m.temp2[,4]*(z.v/12))} else if (nrow(m.temp2)>1){ #if more than 1 cabinet was active during the year m.temp2<-m.temp2[order(m.temp2$endF) , ] z<-nrow(m.temp2) #get their number z.v<-vector(mode='numeric',z) #make a vector of that length z.v[z]<-12-make.number(m.temp2$startM[z]) #record the number of months of the cabinet being active for (k in (z-1):2) {z.v[k]<-make.number(m.temp2$startM[k+1])-make.number(m.temp2$startM[k])} z.v[1]<-12-sum(z.v[-1]) year.table[i, j+1]<-sum(m.temp2[,4]*z.v/12)} #create the weighted yearly position } } year.table<-as.data.frame(year.table) colnames(year.table)<-c('Country',v) for (g in 2:NCOL(year.table)){year.table[,g]<-make.number(year.table[,g],2)} if (long){ cab.table<-melt(year.table, id='Country') colnames(cab.table)<-c("State","Year","position") cab.table$id<-paste(cab.table$Year, cab.table$State, sep=".")} else {cab.table<-year.table} return (cab.table) } #### 4.The same as above but on a monthly basis party.position.monthly <-function (m, long=TRUE, start=1945, end=2010) { u.c<-unique(m$Country)[-c(5,17,27,32,33)] #exclude non-Euroepan countries v1<-seq(start,end, 1) #start and end years v2<-seq(0.01,0.12,0.01) v<-rep(NA, length(v1)*12) for (i in 1:length(v1)) {v[(1+12*(i-1)):(12+12*(i-1))]<-v1[i]+v2} year.table<-matrix(NA, length(u.c),length(v)+1 ) #prepare the matrix with rows the number of countries and columns the nubmer of years plus 1 for (i in 1:length(u.c)) { #for each country m.temp<-subset(m, m$Country==u.c[i]) #subset only the relevant observations year.table[i,1]<-as.character(u.c[i]) #record the name of the country for ( j in 1:length(v)) { #for each year m.temp2<-subset(m.temp, m.temp$startF<=v[j] & m.temp$endF>v[j] ) #subset only the cabinets that have started before and have no yet ended if (nrow(m.temp2)==0) next #if not applicable move on else year.table[i, j+1]<-m.temp2[1,4] #get its position and record it } } year.table<-as.data.frame(year.table) colnames(year.table)<-c('Country',v) for (g in 2:NCOL(year.table)){year.table[,g]<-make.number(year.table[,g],2)} if (long){ cab.table<-melt(year.table, id='Country') colnames(cab.table)<-c("State","Year.month","position") cab.table$id<-paste(cab.table$Year.month, cab.table$State, sep=".")} else {cab.table<-year.table} return (cab.table) } #### 5.Master function for the extraction of manifesto positions for governments. ## Requires the functions party.position() and party.position.yearly() ## The function takes as input the name of the Manifesto variable (party position). ## The list is available here https://manifesto-project.wzb.eu/data/MPDataset_full_codebook.pdf ## By default, the function computes the weighted average position (each party position is weighted by the share of the party from the total of the government coalition) ## If weighted is switched off, a simple average of the governing parties' positions is computed ## If long=True a long data frame (county/year as the row) is returned. If long=F a wide data frame is returned with the coutnries as rows and the years as columns ## the start and end argumetns specify the start and end years of the extraction ## the argument 'period' has two values - 'year' (default) and 'month' which extracts the positions for every month ## N.B. the file 'end dates for the cabinets2.r' is needed to supply the end date of the last governments featured in the Manifesto database manifesto.position <-function (x, weighted=T, period='year', long=T, start=1945, end=2010) { m<-party.position (x, weighted=weighted) assign("m", m, .GlobalEnv) #source('end dates for the cabinets2.r') source ('http://www.dimiter.eu/Data_files/cabinets/end%20dates%20for%20the%20cabinets2.r') assign("m", m, .GlobalEnv) if(period=='month') { t<-party.position.monthly(m, long=long, start=start, end=end) } else t<-party.position.yearly(m, long=long, start=start, end=end) return(t) }