diff --git a/.Rbuildignore b/.Rbuildignore index af495e10..bdeb428d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -14,4 +14,5 @@ ^pkgdown$ ^vignettes/articles$ ^DO_NOT_COMMIT* +^demon.org ^\.#.*$ \ No newline at end of file diff --git a/.gitignore b/.gitignore index c01668a4..aaf63e47 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ docs tools/lib tools/*.html \#\*R:scripts\*\# +demon.org diff --git a/DESCRIPTION b/DESCRIPTION index 1a139044..36234b36 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: pcvr Type: Package Title: Plant Phenotyping and Bayesian Statistics -Version: 1.4.0 +Version: 1.4.1 Authors@R: c(person("Josh", "Sumner", email = "jsumner@danforthcenter.org", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3399-9063")), @@ -21,7 +21,7 @@ License: GPL-2 Encoding: UTF-8 LazyData: true LazyDataCompression: xz -Additional_repositories: https://mc-stan.org/r-packages/ +Additional_repositories: https://stan-dev.r-universe.dev RoxygenNote: 7.3.2 Imports: FactoMineR, diff --git a/R/barg.R b/R/barg.R index a40d766a..1e1001f8 100644 --- a/R/barg.R +++ b/R/barg.R @@ -114,15 +114,16 @@ #' "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2 #' ), type = "brms" #' ) -#' fit_test <- fitGrowth(ss, -#' iter = 600, cores = 1, chains = 1, backend = "cmdstanr", -#' sample_prior = "only" # only sampling from prior for speed -#' ) -#' b <- barg(fit_test, ss) -#' fit_2 <- fit_test -#' fit_list <- list(fit_test, fit_2) -#' x <- barg(fit_list, list(ss, ss)) -#' +#' if (rlang::is_installed("cmdstanr")) { +#' fit_test <- fitGrowth(ss, +#' iter = 600, cores = 1, chains = 1, backend = "cmdstanr", +#' sample_prior = "only" # only sampling from prior for speed +#' ) +#' b <- barg(fit_test, ss) +#' fit_2 <- fit_test +#' fit_list <- list(fit_test, fit_2) +#' x <- barg(fit_list, list(ss, ss)) +#' } #' x <- conjugate( #' s1 = rnorm(10, 10, 1), s2 = rnorm(10, 13, 1.5), method = "t", #' priors = list( diff --git a/R/brmPlot.R b/R/brmPlot.R index 377d5d40..6e7ab0e4 100644 --- a/R/brmPlot.R +++ b/R/brmPlot.R @@ -24,6 +24,7 @@ #' @import ggplot2 #' @import viridis #' @importFrom stats as.formula +#' @importFrom rlang is_installed #' @examples #' \donttest{ #' simdf <- growthSim( @@ -36,8 +37,10 @@ #' list("A" = 130, "B" = 10, "C" = 3), #' df = simdf, type = "brms" #' ) -#' fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) -#' growthPlot(fit = fit, form = y ~ time | group, groups = "a", df = ss$df) +#' if (rlang::is_installed("cmdstanr")) { +#' fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) +#' growthPlot(fit = fit, form = y ~ time | group, groups = "a", df = ss$df) +#' } #' } #' #' @return Returns a ggplot showing a brms model's credible diff --git a/R/brmSurvPlot.R b/R/brmSurvPlot.R index 4643061c..73286579 100644 --- a/R/brmSurvPlot.R +++ b/R/brmSurvPlot.R @@ -17,6 +17,7 @@ #' @import ggplot2 #' @import viridis #' @importFrom stats as.formula +#' @importFrom rlang is_installed #' @examples #' \donttest{ #' set.seed(123) @@ -28,17 +29,20 @@ #' model = "survival weibull", form = y > 100 ~ time | id / group, #' df = df, start = c(0, 5) #' ) -#' fit1 <- fitGrowth(ss1, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") -#' brmSurvPlot(fit1, form = ss1$pcvrForm, df = ss1$df) -#' +#' if (rlang::is_installed("cmdstanr")) { +#' fit1 <- fitGrowth(ss1, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") +#' brmSurvPlot(fit1, form = ss1$pcvrForm, df = ss1$df) +#' } #' # note that using the cumulative hazard to calculate survival is likely to underestimate #' # survival in these plots if events do not start immediately. #' ss2 <- growthSS( #' model = "survival binomial", form = y > 100 ~ time | id / group, #' df = df, start = c(-4, 3) #' ) -#' fit2 <- fitGrowth(ss2, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") -#' brmSurvPlot(fit2, form = ss2$pcvrForm, df = ss2$df) +#' if (rlang::is_installed("cmdstanr")) { +#' fit2 <- fitGrowth(ss2, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") +#' brmSurvPlot(fit2, form = ss2$pcvrForm, df = ss2$df) +#' } #' } #' #' @return Returns a ggplot showing a brms model's credible diff --git a/R/brmViolin.R b/R/brmViolin.R index 744a1dd4..17f804ce 100644 --- a/R/brmViolin.R +++ b/R/brmViolin.R @@ -31,12 +31,13 @@ #' list("A" = 130, "B" = 10, "C" = 3), #' df = simdf, type = "brms" #' ) -#' -#' fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) -#' brmViolin(fit, ss, ".../A_groupd > 1.05") # all groups used -#' brmViolin(fit, ss, "abs(1 - ((...)/(C_groupd - B_groupd))) > 0.05") # rather arbitrary -#' brmViolin(fit, ss, "abs(1 - ((...)/(C_groupa - B_groupd))) > 0.05") # totally arbitrary -#' brmViolin(fit, ss, "A_groupa/A_groupd > 1.05") # only these two groups +#' if (rlang::is_installed("cmdstanr")) { +#' fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) +#' brmViolin(fit, ss, ".../A_groupd > 1.05") # all groups used +#' brmViolin(fit, ss, "abs(1 - ((...)/(C_groupd - B_groupd))) > 0.05") # rather arbitrary +#' brmViolin(fit, ss, "abs(1 - ((...)/(C_groupa - B_groupd))) > 0.05") # totally arbitrary +#' brmViolin(fit, ss, "A_groupa/A_groupd > 1.05") # only these two groups +#' } #' } #' #' @return Returns a ggplot showing a brms model's posterior distributions diff --git a/R/growthSS.R b/R/growthSS.R index 65eaaa57..42ef09ff 100644 --- a/R/growthSS.R +++ b/R/growthSS.R @@ -337,14 +337,6 @@ #' lapply(ss, class) #' ss$initfun() #' # the next step would typically be compiling/fitting the model -#' # here we use very few chains and very few iterations for speed, but more of both is better. -#' \donttest{ -#' fit_test <- fitGrowth(ss, -#' iter = 500, cores = 1, chains = 1, backend = "cmdstanr", -#' control = list(adapt_delta = 0.999, max_treedepth = 20) -#' ) -#' } -#' #' #' # formulas and priors will look different if there is only one group in the data #' diff --git a/R/mvSS.R b/R/mvSS.R index add75639..db225605 100644 --- a/R/mvSS.R +++ b/R/mvSS.R @@ -24,6 +24,7 @@ #' optionally with a plot of simulated growth curves using draws from those priors. #' @seealso \link{fitGrowth} for fitting the model specified by this list. #' @examples +#' \donttest{ #' 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) @@ -34,11 +35,11 @@ #' model = "linear", form = label | value ~ group, df = mv_df, #' start = list("A" = 5), type = "brms", spectral_index = "none" #' ) -#' \donttest{ -#' mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) -#' growthPlot(mod1, ss1$pcvrForm, df = ss1$df) -#' library(ggplot2) -#' ggplot() + stat_brms_model(fit = mod1, ss = ss1) +#' if (rlang::is_installed("cmdstanr")) { +#' mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) +#' growthPlot(mod1, ss1$pcvrForm, df = ss1$df) +#' library(ggplot2) +#' ggplot() + stat_brms_model(fit = mod1, ss = ss1) #' } #' #' # when the model is longitudinal the same model is possible with growthSS @@ -97,8 +98,7 @@ #' } #' })) #' -#' \donttest{ -#' if (rlang::is_installed("mnormt")) { +#' if (rlang::is_installed(c("cmdstanr", "mnormt"))) { #' m2 <- fitGrowth(ss_mv1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) #' growthPlot(m2, ss_mv1$pcvrForm, df = ss_mv1$df) #' } diff --git a/cran-comments.md b/cran-comments.md index 5dcd6dc1..34ee2335 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,8 +1,8 @@ -# pcvr 1.3.1 +# pcvr 1.4.1 ## R CMD check results -0 errors ✔ | 0 warnings ✔ | 2 notes ✖ +0 errors ✔ | 0 warnings ✔ | 1 notes ✖ ❯ checking CRAN incoming feasibility ... [4s/6s] NOTE Maintainer: ‘Josh Sumner ’ @@ -15,14 +15,14 @@ Suggests or Enhances not in mainstream repositories: cmdstanr Availability using Additional_repositories specification: - cmdstanr yes https://mc-stan.org/r-packages/ + cmdstanr yes https://stan-dev.r-universe.dev ❯ checking package namespace information ... OK ❯ checking package dependencies ... NOTE Package suggested but not available for checking: ‘cmdstanr’ ## Notes -Resubmitting due to large changes in the development version. +Resubmitting due to an error on cran when cmdstanr failed to install in testing. No notes seem critical. Names (PlantCV, Kruschke) and words (phenotyping) in DESCRIPTION are not misspelled. diff --git a/man/barg.Rd b/man/barg.Rd index ad453617..0b0ccbc9 100644 --- a/man/barg.Rd +++ b/man/barg.Rd @@ -121,15 +121,16 @@ ss <- growthSS( "sigmaA" = 20, "sigmaB" = 10, "sigmaC" = 2 ), type = "brms" ) -fit_test <- fitGrowth(ss, - iter = 600, cores = 1, chains = 1, backend = "cmdstanr", - sample_prior = "only" # only sampling from prior for speed -) -b <- barg(fit_test, ss) -fit_2 <- fit_test -fit_list <- list(fit_test, fit_2) -x <- barg(fit_list, list(ss, ss)) - +if (rlang::is_installed("cmdstanr")) { + fit_test <- fitGrowth(ss, + iter = 600, cores = 1, chains = 1, backend = "cmdstanr", + sample_prior = "only" # only sampling from prior for speed + ) + b <- barg(fit_test, ss) + fit_2 <- fit_test + fit_list <- list(fit_test, fit_2) + x <- barg(fit_list, list(ss, ss)) +} x <- conjugate( s1 = rnorm(10, 10, 1), s2 = rnorm(10, 13, 1.5), method = "t", priors = list( diff --git a/man/brmPlot.Rd b/man/brmPlot.Rd index 918083c2..02abf2d8 100644 --- a/man/brmPlot.Rd +++ b/man/brmPlot.Rd @@ -61,8 +61,10 @@ ss <- growthSS( list("A" = 130, "B" = 10, "C" = 3), df = simdf, type = "brms" ) -fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) -growthPlot(fit = fit, form = y ~ time | group, groups = "a", df = ss$df) +if (rlang::is_installed("cmdstanr")) { + fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) + growthPlot(fit = fit, form = y ~ time | group, groups = "a", df = ss$df) +} } } diff --git a/man/brmSurvPlot.Rd b/man/brmSurvPlot.Rd index c4a436ca..b0f40f25 100644 --- a/man/brmSurvPlot.Rd +++ b/man/brmSurvPlot.Rd @@ -50,17 +50,20 @@ ss1 <- growthSS( model = "survival weibull", form = y > 100 ~ time | id / group, df = df, start = c(0, 5) ) -fit1 <- fitGrowth(ss1, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") -brmSurvPlot(fit1, form = ss1$pcvrForm, df = ss1$df) - +if (rlang::is_installed("cmdstanr")) { + fit1 <- fitGrowth(ss1, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") + brmSurvPlot(fit1, form = ss1$pcvrForm, df = ss1$df) +} # note that using the cumulative hazard to calculate survival is likely to underestimate # survival in these plots if events do not start immediately. ss2 <- growthSS( model = "survival binomial", form = y > 100 ~ time | id / group, df = df, start = c(-4, 3) ) -fit2 <- fitGrowth(ss2, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") -brmSurvPlot(fit2, form = ss2$pcvrForm, df = ss2$df) +if (rlang::is_installed("cmdstanr")) { + fit2 <- fitGrowth(ss2, iter = 600, cores = 2, chains = 2, backend = "cmdstanr") + brmSurvPlot(fit2, form = ss2$pcvrForm, df = ss2$df) +} } } diff --git a/man/brmViolin.Rd b/man/brmViolin.Rd index 68f20446..679992aa 100644 --- a/man/brmViolin.Rd +++ b/man/brmViolin.Rd @@ -41,12 +41,13 @@ ss <- growthSS( list("A" = 130, "B" = 10, "C" = 3), df = simdf, type = "brms" ) - -fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) -brmViolin(fit, ss, ".../A_groupd > 1.05") # all groups used -brmViolin(fit, ss, "abs(1 - ((...)/(C_groupd - B_groupd))) > 0.05") # rather arbitrary -brmViolin(fit, ss, "abs(1 - ((...)/(C_groupa - B_groupd))) > 0.05") # totally arbitrary -brmViolin(fit, ss, "A_groupa/A_groupd > 1.05") # only these two groups +if (rlang::is_installed("cmdstanr")) { + fit <- fitGrowth(ss, backend = "cmdstanr", iter = 500, chains = 1, cores = 1) + brmViolin(fit, ss, ".../A_groupd > 1.05") # all groups used + brmViolin(fit, ss, "abs(1 - ((...)/(C_groupd - B_groupd))) > 0.05") # rather arbitrary + brmViolin(fit, ss, "abs(1 - ((...)/(C_groupa - B_groupd))) > 0.05") # totally arbitrary + brmViolin(fit, ss, "A_groupa/A_groupd > 1.05") # only these two groups +} } } diff --git a/man/growthSS.Rd b/man/growthSS.Rd index 6b84647e..74e979bd 100644 --- a/man/growthSS.Rd +++ b/man/growthSS.Rd @@ -357,14 +357,6 @@ ss <- growthSS( lapply(ss, class) ss$initfun() # the next step would typically be compiling/fitting the model -# here we use very few chains and very few iterations for speed, but more of both is better. -\donttest{ -fit_test <- fitGrowth(ss, - iter = 500, cores = 1, chains = 1, backend = "cmdstanr", - control = list(adapt_delta = 0.999, max_treedepth = 20) -) -} - # formulas and priors will look different if there is only one group in the data diff --git a/man/mvSS.Rd b/man/mvSS.Rd index f775f9fe..22e8d99b 100644 --- a/man/mvSS.Rd +++ b/man/mvSS.Rd @@ -57,6 +57,7 @@ This function provides a simplified interface to modeling multi-value traits usi Output from this should be passed to \link{fitGrowth} to fit the specified model. } \examples{ +\donttest{ 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) @@ -67,11 +68,11 @@ ss1 <- mvSS( model = "linear", form = label | value ~ group, df = mv_df, start = list("A" = 5), type = "brms", spectral_index = "none" ) -\donttest{ -mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) -growthPlot(mod1, ss1$pcvrForm, df = ss1$df) -library(ggplot2) -ggplot() + stat_brms_model(fit = mod1, ss = ss1) +if (rlang::is_installed("cmdstanr")) { + mod1 <- fitGrowth(ss1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) + growthPlot(mod1, ss1$pcvrForm, df = ss1$df) + library(ggplot2) + ggplot() + stat_brms_model(fit = mod1, ss = ss1) } # when the model is longitudinal the same model is possible with growthSS @@ -130,8 +131,7 @@ unlist(lapply(names(ss_mv1), function(nm) { } })) -\donttest{ -if (rlang::is_installed("mnormt")) { +if (rlang::is_installed(c("cmdstanr", "mnormt"))) { m2 <- fitGrowth(ss_mv1, backend = "cmdstanr", iter = 1000, chains = 1, cores = 1) growthPlot(m2, ss_mv1$pcvrForm, df = ss_mv1$df) } diff --git a/tests/testthat/test-mvSSModels.R b/tests/testthat/test-mvSSModels.R index 0ab8a9bc..fec9dd10 100644 --- a/tests/testthat/test-mvSSModels.R +++ b/tests/testthat/test-mvSSModels.R @@ -144,6 +144,22 @@ test_that("Test brms mv trait longitudinal model", { expect_s3_class(p, "ggplot") }) +test_that("Test brms mv trait longitudinal model from growthSS", { + skip_if_not_installed("brms") + skip_if_not_installed("cmdstanr") + skip_on_cran() + ss_mv1 <- growthSS( + model = "linear", + form = label | resp_weights(value) + trunc(lb = -1, ub = Inf) ~ time | group, + df = mv_df2, start = list("A" = 50) + ) + 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") +}) + test_that("Test nls mv trait longitudinal model", { skip_on_cran() ss_mv1 <- mvSS( diff --git a/tools/pcvrCoverage.rdata b/tools/pcvrCoverage.rdata new file mode 100644 index 00000000..690c92f7 Binary files /dev/null and b/tools/pcvrCoverage.rdata differ diff --git a/vignettes/articles/field_capacity.Rmd b/vignettes/articles/field_capacity.Rmd index 72e49f6a..d610d26e 100644 --- a/vignettes/articles/field_capacity.Rmd +++ b/vignettes/articles/field_capacity.Rmd @@ -584,6 +584,8 @@ head(d) Target field capacities are in the data that the phenotyping core provides and observed field capacities can be calculated from the watering metadata. ```{r} +#| fig.alt: > +#| Example of field capacity in a drought treatment that is working well set.seed(123) d2 <- data.frame( day = rep(1:20, 4), @@ -619,7 +621,11 @@ ggplot(d[d$variable == "field_capacity", ], ) + pcv_theme() + theme(legend.position = "none") +``` +```{r} +#| fig.alt: > +#| Example of field capacity in a drought treatment that is not working well ggplot(d[d$variable == "field_capacity", ], aes(x = day, y = value)) + facet_wrap(~treatment) + diff --git a/vignettes/articles/reading_pcv_data.Rmd b/vignettes/articles/reading_pcv_data.Rmd index 0c8d8b84..0f348c77 100644 --- a/vignettes/articles/reading_pcv_data.Rmd +++ b/vignettes/articles/reading_pcv_data.Rmd @@ -163,7 +163,6 @@ out <- pcv.time(sv, plantingDelay = 0, phenotype = "area_pixels", cutoff = 10, timeCol = "timestamp", group = c("barcode", "rotation"), plot = TRUE ) -out$plot sv <- out$data dim(sv) ``` @@ -173,6 +172,8 @@ Note that in these plots, and particularly clearly in the DAE plot, there are lo Before moving on we'll also check the grouping in our data. Here we see that we have lots of plants with more than one image per day. ```{r} +#| fig.alt: > +#| Checking grouping of data checkGroups(sv, c("DAS", "barcode", "rotation", "genotype", "fertilizer")) ``` @@ -194,6 +195,8 @@ dim(sv_ag_with_outliers) The `pcv.outliers` function can be used to remove outliers relative to a phenotype using cook's distance. Here due to the experimental design having these plants germinate on the machine around 1 percent of data are removed as outliers. The plot shows removed data points in red, although here that is hard to see. ```{r} +#| fig.alt: > +#| Plotting outliers out <- pcv.outliers( df = sv_ag_with_outliers, phenotype = "area_pixels", group = c("DAS", "genotype", "fertilizer"), plotgroup = c("barcode") @@ -206,6 +209,8 @@ dim(sv_ag) It is also useful to check our grouping assumptions again, here we see that there are some plants with multiple images from a single day. ```{r} +#| fig.alt: > +#| Checking Groups again after aggregation/outlier removal checkGroups(sv_ag, c("DAS", "barcode", "genotype", "fertilizer")) ``` @@ -215,6 +220,8 @@ checkGroups(sv_ag, c("DAS", "barcode", "genotype", "fertilizer")) We might also want to check the watering data, which can be read easily from json with `pcv.water`. ```{r} +#| fig.alt: > +#| Plotting watering data water <- pcv.water(paste0(base_url, "metadata.json")) water$genotype <- substr(water$barcode, 3, 5) @@ -244,6 +251,8 @@ A common use for watering data is to look at water use efficiency (WUE). Here we ```{r} +#| fig.alt: > +#| Plotting Pseudo water use efficiency test <- pwue(df = sv_ag, w = water, pheno = "area_pixels", time = "timestamp", id = "barcode") ggplot(test, aes(x = DAS, y = pWUE, color = genotype, group = barcode)) +