-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodglm.R
419 lines (332 loc) · 12.9 KB
/
modglm.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
modglm<-function(model, vars, data, part=NULL, hyps="means", plotby=NULL,type="cpd")
{
ints<<-list()
#This defines a string for the interaction term
(int.var <- paste(vars, collapse = ":"))
jacs<<-list()
#Define Coefficients
# if (is.list(model$coefficients)){
#
# # if(is.null(part)){stop("Model part must be specified for two-part models. use part= in intglm function to specify.\n")}
# # b<-model$coef$count
# if(part=="count"){b<-model$coef$count}
# if(part=="zero"){
# if(grepl("zeroinfl",rb.ssdep$call[1])==T){b<- -(model$coef$zero)}
# else{b<-model$coef$zero}
# }
#
# }
# else{
(b <- model$coef)
# }
# b <- model$coef
#Define design matrix
if(model$call[1]=="gee()"){
dftemp<-na.omit(data[,which(names(data) %in% names(model$coefficients))])
X<-as.data.frame(cbind(rep(1,nrow(dftemp)),dftemp))
}
else(X<-as.data.frame(cbind(rep(1,nrow(model$model)),model$model[,-1])))
if(hyps[1]=="means"){cfs <- matrix(colMeans(X),nrow=1)}
else(cfs<-hyps)
Xcfs<-as.data.frame(matrix(rep(cfs,each=nrow(X)),nrow=nrow(X)))
colnames(X)[1]<-"(Intercept)"
colnames(Xcfs)<-colnames(X)
xmats<<-list()
xmats$obs<-X
xmats$means<-Xcfs
ints<<-list()
for(i in 1:length(xmats)){
X<-xmats[[i]]
# if (is.list(model$coefficients)){
# if(part=="count"){
# if (int.var %in% names(model$coefficients$count)){X[,int.var]<-X[,vars[1]]*X[,vars[2]]}
# }
# if(part=="zero"){
# if (int.var %in% names(model$coefficients$zero)){X[,int.var]<-X[,vars[1]]*X[,vars[2]]}
# }
# }
# else
if (int.var %in% names(model$coefficients)){
X[,int.var]<-X[,vars[1]]*X[,vars[2]]
# modvars<-colnames(vcov(model))
# if((!is.na(modvars[-(which(modvars==int.var))][which(grepl(":",modvars))]))){
# (modvars.noint<-as.vector(na.omit(modvars[-(which(modvars==int.var))][which(grepl(":",modvars))])))
#
# otherints<-strsplit(modvars.noint,":")
#
# for(j in 1:length(otherints)){
# X[,modvars.noint[j]]<-X[,otherints[[j]][1]]*X[,otherints[[j]][2]]
# }
# }
}
X<-as.matrix(X)
# X<-model.matrix(model,model$model)
#matrix multiply n x p design matrix by p x 1 vector of coefficients
(xb <- (X %*% b)[,,drop=F])
#Computes predicted quantities of interest
# if (is.list(model$coefficients)){
# if (part=="zero"){
# hat<-1/(1+ exp(-xb))
# # phatmean<-1/(1+ exp(-xbmean))
# #computes var(Yi)
# deriv1 <- exp(-xb)*(1+exp(-xb))^(-2)
# deriv2 <- ((exp(-xb)-1)*exp(-xb))/((exp(-xb)+1)^3)
# deriv3 <- (exp(-xb)*(exp(2*-xb)-4*exp(-xb)+1))/((exp(-xb)+1)^4)
# }
# else if (part=="count"){
# hat<-exp(xb)
# deriv1 <- exp(xb)
# deriv2 <- exp(xb)
# deriv3 <- exp(xb)
# }
# # else{stop("Error: this two-part model is yet supported in inteff.")}
# }
# else
if(type=="cpd"){
if (model$family$link == "logit")
{
hat<-1/(1+ exp(-xb))
#computes var(Yi)
deriv1 <- exp(-xb)*(1+exp(-xb))^(-2)
deriv2 <- ((exp(-xb)-1)*exp(-xb))/((exp(-xb)+1)^3)
deriv3 <- (exp(-xb)*(exp(2*-xb)-4*exp(-xb)+1))/((exp(-xb)+1)^4)
}
# else if (model$family$link == "inverse")
# {
# hat<-1/(xb)
# deriv1 <- -(1/(xb^2))
# deriv2 <- 2/(xb^3)
# deriv3 <- -(6/(xb^4))
# }
else if (model$family$link == "log")
{
hat<-exp(xb)
deriv1 <- exp(xb)
deriv2 <- exp(xb)
deriv3 <- exp(xb)
}
else if (model$family$link == "identity")
{
hat<-xb
deriv1 <- 1
deriv2 <- 0
deriv3 <- 0
}
}
else if(type=="fd"){
if(model$call[1]=="gee()"){dum <- vars[which(sapply(apply(dftemp[, vars], 2, table), length) ==2)]}
else(dum <- vars[which(sapply(apply(model$model[, vars], 2, table), length) ==2)])
cont <- vars[which(vars != dum)]
X1 <- X2 <- as.data.frame(X)
X1[, dum] <- 1
X2[, dum] <- 0
if (int.var %in% names(model$coefficients)==T){
X1[, int.var] <- X1[, cont] * X1[, dum]
X2[, int.var] <- X2[, cont] * X2[, dum]
bint<-b[int.var]
}
else{
bint<-0
}
X1<-as.matrix(X1)
X2<-as.matrix(X2)
x1b<-X1 %*% b
x2b<-X2 %*% b
if(model$family$link == "logit"){
hat <- 1/(1+ exp(-xb))
hat1 <- 1/(1+ exp(-x1b))
d1f1 <- exp(-x1b)*(1+exp(-x1b))^(-2)#phi1
d2f1 <- ((exp(-x1b)-1)*exp(-x1b))/((exp(-x1b)+1)^3) #d2f1
hat2 <- 1/(1+ exp(-x2b))
d1f2 <- exp(-x2b)*(1+exp(-x2b))^(-2) #phi2
d2f2 <- ((exp(-x2b)-1)*exp(-x2b))/((exp(-x2b)+1)^3) #d2f2
}
else if (model$family$link == "log"){
hat<-exp(X %*% b)
hat1 <- exp(X1 %*% b)
d1f1 <- exp(X1 %*% b)
d2f1 <- exp(X1 %*% b)
hat2 <- exp(X2 %*% b)
d1f2 <- exp(X2 %*% b)
d2f2 <- exp(X2 %*% b)
}
ie1 <- (b[cont] + bint) * d1f1
ie2 <- b[cont] * d1f2
int.est <- ie1 - ie2
deriv1 <- d1f1 - d1f2 + b[cont] * X[, cont] * (d2f1 - d2f2) +
bint * X[, cont] * d2f1
deriv2 <- (b[cont] + bint) * d2f1
deriv3 <- d1f1 + (b[cont] + bint) * d2f1 * X[, cont]
deriv0 <- (b[cont] + bint) * d2f1 - b[cont] * d2f2
}
else if(type=="dd"){
X00 <- X01 <- X10 <- X11<-as.data.frame(X)
X00[, vars[1]] <- 0
X00[, vars[2]] <- 0
X01[, vars[1]] <- 0
X01[, vars[2]] <- 1
X10[, vars[1]] <- 1
X10[, vars[2]] <- 0
X11[, vars[1]] <- 1
X11[, vars[2]] <- 1
if (int.var %in% names(model$coefficients)==T){
X00[, int.var] <- X00[, vars[1]] *X00[, vars[2]]
X01[, int.var] <- X01[, vars[1]] *X01[, vars[2]]
X10[, int.var] <- X10[, vars[1]] *X10[, vars[2]]
X11[, int.var] <- X11[, vars[1]] *X11[, vars[2]]
bint<-b[int.var]
}
else{
bint<-0
}
X00<-as.matrix(X00)
X01<-as.matrix(X01)
X10<-as.matrix(X10)
X11<-as.matrix(X11)
x00b<-X00 %*% b
x01b<-X01 %*% b
x10b<-X10 %*% b
x11b<-X11 %*% b
if(model$family$link == "logit"){
hat <- 1/(1+ exp(-xb))
hat00 <- 1/(1+ exp(-x00b))
d1f00 <- exp(-x00b)*(1+exp(-x00b))^(-2)
hat01 <- 1/(1+ exp(-x01b))
d1f01 <- exp(-x01b)*(1+exp(-x01b))^(-2)
hat10 <- 1/(1+ exp(-x10b))
d1f10 <- exp(-x10b)*(1+exp(-x10b))^(-2)
hat11 <- 1/(1+ exp(-x11b))
d1f11 <- exp(-x11b)*(1+exp(-x11b))^(-2)
}
else if (model$family$link == "log"){
hat<-exp(X %*% b)
hat00 <- exp(x00b)
d1f00 <- exp(x00b)
hat01 <- exp(x01b)
d1f01 <- exp(x01b)
hat10 <- exp(x10b)
d1f10 <- exp(x10b)
hat11 <- exp(x11b)
d1f11 <- exp(x11b)
}
int.est <- (hat11-hat10)-(hat01-hat00)
deriv1 <- d1f11-d1f10
deriv2 <- d1f11-d1f01
deriv3 <- d1f11
deriv0 <- (d1f11-d1f01)-(d1f10-d1f00)
}
# if (is.list(model$coefficients)){
# if(part=="count"){
# if (int.var %in% names(model$coefficients$count)==F){bint<-0}
# }
# if(part=="zero"){
# if (int.var %in% names(model$coefficients$zero)==F){bint<-0}
# }
# }
# else
if(type=="cpd"){
if (int.var %in% names(model$coefficients)==F){bint<-0}
else(bint<-b[int.var])
int.est <- bint * deriv1 +
(b[vars[1]] + bint * X[, vars[2]]) *
(b[vars[2]] + bint * X[, vars[1]]) *
deriv2
b1b4x2 <- b[vars[1]] + bint * X[, vars[2]]
b2b4x1 <- b[vars[2]] + bint * X[, vars[1]]
#Taking derivative of the interaction term with respect to X1
deriv11 <- bint * deriv2 * X[, vars[1]] +
b2b4x1 * deriv2 +
b1b4x2 * b2b4x1 * deriv3 * X[, vars[1]]
#Same with respect to X2
deriv22 <- bint * deriv2 * X[, vars[2]] +
b1b4x2 * deriv2 +
b1b4x2 * b2b4x1 * X[, vars[2]] * deriv3
#With respect to X1X2
deriv44 <- deriv1 +
bint * deriv2 * X[, vars[1]] * X[, vars[2]] +
X[, vars[2]] * b2b4x1 * deriv2 + X[, vars[1]] * b1b4x2 *
deriv2 + b1b4x2 * b2b4x1 * X[, vars[1]] * X[, vars[2]] *
deriv3
#with respect to the intercept?
derivcc <- bint * deriv2 + b1b4x2 * b2b4x1 * deriv3
}
##NOTE: This will add covariate values to the matxi
if (int.var %in% names(model$coefficients)==T){covars <- X[, -c(1, match(c(vars, int.var), names(b)))]}
else {covars <- X[, -c(1, match(vars, names(b)))]}
if (!("matrix" %in% class(covars))) {
covars <- matrix(covars, nrow = nrow(X))
}
if ((int.var %in% names(model$coefficients))==T)
{colnames(covars) <- colnames(X)[-c(1, match(c(vars, int.var),
names(b)))]}
else{colnames(covars) <- colnames(X)[-c(1, match(vars,names(b)))]}
#
# if(dd)(jcovar <- apply(covars, 2, function(x) (2 + 1) *d2f1 - 2 * d2f2 * x))
if(type=="cpd"){jcovar <- apply(covars, 2, function(x) bint * deriv2 * x +b1b4x2 * b2b4x1 * x * deriv3)}
else if(type=="fd"){jcovar <- apply(covars, 2, function(x) ((b[cont] + bint) *d2f1 - b[cont] * d2f2) * x)}
else if(type=="dd"){jcovar <- apply(covars, 2, function(x) ((d1f11 - d1f01) - (d1f10 - d1f00)) * x)}
jcovar <- array(jcovar, dim=dim(covars))
dimnames(jcovar) <- dimnames(covars)
if(type=="cpd")(jac <- cbind(deriv11, deriv22, deriv44, jcovar, derivcc)[,,drop=F])
else if(type=="fd"| type=="dd"){jac<-cbind(deriv1, deriv2, deriv3, jcovar, deriv0)[,,drop=F]}
colnames(jac) <- c(vars, int.var, colnames(jcovar), "(Intercept)")
jac <- jac[, match(colnames(X), colnames(jac)), drop=F]
# ints$jacs[i]<-jac
# if(is.list(model$coefficients)){
# if(part=="count"){
# vcov<-vcov(model)[grepl("count_", colnames(vcov(model))),grepl("count_", colnames(vcov(model)))]}
#
# if(part=="zero"){
# vcov<-vcov(model)[grepl("zero_", colnames(vcov(model))),grepl("zero_", colnames(vcov(model)))]}
#
# se <- sqrt(diag(jac %*% vcov %*% t(jac)))
# }
# else{
if(model$call[1]=="gee()"){se <- sqrt(diag(jac %*% gee_Rap_full$robust.variance %*% t(jac)))}
else{se <- sqrt(diag(jac %*% vcov(model) %*% t(jac)))}
# }
t.val <- int.est/se
ints[[i]] <- data.frame(int.est = int.est,
hat = hat,
se.int.est = se,
t.val = t.val)
jacs[[i]] <- jac
}
ints$aie<-data.frame(aie.est=NA,aie.se.delta=NA)
ints$desc<-data.frame(int.range=NA,prop.sig=NA, prop.pos=NA,prop.neg=NA)
names(ints)<-c("obints","inthyp","aie","desc")
ints$aie$aie.est<-mean(ints$obints$int.est)
if(model$call[1]=="gee()"){ints$aie$aie.se <- sqrt(as.vector(colMeans(jacs[[1]]))%*%gee_Rap_full$robust.variance %*%as.vector(t(colMeans(jacs[[1]]))))}
else{ints$aie$aie.se.delta <- sqrt(as.vector(colMeans(jacs[[1]]))%*%vcov(model)%*%as.vector(t(colMeans(jacs[[1]]))))}
ints$aie$aie.ll<-ints$aie$aie.est-1.96*ints$aie$aie.se.delta
ints$aie$aie.ul<-ints$aie$aie.est+1.96*ints$aie$aie.se.delta
ints$desc["prop.sig"]<-length(which(abs(ints$obints$t.val)>1.96))/length(ints$obints$t.val)
ints$desc["int.range"]<-paste0(round(range(ints$obints$int.est)[1],2),"-",round(range(ints$obints$int.est)[2],2))
ints$desc["prop.pos"]<-length(which(ints$obints$int.est>=0))/length(ints$obints$int.est)
ints$desc["prop.neg"]<-length(which(ints$obints$int.est<0))/length(ints$obints$int.est)
ints$obints$sig[abs(ints$obints$t.val)>=1.96]<-"Sig."
ints$obints$sig[abs(ints$obints$t.val)<1.96]<-"N.S."
ints$inthyp<-as.vector(ints$inthyp[1,])
ints$inthyp["inthyp.ll"]<-ints$inthyp["int.est"]-1.96*ints$inthyp["se.int.est"]
ints$inthyp["inthyp.ul"]<-ints$inthyp["int.est"]+1.96*ints$inthyp["se.int.est"]
ints$model.summary<-summary(model)
require(ggplot2)
plotdf<-ints$obints
if(is.null(plotby)){
ints$intsplot<-ggplot(data=plotdf,aes(x=hat,y=int.est, color=sig)) +
geom_point(size=.75) +
labs(x="Predicted Value",y="Interaction Effect") +
theme_bw()
}
else{
plotdf[,plotby]<-as.factor(data[,plotby])
ints$intsplot<-ggplot(data=plotdf,aes(x=hat,y=betas, color=sig, fill=sig,shape=plotdf[,plotby])) +
geom_point(size=1.5) +
labs(x="Predicted Value",y="Interaction Effect") +
# scale_fill_manual(values=c("white","black")) +
# scale_color_manual(values=c("red","blue")) +
scale_shape_manual(values=c(1,3)) +
theme_bw()
}
invisible(ints)
}