Skip to content

Commit 43706fb

Browse files
authored
Merge pull request #166 from danforthcenter/hierarchical-stat-growthss
Hierarchical stat growthss
2 parents 632de91 + 6af3384 commit 43706fb

7 files changed

Lines changed: 45 additions & 16 deletions

File tree

R/brmPlot.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#' future data if the available data has not reached some point (such as asymptotic size),
1515
#' although prediction using splines outside of the observed range is not necessarily reliable.
1616
#' @param facetGroups logical, should groups be separated in facets? Defaults to TRUE.
17-
#' @param hierarchy_value If a hierarchical model is being plotted, what value should the
18-
#' hierarchical predictor be? If left NULL (the default) the mean value is used. If this is >1L
19-
#' then the x axis will use the hierarchical variable from the model at the mean of the timeRange
17+
#' @param hierarchy_value Value(s) for the hierarchical variable(s), if applicable. Only used
18+
#' for hierarchical brms models. If left NULL (the default) the mean value is used. If this is >1L
19+
#' then the x axis will use the hierarchical variable from the model instead of the "time" variable
20+
#' and plot data across the values of the hierarchical variable at the mean of the timeRange
2021
#' (mean of x values in the model if timeRange is not specified).
2122
#' @param vir_option Viridis color scale to use for plotting credible intervals. Defaults to "plasma".
2223
#' @keywords growth-curve brms

R/pcvsubread.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ pcv.sub.read <- function(inputFile, filters, reader = "read.csv", awk = NULL, ..
2525
x <- suppressMessages(as.data.frame(readingFunction(pipe(awkCommand), ...)))
2626
colnames(x) <- COLS
2727
}
28+
if (nrow(x) < 1) {
29+
stop(paste0(
30+
"0 Rows returned using awk statement:\n", awkCommand,
31+
"\nMost common issues are misspellings or not including a column name and affector."
32+
))
33+
}
2834
return(x)
2935
}

R/stat_brms_model.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
stat_brms_model <- function(mapping = NULL, data = NULL,
77
fit = NULL, ss = NULL, CI = 0.95,
8+
hierarchy_value = NULL,
89
inherit.aes = TRUE, ...) {
910
# These would normally be arguments to a stat layer but they should not be changed
1011
geom <- "ribbon"
@@ -29,12 +30,19 @@ stat_brms_model <- function(mapping = NULL, data = NULL,
2930
c(((1 - i) / 2), (i + (1 - i) / 2))
3031
)
3132
})
33+
# get hierarchy value if NULL
34+
if (is.null(hierarchy_value) && !is.null(parsed_form$hierarchical_predictor)) {
35+
hierarchy_value <- mean(fit$data[[parsed_form$hierarchical_predictor]])
36+
}
3237
# make layer for each of the intervals
3338
layers <- lapply(formatted_prob_list, function(prob_pair) {
3439
lyr <- ggplot2::layer(
3540
stat = stat, data = data, mapping = mapping, geom = geom,
3641
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
37-
params = list(na.rm = na.rm, fit = fit, parsed_form = parsed_form, probs = prob_pair, ...)
42+
params = list(
43+
na.rm = na.rm, fit = fit, parsed_form = parsed_form,
44+
probs = prob_pair, hierarchy_value = hierarchy_value, ...
45+
)
3846
)
3947
return(lyr)
4048
})
@@ -52,7 +60,7 @@ stat_brms_model <- function(mapping = NULL, data = NULL,
5260

5361
statBrmsMod <- ggplot2::ggproto("StatBrm", Stat,
5462
# `specify that there will be extra params`
55-
extra_params = c("na.rm", "fit", "parsed_form", "probs"),
63+
extra_params = c("na.rm", "fit", "parsed_form", "probs", "hierarchy_value"),
5664
# `data setup function`
5765
setup_data = function(data, params) {
5866
#' possible that ss is not a pcvrss object for compatibility with other brms models
@@ -122,14 +130,17 @@ statBrmsMod <- ggplot2::ggproto("StatBrm", Stat,
122130
#' the model and ss objects.
123131
compute_group = function(data, scales,
124132
fit = NULL, parsed_form = NULL, probs = NULL,
133+
hierarchy_value = NULL,
125134
...) {
126135
yvar <- parsed_form$y
127136
xvar <- parsed_form$x
128137
group <- parsed_form$group
138+
hierarchy <- parsed_form$hierarchical_predictor
129139
# make data to use drawing posterior predictions
130140
nd <- data[, c("x", "MOD_GROUP", "PANEL")]
131141
nd <- nd[!duplicated(nd), ]
132142
colnames(nd) <- c(xvar, group, "PANEL")
143+
nd[[hierarchy %||% "none"]] <- hierarchy_value
133144
# make predictions
134145
mod_data <- cbind(nd, predict(fit, newdata = nd, probs = probs))
135146
# lengthen predictions as in brmPlot

R/stat_growthSS.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#' This behaves per normal ggplot2 expectations except
1313
#' that if data is missing (ie, not inherited or specified) then the data from \code{ss} is used.
1414
#' @param fit A model object returned from \code{fitGrowth}.
15-
#' @param ss A \code{pcvrss} object. Only the "pcvrForm" and "df" elements are used.
15+
#' @param ss A \code{\link{pcvrss-class}} object. Only the "pcvrForm" and "df" elements are used.
1616
#' @param inherit.aes Logical, should aesthetics be inherited from top level? Defaults to TRUE.
17-
#' @param CI A vector of credible intervals to plot, defaults to 0.95.
17+
#' @param CI A vector of credible intervals to plot, defaults to 0.95. Only used with brms models.
18+
#' @param hierarchy_value Value for the hierarchical variable, if applicable. Only used
19+
#' for hierarchical brms models. If left NULL (the default) the mean value is used.
1820
#' @param ... Additional arguments passed to the ggplot layer.
1921
#'
2022
#' @details
@@ -25,8 +27,8 @@
2527
#' \item{\strong{brms}: \code{geom_ribbon} for longitudinal plots, \code{geom_rect} for others.}
2628
#' \item{\strong{nlrq}: \code{geom_line}, replicated per each quantile.}
2729
#' \item{\strong{nlme}: \code{geom_smooth}, with ribbon based on the heteroskedastic term.}
28-
#' \item{\strong{nls}: \code{geom_line}, replicated per each quantile.}
29-
#' \item{\strong{nlrq}: \code{geom_line}, replicated per each quantile.}
30+
#' \item{\strong{nls}: \code{geom_line}.}
31+
#' \item{\strong{nlrq}: \code{geom_smooth}.}
3032
#' }
3133
#'
3234
#' @examples

man/brmPlot.Rd

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/stat_growthss.Rd

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-brmsModels.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ test_that("Hierarchical Model Works", {
212212
expect_s3_class(p, "ggplot")
213213
p <- growthPlot(fit, ss$pcvrForm, df = ss$df, hierarchy_value = seq(8, 12, 1))
214214
expect_s3_class(p, "ggplot")
215+
p <- ggplot() + stat_growthss(fit = fit, ss = ss, hierarchy_value = 5)
216+
expect_s3_class(p, "ggplot")
217+
p <- ggplot() + stat_growthss(fit = fit, ss = ss)
218+
expect_s3_class(p, "ggplot")
215219
})
216220

217221
test_that("Changepoint model can be specified", {

0 commit comments

Comments
 (0)