###packages####
install.packages("foreign", dependencies = TRUE)
library(foreign)
install.packages("ggplot2")
library(ggplot2)
install.packages("car")
library(car)
install.packages("nnet")
library(nnet)
install.packages("mctest")
library(mctest)
install.packages("ordinal")
library(ordinal)
install.packages("mlogit")
library(mlogit)



####data###

MyData <- read.csv(file="RIO_Schmidtke Data.csv", header=TRUE, sep=";")



###Figures###


MyData <- read.csv(file="RIO_Schmidtke Data.csv", header=TRUE, sep=";")

p2 <- ggplot(MyData) + 
  geom_point(aes(x = Year, y = IO_Country, colour = recode(MyData$Pattern_ordered, recodes = "1 = 'low-intensity legitimation'; 2 = 'low-intensity delegitimation'; 3 = 'high-intensity legitimation'; 4 = 'high-intensity delegitimation'")), size = 17, shape = 15) + theme_bw() + 
  ylab("") + xlab("") + 
  ggtitle("") + 
  theme(text = element_text(size = 12)) + 
  theme(legend.position = "bottom", text=element_text(size=25)) + 
  guides(colour = guide_legend(nrow = 2))+
  scale_color_manual("Patterns of elite legitimacy communication", values = c("grey1", "grey30", "grey65", "grey85"))+
  scale_x_continuous(breaks=seq(1998,2013,1))
p2

####multinomial regression analysis####
MyData <- read.csv(file="RIO_Schmidtke Data.csv", header=TRUE, sep=";")

### changes DV to a factor variable####
MyData2 <- transform(MyData,leg_pat_mean = factor(leg_pat_mean) )

###sets low-intensity legitimation as base###
MyData3 <- within(MyData2, leg_pat_mean <- relevel(leg_pat_mean, ref= 3))

###the regression equation###
REG=multinom(leg_pat_mean~ speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=MyData3) 
summary (REG)

###calculates standard errors###
z <- summary(REG)$coefficients/summary(REG)$standard.errors
z


###2-tailed z test###
p <- (1-pnorm(abs(z),0,1))*2
p

###calculates the exponentiated coefficients###
exp(coef(REG))


###predicted probabilities###

prob=predict(REG)
prob

###classifiaction table###

ctable=table(prob,MyData$leg_pat_mean)
ctable=t(ctable)
ctable


###Diagnostics and Robustness Checks###

###Multicolinarity tests###

sub=c("speaker_pol","context_sec","context_inst", "HM_authority_zs", "nat_LIzscore", "nat_LLzscore",  "Member")
sub
newdata=MyData[sub]
attach(newdata)
mctest(x=newdata,y=speaker_pol, type="i", corr=TRUE)


###Hausman Test###
###the same regression with mnlogit(required to perform the Hausman Test)###

MyData <- read.csv(file="RIO_Schmidtke Data.csv", header=TRUE, sep=";")
datalong <- mlogit.data(MyData,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model1 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong, reflevel = 3)
summary (model1)
expmodel1 <- exp(coef(model1))
expmodel1

###independence of irrelevant alternatives (IIA) Hausman-McFadden test###
newdata <- subset(MyData, leg_pat_mean < 4) ##creates a subset of the data without HIL, this is required to perform the Hausman-McFadden test###
datalong.sub <- mlogit.data(newdata,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.sub <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.sub, reflevel = 3) ##compute the model with the subset data###
summary (model.sub)
expmodel.sub <- exp(coef(model.sub))
expmodel.sub

hmftest(model1,model.sub)###computes the Hausman-McFadden Test for the model and a model which excludes one category of the DV###

newdata2 <- subset(MyData, leg_pat_mean >1) ##creates a subset of the data without HID, this is required to perform the Hausman-McFadden test###
datalong.sub2 <- mlogit.data(newdata2,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.sub2 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.sub2, reflevel = 3) ##compute the model with the subset data###
summary (model.sub2)
expmodel.sub2 <- exp(coef(model.sub2))
expmodel.sub2

hmftest(model1,model.sub2)###computes the Hausman-McFadden Test for the model and a model which excludes one category of the DV###


newdata3 <- subset(MyData, leg_pat_mean!= 2) ##creates a subset of the data without LID, this is required to perform the Hausman-McFadden test###
datalong.sub3 <- mlogit.data(newdata3,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.sub3 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.sub3, reflevel = 3) ##compute the model with the subset data###
summary (model.sub2)
expmodel.sub2 <- exp(coef(model.sub2))
expmodel.sub2

hmftest(model1,model.sub3)###computes the Hausman-McFadden Test for the model and a model which excludes one category of the DV###

####ROBUSTNESS####

