Skip to content

Commit

Permalink
try appveyor fix; re-style code; update docs; update pkgdown site
Browse files Browse the repository at this point in the history
  • Loading branch information
nhejazi committed Apr 19, 2018
1 parent 61fa138 commit fa496b2
Show file tree
Hide file tree
Showing 63 changed files with 1,687 additions and 454 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ md:
Rscript -e "rmarkdown::render('README.Rmd', output_file = 'README.md')"

site:
Rscript -e "rmarkdown::render('README-norefs.Rmd', output_file = 'README.md')"
Rscript -e "rmarkdown::render('README.Rmd', output_file = 'README.md')"
Rscript -e "pkgdown::build_site()"

check:
Expand Down
3 changes: 2 additions & 1 deletion R/censoring_estimate.R
Expand Up @@ -170,7 +170,8 @@ estimateCensoring <- function(dataList,
g_dC <-
suppressWarnings(
1 - predict(
ctimeMod, newdata = x[, c(
ctimeMod,
newdata = x[, c(
"t", "trt",
names(adjustVars)
)],
Expand Down
18 changes: 12 additions & 6 deletions R/checkInputs.R
Expand Up @@ -306,7 +306,8 @@ checkInputs <- function(ftime,
glm.ftime <- paste0(
"-1 + ", paste0(
"I(t == ", unique(ftime[ftype > 0]),
")", collapse = "+"
")",
collapse = "+"
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype > 0]), ")",
Expand All @@ -323,7 +324,8 @@ checkInputs <- function(ftime,
glm.ctime <- paste0(
"-1 + ", paste0(
"I(t == ", unique(ftime[ftype == 0]),
")", collapse = "+"
")",
collapse = "+"
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype == 0]), ")",
Expand All @@ -342,7 +344,8 @@ checkInputs <- function(ftime,
glm.ctime <- paste0(
"-1 + ", paste0(
"I(t == ", unique(ftime[ftype == 0]),
")", collapse = "+"
")",
collapse = "+"
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype == 0]), ")",
Expand All @@ -354,7 +357,8 @@ checkInputs <- function(ftime,
glm.ftime <- paste0(
"-1 + ", paste0(
"I(t==", unique(ftime[ftype > 0]),
")", collapse = "+"
")",
collapse = "+"
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype > 0]), ")",
Expand Down Expand Up @@ -382,15 +386,17 @@ checkInputs <- function(ftime,
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype == 0]),
")", collapse = "+"
")",
collapse = "+"
)
)
}
if (is.null(glm.ftime)) {
glm.ftime <- paste0(
"-1 + ", paste0(
"I(t == ", unique(ftime[ftype > 0]),
")", collapse = "+"
")",
collapse = "+"
), "+",
paste0(
"I(trt*t == ", unique(ftime[ftype > 0]), ")",
Expand Down
61 changes: 30 additions & 31 deletions R/glm_fast.R
Expand Up @@ -35,37 +35,36 @@ fast_glm <- function(reg_form, data, family, ...) {
)[[1]][1])

# fit speedglm or glm as appropriate
out <- tryCatch(
{
# Obviously, a sparse design matrix is not used when fitting an intercept
# model. In such cases, 'sparse=TRUE' is an inappropriate choice, though
# this is only expected when estimating the treatment mechanism.
speedglm::speedglm(
formula = reg_form,
data = data,
family = family,
method = "Cholesky",
sparse = ifelse(calling_fun == "estimateTreatment",
FALSE, TRUE
),
trace = FALSE,
...
)
},
error = function(cond) {
message(paste0(
"'speedglm' ran into an error in ", calling_fun,
".", "'glm' will be used instead."
))
# Choose a return value in case of error
mod <- stats::glm(
formula = reg_form,
data = data,
family = family,
...
)
return(mod)
}
out <- tryCatch({
# Obviously, a sparse design matrix is not used when fitting an intercept
# model. In such cases, 'sparse=TRUE' is an inappropriate choice, though
# this is only expected when estimating the treatment mechanism.
speedglm::speedglm(
formula = reg_form,
data = data,
family = family,
method = "Cholesky",
sparse = ifelse(calling_fun == "estimateTreatment",
FALSE, TRUE
),
trace = FALSE,
...
)
},
error = function(cond) {
message(paste0(
"'speedglm' ran into an error in ", calling_fun,
".", "'glm' will be used instead."
))
# Choose a return value in case of error
mod <- stats::glm(
formula = reg_form,
data = data,
family = family,
...
)
return(mod)
}
)
return(out)
}
6 changes: 4 additions & 2 deletions R/hazards_estimate.R
Expand Up @@ -117,7 +117,8 @@ estimateHazards <- function(dataList,
dataList <- lapply(dataList, function(x, j) {
suppressWarnings(
x[[paste0("Q", j, "PseudoHaz")]] <- predict(
Qj_mod, newdata = x,
Qj_mod,
newdata = x,
type = "response"
)
)
Expand Down Expand Up @@ -215,7 +216,8 @@ estimateHazards <- function(dataList,
dataList <- lapply(dataList, function(x, j) {
suppressWarnings(
x[[paste0("Q", j, "PseudoHaz")]] <- predict(
Qj_mod, onlySL = TRUE,
Qj_mod,
onlySL = TRUE,
newdata = x[, c("t", "trt", names(adjustVars))]
)[[1]]
)
Expand Down
6 changes: 4 additions & 2 deletions R/iterated_mean_fluctuate.R
Expand Up @@ -84,7 +84,8 @@ fluctuateIteratedMean <- function(wideDataList, t, uniqtrt, whichJ, allJ, t0,
flucForm <- paste(
outcomeName, "~ -1 + offset(stats::qlogis(Q", whichJ,
".", t, ")) +",
paste0("H", uniqtrt, ".", t, collapse = "+"), sep = ""
paste0("H", uniqtrt, ".", t, collapse = "+"),
sep = ""
)

if (!Gcomp) {
Expand All @@ -101,7 +102,8 @@ fluctuateIteratedMean <- function(wideDataList, t, uniqtrt, whichJ, allJ, t0,
wideDataList <- lapply(wideDataList, function(x, t) {
suppressWarnings(
x[[paste0("Q", whichJ, "star.", t)]] <- predict(
flucMod, newdata = x,
flucMod,
newdata = x,
type = "response"
)
)
Expand Down
3 changes: 2 additions & 1 deletion R/makeWideDataList.R
Expand Up @@ -69,7 +69,8 @@ makeWideDataList <- function(dat,
))],
direction = "wide", timevar = "t", idvar = "id"
)
, row.names = NULL
,
row.names = NULL
)
out[, paste0("C.", 1:t0)] <- 0
names(out)[1:(ncol(adjustVars))] <- names(adjustVars)
Expand Down
3 changes: 2 additions & 1 deletion R/printing.R
Expand Up @@ -50,7 +50,8 @@ print.tp.survtmle <- function(x, ...) {

# structure point estimates
est_only <- t(matrix(
unlist(lapply(x, FUN = `[[`, "est")), ncol = len_groups,
unlist(lapply(x, FUN = `[[`, "est")),
ncol = len_groups,
byrow = TRUE
))
est_only <- as.data.frame(est_only)
Expand Down
3 changes: 2 additions & 1 deletion R/timepoints.R
Expand Up @@ -113,7 +113,8 @@ timepoints <- function(object, times, returnModels = FALSE) {
outList[[ct]] <- list(
est = rep(0, length(object$est)),
var = matrix(
NA, nrow = length(object$est),
NA,
nrow = length(object$est),
ncol = length(object$est)
)
)
Expand Down
7 changes: 4 additions & 3 deletions R/tmle_hazard.R
Expand Up @@ -277,7 +277,7 @@ hazard_tmle <- function(ftime,
infCurves <- dat[, grep("D.j", names(dat))]
meanIC <- colMeans(infCurves)

attr(dataList, "fluc") <- rep(Inf, ntrt * nJ ^ 2)
attr(dataList, "fluc") <- rep(Inf, ntrt * nJ^2)
ct <- 0
while (any(abs(meanIC) > tol) & ct <= maxIter) {
ct <- ct + 1
Expand Down Expand Up @@ -319,15 +319,16 @@ hazard_tmle <- function(ftime,
for (z in uniqtrt) {
eval(parse(text = paste(
"est <- rbind(est, dat$margF", j, ".z", z,
".t0[1])", sep = ""
".t0[1])",
sep = ""
)))
rowNames <- c(rowNames, paste(c(z, j), collapse = " "))
}
}
row.names(est) <- rowNames

# calculate standard error
var <- t(as.matrix(infCurves)) %*% as.matrix(infCurves) / n ^ 2
var <- t(as.matrix(infCurves)) %*% as.matrix(infCurves) / n^2
row.names(var) <- colnames(var) <- rowNames

out <- list(
Expand Down
14 changes: 9 additions & 5 deletions R/tmle_mean.R
Expand Up @@ -294,13 +294,15 @@ mean_tmle <- function(ftime,
for (z in seq_along(uniqtrt)) {
thisEst <- eval(parse(text = paste(
"mean(wideDataList[[", z + 1, "]]$Q",
j, "star.1)", sep = ""
j, "star.1)",
sep = ""
)))
est <- rbind(est, thisEst)
rowNames <- c(rowNames, paste(c(uniqtrt[z], j), collapse = " "))
eval(parse(text = paste(
"wideDataList[[1]]$Q", j, "star.0.Z", uniqtrt[z],
" <- rep(thisEst,n)", sep = ""
" <- rep(thisEst,n)",
sep = ""
)))
eval(parse(text = paste(
"wideDataList[[1]]$Q", j, "star.1.Z", uniqtrt[z],
Expand All @@ -323,14 +325,16 @@ mean_tmle <- function(ftime,
"star.", t, " <- wideDataList[[1]]$H",
uniqtrt[z], ".", t,
"*(wideDataList[[1]][,outcomeName] - wideDataList[[1]]$Q",
j, "star.", t, ")", sep = ""
j, "star.", t, ")",
sep = ""
)))
}
eval(parse(text = paste(
"wideDataList[[1]]$D.Z", uniqtrt[z], ".", j,
"star.0 <- wideDataList[[1]]$Q", j, "star.1.Z",
uniqtrt[z], " - wideDataList[[1]]$Q", j,
"star.0.Z", uniqtrt[z], sep = ""
"star.0.Z", uniqtrt[z],
sep = ""
)))
ind <- eval(parse(text = paste(
"grep('D.Z", uniqtrt[z], ".", j,
Expand All @@ -351,7 +355,7 @@ mean_tmle <- function(ftime,
drop = FALSE
]
meanIC <- apply(infCurves, MARGIN = 2, FUN = mean)
var <- t(as.matrix(infCurves)) %*% as.matrix(infCurves) / (n ^ 2)
var <- t(as.matrix(infCurves)) %*% as.matrix(infCurves) / (n^2)
row.names(var) <- colnames(var) <- rowNames

out <- list(
Expand Down

0 comments on commit fa496b2

Please sign in to comment.