## Replication codes for "Satisfied or Not? Exploring the Interplay of Individual, Country and International Organization Characteristics for Negotiation Success"


setwd("X:/DFG2/papers/M2 Paper 2")

library(foreign)

data <- read.csv(file = "r_data.csv")


library(scales)
library(ordinal)
library(tidyverse)
library(ggplot2)
library(ggeffects)
library(ggpubr)
library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(stargazer)
library(texreg)




##codes for generating graphs in the paper

## Graph 1 - DV - outcome satisfaction in percentage

data$v_135 <- factor(data$v_135,
                    levels = c(1,2,3,4,5),
                    labels = c("Least", "2", "3","4","Most"))

graph1 <- ggplot(subset(data, !is.na(v_135)), aes(v_135))+
  geom_bar(aes(y = (..count..)/sum(..count..)), fill = "steelblue") + 
  scale_y_continuous(labels=scales::percent) +
  xlab("Outcome Satisfaction")+
  ylab("")+
  theme_pubclean()

graph1 ##add labs(caption = "N:997") to add a note on the graph


## Graph 2 - DV - outcome satisfaction over different IOs
data$order <- factor(data$v_135, c("Least", "2", "3","4","Most")) # run this code before running the codes for the plot

graph2 <- ggplot(subset(data, !is.na(v_135)), mapping = aes(x = u_io_abbrev, fill=v_135))+
  geom_bar(aes(y = (..count..)/sum(..count..)), position="fill") +
  scale_fill_manual(values = c("Least" = "#ffffcc", "2" = "#a1dab4", "3" = "#41b6c4", "4"="#2c7fb8", "Most"="#253494"))+
  scale_y_continuous(labels=scales::percent) +
  labs(fill="Satisfaction")+
  ylab(NULL)+
  xlab(NULL)+
  theme(axis.text.x = element_text(angle=70, vjust=0.7), panel.background = element_blank(), # have 70 degree angle for a better read
        panel.grid.major.x = element_line(size = 0.2, linetype = 'dotted', colour = "blue"), # draw lines btw the bars and IO names
        legend.position="top", legend.box = "horizontal",
        legend.background=element_blank(),
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(-6,-6,-6,-6)) #  get rid of the unnecessary spacing btw the plot and the legend 

graph2



#essential var transformations prior to running any of our models #

proposal <- data$M_makingproposals*10
io_size <- data$N_2016/10
dvvar <- as.factor(data$v_135)


#interaction models & plots used in the paper, as well as in the appendix
set_theme(base = theme_blank(), 
          title.size=0.1, 
          legend.pos = "top", 
          legend.title.face = "plain",
          axis.linecolor.x = "black", 
          axis.tickslen = 0, # hides tick marks
          axis.title.size = .9,
          axis.textsize = .9,
          legend.size = .7,
          legend.title.size = .8
          )
          

model1 <- clmm(dvvar ~ v_136 + mean_v187 + v_136*mean_v187 + (1 | u_io_abbrev), data = data, na.action = na.omit, 
               control = clmm.control(maxIter = 200L, maxLineIter = 20000, gradTol = 1e-3))


plot_model(model1, type = "pred", terms = c("mean_v187", "v_136[1,5]"), title="", 
           axis.title = c("Extent of Arguing","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")


model2 <-  clmm(dvvar ~ mean_v193 + v_136 + v_136*mean_v193 + (1 | u_io_abbrev), data = data, na.action = na.omit, 
               control = clmm.control(maxIter = 200L, maxLineIter = 20000, gradTol = 1e-3))

plot_model(model2, type = "pred", terms = c("mean_v193", "v_136[1,5]"), title="", 
           axis.title = c("Extent of Bargaining","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")


model3 <- clmm(dvvar ~  v_136 + io_size + v_136*io_size + (1 | u_io_abbrev), data = data, na.action = na.omit, control = clmm.control(maxIter = 200L, maxLineIter = 20000, gradTol = 1e-3)) 

plot_model(model3, type = "pred", terms = c("io_size", "v_136[1,5]"),  title="", 
           axis.title = c("IO Size","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")


model4 <- clmm(dvvar ~ v_136 +  MB_D_consensus  + v_136*MB_D_consensus + (1 | u_io_abbrev), data = data, na.action = na.omit, control = clmm.control(maxIter = 200L, maxLineIter = 20000, gradTol = 1e-3)) 

plot_model(model4, type = "pred", terms = c("MB_D_consensus", "v_136[1,5]"),  title="", 
           axis.title = c("Consensus Rule","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")



model5 <- clmm(dvvar ~ v_136 + proposal + v_136*proposal + (1 | u_io_abbrev), data = data, na.action = na.omit)

plot_model(model5, type = "pred", terms = c("proposal", "v_136[1,5]"),  title="", 
           axis.title = c("Fostering Debates","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")



model6 <- clmm(dvvar ~  v_136 + M_interruptdiscussions + v_136*M_interruptdiscussions + (1 | u_io_abbrev), data = data, na.action = na.omit, control = clmm.control(maxIter = 200L, maxLineIter = 20000, gradTol = 1e-3)) 

plot_model(model6, type = "pred", terms = c("M_interruptdiscussions", "v_136[1,5]"),  title="", 
           axis.title = c("Interruptions of Debates","      High Satisfaction                              Moderate Satisfaction"), 
           legend.title = "Flexibility", colors = "bw")
