-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.R
252 lines (191 loc) · 8.71 KB
/
models.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# libraries survival analysis
library(survival)
library(survminer)
library(survMisc)
library(simPH)
library(flexsurv)
# libraries general
library(tidymodels)
library(tidyverse)
library(MASS)
library(lubridate)
library(Amelia)
library(xtable)
library(haven)
library(foreign)
library(broom)
library(psych)
library(Hmisc)
library(expss)
library(rockchalk)
library(effects)
library(modeldata)
library(randomForest)
library(compare)
library(psych)
library(desc)
library(PerformanceAnalytics)
library(reshape2)
###########################################
# loading: in case it is needed
## BerJoi <- load(file = "/home/fw/Documents/GitHub Projects/CabTerm/BerJoi.RData")
# a first test of cox models with the relevant df and vars
attach(BerJoi)
# help("Surv")
#
## for safety reasons, df is specified here
SurvObj <- Surv(BerJoi$abs_dur, event = BerJoi$discr2019)
# base syntax coxph
# help("coxph")
## kaplan meier model - DISCR19
kmModFULL <- survfit(SurvObj ~ 1)
summary(kmModFULL)
## kaplan meier plot - discr19
plot(kmModFULL, conf.int = F, xlab = "Time of Survival (in days)", ylab = "% of Cabinets 'alive'",
main = "Kaplan-Meier-Plot, pooled hazards")
# # test for full model, whole df - syntax reference
# baseCPH <- coxph(data = BerJoi, SurvObj ~ 1)
# fullCPH <- coxph(data = BerJoi, SurvObj ~ .)
###########################################
# subsetting
subset1 <- subset(BerJoi, select = c("abs_dur", "discr2019", "minor_coalition",
"one_party_minor", "post_election_cabinet","num_cabparties", "max_dur",
"eff_numb_parties", "gov_comp", "right_seat", "comm_seat",
"rl_range", "polarization_bpw", "antisys_seat",
"positive_parl", "pm_diss_pow", "bicameralism",
"semi_presidentialism", "same_pm", "cab_barg_duration"))
#######
# subset with independent vars only for correlations
subset2 <- subset(BerJoi, select = c("num_cabparties", "max_dur",
"eff_numb_parties", "gov_comp", "right_seat", "comm_seat",
"rl_range", "polarization_bpw", "antisys_seat",
"positive_parl", "pm_diss_pow", "bicameralism",
"semi_presidentialism", "same_pm", "cab_barg_duration"))
detach(BerJoi)
#
fre(subset1$one_party_minor)
fre(subset1$minor_coalition)
fre(subset1$post_election_cabinet)
# sub surv object
Surv(subset1$abs_dur, event = subset1$discr2019)
# kaplan plots
subKap <- survfit(Surv(subset1$abs_dur, event = subset1$discr2019) ~ 1)
plot(subKap, conf.int = F, xlab = "Time of Survival (in days)", ylab = "% of Cabinets 'alive'",
main = "Kaplan-Meier-Plot for subset1, pooled hazards")
# testmodel cox subset1 (for syntax reference)
testSub <- coxph(data = subset1, Surv(subset1$abs_dur, event = subset1$discr2019) ~ . - comm_seat - pm_diss_pow - gov_comp - bicameralism - post_election_cabinet - minor_coalition - one_party_minor)
summary(testSub)
cox.zph(testSub)
ggcoxzph(cox.zph(testSub))
# test for correlations between vars
attach(subset1)
PerformanceAnalytics::chart.Correlation(subset1)
# basic corr matrix
cormat <- cor(subset1 - one_party_minor - minor_coalition - post_election_cabinet)
meltedCormat <- reshape2::melt(cormat)
ggplot(data = meltedCormat, aes(x = Var1, y = Var2, fill = value)) + geom_raster()
# corr matrix: upper and lower triangular parts of matrices
## function lower tri
get_lower_tri <- function(cormat){
cormat[upper.tri(cormat)] <- NA
return(cormat)
}
## function upper tri
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)] <- NA
return(cormat)
}
## use functions
lowerTri <- get_lower_tri(cormat)
upperTri <- get_upper_tri(cormat)
# melting upperTri
meltUpper <- reshape2::melt(upperTri, na.rm = T)
meltLower <- reshape2::melt(lowerTri, na.rm = T)
# better heatmap [see this article](http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization)
plotUpperTri <- ggplot(data = meltUpper, aes(Var2, Var1, fill = value)) +
geom_raster(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
#############
plotLowerTri <- ggplot(data = meltLower, aes(Var2, Var1, fill = value))+
geom_raster(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
###############################################
# basic corr matrix - just independent vars
cormat <- cor(subset2)
meltedCormat <- reshape2::melt(cormat)
ggplot(data = meltedCormat, aes(x = Var1, y = Var2, fill = value)) + geom_raster()
## use functions for triangular matrix parts on indep vars
lowerTri <- get_lower_tri(cormat)
upperTri <- get_upper_tri(cormat)
# melting upperTri
meltUpper <- reshape2::melt(upperTri, na.rm = T)
meltLower <- reshape2::melt(lowerTri, na.rm = T)
# better heatmap [see this article](http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization)
plotUpperTri <- ggplot(data = meltUpper, aes(Var2, Var1, fill = value)) +
geom_raster(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
#############
plotLowerTri <- ggplot(data = meltLower, aes(Var2, Var1, fill = value))+
geom_raster(color = "white")+
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Pearson\nCorrelation") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 12, hjust = 1))+
coord_fixed()
############################################### model STRUCTURE
attach(BerJoi)
detach(BerJoi)
###############################################
# subset STRUCTURE
subSTRUCT <- subset(BerJoi, select = c("discr2019", "abs_dur", "num_cabparties", "max_dur", "eff_numb_parties"))
##########
survSTRUCT <- Surv(subSTRUCT$abs_dur, event = subSTRUCT$discr2019)
summary(coxph(data = subSTRUCT, survSTRUCT ~ . - discr2019 - abs_dur))
cox.zph(coxph(data = subSTRUCT, survSTRUCT ~ . - discr2019 - abs_dur))
# sig: effective number of parliament. parties significant - the more parties, the higher the risk of disc2019
############################################### model PREFERENCES
subPREF <- subset(BerJoi, select = c("discr2019", "abs_dur", "rl_polar", "right_seat",
"comm_seat", "rl_range", "polarization_bpw", "antisys_seat"))
##########
survPREF <- Surv(subPREF$abs_dur, event = subPREF$discr2019)
summary(coxph(data = subPREF, survPREF ~ . - discr2019 - abs_dur))
cox.zph(coxph(data = subPREF, survPREF ~ . - discr2019 - abs_dur))
# sig: left party seat share: the higher the amount of left seats, the higher the risk
############################################### model INSTITUTIONS
subINST <- subset(BerJoi, select = c("discr2019", "abs_dur", "positive_parl", "pm_diss_pow",
"bicameralism", "semi_presidentialism"))
##########
survINST <- Surv(subINST$abs_dur, event = subINST$discr2019)
summary(coxph(data = subINST, survINST ~ . - discr2019 - abs_dur))
cox.zph(coxph(data = subINST, survINST ~ . - discr2019 - abs_dur))
# sig: PM dissolution power: the higher the PM diss pow, the lower the risk
############################################### model BARGAINING
subBARG <- subset(BerJoi, select = c("discr2019", "abs_dur", "same_pm", "cab_barg_duration", "max_dur"))
##########
survBARG <- Surv(subBARG$abs_dur, event = subBARG$discr2019)
summary(coxph(data = subBARG, survBARG ~ . - discr2019 - abs_dur))
cox.zph(coxph(data = subBARG, survBARG ~ . - discr2019 - abs_dur))
# sig: borderline significant: same PM - when same PM in cabinet, the lower the risk
# sig: cabinet bargaining duration: the longer it takes, the higher the risk of failure
############################################### model CRITICAL EVENTS
# subCE <-