-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_3_inR_DdPS.R
executable file
·294 lines (184 loc) · 7.26 KB
/
ex_3_inR_DdPS.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
289
## ESP Causal Inference day 3
## R set up:
# Setting read_libs function:
read_libs <- function(packages){
# Creates a vector with the required but not installed packages:
install <- packages[!(packages %in% installed.packages()[, "Package"])]
# if there is any uninstalled package in the list:
if(length(install) > 0){
# Install the uninstalled packages and its dependencies (other required packages in order to function)
install.packages(pkgs = install, dependencies = TRUE)
}
# Applying the "require" function in each package of the list,
# the "character.only = TRUE" argument is meant to use the packages names as strings
invisible(sapply(packages, require, character.only = TRUE))
}
# Using the read libs function:
# call a vector with the necessary packages names:
read_libs(c("tidyverse", "haven", "survey"))
## Reading data:
dat <- read.csv("/Volumes/DDPS/AdditionalMaterial_R/dat/lab1.csv")
view(dat)
# Look at data
names(dat)
nrow(dat)
view(dat)
# Only use subset of data ("complete cases for outcome")
dat_complete <- filter(dat,
is.na( wt82_71 ) == FALSE)
nrow(dat_complete)
view(dat_complete)
# Descriptives
summary( factor( dat_complete$qsmk ) )
summary( dat_complete$wt82_71 )
mean( subset( dat_complete , qsmk == 1 ) $ wt82_71 )
mean( subset( dat_complete, qsmk==0) $ wt82_71 )
# Create new variable
dat_complete$older <- ifelse( dat_complete$age > 50, 1, 0)
###############################
# Here: Exercise Question 1-4 #
###############################
# 1) Dropped:
nrow(dat) - nrow( dat_complete )
# 2)
mean( dat_complete$wt82_71 )
# 3)
sum( dat_complete$qsmk )
nrow( dat_complete )-sum( dat_complete$qsmk )
# 4)
mean( subset( dat_complete, qsmk==1 )$wt82_71 )
mean( subset( dat_complete, qsmk==0 )$wt82_71 )
#############################
# Let's continue analysis #
#############################
# weights through
# 1) tables
table( dat_complete$older,
dat_complete$qsmk ) #cell count, columns=qsmk
proportions <- prop.table( table ( dat_complete$older,
dat_complete$qsmk ) , 1 ) #denominator, columns=qsmk
proportions
dat_weigths <- 1/proportions #weights, columns=qsmk
dat_weigths
# 2) logistic regression
mod <- glm( qsmk ~ older, family = binomial(), data = dat_complete )
dat_complete$probA.d <- predict( mod, type = 'response' )
dat_complete$A.den <- ifelse(dat_complete$qsmk==1,
dat_complete$probA.d,1-dat_complete$probA.d)
dat_complete$w <- 1/dat_complete$A.den
summary(factor(dat_complete$w)) # ca. 1/prop.table(table(dat_complete$older,dat_complete$qsmk),1)
# E(Y^a)with weighted mean
weighted.mean( subset( dat_complete, qsmk==1)$wt82_71, w=subset(dat_complete, qsmk==1)$w)
weighted.mean(subset( dat_complete, qsmk==0)$wt82_71, w=subset(dat_complete, qsmk==0)$w)
###############################
# Here: Exercise Question 5-7 #
###############################
# 5)
unique(dat_complete$w)
# 6)
summary(factor( dat_complete$w ))
dat_weigths
# 7)
weighted.mean(subset ( dat_complete , qsmk == 1 )$wt82_71,
w = subset ( dat_complete , qsmk == 1 ) $ w ) -
weighted.mean(subset ( dat_complete , qsmk == 0 ) $ wt82_71,
w=subset(dat_complete,qsmk==0)$w )
#############################
# Let's continue analysis #
#############################
# IPTW - standard weights
A.den <- glm ( qsmk ~ sex +
factor( race ) +
age +
I( age^2 ) +
factor( education ) +
smokeintensity +
I( smokeintensity^2 ) +
smokeyrs +
I( smokeyrs^2) +
factor(exercise) +
factor(active) +
wt71 +
I(wt71^2),
family = binomial() ,
data = dat_complete)
dat_complete$probA.den <- predict( A.den, type = "response" )
dat_complete$w <- ifelse(dat_complete$qsmk == 1,
1/ dat_complete$probA.den,
1/( 1 - dat_complete$probA.den ) )
# IPTW - stabilized weights
A.num <- glm( qsmk ~ 1,
family = binomial(),
data = dat_complete )
dat_complete$probA.num <- predict( A.num, type = "response" )
dat_complete$sw <- ifelse(dat_complete$qsmk == 1,
dat_complete$probA.num / dat_complete$probA.den,
( 1 - dat_complete$probA.num ) / ( 1 - dat_complete$probA.den ) )
# Summary of weights
summary( dat_complete [ , c('w','sw') ] )
###############################
# Here: Exercise Question 5-7 #
###############################
# 8) + 9)
summary( dat_complete [ , c('w','sw') ] )
#############################
# Let's continue analysis #
#############################
# mean outcomes in the weighted population
weighted.mean( subset( dat_complete , qsmk == 1 )$wt82_71 , w = subset( dat_complete, qsmk == 1 )$w )
weighted.mean( subset( dat_complete , qsmk == 0 )$wt82_71 , w = subset( dat_complete, qsmk == 0 )$w)
weighted.mean( subset( dat_complete, qsmk == 1 )$wt82_71 , w = subset( dat_complete , qsmk == 1 )$sw)
weighted.mean( subset( dat_complete, qsmk == 0 )$wt82_71 , w = subset( dat_complete , qsmk == 0 )$sw)
#############################
# Let's continue analysis #
#############################
# final MSM analysis
modw <- glm( wt82_71 ~ qsmk ,
data = dat_complete ,
weights = sw)
summary(modw)
# for better confidence intervalsuse package "survey"
modw <- svyglm(wt82_71 ~ qsmk,
design = svydesign ( id = ~ seqn,
weights = ~ sw,
data = dat_complete ) )
summary(modw)
confint(modw)
#########################################
# now the g-formula (standardization) #
#########################################
# prepare dataset
dat1 <- dat_complete #first copy of dataset (equal to original)
dat1$interv <- rep(-1 , nrow( dat1 ) )
dat2 <- dat_complete #second copy of dataset (set tx to 0, outcome to missing)
dat2$interv <- rep( 0 , nrow( dat2 ) )
dat2$qsmk <- rep( 0 , nrow( dat2 ) )
dat2$wt82_71 <- rep( NA , nrow( dat2 ) )
dat3 <- dat_complete #third copy of dataset (set tx to 1, outcome to missing)
dat3$interv <- rep( 1 , nrow( dat3 ) )
dat3$qsmk <- rep( 1 , nrow( dat3 ) )
dat3$wt82_71 <- rep( NA , nrow( dat3 ) )
onesample <- as.data.frame( rbind( dat1 , dat2 , dat3 ) )
dim( onesample )
summary( factor( onesample$interv ) )
# Preparing the linear mmodel
modst <- glm(wt82_71 ~ qsmk +
sex +
factor(race) +
age +
I(age^2) +
factor(education) +
smokeintensity +
I(smokeintensity^2) +
smokeyrs +
I(smokeyrs^2) +
factor(exercise) +
factor(active) +
wt71 +
I(wt71^2),
data = onesample)
summary(modst)
# Couldmzt understand what did they do here :)
onesample$meanY <- predict( modst , onesample , type = "response" )
with(onesample,
tapply( meanY , list( interv ), mean ) )