##Rerun the Model with legitimation communication patterns classified on the basis of median legitimation intensity and tone###
datalong_median <- mlogit.data(MyData,shape = "wide",choice = "leg_pat_median") ###transforms the data into long format needed for mlogit###
model1_median <- mlogit(leg_pat_median ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong_median, reflevel = 3)
summary (model1_median)



###Rerun the model with robust,clustered standard errors###
# load in a function to create clustered standard errors for mlogit models
# initial code by Mahmood Ara: http://thetarzan.wordpress.com/2011/06/11/clustered-standard-errors-in-r/
# slightly modified for mlogit models by Justin Esarey on 3/3/2015####

cl.mlogit   <- function(fm, cluster){
  
  # fm: a fitted mlogit model
  # cluster: a data vector with the cluster
  #          identity of each observation in fm
  
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- length(coefficients(fm))
  # edit 6/22/2015: change dfc
  # dfc <- (M/(M-1))*((N-1)/(N-K))
  dfc <- (M/(M-1))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat.=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

#### Compute the model with Robust Clustered Standard Errors on IO level###
model1 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong, reflevel = 3)
summary (model1)

model1.cl1 <- cl.mlogit(model1,MyData$IO)
model1.cl1

#### Compute the model with Robust Clustered Standard Errors on Country level###
model1 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong, reflevel = 3)
summary (model1)

model1.cl2 <- cl.mlogit(model1,MyData$Country)
model1.cl2

###Check for secular trends (split the dataset into two time periods 1998-2005 and 2006-2013)
###period 1998-2005###
period1 <- subset(MyData, Year<2006) ##creates a subset of the data ###
datalong.period1 <- mlogit.data(period1,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.period1 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.period1, reflevel = 3) ##compute the model with the subset data###
summary (model.period1)
expmodel.period1 <- exp(coef(model.period1))
expmodel.period1

###period 2006-2013###
period2 <- subset(MyData, Year>2005) ##creates a subset of the data ###
datalong.period2 <- mlogit.data(period2,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.period2 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.period2, reflevel = 3) ##compute the model with the subset data###
summary (model.period2)
expmodel.period2 <- exp(coef(model.period2))
expmodel.period2

###Check for spatial bias (IOs and countries) with jackknife####
###exclude IOs###
##exclude EU##
jack1 <- subset(MyData, IO >2) ##creates a subset of the data ###
datalong.jack1 <- mlogit.data(jack1,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack1 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack1, reflevel = 3) ##compute the model with the subset data###
summary (model.jack1)
expmodel.jack1 <- exp(coef(model.jack1))
expmodel.jack1

##exclude G8##
jack2 <- subset(MyData, IO %in% c(2,4)) ##creates a subset of the data ###
datalong.jack2 <- mlogit.data(jack2,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack2 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack2, reflevel = 3) ##compute the model with the subset data###
summary (model.jack2)
expmodel.jack2 <- exp(coef(model.jack2))
expmodel.jack2

##exclude UN##
jack3 <- subset(MyData, IO < 4) ##creates a subset of the data ###
datalong.jack3 <- mlogit.data(jack3,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack3 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack3, reflevel = 3) ##compute the model with the subset data###
summary (model.jack3)
expmodel.jack3 <- exp(coef(model.jack3))
expmodel.jack3

###exclude countries###
##exclude CH##
jack4 <- subset(MyData, Country >1) ##creates a subset of the data ###
datalong.jack4 <- mlogit.data(jack4,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack4 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack4, reflevel = 3) ##compute the model with the subset data###
summary (model.jack4)
expmodel.jack4 <- exp(coef(model.jack4))
expmodel.jack4

##exclude DE##
jack5 <- subset(MyData, Country %in% c(1,3,4)) ##creates a subset of the data ###
datalong.jack5 <- mlogit.data(jack5,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack5 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack5, reflevel = 3) ##compute the model with the subset data###
summary (model.jack5)
expmodel.jack5 <- exp(coef(model.jack5))
expmodel.jack5

##exclude GB##
jack6 <- subset(MyData, Country %in% c(1,2,4)) ##creates a subset of the data ###
datalong.jack6 <- mlogit.data(jack6,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack6 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack6, reflevel = 3) ##compute the model with the subset data###
summary (model.jack6)
expmodel.jack6 <- exp(coef(model.jack6))
expmodel.jack6

##exclude US##
jack7 <- subset(MyData, Country <4) ##creates a subset of the data ###
datalong.jack7 <- mlogit.data(jack7,shape = "wide",choice = "leg_pat_mean") ###transforms the data into long format needed for mlogit###
model.jack7 <- mlogit(leg_pat_mean ~ 1 | speaker_pol + context_sec + context_inst + HM_authority_zs + nat_LIzscore + nat_LLzscore + Member, data=datalong.jack7, reflevel = 3) ##compute the model with the subset data###
summary (model.jack7)
expmodel.jack7 <- exp(coef(model.jack7))
expmodel.jack7

