Skip to content

Commit 4626796

Browse files
committed
Improvements from lintr
1 parent 0071be9 commit 4626796

File tree

6 files changed

+44
-53
lines changed

6 files changed

+44
-53
lines changed

R/fsw.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ fsw.ivreg <- function(object) {
113113
fswp[i] <- stats::pf(fsw[i], nendog, wldtst$Res.Df[2L], lower.tail = FALSE)
114114
}
115115

116-
fswres = cbind(fsw, fswdf, fswresdf, fswp)
117-
rownames(fswres) = namesendog
118-
colnames(fswres) = c("F value","d.f.","Residual d.f.","Pr(>F)")
116+
fswres <- cbind(fsw, fswdf, fswresdf, fswp)
117+
rownames(fswres) <- namesendog
118+
colnames(fswres) <- c("F value", "d.f.", "Residual d.f.", "Pr(>F)")
119119

120120
output <- list(fswres = fswres,
121121
namesendog = namesendog,

R/msmm.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,31 +155,31 @@ msmm <- function(formula, instruments, data, subset, na.action,
155155
# code from beginning for ivreg::ivreg()
156156
## set up model.frame() call
157157
cl <- match.call()
158-
if(missing(data)) data <- environment(formula)
158+
if (missing(data)) data <- environment(formula)
159159
mf <- match.call(expand.dots = FALSE)
160160
m <- match(c("formula", "data", "subset", "na.action", "weights", "offset"), names(mf), 0)
161161
mf <- mf[c(1, m)]
162162
mf$drop.unused.levels <- TRUE
163163
## handle instruments for backward compatibility
164-
if(!missing(instruments)) {
164+
if (!missing(instruments)) {
165165
formula <- Formula::as.Formula(formula, instruments)
166166
cl$instruments <- NULL
167167
cl$formula <- formula(formula)
168168
} else {
169169
formula <- Formula::as.Formula(formula)
170170
}
171-
if(length(formula)[2L] == 3L) formula <- Formula::as.Formula(
171+
if (length(formula)[2L] == 3L) formula <- Formula::as.Formula(
172172
formula(formula, rhs = c(2L, 1L), collapse = TRUE),
173173
formula(formula, lhs = 0L, rhs = c(3L, 1L), collapse = TRUE)
174174
)
175175
stopifnot(length(formula)[1L] == 1L, length(formula)[2L] %in% 1L:2L)
176176
## try to handle dots in formula
177177
has_dot <- function(formula) inherits(try(stats::terms(formula), silent = TRUE), "try-error")
178-
if(has_dot(formula)) {
178+
if (has_dot(formula)) {
179179
f1 <- formula(formula, rhs = 1L)
180180
f2 <- formula(formula, lhs = 0L, rhs = 2L)
181181
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))
183183
}
184184
## call model.frame()
185185
mf$formula <- formula
@@ -190,7 +190,7 @@ msmm <- function(formula, instruments, data, subset, na.action,
190190
mt <- stats::terms(formula, data = data)
191191
mtX <- stats::terms(formula, data = data, rhs = 1)
192192
X <- stats::model.matrix(mtX, mf, contrasts)
193-
if(length(formula)[2] < 2L) {
193+
if (length(formula)[2] < 2L) {
194194
mtZ <- NULL
195195
Z <- NULL
196196
} else {
@@ -234,13 +234,13 @@ msmm <- function(formula, instruments, data, subset, na.action,
234234
stop("With tsls and tslsalt, only 1 exposure variable is allowed.")
235235

236236
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)
238238
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)
240240
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])
242242
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])
244244

245245
class(output) <- append("msmm", class(output))
246246
output
@@ -268,10 +268,10 @@ msmm_tsls <- function(x, y, z) {
268268
logcrrse <- msm::deltamethod(~ log(-1 / x2), beta, estvar)
269269

270270
# 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)))
272272

273273
# baseline risk
274-
ey0ci <- cbind(stats::coef(fit), stats::confint(fit))[1,]
274+
ey0ci <- cbind(stats::coef(fit), stats::confint(fit))[1, ]
275275

276276
# list of results to return
277277
reslist <- list(stage1 = stage1,
@@ -304,7 +304,7 @@ msmm_tsls_alt <- function(x, y, z) {
304304
logcrrse <- msm::deltamethod(~ log(-1 * x2), beta, estvar)
305305

306306
# 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)))
308308

309309
# list of results to return
310310
reslist <- list(stage1 = stage1,
@@ -314,30 +314,30 @@ msmm_tsls_alt <- function(x, y, z) {
314314
return(reslist)
315315
}
316316

317-
msmmMoments <- function(theta, x){
317+
msmmMoments <- function(theta, x) {
318318
# extract variables from x
319-
Y <- as.matrix(x[,"y"])
319+
Y <- as.matrix(x[, "y"])
320320
xcolstop <- length(theta)
321-
X <- as.matrix(x[,2:xcolstop])
321+
X <- as.matrix(x[, 2:xcolstop])
322322
zcolstart <- 1 + length(theta) # 1 is y, length(theta) is nX
323323
zcolstop <- ncol(x)
324-
Z <- as.matrix(x[,zcolstart:zcolstop])
324+
Z <- as.matrix(x[, zcolstart:zcolstop])
325325
nZ <- zcolstop - zcolstart + 1
326326
nZp1 <- nZ + 1
327327

328328
linearpredictor <- -1 * X %*% as.matrix(theta[-1])
329329

330330
# moments
331331
moments <- matrix(nrow = nrow(x), ncol = nZp1, NA)
332-
moments[,1] <- (Y*exp(linearpredictor) - theta[1])
332+
moments[, 1] <- (Y*exp(linearpredictor) - theta[1])
333333
for (i in 1:nZ) {
334334
j <- i + 1
335-
moments[,j] <- (Y*exp(linearpredictor) - theta[1])*Z[,i]
335+
moments[, j] <- (Y*exp(linearpredictor) - theta[1])*Z[, i]
336336
}
337337
return(moments)
338338
}
339339

340-
msmm_gmm <- function(x, y, z, xnames, t0){
340+
msmm_gmm <- function(x, y, z, xnames, t0) {
341341

342342
x <- as.matrix(x)
343343
dat = data.frame(y, x, z)

R/tsps.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ tsps <- function(formula, instruments, data, subset, na.action,
328328

329329
if (tsps_env$anycovs) {
330330
stage2linpred <- as.matrix(cbind(linearpredictor, covariates))
331-
}
332-
else {
331+
} else {
333332
stage2linpred <- linearpredictor
334333
}
335334

@@ -387,8 +386,7 @@ tsps <- function(formula, instruments, data, subset, na.action,
387386

388387
if (tsps_env$anycovs) {
389388
stage2linpred <- as.matrix(cbind(linearpredictor, covariates))
390-
}
391-
else {
389+
} else {
392390
stage2linpred <- linearpredictor
393391
}
394392

@@ -405,7 +403,7 @@ tsps <- function(formula, instruments, data, subset, na.action,
405403
return(moments)
406404
}
407405

408-
tspsLogitMoments <- function(theta, x){
406+
tspsLogitMoments <- function(theta, x) {
409407
# extract variables from x
410408
Y <- as.matrix(x[,"y"])
411409
X <- x[, tsps_env$xnames]

R/tsri.R

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@ tsri <- function(formula, instruments, data, subset, na.action,
9898
mf <- mf[c(1, m)]
9999
mf$drop.unused.levels <- TRUE
100100
## handle instruments for backward compatibility
101-
if(!missing(instruments)) {
101+
if (!missing(instruments)) {
102102
formula <- Formula::as.Formula(formula, instruments)
103103
cl$instruments <- NULL
104104
cl$formula <- formula(formula)
105105
} else {
106106
formula <- Formula::as.Formula(formula)
107107
}
108-
if(length(formula)[2L] == 3L) formula <- Formula::as.Formula(
108+
if (length(formula)[2L] == 3L) formula <- Formula::as.Formula(
109109
formula(formula, rhs = c(2L, 1L), collapse = TRUE),
110110
formula(formula, lhs = 0L, rhs = c(3L, 1L), collapse = TRUE)
111111
)
112112
stopifnot(length(formula)[1L] == 1L, length(formula)[2L] %in% 1L:2L)
113113
## try to handle dots in formula
114114
has_dot <- function(formula) inherits(try(stats::terms(formula), silent = TRUE), "try-error")
115-
if(has_dot(formula)) {
115+
if (has_dot(formula)) {
116116
f1 <- formula(formula, rhs = 1L)
117117
f2 <- formula(formula, lhs = 0L, rhs = 2L)
118118
if (!has_dot(f1) && has_dot(f2)) formula <- Formula::as.Formula(f1,
@@ -175,18 +175,15 @@ tsri <- function(formula, instruments, data, subset, na.action,
175175
}
176176
if (link == "identity") {
177177
stage2 <- stats::lm(Y ~ X[,2] + res)
178-
}
179-
else if (link == "logadd") {
178+
} else if (link == "logadd") {
180179
stage2 <- stats::glm(Y ~ X[,2] + res, family = stats::poisson(link = "log"))
181-
}
182-
else if (link == "logmult") {
180+
} else if (link == "logmult") {
183181
Ystar <- Y
184182
Ystar[Y == 0] <- 0.001
185-
stage2 <- stats::glm(Ystar ~ X[,2] + res, family = stats::Gamma(link = "log"),
183+
stage2 <- stats::glm(Ystar ~ X[, 2] + res, family = stats::Gamma(link = "log"),
186184
control = list(maxit = 1E5))
187-
}
188-
else if (link == "logit") {
189-
stage2 <- stats::glm(Y ~ X[,2] + res, family = stats::binomial(link = "logit"))
185+
} else if (link == "logit") {
186+
stage2 <- stats::glm(Y ~ X[, 2] + res, family = stats::binomial(link = "logit"))
190187
}
191188
t0 <- c(t0, stats::coef(stage2))
192189

@@ -216,23 +213,20 @@ tsri <- function(formula, instruments, data, subset, na.action,
216213
x <- x[,!(colnames(x) %in% tsri_env$covariatenames), drop = FALSE]
217214
}
218215

219-
dat = data.frame(y, x, z)
216+
dat <- data.frame(y, x, z)
220217

221218
if (is.null(t0))
222219
t0 <- rep(0, ncol(x) + 1)
223220

224221
# gmm fit
225222
if (link == "identity") {
226223
fit <- gmm::gmm(tsriIdentityMoments, x = dat, t0 = t0, vcov = "iid")
227-
}
228-
else if (link == "logadd") {
224+
} else if (link == "logadd") {
229225
fit <- gmm::gmm(tsriLogaddMoments, x = dat, t0 = t0, vcov = "iid")
230-
}
231-
else if (link == "logmult") {
226+
} else if (link == "logmult") {
232227
fit <- gmm::gmm(tsriLogmultMoments, x = dat, t0 = t0, vcov = "iid",
233228
itermax = 1E7)
234-
}
235-
else if (link == "logit") {
229+
} else if (link == "logit") {
236230
fit <- gmm::gmm(tsriLogitMoments, x = dat, t0 = t0, vcov = "iid")
237231
}
238232

vignettes/compare-smm-fits.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ n <- 5000
2828
psi0 <- 0.5
2929
psi1 <- 0.2
3030
Z <- rbinom(n, 1, 0.5)
31-
X <- rbinom(n, 1, 0.7*Z + 0.2*(1 - Z))
31+
X <- rbinom(n, 1, 0.7*Z + 0.2*(1 - Z))
3232
m0 <- plogis(1 + 0.8*X - 0.39*Z)
33-
Y <- rbinom(n, 1, plogis(psi0*X + log(m0/(1 - m0))))
33+
Y <- rbinom(n, 1, plogis(psi0*X + log(m0/(1 - m0))))
3434
dat <- data.frame(Z, X, Y)
3535
```
3636

vignettes/f-statistic-comparison.Rmd

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ fsw(mod)
3939
## Comparison with F-statistic from lfe package
4040

4141
```{r}
42-
modst2 <- felm(lwage ~ 1 | 0 | (educ | exper ~ age + kidslt6 + kidsge6),
43-
data = dat)
42+
modst2 <- felm(lwage ~ 1 |
43+
0 | (educ | exper ~ age + kidslt6 + kidsge6), data = dat)
4444
summary(modst2)
45-
t(sapply(modst2$stage1$lhs,
46-
function(lh) waldtest(modst2$stage1,
47-
~ age | kidslt6 | kidsge6, lhs = lh)))
45+
t(sapply(modst2$stage1$lhs, function(lh)
46+
waldtest(modst2$stage1, ~ age | kidslt6 | kidsge6, lhs = lh)))
4847
condfstat(modst2, quantiles = c(0.025, 0.975))
4948
```
5049

0 commit comments

Comments
 (0)