-
Notifications
You must be signed in to change notification settings - Fork 0
/
stegen_multivar.R
202 lines (120 loc) · 4.57 KB
/
stegen_multivar.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
# Stegen case study
# Example of solution R-script
# EpiConcept
# February 2012
# Multivariable analysis
# Esther Kissling - Gilles DESVE
# R-code written by Alexander Spina September 2018
#### Reading in files ####
load("stegen1.Rda")
#### Question 7 ####
# Create logit regression model with tira as exposure variable
model1 <- glm(ill~tira,
data = tira.data,
family = binomial(link = "logit"))
# Gives an overview of key elements of the model
summary(model1)
# install broom if you have not already done so
install.packages("broom")
#load broom to this session
library(broom)
# Obtaining the key output of the regression model including ORs and CIs
model1op <- tidy(model1, exponentiate = TRUE, conf.int = TRUE)
# view your cleaned output
model1op
# We add beer to the model
model2 <- glm(ill ~ tira + beer,
data = tira.data,
family = binomial(link = "logit"))
# clean up your output and exponentiate
model2op <- tidy(model2, exponentiate = TRUE, conf.int = TRUE)
# view your cleaned output
model2op
# Here we use tportion a factor variable
model3 <- glm(ill~tportion,
data = tira.data,
family = binomial(link = "logit"))
# clean up your output and exponentiate
model3op <- tidy(model3, exponentiate = TRUE, conf.int = TRUE)
# view your cleaned output
model3op
# Update the previous model with a new formula
model4 <- update(model2,
formula = ill ~ tira + beer + mousse)
#clean and exponentiate
model4op <- tidy(model4, exponentiate = TRUE, conf.int = TRUE)
#view
model4op
#### Question 8 - optional ####
# Binomial regression with one independent variable
#specify link as log rather than logit
bin1 <- glm(ill ~ tira,
data = tira.data,
family = binomial(link = "log"))
#clean output and exponentiate
bin1op <- tidy(bin1, exponentiate = TRUE, conf.int = TRUE)
#view
bin1op
#### Question 9 - optional ####
#use non missing dataset
load("stegen_nomissing.Rda")
# Only one independent variable
model5 <- glm(ill~tira,
data = tira.data,
family = binomial(link = "logit"))
# As before, we can update the previous model and just write the new formula
model6 <- update(model5,
formula = ill ~ tira + beer)
anova(model5, model6, test = "Chisq")
#### Question 10 - optional ####
#Automated selection of best fit based on AIC
#define variables
vars <- c("sex", "tira", "age",
"dmousse", "wmousse", "beer",
"fruitsalad", "redjelly", "tportion",
"mportion", "salmon", "mince",
"tomato", "horseradish", "chickenwin",
"roastbeef", "pork")
#put variables in a formula for inputting to regression
form <- formula(paste0("ill ~ ",paste0(vars, collapse = "+")))
#stepwise regression to find best based on AIC
bestmodel <- step(glm(form, data = tira.data,
family = binomial(link = "logit")))
#clean the output of a the best fit model
final <- tidy(bestmodel, exponentiate = TRUE, conf.int = TRUE)
#### additional ####
# need to figure out simplest way for predict()
#### Question 11 - optional #####
#load your non missing dataset
load("stegen_nomissing.Rda")
# Check for interaction between tira and beer
tirabeer <- glm(ill ~ tira*beer,
data = tira.data,
family = binomial(link = "logit"))
tirabeerop <- tidy(tirabeer, exponentiate = TRUE, conf.int = TRUE)
tirabeerop
# Stratified by tira
#We can use names to extract the coefficient names
# check with: names(coef(tirabeer))
# linfct specifies the required combination:
#In this case we want beer and tira and beer:tira=0
# The odds of illness among those who
#drank beer and consumed tiramisu compared to those
#who consumed neither tiramisu nor beer
a <- summary(glht(tirabeer, linfct = c("beerYes + tiraYes:beerYes = 0")))
ci <- confint(a)
# Put together (cbind) a table with the exponent of the coefficients and CI, and p-value
table_interact <- round(cbind(OR = exp(coef(a)),
Interval = exp(ci$confint),
Pvalue = a$test$pvalues),
digits = 3)
table_interact
#Stratified by beer
a <- summary(glht(tirabeer, linfct = c("tiraYes + tiraYes:beerYes = 0")))
ci <- confint(a)
# Put together (cbind) a table with the exponent of the coefficients and CI, and p-value
table_interact <- round(cbind(OR = exp(coef(a)),
Interval = exp(ci$confint),
Pvalue = a$test$pvalues),
digits = 3)
table_interact