diff --git a/R/BinomialGLMsClass.R b/R/BinomialGLMsClass.R index b627f74..a8eccec 100644 --- a/R/BinomialGLMsClass.R +++ b/R/BinomialGLMsClass.R @@ -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) diff --git a/R/BinomialMLclass.R b/R/BinomialMLclass.R index 25a97b0..76a4e67 100644 --- a/R/BinomialMLclass.R +++ b/R/BinomialMLclass.R @@ -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, @@ -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) }, diff --git a/R/BinomialModelClass.R b/R/BinomialModelClass.R index b408fee..4619a19 100644 --- a/R/BinomialModelClass.R +++ b/R/BinomialModelClass.R @@ -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 @@ -77,7 +77,7 @@ 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']] @@ -85,7 +85,7 @@ BinaryOutcomeModel <- R6Class(classname = "BinaryOutcomeModel", } 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 @@ -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, ...) } diff --git a/R/reports.R b/R/reports.R index a9ddd58..af174b0 100644 --- a/R/reports.R +++ b/R/reports.R @@ -2,6 +2,7 @@ # @importFrom pander evalsOptions NULL +# nocov start #' Open file #' #' Tries to open a file with operating system's default program. @@ -31,6 +32,7 @@ openFileInOS <- function(f) { } } } +# nocov end # --------------------------------------------------------------------------------------------- #' Generate report(s) with modeling stats and survival estimates using pandoc. @@ -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)) diff --git a/R/zzz.R b/R/zzz.R index c07b97b..383fb76 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -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. @@ -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 @@ -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, diff --git a/man/set_all_stremr_options.Rd b/man/set_all_stremr_options.Rd index cc53050..c9008c0 100644 --- a/man/set_all_stremr_options.Rd +++ b/man/set_all_stremr_options.Rd @@ -13,7 +13,7 @@ set_all_stremr_options(fit.package = c("speedglm", "glm", "h2o"), \arguments{ \item{fit.package}{Specify the default package for performing model fitting: c("speedglm", "glm", "h2o")} -\item{fit.algorithm}{Specify the default fitting algorithm: c("glm", "gbm", "randomForest", "SuperLearner")} +\item{fit.algorithm}{Specify the default fitting algorithm: c("glm", "gbm", "randomForest", "deeplearning", "SuperLearner")} \item{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} diff --git a/man/survMSM.Rd b/man/survMSM.Rd index 9fef5e1..eb747d0 100644 --- a/man/survMSM.Rd +++ b/man/survMSM.Rd @@ -165,6 +165,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, diff --git a/tests/RUnit/RUnit_tests_04a_sims10K_speedglm.R b/tests/RUnit/RUnit_tests_04a_sims10K_speedglm.R index a49d7a1..1fb8ff5 100644 --- a/tests/RUnit/RUnit_tests_04a_sims10K_speedglm.R +++ b/tests/RUnit/RUnit_tests_04a_sims10K_speedglm.R @@ -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 diff --git a/tests/RUnit/RUnit_tests_04b_sims10K_h2oGLM.R b/tests/RUnit/RUnit_tests_04b_sims10K_h2oGLM.R index 58a543b..47672f6 100644 --- a/tests/RUnit/RUnit_tests_04b_sims10K_h2oGLM.R +++ b/tests/RUnit/RUnit_tests_04b_sims10K_h2oGLM.R @@ -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) } } diff --git a/tests/RUnit/RUnit_tests_04c_sims10K_h2o_ALL_ML.R b/tests/RUnit/RUnit_tests_04c_sims10K_h2o_ALL_ML.R index d4230d4..7629529 100644 --- a/tests/RUnit/RUnit_tests_04c_sims10K_h2o_ALL_ML.R +++ b/tests/RUnit/RUnit_tests_04c_sims10K_h2o_ALL_ML.R @@ -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 @@ -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 @@ -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 diff --git a/tests/RUnit/RUnit_tests_05_stremr_example_SL.R b/tests/RUnit/RUnit_tests_05_stremr_example_SL.R index 8801824..4c13add 100644 --- a/tests/RUnit/RUnit_tests_05_stremr_example_SL.R +++ b/tests/RUnit/RUnit_tests_05_stremr_example_SL.R @@ -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: diff --git a/tests/RUnit/RUnit_tests_06_direct_regression_def.R b/tests/RUnit/RUnit_tests_06_direct_regression_def.R index 5fb6b57..9e9bbfd 100644 --- a/tests/RUnit/RUnit_tests_06_direct_regression_def.R +++ b/tests/RUnit/RUnit_tests_06_direct_regression_def.R @@ -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) } diff --git a/tests/examples/4_survMSM_example.R b/tests/examples/4_survMSM_example.R index 871e128..2b4f585 100644 --- a/tests/examples/4_survMSM_example.R +++ b/tests/examples/4_survMSM_example.R @@ -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,