-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimating_equations.R
executable file
·443 lines (399 loc) · 16 KB
/
estimating_equations.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#'@title Estimating equation for ELCIC under GLM
#'@description A specified estimating equation for ELCIC under GLM. This estimating equation is used for marginal mean selection.
#'@usage ee.glm(x, y, beta, dist)
#'@param x A matrix containing covariates. The first column should be all ones corresponding to the intercept. See more details in
#'@param y A vector containing outcomes.
#'@param beta A plug-in estimator solved by an external estimating procedure.
#'@param dist A specified distribution. It can be "gaussian", "poisson",and "binomial".
#'
#'@return A matrix containing values of calculated estimating equations.
#'
#'@note "x" and "y" should be all observed.
#'
#'@examples
#'## tests
#'# load data
#'data(glmsimdata)
#'x<-glmsimdata$x
#'y<-glmsimdata$y
#'# obtain the estimates. Note that x matrix already contains intercept.
#'fit<-glm(y~x-1,family="poisson")
#'beta<-fit$coefficients
#'ee.matrix<-ee.glm(x, y, beta, dist="poisson")
#'apply(ee.matrix,1,mean)
#'
#'@export
######for cross-sectional glm
ee.glm<-function (x,y,beta,dist)
{
full<-ncol(x)
samplesize<-nrow(x)
ee<-matrix(0,nrow=full,ncol=samplesize)
switch(dist,
"gaussian"={ for (i in 1:samplesize)
{
ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-(t(as.matrix(x[i,],ncol=1))%*%beta))
}
},
"binomial"={ for (i in 1:samplesize)
{
ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-(1+exp(-t(as.matrix(x[i,],ncol=1))%*%beta))^(-1))
}
},
"poisson"={for (i in 1:samplesize)
{
ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-exp(t(as.matrix(x[i,],ncol=1))%*%beta))
}
},
stop("Invalid type of dist. It should be one of gaussian,binomial,poisson")
)
ee
# if(dist=="gaussian")
# {
# for (i in 1:samplesize)
# {
# ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-(t(as.matrix(x[i,],ncol=1))%*%beta))
# }
# }else if (dist=="poisson")
# {
# for (i in 1:samplesize)
# {
# ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-exp(t(as.matrix(x[i,],ncol=1))%*%beta))
# }
# }else if (dist=="binomial")
# {
# for (i in 1:samplesize)
# {
# ee[,i]<-as.matrix(x[i,],ncol=1)%*%(y[i]-(1+exp(-t(as.matrix(x[i,],ncol=1))%*%beta))^(-1))
# }
# }
}
#######################################################
###### for joint selection of marginal mean and working correlation
#'@title Estimating equation for GEE without missingness or with data missing completely at random.
#'@description Calculate estimating equation from GEE in ELCIC without missingness or missing completely at random. This estimating equation is used for joint selection of marginal mean and working correlation structure.
#'@usage ee.gee(y,x,r,id,beta,rho,phi,dist,corstr)
#'@param x A matrix containing covariates. The first column should be all ones the represents the intercept.
#'@param y A vector containing outcomes.
#'@param r A vector indicating the observation of outcomes: 1 for observed records, and 0 for unobserved records. The default setup is that all data are observed. See more in details section.
#'@param id A vector indicating subject id.
#'@param beta A plug-in estimator solved by an external estimation procedure.
#'@param rho A correlation coefficients obtained from an external estimation procedure, such as GEE.
#'@param phi An over-dispersion parameter obtained from an external estimation procedure, such as GEE.
#'@param dist A specified distribution. It can be "gaussian", "poisson",and "binomial".
#'@param corstr A candidate correlation structure. It can be "independence","exchangeable", and "ar1".
#'
#'@return A matrix containing values of calculated estimating equations.
#'
#'@details If the element in argument "r" equals zero, the corresponding rows of "x" and "y" should be all zeros.
#'
#'@examples
#'## tests
#'# load data
#'data(geesimdata)
#'x<-geesimdata$x
#'y<-geesimdata$y
#'id<-geesimdata$id
#'corstr<-"exchangeable"
#'dist<-"poisson"
#'# obtain the estimates
#'library(geepack)
#'# x matrix already include the intercept column.
#'fit<-geeglm(y~x-1,data=geesimdata,family =dist,id=id,corstr = "ar1")
#'beta<-fit$coefficients
#'rho<-unlist(summary(fit)$corr[1])
#'phi<-unlist(summary(fit)$dispersion[1])
#'r<-rep(1,nrow(x))
#'ee.matrix<-ee.gee(y,x,r,id,beta,rho,phi,dist,corstr)
#'apply(ee.matrix,1,mean)
#'
#'@export
#'
######for longitudinal data gee
ee.gee<-function(y,x,r,id,beta,rho,phi,dist,corstr)
{ #full wgee
n<-length(unique(id))
p<-length(beta)
time<-length(id)/n
A<-diag(1,time,time)
rho<-roo(rho,time,corstr)
R<-R(rho,id)
W<-diag(1,time,time)
V<-v(x,beta,dist)
wgeef<-rep()
#z.col<-ncol(z)
m_num_vector<-rep()
for(m in 1:(time-1))
{
m_num<-0
for (i in 1:n) #formula for rho
{
WW<-W*r[((i-1)*time+1):(i*time)]
for (j in 1:(time-m))
{
m_num<-m_num+WW[j+m,j+m]*WW[j,j]
}
}
m_num_vector<-c(m_num_vector,m_num)
}
y[which(is.na(y))]<-0
for (i in 1:n)
{
#piii<-pii(pi[((i-1)*time+1):(i*time)]) #used when we have ipw
AA<-A*V$v[((i-1)*time+1):(i*time)]^(-0.5)
#WW<-W*r[((i-1)*time+1):(i*time)]*piii #used when we have ipw
WW<-W*r[((i-1)*time+1):(i*time)]
e<-y[((i-1)*time+1):(i*time)]-V$mu[((i-1)*time+1):(i*time)]
wgeei<-1/phi*t(V$der[((i-1)*time+1):(i*time),])%*%AA%*%solve(R)%*%AA%*%WW%*%e
e<-e*(V$v[((i-1)*time+1):(i*time)])^(-0.5)
for (m in 1:(time-1)) #formula for rho
{error<-0
for (j in 1:(time-m))
{
error<-error+e[j]*e[j+m]*WW[j+m,j+m]*WW[j,j]
}
error<-error-rho[m]*phi*(m_num_vector[m]-p)/n #directly use phi might be more efficient
wgeei<-rbind(wgeei,error)
}
wgeef<-cbind(wgeef,wgeei)
}
return(wgeef)
}
#'@title Estimating equation for weighted GEE (WGEE) for missing longitudinal data under the mechanism of missing at random and drop-out
#'@description Calculate estimating equation from WGEE for missing longitudinal data under the mechanism of missing at random and drop-out. This estimating equation is used for joint selection of marginal mean and "working" correlation structure.
#'@usage ee.wgee(y,x,r,pi,id,time,beta,rho,phi,dist,corstr)
#'@param x A matrix containing covariates. The first column should be all ones corresponding to the intercept.
#'@param y A vector containing outcomes. use NA to indicate missing outcomes.
#'@param r A vector indicating the observation of outcomes: 1 for observed records, and 0 for unobserved records.
#'@param pi A vector containing observing probabilities across all observations.
#'@param time The number of observations for each subject.
#'@param id A vector indicating subject id.
#'@param beta A plug-in estimator solved by an external estimation procedure, such as WGEE.
#'@param rho A correlation coefficients obtained from an external estimation procedure, such as WGEE.
#'@param phi An over-dispersion parameter obtained from an external estimation procedure, such as GEE.
#'@param dist A specified distribution. It can be "gaussian", "poisson",and "binomial".
#'@param corstr A candidate correlation structure. It can be "independence","exchangeable", and "ar1".
#'
#'@return A matrix containing values of calculated estimating equations.
#'
#'@examples
#'## tests
#'# load data
#'data(wgeesimdata)
#'library(wgeesel)
#'data_wgee<-data.frame(do.call(cbind,wgeesimdata))
#'corstr<-"exchangeable"
#'dist<-"binomial"
#'id<-data_wgee$id
#'# obtain the estimates.
#'# Note that "obs_ind" is an indicator of observations in the missing data model.
#'fit<-wgee(y~x1+x2+x3,data_wgee,id,family=dist,corstr =corstr,
#' scale = NULL,mismodel =obs_ind~x_mis1)
#'beta<-as.vector(summary(fit)$beta)
#'rho<-summary(fit)$corr
#'phi<-summary(fit)$phi
#'#calculate observing probabilies for all observations
#'gamma<-as.vector(summary(fit$mis_fit)$coefficients[,1])
#'x_mis<-wgeesimdata$x_mis
#'pi<-cond.prob(x_mis,gamma,id,time=3)
#'wgee.matrix<-ee.wgee(y=wgeesimdata$y,x=wgeesimdata$x,r=wgeesimdata$obs_ind,
#'pi=pi,id=wgeesimdata$id,time=3,beta=beta,rho=rho,phi=phi,dist=dist,corstr=corstr)
#'apply(wgee.matrix,1,mean)
#'
#'@export
ee.wgee<-function(y,x,r,pi,id,time,beta,rho,phi,dist,corstr)
{ #full wgee
n<-length(unique(id))
p<-length(beta)
rho<-roo(rho,time,corstr)
A<-diag(1,time,time)
R<-R(rho,id)
W<-diag(1,time,time)
V<-v(x,beta,dist)
wgeef<-rep()
# z.col<-ncol(z)
# rlag<-ylag(id,r,1)
# rlag[is.na(rlag)]<-1
# S<-matrix(rep(0),nrow = z.col, ncol = n )
y[which(is.na(y))]<-0
for (i in 1:n)
{
piii<-marg.prob(pi[((i-1)*time+1):(i*time)])
AA<-A*V$v[((i-1)*time+1):(i*time)]^(-0.5)
WW<-W*r[((i-1)*time+1):(i*time)]*piii
e<-y[((i-1)*time+1):(i*time)]-V$mu[((i-1)*time+1):(i*time)]
wgeei<-1/phi*t(V$der[((i-1)*time+1):(i*time),])%*%AA%*%solve(R)%*%AA%*%WW%*%e
e<-e*(V$v[((i-1)*time+1):(i*time)])^(-0.5)
for (m in 1:(time-1)) #formula for rho
{error<-0
for (j in 1:(time-m))
{
error<-error+e[j]*e[j+m]*WW[j+m,j+m]
}
error<-error-rho[m]*phi*(n*(time-m)-p)/n #directly use phi might be more efficient
wgeei<-rbind(wgeei,error)
}
wgeef<-cbind(wgeef,wgeei)
# ss<-t(rlag[id==i]*(r[id==i]-lambda[id==i])*z[id==i,])
# S[,i]<-as.matrix(apply(ss,1,sum))
}
return(wgeef)
}
#######################################################
###### only for marginal mean selection
#'@title Estimating equation of marginal mean for GEE without missingness or missing completely at random
#'@description Calculate estimating equation from GEE in ELCIC. This estimating equation is used for marginal mean selection.
#'@usage ee.gee.mean(y,x,r,id,beta,rho,phi,dist,corstr)
#'@param x A matrix containing covariates. The first column should be all ones corresponding to the intercept.
#'@param y A vector containing outcomes.
#'@param r A vector indicating the observation of outcomes: 1 for observed records, and 0 for unobserved records. The default setup is that all data are observed. See more in details section.
#'@param id A vector indicating subject id.
#'@param beta A plug-in estimator solved by an external estimation procedure, such as GEE.
#'@param rho A correlation coefficients obtained from an external estimation procedure, such as GEE.
#'@param phi An over-dispersion parameter obtained from an external estimation procedure, such as GEE.
#'@param dist A specified distribution. It can be "gaussian", "poisson",and "binomial".
#'@param corstr A candidate correlation structure. It can be "independence","exchangeable", and "ar1".
#'
#'@return A matrix containing values of calculated estimating equations.
#'
#'@details If the element in argument "r" equals zero, the corresponding rows of "x" and "y" should be all zeros.
#'
#'@note corstr should be prespecified.
#'
#'@examples
#'## tests
#'# load data
#'data(geesimdata)
#'x<-geesimdata$x
#'y<-geesimdata$y
#'id<-geesimdata$id
#'corstr<-"exchangeable"
#'dist<-"poisson"
#'# obtain the estimates
#'library(geepack)
#'fit<-geeglm(y~x-1,data=geesimdata,family =dist,id=id,corstr = corstr)
#'beta<-fit$coefficients
#'rho<-unlist(summary(fit)$corr[1])
#'phi<-unlist(summary(fit)$dispersion[1])
#'r<-rep(1,nrow(x))
#'ee.matrix<-ee.gee.mean(y,x,r,id,beta,rho,phi,dist,corstr)
#'apply(ee.matrix,1,mean)
#'
#'@export
ee.gee.mean<-function(y,x,r,id,beta,rho,phi,dist,corstr)
{ #full wgee
n<-length(unique(id))
p<-length(beta)
time<-length(id)/n
A<-diag(1,time,time)
rho<-roo(rho,time,corstr)
R<-R(rho,id)
W<-diag(1,time,time)
V<-v(x,beta,dist)
wgeef<-rep()
#z.col<-ncol(z)
y[which(is.na(y))]<-0
for (i in 1:n)
{
#piii<-pii(pi[((i-1)*time+1):(i*time)]) #used when we have ipw
AA<-A*V$v[((i-1)*time+1):(i*time)]^(-0.5)
#WW<-W*r[((i-1)*time+1):(i*time)]*piii #used when we have ipw
WW<-W*r[((i-1)*time+1):(i*time)]
e<-y[((i-1)*time+1):(i*time)]-V$mu[((i-1)*time+1):(i*time)]
wgeei<-1/phi*t(V$der[((i-1)*time+1):(i*time),])%*%AA%*%solve(R)%*%AA%*%WW%*%e
# e<-e*(V$v[((i-1)*time+1):(i*time)])^(-0.5)
# for (m in 1:(time-1)) #formula for rho
# {error<-0
# for (j in 1:(time-m))
# {
# error<-error+e[j]*e[j+m]*WW[j+m,j+m]
# }
# error<-error-rho[m]*phi*(n*(time-m)-p)/n #directly use phi might be more efficient
# wgeei<-rbind(wgeei,error)
# }
#
wgeef<-cbind(wgeef,wgeei)
}
return(wgeef)
}
#'@title Estimating equation for marginal mean under WGEE for missing longitudinal data under the mechanism of missing at random and drop-out
#'@description Calculate estimating function from WGEE. This estimating function is used for marginal mean selection.
#'@usage ee.wgee.mean(y,x,r,pi,id,time,beta,rho,phi,dist,corstr)
#'@param x A matrix containing covariates. The first column should be all ones corresponding to the intercept.
#'@param y A vector containing outcomes. use NA to indicate missing outcomes.
#'@param r A vector indicating the observation of outcomes: 1 for observed records, and 0 for unobserved records.
#'@param pi A vector containing observing probabilities across all observations.
#'@param time The number of observations for each subject
#'@param id A vector indicating subject id.
#'@param beta A plug-in estimator solved by an external estimation procedure, such as WGEE.
#'@param rho A correlation coefficients obtained from an external estimation procedure, such as WGEE.
#'@param phi An over-dispersion parameter obtained from an external estimation procedure, such as GEE.
#'@param dist A specified distribution. It can be "gaussian", "poisson",and "binomial".
#'@param corstr A candidate correlation structure. It can be "independence","exchangeable", and "ar1".
#'
#'@note corstr should be prespecified.
#'
#'@return A matrix containing values of calculated estimating equations.
#'
#'@examples
#'## tests
#'# load data
#'data(wgeesimdata)
#'library(wgeesel)
#'data_wgee<-data.frame(do.call(cbind,wgeesimdata))
#'corstr<-"exchangeable"
#'dist<-"binomial"
#'id<-data_wgee$id
#'# obtain the estimates.
#'# Note that "obs_ind" is an indicator of observations in the missing data model.
#'fit<-wgee(y~x1+x2+x3,data_wgee,id,family=dist,corstr =corstr,
#' scale = NULL,mismodel =obs_ind~x_mis1)
#'beta<-as.vector(summary(fit)$beta)
#'rho<-summary(fit)$corr
#'phi<-summary(fit)$phi
#'#calculate observing probabilies for all observations
#'gamma<-as.vector(summary(fit$mis_fit)$coefficients[,1])
#'x_mis<-wgeesimdata$x_mis
#'pi<-cond.prob(x_mis,gamma,id,time=3)
#'wgee.matrix<-ee.wgee.mean(y=wgeesimdata$y,x=wgeesimdata$x,r=wgeesimdata$obs_ind,
#'pi=pi,id=wgeesimdata$id,time=3,beta=beta,rho=rho,phi=phi,dist=dist,corstr=corstr)
#'apply(wgee.matrix,1,mean)
#'@export
ee.wgee.mean<-function(y,x,r,pi,id,time,beta,rho,phi,dist,corstr)
{ #full wgee
n<-length(unique(id))
p<-length(beta)
rho<-roo(rho,time,corstr)
A<-diag(1,time,time)
R<-R(rho,id)
W<-diag(1,time,time)
V<-v(x,beta,dist)
wgeef<-rep()
# z.col<-ncol(z)
# rlag<-ylag(id,r,1)
# rlag[is.na(rlag)]<-1
# S<-matrix(rep(0),nrow = z.col, ncol = n )
y[which(is.na(y))]<-0
for (i in 1:n)
{
piii<-marg.prob(pi[((i-1)*time+1):(i*time)])
AA<-A*V$v[((i-1)*time+1):(i*time)]^(-0.5)
WW<-W*r[((i-1)*time+1):(i*time)]*piii
e<-y[((i-1)*time+1):(i*time)]-V$mu[((i-1)*time+1):(i*time)]
wgeei<-1/phi*t(V$der[((i-1)*time+1):(i*time),])%*%AA%*%solve(R)%*%AA%*%WW%*%e
# e<-e*(V$v[((i-1)*time+1):(i*time)])^(-0.5)
# for (m in 1:(time-1)) #formula for rho
# {error<-0
# for (j in 1:(time-m))
# {
# error<-error+e[j]*e[j+m]*WW[j+m,j+m]
# }
# error<-error-rho[m]*phi*(n*(time-m)-p)/n #directly use phi might be more efficient
# wgeei<-rbind(wgeei,error)
# }
wgeef<-cbind(wgeef,wgeei)
# ss<-t(rlag[id==i]*(r[id==i]-lambda[id==i])*z[id==i,])
# S[,i]<-as.matrix(apply(ss,1,sum))
}
return(wgeef)
}