-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWork3_gee modeling.R
143 lines (91 loc) · 4.05 KB
/
Work3_gee modeling.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
########## loading packages ##########
library(dplyr)
library(sjlabelled)
library(geepack)
library(MuMIn)
library(broom)
library(moonBook)
library(flextable)
library(texreg)
# remotes::install_github("sarbearschwartz/texreghelpr")
library(texreghelpr)
setwd("~/Desktop/Yonsei/Semester_2/Probabilistic Graphical Model/Final")
iadata <- read.csv("iadata.csv")
iadata2 <- iadata %>% arrange(N_ID, year)
id <- iadata2$N_ID
y <- iadata2$IA - 1
X <- iadata2 %>% select(-c(N_ID, IA)) %>% transmute_all(., as.factor)
d <- cbind(id, y, X)
colnames(d)[1:2] <- c("N_ID", "IA")
## fitting GEEs with different correlation structures
M1 <- geeglm(IA ~ year + peridep + mdep + gender + smart +
happy + sleep + health + CBCL + family + income + empstatus,
id = N_ID, data = d, family = binomial(link=logit),
corstr = 'independence')
M2 <- geeglm(IA ~ year + peridep + mdep + gender + smart +
happy + sleep + health + CBCL + family + income + empstatus,
id = N_ID, data = d, family = binomial(link=logit),
corstr = 'exchangeable')
MuMIn::QIC(M1, M2, typeR = TRUE)
## Fitting GEEs with important predictors only
M3 <- geeglm(IA ~ peridep + mdep + smart + CBCL + income + empstatus,
id = N_ID, data = d, family = binomial(link=logit),
corstr = 'exchangeable')
summary(M3)
########## Making some edits to the gee_stepper function in the "pstools" package ##########
gee_stepper <- function(fit, upper) {
preds <- all.vars(as.list(upper)[[3]])
preds_out <- preds
preds_in <- character(0)
fit0 <- stats::update(fit, formula = . ~ 1)
while(TRUE) {
fits <-
c(list(fit0),
lapply(preds_out, function(p) {stats::update(fit0, formula = stats::as.formula(paste(". ~ . +", p))) }),
lapply(preds_in, function(p) {stats::update(fit0, formula = stats::as.formula(paste(". ~ . -", p))) })
)
minqic <- which.min(sapply(fits, function(x) {MuMIn::QIC(x, typeR = TRUE)}))
fit0 <- fits[[minqic]]
status <-
dplyr::data_frame(preds = sapply(fits, function(f) {as.character(stats::formula(f))[3]}),
qic = sapply(fits, function(x) {MuMIn::QIC(x, typeR = TRUE)}),
pick = seq_along(fits) == minqic)
preds_in <- all.vars(as.list(stats::formula(fit0))[[3]])
preds_out <- dplyr::setdiff(preds, preds_in)
print(status)
if (minqic == 1) {
break
}
}
fit0
}
sapply(fits, function(f) {as.character(stats::formula(f))[3]})
########## Variable selection ##########
# Option 1) Stepwise selection
stepwise <- gee_stepper(M2, upper = formula(M2))
stepwise$coefficients
# Option 2) Comparing all the subsets of variables (accurate, but computaionally intensive)
# M2 <- update(M2, na.action = 'na.fail')
# subsets <- dredge(M2, rank = "QIC", typeR = TRUE)
optM <- geeglm(IA ~ year + peridep + mdep + gender + sleep + CBCL + empstatus,
id = N_ID, data = d,
family = binomial, corstr = 'exchangeable')
########## Adjusted odds ratios for the full and the optimal models ##########
summary(M2) # full model
tb_full <- broom::tidy(M2)
tb_full_or <- moonBook::extractOR(M2); tb_full_or
result <- cbind(tb_full, tb_full_or)
f <- function(x) {paste0(x["OR"], " [", x["lcl"], ",", x["ucl"], "]")}
result["OR [95% CI]"] <- apply(tb_full_or, 1, f)
tb <- result[c("term", "estimate", "std.error", "statistic", "p.value", "OR [95% CI]")]
names(tb) <- c("Variable", "Estimate", "Std. error", "Wald", "p-value", "OR [95% CI]")
x <- tb %>% flextable; x
summary(optM) # optimal model
tb_reduced <- broom::tidy(optM)
tb_reduced_or <- moonBook::extractOR(optM); tb_reduced_or
result <- cbind(tb_reduced, tb_reduced_or)
f <- function(x) {paste0(x["OR"], " [", x["lcl"], ",", x["ucl"], "]")}
result["OR [95% CI]"] <- apply(tb_reduced_or, 1, f)
tb <- result[c("term", "estimate", "std.error", "statistic", "p.value", "OR [95% CI]")]
names(tb) <- c("Variable", "Estimate", "Std. error", "Wald", "p-value", "OR [95% CI]")
x <- tb %>% flextable; x