Skip to content

Commit

Permalink
catching pandoc dependency and giving informative error
Browse files Browse the repository at this point in the history
  • Loading branch information
osofr committed Oct 1, 2016
1 parent b355211 commit b1f078d
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 36 deletions.
4 changes: 3 additions & 1 deletion R/BinomialGLMsClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ BinomialGLM <- R6Class(classname = "BinomialGLM",
self$ParentModel <- ParentModel
self$classify <- ParentModel$classify
self$model_contrl <- ParentModel$model_contrl
if (!("glm" %in% fit.algorithm)) warning("over-riding fit.algorithm option with 'glm', since fit.package was set to 'speedglm' or 'glm'")

# if (!("glm" %in% fit.algorithm)) warning("over-riding fit.algorithm option with 'glm', since fit.package was set to 'speedglm' or 'glm'")

assert_that(any(c("glm", "speedglm") %in% fit.package))
self$fit.class <- fit.package
class(self$fit.class) <- c(class(self$fit.class), self$fit.class)
Expand Down
3 changes: 1 addition & 2 deletions R/BinomialMLclass.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ BinomialH2O <- R6Class(classname = "BinomialH2O",
portable = TRUE,
class = TRUE,
public = list(
fit.class = c("glm", "randomForest", "gbm", "deeplearning", "SuperLearner"),
fit.class = character(),
model.fit = list(coef = NA, fitfunname = NA, linkfun = NA, nobs = NA, params = NA, H2O.model.object = NA),
outfactors = NA,
nfolds = 5,
Expand Down Expand Up @@ -261,7 +261,6 @@ Please type this into the R terminal:

self$fit.class <- fit.algorithm
class(self$fit.class) <- c(class(self$fit.class), "h2o" %+% self$fit.class)
# class(self$model.fit) <- c(class(self$model.fit), "h2o" %+% self$fit.class)
invisible(self)
},

Expand Down
10 changes: 5 additions & 5 deletions R/BinomialModelClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ BinaryOutcomeModel <- R6Class(classname = "BinaryOutcomeModel",
is.fitted = FALSE,

binomialModelObj = NULL, # object of class binomialModelObj that is used in fitting / prediction, never saved (need to be initialized with $new())
fit.package = c("speedglm", "glm", "h2o"),
fit.algorithm = c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner"),
fit.package = character(),
fit.algorithm = character(),
model_contrl = list(),

n = NA_integer_, # number of rows in the input data
Expand All @@ -77,15 +77,15 @@ BinaryOutcomeModel <- R6Class(classname = "BinaryOutcomeModel",
} else {
self$fit.package <- reg$fit.package[1]
}
if (!(self$fit.package %in% c("speedglm", "glm", "h2o"))) stop("fit.package must be one of: 'speedglm', 'glm', 'h2o'")
if (!(self$fit.package %in% allowed.fit.package)) stop("fit.package must be one of: " %+% paste0(allowed.fit.package, collapse=", "))

if ("fit.algorithm" %in% names(self$model_contrl)) {
self$fit.algorithm <- self$model_contrl[['fit.algorithm']]
assert_that(is.character(self$fit.algorithm))
} else {
self$fit.algorithm <- reg$fit.algorithm[1]
}
if (!(self$fit.algorithm %in% c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner"))) stop("fit.algorithm must be one of: 'glm', 'gbm', 'randomForest', 'deeplearning', 'SuperLearner'")
if (!(self$fit.algorithm %in% allowed.fit.algorithm)) stop("fit.algorithm must be one of: " %+% paste0(allowed.fit.algorithm, collapse=", "))

assert_that(is.string(reg$outvar))
self$outvar <- reg$outvar
Expand Down Expand Up @@ -140,7 +140,7 @@ BinaryOutcomeModel <- R6Class(classname = "BinaryOutcomeModel",

if (inherits(model.fit, "try-error")) {
message("running " %+% self$binomialModelObj$fit.class %+% " with h2o has failed, trying to run speedglm as a backup...")
self$binomialModelObj <- BinomialGLM$new(fit.algorithm = "GLM", fit.package = "speedglm", ParentModel = self, ...)
self$binomialModelObj <- BinomialGLM$new(fit.algorithm = "glm", fit.package = "speedglm", ParentModel = self, ...)
self$binomialModelObj$params <- list(outvar = self$outvar, predvars = self$predvars, stratify = self$subset_exprs)
model.fit <- self$binomialModelObj$fit(data, self$outvar, self$predvars, self$subset_idx, ...)
}
Expand Down
10 changes: 10 additions & 0 deletions R/reports.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# @importFrom pander evalsOptions
NULL

# nocov start
#' Open file
#'
#' Tries to open a file with operating system's default program.
Expand Down Expand Up @@ -31,6 +32,7 @@ openFileInOS <- function(f) {
}
}
}
# nocov end

# ---------------------------------------------------------------------------------------------
#' Generate report(s) with modeling stats and survival estimates using pandoc.
Expand Down Expand Up @@ -67,6 +69,14 @@ make_report_rmd <- function(OData, MSM, NPMSM, TMLE, GCOMP, wts_data, SurvByRegi
file.name = getOption('stremr.file.name'), file.path = getOption('stremr.file.path'),
openFile = TRUE, keep_md = FALSE, keep_tex = FALSE, ...) {
optArgReport <- list(...)

if (!rmarkdown::pandoc_available(version = "1.12.3"))
stop(
"Report functionality requires pandoc (version 1.12.3 or higher).
Please install it.
For more information, go to: http://pandoc.org/installing.html",
call. = FALSE)

if ("author" %in% names(optArgReport)) {
author <- optArgReport[['author']]
assert_that(is.character(author))
Expand Down
18 changes: 9 additions & 9 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ gvars$tolerr <- 10^-12 # tolerance error: assume for abs(a-b) < gvars$toler
gvars$sVartypes <- list(bin = "binary", cat = "categor", cont = "contin")
gvars$noCENScat <- 0L # the reference category that designates continuation of follow-up

allowed.fit.package <- c("speedglm", "glm", "h2o")
allowed.fit.algorithm = c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner")
allowed.bin.method = c("equal.mass", "equal.len", "dhist")

#' Querying/setting a single \code{stremr} option
#'
#' To list all \code{stremr} options, just run this function without any parameters provided. To query only one value, pass the first parameter. To set that, use the \code{value} parameter too.
Expand Down Expand Up @@ -69,7 +73,7 @@ print_stremr_opts <- function() {
#' The preferred way to set options for \code{stremr} is to use \code{\link{stremrOptions}}, which allows specifying individual options without having to reset all other options.
#' To reset all options to their defaults simply run \code{set_all_stremr_options()} without any parameters/arguments.
#' @param fit.package Specify the default package for performing model fitting: c("speedglm", "glm", "h2o")
#' @param fit.algorithm Specify the default fitting algorithm: c("glm", "gbm", "randomForest", "SuperLearner")
#' @param fit.algorithm Specify the default fitting algorithm: c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner")
#' @param bin.method The method for choosing bins when discretizing and fitting the conditional continuous summary
#' exposure variable \code{sA}. The default method is \code{"equal.len"}, which partitions the range of \code{sA}
#' into equal length \code{nbins} intervals. Method \code{"equal.mass"} results in a data-adaptive selection of the bins
Expand Down Expand Up @@ -113,17 +117,13 @@ set_all_stremr_options <- function( fit.package = c("speedglm", "glm", "h2o"),
) {

old.opts <- gvars$opts
bin.method <- bin.method[1L]

fit.package <- fit.package[1L]
assert_that(fit.package %in% c("speedglm", "glm", "h2o"))

fit.algorithm <- fit.algorithm[1L]
if (!(fit.algorithm %in% c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner"))) stop("fit.algorithm must be one of: 'glm', 'gbm', 'randomForest', 'deeplearning', 'SuperLearner'")

if (!(bin.method %in% c("equal.len", "equal.mass", "dhist"))) {
stop("bin.method argument must be either 'equal.len', 'equal.mass' or 'dhist'")
}
bin.method <- bin.method[1]
if (!(fit.package %in% allowed.fit.package)) stop("fit.package must be one of: " %+% paste0(allowed.fit.package, collapse=", "))
if (!(fit.algorithm %in% allowed.fit.algorithm)) stop("fit.algorithm must be one of: " %+% paste0(allowed.fit.algorithm, collapse=", "))
if (!(bin.method %in% allowed.bin.method)) stop("bin.method must be one of: " %+% paste0(allowed.bin.method, collapse=", "))

opts <- list(
fit.package = fit.package,
Expand Down
2 changes: 1 addition & 1 deletion man/set_all_stremr_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/survMSM.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions tests/RUnit/RUnit_tests_04a_sims10K_speedglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,28 @@ test.speedglm.allestimators10Kdata <- function() {
# report.path <- "/home/ubuntu/stremr_example"
# file.path = report.path,
# test for opening file in local OS
make_report_rmd(OData, file.name = "sim.data.example.fup2", title = "Custom", author = "Jane Doe", openFile = FALSE)
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, file.name = "sim.data.example.fup2", title = "Custom", author = "Insert Author Name", openFile = FALSE)

make_report_rmd(OData, NPMSM = list(surv1, surv2), MSM = MSM.IPAW, GCOMP = list(gcomp_est1, gcomp_est2), TMLE = list(tmle_est_par1, tmle_est_par2),
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, NPMSM = list(surv1, surv2), MSM = MSM.IPAW, GCOMP = list(gcomp_est1, gcomp_est2), TMLE = list(tmle_est_par1, tmle_est_par2),
format = "html",
AddFUPtables = TRUE,
openFile = FALSE,
MSM.RDtables = get_MSM_RDs(MSM.IPAW, t.periods.RDs = c(12, 15), getSEs = TRUE),
TMLE.RDtables = get_TMLE_RDs(list(tmle_est_par1, tmle_est_par2), t.periods.RDs = c(1, 4)),
WTtables = get_wtsummary(MSM.IPAW$wts_data, cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Author Name", y_legend = 0.99, x_legend = 9.5)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.99, x_legend = 9.5)

make_report_rmd(OData, NPMSM = list(surv1, surv2), MSM = MSM.IPAW, TMLE = list(tmle_est_par1, tmle_est_par2),
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, NPMSM = list(surv1, surv2), MSM = MSM.IPAW, TMLE = list(tmle_est_par1, tmle_est_par2),
format = "pdf",
AddFUPtables = TRUE,
openFile = FALSE,
MSM.RDtables = get_MSM_RDs(MSM.IPAW, t.periods.RDs = c(12, 15), getSEs = TRUE),
TMLE.RDtables = get_TMLE_RDs(list(tmle_est_par1, tmle_est_par2), t.periods.RDs = c(1, 4)),
WTtables = get_wtsummary(MSM.IPAW$wts_data, cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Author Name", y_legend = 0.99, x_legend = 9.5)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.99, x_legend = 9.5)

# ---------------------------------------------------------------------------------------------------------
# TMLE / GCOMP with a stochastic intervention on MONITOR
Expand Down
5 changes: 3 additions & 2 deletions tests/RUnit/RUnit_tests_04b_sims10K_h2oGLM.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ test.h2oglm.IPW.MSM.10Kdata <- function() {
est_name = "IPAW", getSEs = TRUE)
# names(MSM.IPAW)
# MSM.IPAW$St
make_report_rmd(OData, MSM = MSM.IPAW,
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, MSM = MSM.IPAW,
AddFUPtables = TRUE, openFile = FALSE,
RDtables = get_MSM_RDs(MSM.IPAW, t.periods.RDs = c(12, 15), getSEs = TRUE),
WTtables = get_wtsummary(MSM.IPAW$wts_data, cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Jane Doe", y_legend = 0.95)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)
}

}
15 changes: 9 additions & 6 deletions tests/RUnit/RUnit_tests_04c_sims10K_h2o_ALL_ML.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ test.h2o.ALL.ML.allestimators10Kdata <- function() {
wts.St.dhigh <- getIPWeights(OData, intervened_TRT = "gTI.dhigh")
surv2 <- survNPMSM(wts.St.dhigh, OData)

make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
AddFUPtables = TRUE,
openFile = FALSE,
WTtables = get_wtsummary(list(wts.St.dlow, wts.St.dhigh), cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Jane Doe", y_legend = 0.95)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)

# ----------------------------------------------------------------
# FIT PROPENSITY SCORES WITH gbm
Expand All @@ -109,11 +110,12 @@ test.h2o.ALL.ML.allestimators10Kdata <- function() {
wts.St.dhigh <- getIPWeights(OData, intervened_TRT = "gTI.dhigh")
surv2 <- survNPMSM(wts.St.dhigh, OData)

make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
AddFUPtables = TRUE,
openFile = FALSE,
WTtables = get_wtsummary(list(wts.St.dlow, wts.St.dhigh), cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Jane Doe", y_legend = 0.95)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)

# ----------------------------------------------------------------
# FIT PROPENSITY SCORES WITH deeplearning
Expand All @@ -127,11 +129,12 @@ test.h2o.ALL.ML.allestimators10Kdata <- function() {
wts.St.dhigh <- getIPWeights(OData, intervened_TRT = "gTI.dhigh")
surv2 <- survNPMSM(wts.St.dhigh, OData)

make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, NPMSM = list(surv1, surv2), wts_data = list(wts.St.dlow, wts.St.dhigh),
AddFUPtables = TRUE,
openFile = FALSE,
WTtables = get_wtsummary(list(wts.St.dlow, wts.St.dhigh), cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Jane Doe", y_legend = 0.95)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)

# ---------------------------------------------------------------------------------------------------------
# TMLE w/ h2o random forest
Expand Down
5 changes: 3 additions & 2 deletions tests/RUnit/RUnit_tests_05_stremr_example_SL.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ test.h2oEnsemble <- function() {

# report.path <- "/set/your/report/path/"
# file.path = report.path
make_report_rmd(OData, openFile = FALSE,
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, openFile = FALSE,
# AddFUPtables = TRUE,
# MSM.RDtables = get_MSM_RDs(MSM.IPAW, t.periods.RDs = c(12, 15), getSEs = FALSE),
# WTtables = get_wtsummary(MSM.IPAW$wts_data, cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Oleg Sofrygin", y_legend = 0.95)
file.name = "sim.data.example.fup", title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)

# ---------------------------------------------------------------------------------------------------------
# ERROR CHECK: no hyper params for randomForest:
Expand Down
9 changes: 6 additions & 3 deletions tests/RUnit/RUnit_tests_06_direct_regression_def.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,16 @@ notest.savedSL.10Kdata <- function() {
IPW_estimates
St.dhigh3

report.path <- "/Users/olegsofrygin/Dropbox/KP/monitoring_simstudy/stremr_examples"
make_report_rmd(OData, openFile = FALSE,
# report.path <- "/Users/olegsofrygin/Dropbox/KP/monitoring_simstudy/stremr_examples"
if (rmarkdown::pandoc_available(version = "1.12.3"))
make_report_rmd(OData, openFile = FALSE,
# MSM = MSM.IPAW,
# AddFUPtables = TRUE,
# MSM.RDtables = get_MSM_RDs(MSM.IPAW, t.periods.RDs = c(12, 15), getSEs = FALSE),
# WTtables = get_wtsummary(MSM.IPAW$wts_data, cutoffs = c(0, 0.5, 1, 10, 20, 30, 40, 50, 100, 150), by.rule = TRUE),
file.name = "sim.data.example.fup", file.path = report.path, title = "Custom Report Title", author = "Oleg Sofrygin", y_legend = 0.95)
file.name = "sim.data.example.fup",
# file.path = report.path,
title = "Custom Report Title", author = "Insert Author Name", y_legend = 0.95)

h2o::h2o.shutdown(prompt = FALSE)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/4_survMSM_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ IPW_MSM_res$MSM.fit

# ------------------------------------------------------------------
# Generate automatic html report with results of the analysis
# This assumes that pandoc is already installed
# For more information, go to: http://pandoc.org/installing.html
# ------------------------------------------------------------------
\dontrun{
make_report_rmd(OData, MSM = IPW_MSM_res,
Expand Down

0 comments on commit b1f078d

Please sign in to comment.