-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.logistic_loglin.R
288 lines (263 loc) · 14.7 KB
/
5.logistic_loglin.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
library(survey)
library(jtools)
options(survey.lonely.psu="adjust")
#-------European ------
ds_ll0 <- ISC_lvR %>% filter(!COUNTRY %in% c(CNTne, CNT2cne)) %>%
dplyr::select(all_of(Id), all_of(sampleID), all_of(Scales), all_of(Scalesb), all_of(Man_cate), all_of(Man_cont))
Man_cateSignf <- Man_cate[!grepl(paste0(c("T_HIGHEDFA", "T_HISCED"), collapse = "|"), Man_cate)]
#Selection of variables to be used in models for availability
Man_cateC2C3 <- Man_cateSignf[!grepl(paste0(c("T_HIGHEDFA", "T_HISCED"), collapse = "|"), Man_cateSignf)]
form <- paste0(paste(Man_cateC2C3, collapse = " + "))
#Special selection for cycle 1 for availability
Man_cateC1 <- Man_cateSignf[!grepl(paste0(c("T_HIGHEDEXP", "T_RELIG", "T_HISCED", "T_PROTES1"), collapse = "|"), Man_cateSignf)]
formC1 <- paste0(paste(Man_cateC1, collapse = " + "))
survey.designC1 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0[ds_ll0$cycle == "C1",], strata = ~IDJK, nest = TRUE)
survey.designC2 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0[ds_ll0$cycle == "C2",], strata = ~IDJK, nest = TRUE)
survey.designC3 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0[ds_ll0$cycle == "C3",], strata = ~IDJK, nest = TRUE)
###########################################################
#############Log linear regression / Poisson ###############
###########################################################
logl <- vector(mode = 'list', length = length(Scales))
Tlogl <- vector(mode = 'list', length = length(Scales))
for(i in 1:length(Scales)){
if(Scales[i] == "T_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")}
else {t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
logl[[i]][[j]] <- svyglm(as.formula(paste("as.numeric(",Scales[i],") ~", form )),
data = ds_ll0[ds_ll0$cycle == paste0("C",j),], family = poisson, design = Survey.design)
}
if (Scales[i] == "T_ETHNEQ5") Tlogl[[i]] <- tab_model(logl[[i]][[2]],logl[[i]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))))
else Tlogl[[i]] <- tab_model(logl[[i]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))))
names(Tlogl)[[i]] <- Scales[i]
}
rm(logl)
###########################################################
############# Ordinal regression #####################
###########################################################
ordl <- vector(mode = 'list', length = length(Scales))
Tordl <- list()
for(i in 1:length(Scales)){
if(Scales[i] == "T_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")}
else {t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
ordl[[i]][[j]] <- svyolr(as.formula(paste(Scales[i], "~", form )), design = Survey.design)
}
if (Scales[i] == "T_ETHNEQ5") Tordl[[i]] <- tab_model(ordl[[i]][[2]],ordl[[i]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))))
else Tordl[[i]] <- tab_model(ordl[[i]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = str_remove(sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))), "Moving/|<|>|Rights and Responsibilities/|Roles women and men/"))
names(Tordl)[[i]] <- Scales[i]
}
rm(ordl)
##############################################################
###################Logistic regression########################
##############################################################
logit <- vector(mode = 'list', length = length(Scalesb))
Tlogit <- list()
for(i in 1:length(Scalesb)){
if(Scalesb[i] == "bT_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")}
else {t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
logit[[i]][[j]] <- svyglm(as.formula(paste(Scalesb[i], "~", form )),
data = ds_ll0[ds_ll0$cycle == paste0("C",j),], family = binomial, design = Survey.design)
}
if (Scalesb[i] == "bT_ETHNEQ5") Tlogit[[i]] <- tab_model(logit[[i]][[2]],logit[[i]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))))
else Tlogit[[i]] <- tab_model(logit[[i]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = str_remove(sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))), "Moving/|<|>|Rights and Responsibilities/|Roles women and men/"))
names(Tlogit)[[i]] <- Scalesb[i]
}
rm(logit)
#-------Non European ------
ds_ll0ne <- ISC_lvR %>% filter(COUNTRY %in% c(CNTne, CNT2cne)) %>%
dplyr::select(all_of(Id), all_of(sampleID), all_of(Scales), all_of(Scalesb), all_of(Man_cate), all_of(Man_cont))
Man_cateSignf <- Man_cate[!grepl(paste0(c("T_HIGHEDFA", "T_HISCED"), collapse = "|"), Man_cate)]
#Selection of variables to be used in models for availability
Man_cateC2C3 <- Man_cateSignf[!grepl(paste0(c("T_HIGHEDFA", "T_HISCED"), collapse = "|"), Man_cateSignf)]
form <- paste0(paste(Man_cateC2C3, collapse = " + "))
#Special selection for cycle 1 for availability
Man_cateC1 <- Man_cateSignf[!grepl(paste0(c("T_HIGHEDEXP", "T_RELIG", "T_HISCED", "T_PROTES1"), collapse = "|"), Man_cateSignf)]
formC1 <- paste0(paste(Man_cateC1, collapse = " + "))
ScalesNe <- Scales
survey.designC1 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0ne[ds_ll0ne$cycle == "C1",], strata = ~IDJK, nest = TRUE)
survey.designC2 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0ne[ds_ll0ne$cycle == "C2",], strata = ~IDJK, nest = TRUE)
survey.designC3 <- svydesign(ids = ~IDCL, weights = ~SENWGT, data=ds_ll0ne[ds_ll0ne$cycle == "C3",], strata = ~IDJK, nest = TRUE)
###########################################################
#############Log linear regression / Poisson ###############
###########################################################
Nelogl <- vector(mode = 'list', length = length(ScalesNe))
NeTlogl <- list()
for(i in 1:length(ScalesNe)){
if(ScalesNe[i] == "T_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")} else {
t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")
if(grepl("T_IMMIEQ[1-9]",ScalesNe[i])) {t <- 1:2; dv <- c("CIVED 1999", "ICCS 2009")} else {t <- 1:3; dv <-c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
Nelogl[[i]][[j]] <- svyglm(as.formula(paste("as.numeric(",ScalesNe[i],") ~", form )),
data = ds_ll0ne[ds_ll0ne$cycle == paste0("C",j),], family = poisson, design = Survey.design)
}
if (ScalesNe[i] == "T_ETHNEQ5") NeTlogl[[i]] <- tab_model(Nelogl[[i]][[2]],Nelogl[[i]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNe[i]))))) else NeTlogl[[i]] <- tab_model(Nelogl[[i]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNe[i])))))
names(NeTlogl)[[i]] <- ScalesNe[i]
}
rm(Nelogl)
###########################################################
############# Ordinal regression #####################
###########################################################
Neordl <- vector(mode = 'list', length = length(ScalesNe))
NeTordl <- list()
for(k in 1:length(ScalesNe)){
if(ScalesNe[k] == "T_ETHNEQ5") {
t <- 2:3
dv <- c( "ICCS 2009", "ICCS 2016")} else {
t <- 1:3
dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")
if(grepl("T_IMMIEQ[1-9]",ScalesNe[k])) {t <- 1:2; dv <- c("CIVED 1999", "ICCS 2009")} else {
t <- 1:3; dv <-c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
Neordl[[k]][[j]] <- svyolr(as.formula(paste(ScalesNe[k], "~", form )), design = Survey.design)
}
if (ScalesNe[k] == "T_ETHNEQ5") NeTordl[[k]] <- tab_model(Neordl[[k]][[2]],Neordl[[k]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNe[k])))))
else NeTordl[[k]] <- tab_model(Neordl[[k]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = str_remove(sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNe[k])))), "Moving/|<|>|Rights and Responsibilities/|Roles women and men/"))
names(NeTordl)[[k]] <- ScalesNe[k]
}
rm(Neordl)
##############################################################
###################Logistic regression########################
##############################################################
ScalesNeb <- Scalesb
Nelogit <- vector(mode = 'list', length = length(ScalesNeb))
NeTlogit <- list()
for(i in 1:length(ScalesNeb)){
if(ScalesNeb[i] == "bT_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")}
else {t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")
if(grepl("bT_IMMIEQ[1-9]",ScalesNeb[i])) {t <- 1:2; dv <- c("CIVED 1999", "ICCS 2009")}else {t <- 1:3; dv <-c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
}
for (j in t) {
if(j==1) {
form <- formC1
Survey.design = survey.designC1
} else if(j==2) {
form <- form
Survey.design = survey.designC2
} else if(j==3) {
form <- form
Survey.design = survey.designC3
}
Nelogit[[i]][[j]] <- svyglm(as.formula(paste(ScalesNeb[i], "~", form )),
data = ds_ll0ne[ds_ll0ne$cycle == paste0("C",j),], family = binomial, design = Survey.design)
}
if (ScalesNeb[i] == "bT_ETHNEQ5") NeTlogit[[i]] <- tab_model(Nelogit[[i]][[2]],Nelogit[[i]][[3]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNeb[i])))))
else NeTlogit[[i]] <- tab_model(Nelogit[[i]], dv.labels = dv,
collapse.ci = TRUE, p.style = "stars", title = str_remove(sjlabelled::get_label(eval(parse(text=paste0("ds_ll0ne$",ScalesNeb[i])))), "Moving/|<|>|Rights and Responsibilities/|Roles women and men/"))
names(NeTlogit)[[i]] <- ScalesNeb[i]
}
rm(Nelogit)
#--------
##############################################################
################Multinomial regression########################
##############################################################
# library(nnet)
# multl <- vector(mode = 'list', length = length(Scales))
# Tmultl <- list()
# for(i in 1:length(Scales)){
# if(Scales[i] == "T_ETHNEQ5") {t <- 2:3; dv <- c( "ICCS 2009", "ICCS 2016")}
# else {t <- 1:3; dv <- c("CIVED 1999", "ICCS 2009", "ICCS 2016")}
# for (j in t) {
# if(j==1) {
# form <- formC1
# Survey.design = survey.designC1
# } else if(j==2) {
# form <- form
# Survey.design = survey.designC2
# } else if(j==3) {
# form <- form
# Survey.design = survey.designC3
# }
#
# multl[[i]][[j]] <- multinom(as.formula(paste(Scales[i], "~", form )), data = ds_ll0[ds_ll0$cycle == paste0("C",j),], design = Survey.design)
# }
# if (Scales[i] == "T_ETHNEQ5") Tmultl[[i]] <- tab_model(multl[[i]][[2]],multl[[i]][[3]], dv.labels = dv,
# collapse.ci = TRUE, p.style = "stars", title = sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))))
# else Tmultl[[i]] <- tab_model(multl[[i]][[i]], dv.labels = dv,
# collapse.ci = TRUE, p.style = "stars", title = str_remove(sjlabelled::get_label(eval(parse(text=paste0("ds_ll0$",Scales[i])))), "Moving/|<|>|Rights and Responsibilities/|Roles women and men/"))
# names(Tmultl)[[i]] <- Scales[i]
# }
# rm(multl)
#
#
# des2<-as.svrepdesign(survey.designC3, type="bootstrap" , replicates=10)
# mfit<-withReplicates(des2, quote(coef(multinom(as.formula(paste(Scales[i], "~", form )), weights=.weights, trace=F ))))
# mfitcoef<-data.frame(matrix(attr(attr(mfit, "var"), "means")[-1:-4], nrow=4, ncol=length(Man_cateC2C3), byrow=F))
# names(mfitcoef)<-names(coef(ex1)[-1])
# round(exp(mfitcoef), 3) # odds ratios
# mlt_GNDREQ1C1 <- multinom(as.formula(paste("as.numeric(bT_GNDREQ1) ~", form )),
# data = ds_ll0[ds_ll0$cycle == "C1",], weights=SENWGT)
# mlt_GNDREQ1C2 <- multinom(as.formula(paste("as.numeric(bT_GNDREQ1) ~", form )),
# data = ds_ll0[ds_ll0$cycle == "C2",], weights=SENWGT)
# mlt_GNDREQ1C2 <- multinom(as.formula(paste("as.numeric(bT_GNDREQ1) ~", form )),
# data = ds_ll0[ds_ll0$cycle == "C3",], weights=SENWGT)
# mlt_GNDREQ2C1 <- multinom(as.formula(paste("as.numeric(bT_GNDREQ2) ~", form )),
# data = ds_ll0[ds_ll0$cycle == "C1",], weights=SENWGT)
# ml11 <- tab_model(mlt_GNDREQ1, collapse.ci = TRUE, p.style = "stars", auto.label = FALSE,
# dv.labels = c(str_remove(attributes(ds_ll0$T_GNDREQ1)$label, "Rights and Responsibilities/Roles women and men/")))
#
# ml12 <- tab_model(mlt_GNDREQ2, collapse.ci = TRUE, p.style = "stars", auto.label = FALSE,
# dv.labels = c(str_remove(attributes(ds_ll0$T_GNDREQ2)$label, "Rights and Responsibilities/Roles women and men/")))