@@ -155,31 +155,31 @@ msmm <- function(formula, instruments, data, subset, na.action,
155
155
# code from beginning for ivreg::ivreg()
156
156
# # set up model.frame() call
157
157
cl <- match.call()
158
- if (missing(data )) data <- environment(formula )
158
+ if (missing(data )) data <- environment(formula )
159
159
mf <- match.call(expand.dots = FALSE )
160
160
m <- match(c(" formula" , " data" , " subset" , " na.action" , " weights" , " offset" ), names(mf ), 0 )
161
161
mf <- mf [c(1 , m )]
162
162
mf $ drop.unused.levels <- TRUE
163
163
# # handle instruments for backward compatibility
164
- if (! missing(instruments )) {
164
+ if (! missing(instruments )) {
165
165
formula <- Formula :: as.Formula(formula , instruments )
166
166
cl $ instruments <- NULL
167
167
cl $ formula <- formula(formula )
168
168
} else {
169
169
formula <- Formula :: as.Formula(formula )
170
170
}
171
- if (length(formula )[2L ] == 3L ) formula <- Formula :: as.Formula(
171
+ if (length(formula )[2L ] == 3L ) formula <- Formula :: as.Formula(
172
172
formula(formula , rhs = c(2L , 1L ), collapse = TRUE ),
173
173
formula(formula , lhs = 0L , rhs = c(3L , 1L ), collapse = TRUE )
174
174
)
175
175
stopifnot(length(formula )[1L ] == 1L , length(formula )[2L ] %in% 1L : 2L )
176
176
# # try to handle dots in formula
177
177
has_dot <- function (formula ) inherits(try(stats :: terms(formula ), silent = TRUE ), " try-error" )
178
- if (has_dot(formula )) {
178
+ if (has_dot(formula )) {
179
179
f1 <- formula(formula , rhs = 1L )
180
180
f2 <- formula(formula , lhs = 0L , rhs = 2L )
181
181
if (! has_dot(f1 ) && has_dot(f2 )) formula <- Formula :: as.Formula(f1 ,
182
- stats :: update(formula(formula , lhs = 0L , rhs = 1L ), f2 ))
182
+ stats :: update(formula(formula , lhs = 0L , rhs = 1L ), f2 ))
183
183
}
184
184
# # call model.frame()
185
185
mf $ formula <- formula
@@ -190,7 +190,7 @@ msmm <- function(formula, instruments, data, subset, na.action,
190
190
mt <- stats :: terms(formula , data = data )
191
191
mtX <- stats :: terms(formula , data = data , rhs = 1 )
192
192
X <- stats :: model.matrix(mtX , mf , contrasts )
193
- if (length(formula )[2 ] < 2L ) {
193
+ if (length(formula )[2 ] < 2L ) {
194
194
mtZ <- NULL
195
195
Z <- NULL
196
196
} else {
@@ -234,13 +234,13 @@ msmm <- function(formula, instruments, data, subset, na.action,
234
234
stop(" With tsls and tslsalt, only 1 exposure variable is allowed." )
235
235
236
236
if (estmethod == " gmm" )
237
- output = msmm_gmm(x = X [,- 1 ], y = Y , z = Z [,- 1 ], xnames = xnames , t0 = t0 )
237
+ output <- msmm_gmm(x = X [, - 1 ], y = Y , z = Z [,- 1 ], xnames = xnames , t0 = t0 )
238
238
if (estmethod == " gmmalt" )
239
- output = msmm_gmm_alt(x = X [,- 1 ], y = Y , z = Z [,- 1 ], xnames = xnames , t0 = t0 )
239
+ output <- msmm_gmm_alt(x = X [, - 1 ], y = Y , z = Z [,- 1 ], xnames = xnames , t0 = t0 )
240
240
if (estmethod == " tsls" )
241
- output = msmm_tsls(x = X [,- 1 ], y = Y , z = Z [,- 1 ])
241
+ output <- msmm_tsls(x = X [, - 1 ], y = Y , z = Z [,- 1 ])
242
242
if (estmethod == " tslsalt" )
243
- output = msmm_tsls_alt(x = X [,- 1 ], y = Y , z = Z [,- 1 ])
243
+ output <- msmm_tsls_alt(x = X [, - 1 ], y = Y , z = Z [,- 1 ])
244
244
245
245
class(output ) <- append(" msmm" , class(output ))
246
246
output
@@ -268,10 +268,10 @@ msmm_tsls <- function(x, y, z) {
268
268
logcrrse <- msm :: deltamethod(~ log(- 1 / x2 ), beta , estvar )
269
269
270
270
# crr with 95% CI
271
- crrci <- unname(c(- 1 / beta [2 ], exp(logcrr - 1.96 * logcrrse ), exp(logcrr + 1.96 * logcrrse )))
271
+ crrci <- unname(c(- 1 / beta [2 ], exp(logcrr - 1.96 * logcrrse ), exp(logcrr + 1.96 * logcrrse )))
272
272
273
273
# baseline risk
274
- ey0ci <- cbind(stats :: coef(fit ), stats :: confint(fit ))[1 ,]
274
+ ey0ci <- cbind(stats :: coef(fit ), stats :: confint(fit ))[1 , ]
275
275
276
276
# list of results to return
277
277
reslist <- list (stage1 = stage1 ,
@@ -304,7 +304,7 @@ msmm_tsls_alt <- function(x, y, z) {
304
304
logcrrse <- msm :: deltamethod(~ log(- 1 * x2 ), beta , estvar )
305
305
306
306
# crr with 95% CI
307
- crrci <- unname(c(- 1 * beta [2 ], exp(logcrr - 1.96 * logcrrse ), exp(logcrr + 1.96 * logcrrse )))
307
+ crrci <- unname(c(- 1 * beta [2 ], exp(logcrr - 1.96 * logcrrse ), exp(logcrr + 1.96 * logcrrse )))
308
308
309
309
# list of results to return
310
310
reslist <- list (stage1 = stage1 ,
@@ -314,30 +314,30 @@ msmm_tsls_alt <- function(x, y, z) {
314
314
return (reslist )
315
315
}
316
316
317
- msmmMoments <- function (theta , x ){
317
+ msmmMoments <- function (theta , x ) {
318
318
# extract variables from x
319
- Y <- as.matrix(x [," y" ])
319
+ Y <- as.matrix(x [, " y" ])
320
320
xcolstop <- length(theta )
321
- X <- as.matrix(x [,2 : xcolstop ])
321
+ X <- as.matrix(x [, 2 : xcolstop ])
322
322
zcolstart <- 1 + length(theta ) # 1 is y, length(theta) is nX
323
323
zcolstop <- ncol(x )
324
- Z <- as.matrix(x [,zcolstart : zcolstop ])
324
+ Z <- as.matrix(x [, zcolstart : zcolstop ])
325
325
nZ <- zcolstop - zcolstart + 1
326
326
nZp1 <- nZ + 1
327
327
328
328
linearpredictor <- - 1 * X %*% as.matrix(theta [- 1 ])
329
329
330
330
# moments
331
331
moments <- matrix (nrow = nrow(x ), ncol = nZp1 , NA )
332
- moments [,1 ] <- (Y * exp(linearpredictor ) - theta [1 ])
332
+ moments [, 1 ] <- (Y * exp(linearpredictor ) - theta [1 ])
333
333
for (i in 1 : nZ ) {
334
334
j <- i + 1
335
- moments [,j ] <- (Y * exp(linearpredictor ) - theta [1 ])* Z [,i ]
335
+ moments [, j ] <- (Y * exp(linearpredictor ) - theta [1 ])* Z [, i ]
336
336
}
337
337
return (moments )
338
338
}
339
339
340
- msmm_gmm <- function (x , y , z , xnames , t0 ){
340
+ msmm_gmm <- function (x , y , z , xnames , t0 ) {
341
341
342
342
x <- as.matrix(x )
343
343
dat = data.frame (y , x , z )
0 commit comments