diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 262a3bb5..9859588f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -47,7 +47,7 @@ jobs: - name: Create and populate check.Renviron file run: | - echo R_CHECK_DONTTEST_EXAMPLES=false >> ~/check.Renviron + echo R_CHECK_DONTTEST_EXAMPLES=true >> ~/check.Renviron echo R_LIBS=/usr/lib/R/site-library >> ~/check.Renviron shell: bash @@ -61,8 +61,8 @@ jobs: cmdstanr::check_cmdstan_toolchain(fix = TRUE) cmdstanr::install_cmdstan(cores = parallel::detectCores()) shell: Rscript {0} - + - uses: r-lib/actions/check-r-package@v2 with: - upload-snapshots: true - build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' + build_args: 'c("--no-manual", "--no-build-vignettes")' + args: 'c("--no-manual", "--ignore-vignettes")' \ No newline at end of file diff --git a/tests/testthat/test-brmsInterceptModels.R b/tests/testthat/test-brmsInterceptModels.R index aa55a8df..6fa84e32 100644 --- a/tests/testthat/test-brmsInterceptModels.R +++ b/tests/testthat/test-brmsInterceptModels.R @@ -28,6 +28,7 @@ test_that("Logistic Decay brms Model with Intercept", { skip_if_not_installed("brms") skip_if_not_installed("cmdstanr") skip_on_cran() + skip_on_ci() options(cmdstanr_warn_inits = FALSE) set.seed(123) logistic_df <- growthSim( @@ -52,6 +53,7 @@ test_that("Gompertz brms model pipeline", { skip_if_not_installed("brms") skip_if_not_installed("cmdstanr") skip_on_cran() + skip_on_ci() options(cmdstanr_warn_inits = FALSE) set.seed(123) simdf <- growthSim( @@ -79,6 +81,7 @@ test_that("intercept in submodel works", { skip_if_not_installed("brms") skip_if_not_installed("cmdstanr") skip_on_cran() + skip_on_ci() options(cmdstanr_warn_inits = FALSE) set.seed(123) simdf <- growthSim("gompertz", @@ -105,6 +108,7 @@ test_that("intercepts work in a changepoint model", { skip_if_not_installed("brms") skip_if_not_installed("cmdstanr") skip_on_cran() + skip_on_ci() options(cmdstanr_warn_inits = FALSE) set.seed(123) simdf <- growthSim("linear + linear", diff --git a/tests/testthat/test-brmsModels.R b/tests/testthat/test-brmsModels.R index cf529bc6..32f1f581 100644 --- a/tests/testthat/test-brmsModels.R +++ b/tests/testthat/test-brmsModels.R @@ -203,7 +203,8 @@ test_that("Hierarchical Model Works", { df = simdf, type = "brms", hierarchy = list("A" = "int_linear") ) - fit <- fitGrowth(ss, iter = 600, cores = 1, chains = 1, backend = "cmdstanr") + fit <- fitGrowth(ss, iter = 200, cores = 1, chains = 1, backend = "cmdstanr", + refresh = 0, silent = 2) expect_s3_class(fit, "brmsfit") p <- growthPlot(fit, ss$pcvrForm, df = ss$df) expect_s3_class(p, "ggplot") @@ -214,6 +215,7 @@ test_that("Hierarchical Model Works", { test_that("Changepoint model can be specified", { skip_if_not_installed("brms") skip_if_not_installed("cmdstanr") + skip_on_cran() set.seed(123) noise <- do.call(rbind, lapply(1:30, function(i) { chngpt <- c(20, 21) @@ -281,7 +283,8 @@ test_that("weibull survival", { prior <- c(0, 5) ss <- growthSS(model = model, form = form, df = df, start = prior) expect_equal(ss$prior$coef, c("groupa", "groupb")) - fit <- fitGrowth(ss, iter = 600, cores = 1, chains = 1, backend = "cmdstanr") + fit <- fitGrowth(ss, iter = 200, cores = 1, chains = 1, backend = "cmdstanr", + refresh = 0, silent = 2) expect_s3_class(fit, "brmsfit") plot <- growthPlot(fit, form = ss$pcvrForm, df = ss$df) expect_s3_class(plot, "ggplot") @@ -304,7 +307,8 @@ test_that("binomial survival", { ) prior <- c(0, 5) ss <- growthSS(model = model, form = form, df = df, start = prior) - fit <- fitGrowth(ss, iter = 600, cores = 1, chains = 1, backend = "cmdstanr") + fit <- fitGrowth(ss, iter = 200, cores = 1, chains = 1, backend = "cmdstanr", + refresh = 0, silent = 2) expect_s3_class(fit, "brmsfit") plot <- growthPlot(fit, form = ss$pcvrForm, df = ss$df) expect_s3_class(plot, "ggplot") @@ -312,8 +316,8 @@ test_that("binomial survival", { test_that(".brmSurvSS options all work", { set.seed(123) - df <- growthSim("logistic", - n = 20, t = 25, + df <- growthSim( + "logistic", n = 20, t = 25, params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) ) surv <- .survModelParser("survival weibull") @@ -358,7 +362,6 @@ test_that(".brmSurvSS options all work", { ) ) expect_equal(names(ss4), c("df", "family", "formula", "prior", "initfun", "pcvrForm")) - surv <- .survModelParser("survival binomial") ss <- suppressMessages( .brmsSurvSS( @@ -402,958 +405,1004 @@ test_that(".brmSurvSS options all work", { #* ***** `Not Run on the remote` ***** #* *********************************** -if (file.exists("/home/josh/Desktop/")) { - # only run locally, don't test for each R-CMD Check - test_that("Gompertz brms model pipeline", { - set.seed(123) - simdf <- growthSim("gompertz", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(0.25, 0.25)) - ) +test_that("Gompertz brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("gompertz", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(0.25, 0.25)) + ) - ss <- growthSS( - model = "gompertz", form = y ~ time | id / group, sigma = "int", - list("A" = 130, "B" = 10, "C" = 1), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C")) + ss <- growthSS( + model = "gompertz", form = y ~ time | id / group, sigma = "int", + list("A" = 130, "B" = 10, "C" = 1), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C")) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/gompertz_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("4 Parameter Logistic brms model pipeline", { - set.seed(123) - simdf <- growthSim("logistic4", - n = 20, t = 25, - params = list( - "A" = c(200, 160), - "B" = c(13, 11), - "C" = c(3, 2.5), - "D" = c(5, 20) - ) - ) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/gompertz_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - ss <- growthSS( - model = "logistic4", form = y ~ time | id / group, sigma = "int", - list("A" = 130, "B" = 10, "C" = 1, "D" = 2), - df = simdf, type = "brms" +test_that("4 Parameter Logistic brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic4", + n = 20, t = 25, + params = list( + "A" = c(200, 160), + "B" = c(13, 11), + "C" = c(3, 2.5), + "D" = c(5, 20) ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "D")) + ) + ss <- growthSS( + model = "logistic4", form = y ~ time | id / group, sigma = "int", + list("A" = 130, "B" = 10, "C" = 1, "D" = 2), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "D")) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/4PLogistic_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("5 Parameter Logistic brms model pipeline", { - set.seed(123) - simdf <- growthSim("logistic5", - n = 20, t = 25, - params = list( - "A" = c(200, 160), - "B" = c(13, 11), - "C" = c(2.75, 3.25), - "D" = c(5, 20), - "E" = c(1, 1.5) - ) - ) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/4PLogistic_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - ss <- growthSS( - model = "logistic5", form = y ~ time | id / group, sigma = "int", - list("A" = 130, "B" = 10, "C" = 1, "D" = 2, "E" = 0.75), - df = simdf, type = "brms" +test_that("5 Parameter Logistic brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic5", + n = 20, t = 25, + params = list( + "A" = c(200, 160), + "B" = c(13, 11), + "C" = c(2.75, 3.25), + "D" = c(5, 20), + "E" = c(1, 1.5) ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "D", "E")) + ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "logistic5", form = y ~ time | id / group, sigma = "int", + list("A" = 130, "B" = 10, "C" = 1, "D" = 2, "E" = 0.75), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "D", "E")) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/5PLogistic_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Monomolecular brms model pipeline", { - set.seed(123) - simdf <- growthSim("monomolecular", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(0.01, 0.08)) - ) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - ss <- growthSS( - model = "monomolecular", form = y ~ time | id / group, sigma = "int", - list("A" = 130, "B" = 1), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "B")) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/5PLogistic_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") +test_that("Monomolecular brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("monomolecular", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(0.01, 0.08)) + ) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/monomolecular_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Exponential brms model pipeline", { - set.seed(123) - simdf <- growthSim("exponential", - n = 20, t = 25, - params = list("A" = c(15, 12), "B" = c(0.1, 0.085)) - ) + ss <- growthSS( + model = "monomolecular", form = y ~ time | id / group, sigma = "int", + list("A" = 130, "B" = 1), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B")) - ss <- growthSS( - model = "exponential", form = y ~ time | id / group, sigma = "int", - list("A" = 10, "B" = 1), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "B")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/monomolecular_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/exponential_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Power law brms model pipeline", { - set.seed(123) - simdf <- growthSim("power law", - n = 20, t = 25, - params = list("A" = c(15, 12), "B" = c(0.75, 0.8)) - ) +test_that("Exponential brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("exponential", + n = 20, t = 25, + params = list("A" = c(15, 12), "B" = c(0.1, 0.085)) + ) - ss <- growthSS( - model = "power law", form = y ~ time | id / group, sigma = "linear", - list("A" = 10, "B" = 1), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "B")) + ss <- growthSS( + model = "exponential", form = y ~ time | id / group, sigma = "int", + list("A" = 10, "B" = 1), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B")) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/powerlaw_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("linear brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear", - n = 20, t = 25, - params = list("A" = c(15, 12)) - ) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/exponential_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - ss <- growthSS( - model = "linear", form = y ~ time | id / group, sigma = "int", - list("A" = 5), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A")) +test_that("Power law brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("power law", + n = 20, t = 25, + params = list("A" = c(15, 12), "B" = c(0.75, 0.8)) + ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "power law", form = y ~ time | id / group, sigma = "linear", + list("A" = 10, "B" = 1), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B")) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linear_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("logarithmic brms model pipeline", { - set.seed(123) - simdf <- growthSim("logarithmic", - n = 20, t = 25, - params = list("A" = c(15, 12)) - ) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - ss <- growthSS( - model = "logarithmic", form = y ~ time | id / group, sigma = "int", - list("A" = 5), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A")) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/powerlaw_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") +test_that("linear brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear", + n = 20, t = 25, + params = list("A" = c(15, 12)) + ) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/logarithmic_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("linear sub model with prior brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear", - n = 20, t = 25, - params = list("A" = c(15, 12)) - ) + ss <- growthSS( + model = "linear", form = y ~ time | id / group, sigma = "int", + list("A" = 5), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A")) - model <- "linear" - form <- y ~ time | id / group - sigma <- "linear" - priors <- list("A" = 5, "sigmaA" = 2) - df <- simdf - type <- "brms" - - ss <- growthSS( - model = "linear", form = y ~ time | id / group, sigma = "linear", - list("A" = 5, "sigmaA" = 2), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "sigmaA")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linear_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linear_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("GAM brms model pipeline", { - set.seed(123) - simdf <- growthSim("logistic", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) - ) +test_that("logarithmic brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logarithmic", + n = 20, t = 25, + params = list("A" = c(15, 12)) + ) - ss <- growthSS( - model = "gam", form = y ~ time | id / group, sigma = "int", - df = simdf, type = "brms" - ) + ss <- growthSS( + model = "logarithmic", form = y ~ time | id / group, sigma = "int", + list("A" = 5), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A")) - fit <- suppressWarnings(fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - )) - expect_s3_class(fit, "brmsfit") + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/gam_fitGrowth.png", plot, - width = 10, - height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/logarithmic_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) +test_that("linear sub model with prior brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear", + n = 20, t = 25, + params = list("A" = c(15, 12)) + ) + model <- "linear" + form <- y ~ time | id / group + sigma <- "linear" + priors <- list("A" = 5, "sigmaA" = 2) + df <- simdf + type <- "brms" - test_that("linear+linear brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear + linear", - n = 20, t = 25, - params = list("linear1A" = c(15), "changePoint1" = c(8), "linear2A" = c(3)) - ) + ss <- growthSS( + model = "linear", form = y ~ time | id / group, sigma = "linear", + list("A" = 5, "sigmaA" = 2), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "sigmaA")) - ss <- growthSS( - model = "linear + linear", form = y ~ time, sigma = "spline", - list("linear1A" = 10, "changePoint1" = 5, "linear2A" = 2), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "", "linear1A", "changePoint1", "linear2A")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linear_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLinear_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("linear+logistic brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear + logistic", - n = 20, t = 25, - params = list( - "linear1A" = c(15, 12), "changePoint1" = c(8, 6), - "logistic2A" = c(100, 150), "logistic2B" = c(10, 8), "logistic2C" = c(3, 2.5) - ) - ) +test_that("GAM brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) - ss <- growthSS( - model = "linear + logistic", form = y ~ time | id / group, sigma = "spline", - list( - "linear1A" = 10, "changePoint1" = 5, - "logistic2A" = 100, "logistic2B" = 10, "logistic2C" = 3 - ), - df = simdf, type = "brms" - ) + ss <- growthSS( + model = "gam", form = y ~ time | id / group, sigma = "int", + df = simdf, type = "brms" + ) - expect_equal(ss$prior$nlpar, c( - "", "", "linear1A", "changePoint1", - "logistic2A", "logistic2B", "logistic2C" - )) + fit <- suppressWarnings(fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + )) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/gam_fitGrowth.png", plot, + width = 10, + height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLogistic_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - test_that("linear+gam brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear + logistic", - n = 20, t = 25, # using logistic data, but modeling as a gam - params = list( - "linear1A" = c(15, 12), "changePoint1" = c(8, 6), - "logistic2A" = c(100, 150), "logistic2B" = c(10, 8), "logistic2C" = c(3, 2.5) - ) - ) +test_that("linear+linear brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear + linear", + n = 20, t = 25, + params = list("linear1A" = c(15), "changePoint1" = c(8), "linear2A" = c(3)) + ) - ss <- growthSS( - model = "linear + gam", form = y ~ time | id / group, sigma = "homo", - list("linear1A" = 10, "changePoint1" = 5), - df = simdf, type = "brms" - ) + ss <- growthSS( + model = "linear + linear", form = y ~ time, sigma = "spline", + list("linear1A" = 10, "changePoint1" = 5, "linear2A" = 2), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "", "linear1A", "changePoint1", "linear2A")) - expect_equal(ss$prior$nlpar, c("", "linear1A", "changePoint1")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLinear_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusGAM_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" +test_that("linear+logistic brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear + logistic", + n = 20, t = 25, + params = list( + "linear1A" = c(15, 12), "changePoint1" = c(8, 6), + "logistic2A" = c(100, 150), "logistic2B" = c(10, 8), "logistic2C" = c(3, 2.5) ) - expect_s3_class(plot, "ggplot") - }) + ) + ss <- growthSS( + model = "linear + logistic", form = y ~ time | id / group, sigma = "spline", + list( + "linear1A" = 10, "changePoint1" = 5, + "logistic2A" = 100, "logistic2B" = 10, "logistic2C" = 3 + ), + df = simdf, type = "brms" + ) - test_that("linear + linear + linear brms model pipeline", { - set.seed(123) - simdf <- growthSim("linear + linear + linear", - n = 25, t = 50, - params = list( - "linear1A" = c(10, 12), "changePoint1" = c(8, 6), "linear2A" = c(1, 2), - "changePoint2" = c(25, 30), "linear3A" = c(20, 24) - ) - ) + expect_equal(ss$prior$nlpar, c( + "", "", "linear1A", "changePoint1", + "logistic2A", "logistic2B", "logistic2C" + )) - ss <- growthSS( - model = "linear + linear + linear", form = y ~ time | id / group, sigma = "spline", - list("linear1A" = 10, "changePoint1" = 5, "linear2A" = 2, "changePoint2" = 15, "linear3A" = 5), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c( - "", "", "linear1A", "changePoint1", - "linear2A", "changePoint2", "linear3A" - )) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLogistic_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLinearPlusLinear_fitGrowth.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Logistic brms logistic sub model pipeline", { - set.seed(123) - simdf <- growthSim("logistic", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) - ) - ss <- growthSS( - model = "logistic", form = y ~ time | id / group, sigma = "logistic", - list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2), - df = simdf, type = "brms" +test_that("linear+gam brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear + logistic", + n = 20, t = 25, # using logistic data, but modeling as a gam + params = list( + "linear1A" = c(15, 12), "changePoint1" = c(8, 6), + "logistic2A" = c(100, 150), "logistic2B" = c(10, 8), "logistic2C" = c(3, 2.5) ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB", "sigmaC")) + ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) # that's fast - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "linear + gam", form = y ~ time | id / group, sigma = "homo", + list("linear1A" = 10, "changePoint1" = 5), + df = simdf, type = "brms" + ) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_logisticSubModel.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Logistic brms gompertz sub model pipeline", { - set.seed(123) - simdf <- growthSim("logistic", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) - ) + expect_equal(ss$prior$nlpar, c("", "linear1A", "changePoint1")) - ss <- growthSS( - model = "logistic", form = y ~ time | id / group, sigma = "gompertz", - list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB", "sigmaC")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusGAM_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_gompSubModel.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Logistic brms monomolecular sub model pipeline", { - set.seed(123) - simdf <- growthSim("logistic", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) - ) - ss <- growthSS( - model = "logistic", form = y ~ time | id / group, sigma = "monomolecular", - list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 5, "sigmaB" = 0.5), - df = simdf, type = "brms" +test_that("linear + linear + linear brms model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("linear + linear + linear", + n = 25, t = 50, + params = list( + "linear1A" = c(10, 12), "changePoint1" = c(8, 6), "linear2A" = c(1, 2), + "changePoint2" = c(25, 30), "linear3A" = c(20, 24) ) - expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB")) + ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "linear + linear + linear", form = y ~ time | id / group, sigma = "spline", + list("linear1A" = 10, "changePoint1" = 5, "linear2A" = 2, "changePoint2" = 15, "linear3A" = 5), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c( + "", "", "linear1A", "changePoint1", + "linear2A", "changePoint2", "linear3A" + )) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_monoSubModel.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - - test_that("int+int homoskedastic model pipeline", { - set.seed(123) - - noise <- do.call(rbind, lapply(1:30, function(i) { - chngpt <- rnorm(2, 18, 2) - noise_i <- rbind( - data.frame( - id = paste0("id_", i), time = 1:chngpt[1], group = "a", - y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) - ), - data.frame( - id = paste0("id_", i), time = 1:chngpt[2], group = "b", - y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) - ) - ) - return(noise_i) - })) - noise2 <- do.call(rbind, lapply(1:30, function(i) { - start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) - start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) - - noise2_i <- rbind( - data.frame( - id = paste0("id_", i), time = start1:40, group = "a", - y = c(runif(length(start1:40), 15, 50)) - ), - data.frame( - id = paste0("id_", i), time = start2:40, group = "b", - y = c(runif(length(start2:40), 15, 50)) - ) - ) - return(noise2_i) - })) - simdf <- rbind(noise, noise2) - - ss <- growthSS( - model = "int + int", form = y ~ time | id / group, sigma = "int", - list("int1" = 10, "changePoint1" = 10, "int2" = 20), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "int1", "changePoint1", "int2")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/linearPlusLinearPlusLinear_fitGrowth.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_fitGrowth.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("int+int fixed changepoint homoskedastic model pipeline", { - set.seed(123) - - noise <- do.call(rbind, lapply(1:30, function(i) { - chngpt <- c(20, 21) - noise_i <- rbind( - data.frame( - id = paste0("id_", i), time = 1:chngpt[1], group = "a", - y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) - ), - data.frame( - id = paste0("id_", i), time = 1:chngpt[2], group = "b", - y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) - ) - ) - return(noise_i) - })) - noise2 <- do.call(rbind, lapply(1:30, function(i) { - start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) - start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) - - noise2_i <- rbind( - data.frame( - id = paste0("id_", i), time = start1:40, group = "a", - y = c(runif(length(start1:40), 15, 50)) - ), - data.frame( - id = paste0("id_", i), time = start2:40, group = "b", - y = c(runif(length(start2:40), 15, 50)) - ) - ) - return(noise2_i) - })) - simdf <- rbind(noise, noise2) - - ss <- growthSS( - model = "int + int", form = y ~ time | id / group, sigma = "int", - list("int1" = 10, "fixedChangePoint1" = 20, "int2" = 20), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c("", "int1", "int2")) +test_that("Logistic brms logistic sub model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "logistic", form = y ~ time | id / group, sigma = "logistic", + list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB", "sigmaC")) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_fixedChngpt_fitGrowth.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) # that's fast + expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_logisticSubModel.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) +test_that("Logistic brms gompertz sub model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) - test_that("int+int thresholded homoskedasticity model pipeline", { - set.seed(123) + ss <- growthSS( + model = "logistic", form = y ~ time | id / group, sigma = "gompertz", + list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB", "sigmaC")) - noise <- do.call(rbind, lapply(1:30, function(i) { - chngpt <- rnorm(2, 18, 2) - noise_i <- rbind( - data.frame( - id = paste0("id_", i), time = 1:chngpt[1], group = "a", - y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) - ), - data.frame( - id = paste0("id_", i), time = 1:chngpt[2], group = "b", - y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) - ) - ) - return(noise_i) - })) - noise2 <- do.call(rbind, lapply(1:30, function(i) { - start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) - start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) - - noise2_i <- rbind( - data.frame( - id = paste0("id_", i), time = start1:40, group = "a", - y = c(runif(length(start1:40), 15, 50)) - ), - data.frame( - id = paste0("id_", i), time = start2:40, group = "b", - y = c(runif(length(start2:40), 15, 50)) - ) - ) - return(noise2_i) - })) - simdf <- rbind(noise, noise2) - - ss <- growthSS( - model = "int + int", form = y ~ time | id / group, sigma = "int + int", - list( - "int1" = 10, "changePoint1" = 10, "int2" = 20, "sigmaint1" = 10, - "sigmachangePoint1" = 10, "sigmaint2" = 10 - ), - df = simdf, type = "brms" - ) - expect_equal(ss$prior$nlpar, c( - "", "int1", "changePoint1", "int2", "sigmaint1", - "sigmachangePoint1", "sigmaint2" - )) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_gompSubModel.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_heteroskedastic_fitGrowth.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) +test_that("Logistic brms monomolecular sub model pipeline", { + skip_on_ci() + skip_on_cran() + set.seed(123) + simdf <- growthSim("logistic", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) + + ss <- growthSS( + model = "logistic", form = y ~ time | id / group, sigma = "monomolecular", + list("A" = 130, "B" = 10, "C" = 3, "sigmaA" = 5, "sigmaB" = 0.5), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "A", "B", "C", "sigmaA", "sigmaB")) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_monoSubModel.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - test_that("int + linear model and submodel pipeline", { - set.seed(123) - noise <- do.call(rbind, lapply(1:30, function(i) { - chngpt <- rnorm(2, 18, 2) - noise_i <- rbind( - data.frame( - id = paste0("id_", i), time = 1:chngpt[1], group = "a", - y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) - ), - data.frame( - id = paste0("id_", i), time = 1:chngpt[2], group = "b", - y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) - ) +test_that("int+int homoskedastic model pipeline", { + set.seed(123) + skip_on_ci() + skip_on_cran() + noise <- do.call(rbind, lapply(1:30, function(i) { + chngpt <- rnorm(2, 18, 2) + noise_i <- rbind( + data.frame( + id = paste0("id_", i), time = 1:chngpt[1], group = "a", + y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) + ), + data.frame( + id = paste0("id_", i), time = 1:chngpt[2], group = "b", + y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) ) - return(noise_i) - })) - signal <- growthSim("linear", - n = 30, t = 20, - params = list("A" = c(3, 5)) ) - signal <- do.call(rbind, lapply(unique(paste0(signal$id, signal$group)), function(int) { - noisesub <- noise[paste0(noise$id, noise$group) == int, ] - signalSub <- signal[paste0(signal$id, signal$group) == int, ] - y_end <- noisesub[noisesub$time == max(noisesub$time), "y"] - signalSub$time <- signalSub$time + max(noisesub$time) - signalSub$y <- y_end + signalSub$y - return(signalSub) - })) - simdf <- rbind(noise, signal) - ss <- growthSS( - model = "int + linear", form = y ~ time | id / group, sigma = "int + linear", - list( - "int1" = 10, "changePoint1" = 10, "linear2A" = 20, "sigmaint1" = 10, - "sigmachangePoint1" = 10, "sigmalinear2A" = 10 + return(noise_i) + })) + noise2 <- do.call(rbind, lapply(1:30, function(i) { + start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) + start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) + + noise2_i <- rbind( + data.frame( + id = paste0("id_", i), time = start1:40, group = "a", + y = c(runif(length(start1:40), 15, 50)) ), - df = simdf, type = "brms" + data.frame( + id = paste0("id_", i), time = start2:40, group = "b", + y = c(runif(length(start2:40), 15, 50)) + ) ) - expect_equal(ss$prior$nlpar, c( - "", "int1", "changePoint1", "linear2A", "sigmaint1", - "sigmachangePoint1", "sigmalinear2A" - )) + return(noise2_i) + })) + simdf <- rbind(noise, noise2) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + ss <- growthSS( + model = "int + int", form = y ~ time | id / group, sigma = "int", + list("int1" = 10, "changePoint1" = 10, "int2" = 20), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "int1", "changePoint1", "int2")) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df, timeRange = 1:40) - ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusLinear_heteroskedIntPlusLinear_fitGrowth.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - - test_that("int + Logistic brms int+spline sub model pipeline", { - set.seed(123) - noise <- do.call(rbind, lapply(1:30, function(i) { - chngpt <- rnorm(2, 18, 2) - noise_i <- rbind( - data.frame( - id = paste0("id_", i), time = 1:chngpt[1], group = "a", - y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) - ), - data.frame( - id = paste0("id_", i), time = 1:chngpt[2], group = "b", - y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) - ) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_fitGrowth.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) + +test_that("int+int fixed changepoint homoskedastic model pipeline", { + set.seed(123) + skip_on_ci() + skip_on_cran() + noise <- do.call(rbind, lapply(1:30, function(i) { + chngpt <- c(20, 21) + noise_i <- rbind( + data.frame( + id = paste0("id_", i), time = 1:chngpt[1], group = "a", + y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) + ), + data.frame( + id = paste0("id_", i), time = 1:chngpt[2], group = "b", + y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) ) - return(noise_i) - })) - signal <- growthSim("logistic", - n = 20, t = 30, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) ) - signal <- do.call(rbind, lapply(unique(paste0(signal$id, signal$group)), function(int) { - noisesub <- noise[paste0(noise$id, noise$group) == int, ] - signalSub <- signal[paste0(signal$id, signal$group) == int, ] - y_end <- noisesub[noisesub$time == max(noisesub$time), "y"] - signalSub$time <- signalSub$time + max(noisesub$time) - signalSub$y <- y_end + signalSub$y - return(signalSub) - })) - simdf <- rbind(noise, signal) - simdf <- simdf[simdf$time < 45, ] - - ss <- growthSS( - model = "int+logistic", form = y ~ time | id / group, sigma = "int + spline", - list( - "int1" = 5, "changePoint1" = 10, "logistic2A" = 130, "logistic2B" = 10, "logistic2C" = 3, - "sigmaint1" = 5, "sigmachangePoint1" = 15 + return(noise_i) + })) + noise2 <- do.call(rbind, lapply(1:30, function(i) { + start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) + start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) + + noise2_i <- rbind( + data.frame( + id = paste0("id_", i), time = start1:40, group = "a", + y = c(runif(length(start1:40), 15, 50)) ), - df = simdf, type = "brms" + data.frame( + id = paste0("id_", i), time = start2:40, group = "b", + y = c(runif(length(start2:40), 15, 50)) + ) ) + return(noise2_i) + })) + simdf <- rbind(noise, noise2) + ss <- growthSS( + model = "int + int", form = y ~ time | id / group, sigma = "int", + list("int1" = 10, "fixedChangePoint1" = 20, "int2" = 20), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c("", "int1", "int2")) - expect_equal(ss$prior$nlpar, c( - "", "int1", "changePoint1", "logistic2A", "logistic2B", "logistic2C", - "sigmaint1", "sigmachangePoint1" - )) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_fixedChngpt_fitGrowth.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/intPluslogistic_intPlusGAMSubModel.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - test_that("fixed and estimated changepoints can be mixed in growth formula", { - simdf1 <- growthSim( - model = "logistic", n = 20, t = 20, - params = list("A" = c(180, 160), "B" = c(9, 11), "C" = c(3, 3.5)) +test_that("int+int thresholded homoskedasticity model pipeline", { + set.seed(123) + skip_on_ci() + skip_on_cran() + noise <- do.call(rbind, lapply(1:30, function(i) { + chngpt <- rnorm(2, 18, 2) + noise_i <- rbind( + data.frame( + id = paste0("id_", i), time = 1:chngpt[1], group = "a", + y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) + ), + data.frame( + id = paste0("id_", i), time = 1:chngpt[2], group = "b", + y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) + ) ) + return(noise_i) + })) + noise2 <- do.call(rbind, lapply(1:30, function(i) { + start1 <- max(noise[noise$id == paste0("id_", i) & noise$group == "a", "time"]) + start2 <- max(noise[noise$id == paste0("id_", i) & noise$group == "b", "time"]) - simdf2 <- growthSim( - model = "linear + linear", n = 20, t = 20, - params = list("linear1A" = c(6, 8), "changePoint1" = c(7, 9), "linear2A" = c(15, 20)) + noise2_i <- rbind( + data.frame( + id = paste0("id_", i), time = start1:40, group = "a", + y = c(runif(length(start1:40), 15, 50)) + ), + data.frame( + id = paste0("id_", i), time = start2:40, group = "b", + y = c(runif(length(start2:40), 15, 50)) + ) ) + return(noise2_i) + })) + simdf <- rbind(noise, noise2) - simdf2_adj <- do.call(rbind, lapply(unique(paste0(simdf2$id, simdf2$group)), function(int) { - p1 <- simdf1[paste0(simdf1$id, simdf1$group) == int, ] - p2 <- simdf2[paste0(simdf2$id, simdf2$group) == int, ] - y_end_p1 <- p1[p1$time == max(p1$time), "y"] - p2$time <- p2$time + max(p1$time) - p2$y <- y_end_p1 + p2$y - return(p2) - })) - simdf <- rbind(simdf1, simdf2_adj) - - ss <- growthSS( - model = "logistic+linear+linear", form = y ~ time | id / group, - sigma = "logistic+linear", df = simdf, - start = list( - "logistic1A" = 130, "logistic1B" = 10, "logistic1C" = 3.5, - "fixedChangePoint1" = 20, "linear2A" = 5, "changePoint2" = 28, "linear3A" = 20, - "sigmalogistic1A" = 10, "sigmalogistic1B" = 12, "sigmalogistic1C" = 20, - "sigmafixedChangePoint1" = 20, "sigmalinear2A" = 3 - ), type = "brms" - ) + ss <- growthSS( + model = "int + int", form = y ~ time | id / group, sigma = "int + int", + list( + "int1" = 10, "changePoint1" = 10, "int2" = 20, "sigmaint1" = 10, + "sigmachangePoint1" = 10, "sigmaint2" = 10 + ), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c( + "", "int1", "changePoint1", "int2", "sigmaint1", + "sigmachangePoint1", "sigmaint2" + )) - expect_equal(ss$prior$nlpar, c( - "", "logistic1A", "logistic1B", "logistic1C", "linear2A", "changePoint2", - "linear3A", "sigmalogistic1A", "sigmalogistic1B", "sigmalogistic1C", - "sigmalinear2A" - )) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusInt_heteroskedastic_fitGrowth.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/threePart_fixedAndEstimatedChangepoint.png", - plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("logistic decay as a segment", { - simdf <- growthSim( - model = "logistic + logistic decay", n = 20, t = 45, - params = list( - "logistic1A" = c(120, 140), "logistic1B" = c(12, 10), "logistic1C" = c(3, 3.5), - "changePoint1" = c(20, 23), - "logistic2A" = c(90, 100), "logistic2B" = c(11, 13), "logistic2C" = c(3, 3.5) + + + +test_that("int + linear model and submodel pipeline", { + set.seed(123) + skip_on_ci() + skip_on_cran() + noise <- do.call(rbind, lapply(1:30, function(i) { + chngpt <- rnorm(2, 18, 2) + noise_i <- rbind( + data.frame( + id = paste0("id_", i), time = 1:chngpt[1], group = "a", + y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) + ), + data.frame( + id = paste0("id_", i), time = 1:chngpt[2], group = "b", + y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) ) ) - ss <- growthSS( - model = "logistic + logistic decay", form = y ~ time | id / group, sigma = "spline", - list( - "logistic1A" = 100, "logistic1B" = 10, "logistic1C" = 3, "changePoint1" = 20, - "logistic2A" = 100, "logistic2B" = 10, "logistic2C" = 3 + return(noise_i) + })) + signal <- growthSim("linear", + n = 30, t = 20, + params = list("A" = c(3, 5)) + ) + signal <- do.call(rbind, lapply(unique(paste0(signal$id, signal$group)), function(int) { + noisesub <- noise[paste0(noise$id, noise$group) == int, ] + signalSub <- signal[paste0(signal$id, signal$group) == int, ] + y_end <- noisesub[noisesub$time == max(noisesub$time), "y"] + signalSub$time <- signalSub$time + max(noisesub$time) + signalSub$y <- y_end + signalSub$y + return(signalSub) + })) + simdf <- rbind(noise, signal) + ss <- growthSS( + model = "int + linear", form = y ~ time | id / group, sigma = "int + linear", + list( + "int1" = 10, "changePoint1" = 10, "linear2A" = 20, "sigmaint1" = 10, + "sigmachangePoint1" = 10, "sigmalinear2A" = 10 + ), + df = simdf, type = "brms" + ) + expect_equal(ss$prior$nlpar, c( + "", "int1", "changePoint1", "linear2A", "sigmaint1", + "sigmachangePoint1", "sigmalinear2A" + )) + + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df, timeRange = 1:40) + ggsave("~/scripts/fahlgren_lab/labMeetings/intPlusLinear_heteroskedIntPlusLinear_fitGrowth.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) + + +test_that("int + Logistic brms int+spline sub model pipeline", { + set.seed(123) + skip_on_ci() + skip_on_cran() + noise <- do.call(rbind, lapply(1:30, function(i) { + chngpt <- rnorm(2, 18, 2) + noise_i <- rbind( + data.frame( + id = paste0("id_", i), time = 1:chngpt[1], group = "a", + y = c(runif(chngpt[1] - 1, 0, 20), rnorm(1, 5, 1)) ), - df = simdf, type = "brms" - ) - fit <- fitGrowth(ss, - backend = "cmdstanr", iter = 500, chains = 1, cores = 1, - refresh = 0, silent = 2 - ) - plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) - ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_plus_logisticDecay.png", plot, - width = 10, height = 6, dpi = 300, bg = "#ffffff" - ) - expect_s3_class(plot, "ggplot") - }) - - test_that("Test flexsurv model", { - set.seed(123) - model <- "survival gompertz" - form <- y > 100 ~ time | id / group - df <- growthSim("logistic", - n = 20, t = 25, - params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) - ) - ss <- growthSS(model = model, form = form, df = df, type = "flexsurv") - library(flexsurv) - fit <- fitGrowth(ss) - p <- growthPlot(fit, form = ss$pcvrForm, df = ss$df) - expect_s3_class(p, "ggplot") - }) - - test_that("Logistic Poisson Model", { - set.seed(123) - form <- y ~ time | id / group - df <- growthSim("count: logistic", - n = 20, t = 25, - params = list("A" = c(10, 12), "B" = c(13, 11), "C" = c(3, 3.5)) - ) - ss <- growthSS( - model = "poisson: logistic", form = y ~ time | id / group, sigma = NULL, - df = df, start = list("A" = 8, "B" = 10, "C" = 3) - ) - lapply(ss, head) - fit <- fitGrowth(ss, - iter = 600, cores = 1, chains = 1, backend = "cmdstanr", - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") - p <- growthPlot(fit, ss$pcvrForm, df = ss$df) - expect_s3_class(p, "ggplot") - }) - - test_that("Beta DRC Model", { - set.seed(123) - form <- y ~ time | id / group - df <- growthSim( - "beta", - n = 20, t = 50, - params = list( - "A" = c(10, 10), - "B" = c(1.25, 1.3), - "C" = c(20, 22), - "D" = c(5, 5), - "E" = c(30, 32) + data.frame( + id = paste0("id_", i), time = 1:chngpt[2], group = "b", + y = c(runif(chngpt[2] - 1, 0, 20), rnorm(1, 5, 1)) ) ) - #* consider using ss with nls to get ideas for parameters - ss <- growthSS( - model = "beta", form = y ~ time | id / group, sigma = NULL, - df = df, start = list("A" = 10, "B" = 1, "C" = 15, "D" = 3, "E" = 25) + return(noise_i) + })) + signal <- growthSim("logistic", + n = 20, t = 30, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) + signal <- do.call(rbind, lapply(unique(paste0(signal$id, signal$group)), function(int) { + noisesub <- noise[paste0(noise$id, noise$group) == int, ] + signalSub <- signal[paste0(signal$id, signal$group) == int, ] + y_end <- noisesub[noisesub$time == max(noisesub$time), "y"] + signalSub$time <- signalSub$time + max(noisesub$time) + signalSub$y <- y_end + signalSub$y + return(signalSub) + })) + simdf <- rbind(noise, signal) + simdf <- simdf[simdf$time < 45, ] + + ss <- growthSS( + model = "int+logistic", form = y ~ time | id / group, sigma = "int + spline", + list( + "int1" = 5, "changePoint1" = 10, "logistic2A" = 130, "logistic2B" = 10, "logistic2C" = 3, + "sigmaint1" = 5, "sigmachangePoint1" = 15 + ), + df = simdf, type = "brms" + ) + + + expect_equal(ss$prior$nlpar, c( + "", "int1", "changePoint1", "logistic2A", "logistic2B", "logistic2C", + "sigmaint1", "sigmachangePoint1" + )) + + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/intPluslogistic_intPlusGAMSubModel.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) + + +test_that("fixed and estimated changepoints can be mixed in growth formula", { + skip_on_cran() + skip_on_ci() + simdf1 <- growthSim( + model = "logistic", n = 20, t = 20, + params = list("A" = c(180, 160), "B" = c(9, 11), "C" = c(3, 3.5)) + ) + + simdf2 <- growthSim( + model = "linear + linear", n = 20, t = 20, + params = list("linear1A" = c(6, 8), "changePoint1" = c(7, 9), "linear2A" = c(15, 20)) + ) + + simdf2_adj <- do.call(rbind, lapply(unique(paste0(simdf2$id, simdf2$group)), function(int) { + p1 <- simdf1[paste0(simdf1$id, simdf1$group) == int, ] + p2 <- simdf2[paste0(simdf2$id, simdf2$group) == int, ] + y_end_p1 <- p1[p1$time == max(p1$time), "y"] + p2$time <- p2$time + max(p1$time) + p2$y <- y_end_p1 + p2$y + return(p2) + })) + simdf <- rbind(simdf1, simdf2_adj) + + ss <- growthSS( + model = "logistic+linear+linear", form = y ~ time | id / group, + sigma = "logistic+linear", df = simdf, + start = list( + "logistic1A" = 130, "logistic1B" = 10, "logistic1C" = 3.5, + "fixedChangePoint1" = 20, "linear2A" = 5, "changePoint2" = 28, "linear3A" = 20, + "sigmalogistic1A" = 10, "sigmalogistic1B" = 12, "sigmalogistic1C" = 20, + "sigmafixedChangePoint1" = 20, "sigmalinear2A" = 3 + ), type = "brms" + ) + + expect_equal(ss$prior$nlpar, c( + "", "logistic1A", "logistic1B", "logistic1C", "linear2A", "changePoint2", + "linear3A", "sigmalogistic1A", "sigmalogistic1B", "sigmalogistic1C", + "sigmalinear2A" + )) + + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/threePart_fixedAndEstimatedChangepoint.png", + plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) + +test_that("logistic decay as a segment", { + skip_on_ci() + skip_on_cran() + simdf <- growthSim( + model = "logistic + logistic decay", n = 20, t = 45, + params = list( + "logistic1A" = c(120, 140), "logistic1B" = c(12, 10), "logistic1C" = c(3, 3.5), + "changePoint1" = c(20, 23), + "logistic2A" = c(90, 100), "logistic2B" = c(11, 13), "logistic2C" = c(3, 3.5) ) - expect_s3_class(ss, "pcvrss") - tryCatch( - { - fit <- fitGrowth(ss, - iter = 600, cores = 1, chains = 1, backend = "cmdstanr", - refresh = 0, silent = 2 - ) - expect_s3_class(fit, "brmsfit") - p <- growthPlot(fit, ss$pcvrForm, df = ss$df) - expect_s3_class(p, "ggplot") - }, - error = function(err) {} + ) + ss <- growthSS( + model = "logistic + logistic decay", form = y ~ time | id / group, sigma = "spline", + list( + "logistic1A" = 100, "logistic1B" = 10, "logistic1C" = 3, "changePoint1" = 20, + "logistic2A" = 100, "logistic2B" = 10, "logistic2C" = 3 + ), + df = simdf, type = "brms" + ) + fit <- fitGrowth(ss, + backend = "cmdstanr", iter = 500, chains = 1, cores = 1, + refresh = 0, silent = 2 + ) + plot <- growthPlot(fit = fit, form = ss$pcvrForm, df = ss$df) + ggsave("~/scripts/fahlgren_lab/labMeetings/logistic_plus_logisticDecay.png", plot, + width = 10, height = 6, dpi = 300, bg = "#ffffff" + ) + expect_s3_class(plot, "ggplot") +}) + +test_that("Test flexsurv model", { + set.seed(123) + skip_on_ci() + skip_on_cran() + model <- "survival gompertz" + form <- y > 100 ~ time | id / group + df <- growthSim("logistic", + n = 20, t = 25, + params = list("A" = c(200, 160), "B" = c(13, 11), "C" = c(3, 3.5)) + ) + ss <- growthSS(model = model, form = form, df = df, type = "flexsurv") + library(flexsurv) + fit <- fitGrowth(ss) + p <- growthPlot(fit, form = ss$pcvrForm, df = ss$df) + expect_s3_class(p, "ggplot") +}) + +test_that("Logistic Poisson Model", { + set.seed(123) + skip_on_ci() + skip_on_cran() + form <- y ~ time | id / group + df <- growthSim("count: logistic", + n = 5, t = 10, + params = list("A" = c(10, 12), "B" = c(13, 11), "C" = c(3, 3.5)) + ) + ss <- growthSS( + model = "poisson: logistic", form = y ~ time | id / group, sigma = NULL, + df = df, start = list("A" = 8, "B" = 10, "C" = 3) + ) + fit <- fitGrowth(ss, + iter = 200, cores = 1, chains = 1, backend = "cmdstanr", + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + p <- growthPlot(fit, ss$pcvrForm, df = ss$df) + expect_s3_class(p, "ggplot") +}) + +test_that("Beta DRC Model", { + set.seed(123) + skip_on_ci() + skip_on_cran() + form <- y ~ time | id / group + df <- growthSim( + "beta", + n = 20, t = 50, + params = list( + "A" = c(10, 10), + "B" = c(1.25, 1.3), + "C" = c(20, 22), + "D" = c(5, 5), + "E" = c(30, 32) ) - }) -} + ) + #* consider using ss with nls to get ideas for parameters + ss <- growthSS( + model = "beta", form = y ~ time | id / group, sigma = NULL, + df = df, start = list("A" = 10, "B" = 1, "C" = 15, "D" = 3, "E" = 25) + ) + expect_s3_class(ss, "pcvrss") + tryCatch( + { + fit <- fitGrowth(ss, + iter = 200, cores = 1, chains = 1, backend = "cmdstanr", + refresh = 0, silent = 2 + ) + expect_s3_class(fit, "brmsfit") + p <- growthPlot(fit, ss$pcvrForm, df = ss$df) + expect_s3_class(p, "ggplot") + }, + error = function(err) {} + ) +}) diff --git a/tests/testthat/test-long_mv_workflow.R b/tests/testthat/test-long_mv_workflow.R index b921f447..b63822a2 100644 --- a/tests/testthat/test-long_mv_workflow.R +++ b/tests/testthat/test-long_mv_workflow.R @@ -7,13 +7,7 @@ test_that("reading mv github data as long works", { "main/pcv4-multi-value-traits.csv" ), mode = "long") - expect_equal(dim(mv), c(513720, 21)) - expect_equal(colnames(mv), c( - "camera", "imgtype", "zoom", "exposure", "gain", "frame", "rotation", - "lifter", "timestamp", "id", "barcode", "treatment", "velocity", - "cartag", "measurementlabel", "other", "image", "sample", "trait", - "value", "label" - )) + expect_equal(dim(mv), c(513720, 6)) mv$genotype <- substr(mv$barcode, 3, 5) mv$genotype <- ifelse(mv$genotype == "002", "B73", @@ -27,14 +21,14 @@ test_that("reading mv github data as long works", { ) # test pcv.time mv <- pcv.time(mv, timeCol = "timestamp", group = "barcode", plot = FALSE) - expect_equal(dim(mv), c(513720, 24)) - expect_equal(colnames(mv)[24], "DAS") + expect_equal(dim(mv), c(513720, 9)) + expect_equal(colnames(mv)[9], "DAS") # test pcv.outliers mvNoOutliers <- suppressWarnings(pcv.outliers( df = mv, phenotype = "hue_frequencies", naTo0 = FALSE, plot = TRUE, - group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode", "rotation") + group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode") )) pct_removed <- nrow(mvNoOutliers$data) / nrow(mv) @@ -43,11 +37,11 @@ test_that("reading mv github data as long works", { mvNoOutliers <- suppressWarnings(pcv.outliers( df = mv, phenotype = "hue_frequencies", naTo0 = FALSE, plot = FALSE, outlierMethod = "mahalanobis", - group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode", "rotation") + group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode") )) - pct_removed <- nrow(mvNoOutliers) / nrow(mv) - expect_equal(pct_removed, 0.945, tolerance = 0.015) + pct_kept <- nrow(mvNoOutliers) / nrow(mv) + expect_true(pct_kept > 0.95) #* test joyplot joyplot <- pcv.joyplot(mv[mv$DAS == 18, ], diff --git a/tests/testthat/test-mvSSModels.R b/tests/testthat/test-mvSSModels.R index fbedd138..df79c409 100644 --- a/tests/testthat/test-mvSSModels.R +++ b/tests/testthat/test-mvSSModels.R @@ -19,8 +19,9 @@ test_that("Test spectral index helpers", { #* `Non-Longitudinal Multi-Value Trait Models` set.seed(123) -mv_df <- mvSim(dists = list(rnorm = list(mean = 100, sd = 30)), wide = FALSE) -mv_df$group <- rep(c("a", "b"), times = 900) +mv_df <- mvSim(dists = list(rnorm = list(mean = 100, sd = 30)), wide = FALSE, + n_samples = 3, counts = 50) +mv_df$group <- rep(c("a", "b"), times = 270) mv_df <- mv_df[mv_df$value > 0, ] mv_df$label <- as.numeric(gsub("sim_", "", mv_df$variable)) @@ -35,7 +36,8 @@ test_that("Test brms mv trait non-longitudinal model skew model", { start = list("A" = 5), type = "brms", spectral_index = "ci_rededge" ) expect_equal(ss1$family, "skew_normal") - mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) + mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 200, chains = 1, cores = 1, + refresh = 0, silent = 2) expect_s3_class(mod1, "brmsfit") p <- growthPlot(mod1, ss1$pcvrForm, df = ss1$df) expect_s3_class(p, "ggplot") @@ -50,7 +52,8 @@ test_that("Test brms mv trait non-longitudinal model", { start = list("A" = 5), type = "brms", spectral_index = "none" ) expect_equal(ss1$family, "student") - mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) + mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 200, chains = 1, cores = 1, + refresh = 0, silent = 2) expect_s3_class(mod1, "brmsfit") p <- growthPlot(mod1, ss1$pcvrForm, df = ss1$df) expect_s3_class(p, "ggplot") @@ -74,6 +77,12 @@ test_that("Test nls mv trait non-longitudinal model", { test_that("Test nlrq mv trait non-longitudinal model", { skip_on_cran() + set.seed(123) + mv_df <- mvSim(dists = list(rnorm = list(mean = 100, sd = 30)), wide = FALSE, + n_samples = 10, counts = 1000) # this test needs larger data + mv_df$group <- rep(c("a", "b"), times = 900) + mv_df <- mv_df[mv_df$value > 0, ] + mv_df$label <- as.numeric(gsub("sim_", "", mv_df$variable)) ss1 <- mvSS( model = "linear", form = label | value ~ group, df = mv_df, tau = 0.5, start = list("A" = 5), type = "nlrq", spectral_index = "none" @@ -98,25 +107,21 @@ test_that("Test nlrq mv trait non-longitudinal model", { m1 <- mvSim( dists = list( rnorm = list(mean = 100, sd = 30), - rnorm = list(mean = 110, sd = 25), - rnorm = list(mean = 120, sd = 20), - rnorm = list(mean = 135, sd = 15) + rnorm = list(mean = 110, sd = 25) ), - wide = FALSE, n = 6 + wide = FALSE, n = 3, counts = 100 ) -m1$time <- rep(1:4, times = 6 * 180) +m1$time <- rep(1:2, times = 3 * 180) m2 <- mvSim( dists = list( rnorm = list(mean = 85, sd = 25), - rnorm = list(mean = 95, sd = 20), - rnorm = list(mean = 105, sd = 15), - rnorm = list(mean = 110, sd = 15) + rnorm = list(mean = 95, sd = 20) ), - wide = FALSE, n = 6 + wide = FALSE, n = 3, counts = 100 ) -m2$time <- rep(1:4, times = 6 * 180) +m2$time <- rep(1:2, times = 3 * 180) mv_df2 <- rbind(m1, m2) -mv_df2$group <- rep(c("a", "b"), each = 4320) +mv_df2$group <- rep(c("a", "b"), each = 1080) mv_df2 <- mv_df2[mv_df2$value > 0, ] mv_df2$label <- as.numeric(gsub("sim_", "", mv_df2$variable)) @@ -128,7 +133,8 @@ test_that("Test brms mv trait longitudinal model", { model = "linear", form = label | value ~ time | group, df = mv_df2, start = list("A" = 50), type = "brms", spectral_index = "none" ) - fit <- fitGrowth(ss_mv1, backend = "cmdstanr", iter = 600, chains = 1, cores = 1) + fit <- fitGrowth(ss_mv1, backend = "cmdstanr", iter = 100, chains = 1, cores = 1, + refresh = 0, silent = 2) expect_s3_class(fit, "brmsfit") p <- growthPlot(fit, ss_mv1$pcvrForm, df = ss_mv1$df) expect_s3_class(p, "ggplot") diff --git a/tests/testthat/test-survSS_helpers.R b/tests/testthat/test-survSS_helpers.R index ac0d1822..82eed860 100644 --- a/tests/testthat/test-survSS_helpers.R +++ b/tests/testthat/test-survSS_helpers.R @@ -60,3 +60,6 @@ test_that(".flexSurvSS returns errors and is covered", { expect_equal(names(ss2$formula), c("f1", "f2")) expect_true(identical(ss2$df, df2)) }) +if (!interactive()) { + dev.off() +} diff --git a/tests/testthat/test-sv-conjugate.R b/tests/testthat/test-sv-conjugate.R index a40cc791..be127b45 100644 --- a/tests/testthat/test-sv-conjugate.R +++ b/tests/testthat/test-sv-conjugate.R @@ -441,3 +441,7 @@ test_that("bivariate conjugate lognormal works", { p <- plot(out2) expect_s3_class(p, "ggplot") }) + +if (!interactive()) { + dev.off() +} diff --git a/tests/testthat/test-wide_mv_workflow.R b/tests/testthat/test-wide_mv_workflow.R index 9cdd2b4c..1c5214a9 100644 --- a/tests/testthat/test-wide_mv_workflow.R +++ b/tests/testthat/test-wide_mv_workflow.R @@ -7,7 +7,7 @@ test_that("reading mv github data as long works", { "https://media.githubusercontent.com/media/joshqsumner/pcvrTestData/", "main/pcv4-multi-value-traits.csv" ), mode = "wide", reader = "fread") - expect_equal(dim(mv), c(2854, 198)) + expect_equal(dim(mv), c(2854, 183)) mv$genotype <- substr(mv$barcode, 3, 5) mv$genotype <- ifelse(mv$genotype == "002", "B73", @@ -21,8 +21,8 @@ test_that("reading mv github data as long works", { ) # test pcv.time mv <- pcv.time(mv, timeCol = "timestamp", group = "barcode", plot = FALSE) - expect_equal(dim(mv), c(2854, 201)) - expect_equal(colnames(mv)[201], "DAS") + expect_equal(dim(mv), c(2854, 186)) + expect_equal(colnames(mv)[186], "DAS") # test pcv.outliers @@ -38,7 +38,7 @@ test_that("reading mv github data as long works", { mvNoOutliers <- suppressWarnings(pcv.outliers( df = mv, phenotype = phenotypes, naTo0 = FALSE, plot = TRUE, outlierMethod = "mahalanobis", - group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode", "rotation") + group = c("DAS", "genotype", "fertilizer"), cutoff = 3, plotgroup = c("barcode") )) pct_removed <- nrow(mvNoOutliers$data) / nrow(mv) @@ -65,7 +65,7 @@ test_that("reading mv github data as long works", { df = mv, group = c("DAS", "genotype", "fertilizer"), n_per_group = 2, - labelCol = "camera" # if only some of the id columns are there then error should be thrown + labelCol = "genotype" # if only some of the id columns are there then error should be thrown ) ) @@ -79,7 +79,6 @@ test_that("reading mv github data as long works", { ) expect_s3_class(emd$plot, "ggplot") expect_equal(dim(emd$data), c(110, 7)) - expect_equal(sum(emd$data$emd), 5684.034, tolerance = 0.01) #* test network net <- pcv.net(emd$data, filter = 0.05) @@ -87,3 +86,7 @@ test_that("reading mv github data as long works", { expect_equal(dim(net$nodes), c(10, 12)) expect_equal(dim(net$edges), c(30, 11)) }) + +if (!interactive()) { + dev.off() +} diff --git a/tests/testthat/test-wide_sv_workflow.R b/tests/testthat/test-wide_sv_workflow.R index e55930fc..51d2606e 100644 --- a/tests/testthat/test-wide_sv_workflow.R +++ b/tests/testthat/test-wide_sv_workflow.R @@ -77,14 +77,9 @@ test_that("reading sv github data as wide works", { model = "gompertz", form = area_pixels ~ DAS | barcode / group, sigma = "spline", df = sv, start = list("A" = 130, "B" = 10, "C" = 0.5) ) - expect_type(ss, "list") - - expect_s3_class(ss[["formula"]], "brmsformula") - expect_s3_class(ss[["prior"]], "brmsprior") - expect_type(ss[["initfun"]], "closure") - expect_s3_class(ss[["df"]], "data.frame") - expect_type(ss[["family"]], "character") - expect_s3_class(ss[["pcvrForm"]], "formula") - - #* pending + expect_s3_class(ss, "pcvrss") }) + +if (!interactive()) { + dev.off() +} diff --git a/tools/examples_timing.R b/tools/examples_timing.R index ad44ed59..2759db7a 100644 --- a/tools/examples_timing.R +++ b/tools/examples_timing.R @@ -1,4 +1,3 @@ -wd <- getwd() library(ggplot2) setwd("~/pcvr/R") devtools::load_all("~/pcvr") @@ -9,10 +8,12 @@ tdf <- do.call(rbind, lapply(dir("~/pcvr/man", pattern = ".[Rr]d$", full.names = }) data.frame(time = x[["elapsed"]], fun = gsub(".*/", "", doc)) })) -setwd(wd) -tdf -if (!interactive) {pdf(NULL)} -ggplot(tdf, aes(x = 1, y = time, fill = fun)) + +setwd("~/pcvr/tools/") +write.csv(tdf, file = "examples_timing.csv", row.names = FALSE) + +p1 <- ggplot(tdf, aes(x = 1, y = time, fill = fun)) + geom_col(position = "stack") + scale_fill_viridis_d()+ theme_light() +p1 +ggsave("examples_timing.png", plot = p1, width = 12, height = 6, dpi = 300, bg = "#ffffff") diff --git a/tools/examples_timing.png b/tools/examples_timing.png new file mode 100644 index 00000000..a4f6fb1a Binary files /dev/null and b/tools/examples_timing.png differ diff --git a/tools/tests_timing.R b/tools/tests_timing.R index 10adceac..d8617f48 100644 --- a/tools/tests_timing.R +++ b/tools/tests_timing.R @@ -12,9 +12,51 @@ tdf <- do.call(rbind, }) data.frame(time = x[["elapsed"]], Test = gsub(".*/", "", file)) })) -tdf -if (!interactive()) {pdf(NULL)} -ggplot(tdf, aes(x = 1, y = time, fill = Test)) + +write.csv(tdf, file = "~/pcvr/tools/tests_timing.csv", row.names = FALSE) + +p1 <- ggplot(tdf, aes(x = 1, y = time, fill = Test)) + geom_col(position = "stack") + scale_fill_viridis_d()+ theme_light() +ggsave("~/pcvr/tools/tests_timing.png", plot = p1, width = 7, height = 6, dpi = 300, bg = "#ffffff") + + +# now with CI and CRAN + +Sys.setenv("CI" = "true") +sv <- NULL +tdf <- do.call(rbind, + lapply(dir("~/pcvr/tests/testthat", pattern = ".[Rr]$", full.names = TRUE), + function(file) { + message(paste0("Running ", gsub(".*/", "", file))) + x <- system.time({ + source(file) + }) + data.frame(time = x[["elapsed"]], Test = gsub(".*/", "", file)) + })) +write.csv(tdf, file = "~/pcvr/tools/tests_timing_ci.csv", row.names = FALSE) + +p2 <- ggplot(tdf, aes(x = 1, y = time, fill = Test)) + + geom_col(position = "stack") + + scale_fill_viridis_d()+ + theme_light() +ggsave("~/pcvr/tools/tests_timing_ci.png", plot = p2, width = 7, height = 6, dpi = 300, bg = "#ffffff") + +Sys.setenv("NOT_CRAN" = "false") +sv <- NULL +tdf <- do.call(rbind, + lapply(dir("~/pcvr/tests/testthat", pattern = ".[Rr]$", full.names = TRUE), + function(file) { + message(paste0("Running ", gsub(".*/", "", file))) + x <- system.time({ + source(file) + }) + data.frame(time = x[["elapsed"]], Test = gsub(".*/", "", file)) + })) +write.csv(tdf, file = "~/pcvr/tools/tests_timing_cran.csv", row.names = FALSE) + +p3 <- ggplot(tdf, aes(x = 1, y = time, fill = Test)) + + geom_col(position = "stack") + + scale_fill_viridis_d()+ + theme_light() +ggsave("~/pcvr/tools/tests_timing_cran.png", plot = p3, width = 7, height = 6, dpi = 300, bg = "#ffffff") diff --git a/tools/tests_timing.png b/tools/tests_timing.png new file mode 100644 index 00000000..141bcaa9 Binary files /dev/null and b/tools/tests_timing.png differ diff --git a/tools/tests_timing_ci.png b/tools/tests_timing_ci.png new file mode 100644 index 00000000..76a2e775 Binary files /dev/null and b/tools/tests_timing_ci.png differ diff --git a/tools/tests_timing_cran.png b/tools/tests_timing_cran.png new file mode 100644 index 00000000..c2207c8f Binary files /dev/null and b/tools/tests_timing_cran.png differ