Skip to content

Commit

Permalink
update 'epilepsy' data set
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Feb 19, 2019
1 parent 2e3a788 commit f03bea1
Show file tree
Hide file tree
Showing 52 changed files with 460 additions and 285 deletions.
2 changes: 1 addition & 1 deletion R/brm.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
#' # and half cauchy priors for standard deviations of group-level effects
#' bprior1 <- prior(student_t(5,0,10), class = b) +
#' prior(cauchy(0,2), class = sd)
#' fit1 <- brm(count ~ log_Age_c + log_Base4_c * Trt + (1|patient),
#' fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson(), prior = bprior1)
#'
#' # generate a summary of the results
Expand Down
32 changes: 16 additions & 16 deletions R/brmsfit-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fixef.brmsfit <- function(object, summary = TRUE, robust = FALSE,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c + (1+Trt_c|visit),
#' fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
#' data = epilepsy, family = gaussian(), chains = 2)
#' vcov(fit)
#' }
Expand Down Expand Up @@ -126,7 +126,7 @@ vcov.brmsfit <- function(object, correlation = FALSE, pars = NULL, ...) {
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c + (1+Trt_c|visit),
#' fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
#' data = epilepsy, family = gaussian(), chains = 2)
#' ranef(fit)
#' }
Expand Down Expand Up @@ -199,7 +199,7 @@ ranef.brmsfit <- function(object, summary = TRUE, robust = FALSE,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c + (1+Trt_c|visit),
#' fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
#' data = epilepsy, family = gaussian(), chains = 2)
#' ## extract population and group-level coefficients separately
#' fixef(fit)
Expand Down Expand Up @@ -307,7 +307,7 @@ coef.brmsfit <- function(object, summary = TRUE, robust = FALSE,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c + (1+Trt_c|visit),
#' fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
#' data = epilepsy, family = gaussian(), chains = 2)
#' VarCorr(fit)
#' }
Expand Down Expand Up @@ -492,7 +492,7 @@ as.array.brmsfit <- function(x, ...) {
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c,
#' fit <- brm(count ~ zAge + zBase * Trt,
#' data = epilepsy, family = negbinomial())
#' posterior_interval(fit)
#' }
Expand Down Expand Up @@ -587,7 +587,7 @@ as.mcmc.brmsfit <- function(x, pars = NA, exact_match = FALSE,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c
#' fit <- brm(count ~ zAge + zBase * Trt
#' + (1|patient) + (1|obs),
#' data = epilepsy, family = poisson(),
#' prior = c(prior(student_t(5,0,10), class = b),
Expand Down Expand Up @@ -1073,7 +1073,7 @@ launch_shinystan.brmsfit <- function(
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c
#' fit <- brm(count ~ zAge + zBase * Trt
#' + (1|patient) + (1|visit),
#' data = epilepsy, family = "poisson")
#' plot(fit)
Expand Down Expand Up @@ -1225,7 +1225,7 @@ stanplot.brmsfit <- function(object, pars = NA, type = "intervals",
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c
#' fit <- brm(count ~ zAge + zBase * Trt
#' + (1|patient) + (1|obs),
#' data = epilepsy, family = poisson())
#'
Expand Down Expand Up @@ -1365,7 +1365,7 @@ pp_check.brmsfit <- function(object, type, nsamples, group = NULL,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt_c
#' fit <- brm(count ~ zAge + zBase * Trt
#' + (1|patient) + (1|visit),
#' data = epilepsy, family = "poisson")
#' pairs(fit, pars = parnames(fit)[1:3], exact_match = TRUE)
Expand Down Expand Up @@ -1918,7 +1918,7 @@ predictive_error.brmsfit <- function(
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Base4_c, data = epilepsy, family = poisson())
#' fit <- brm(count ~ zBase, data = epilepsy, family = poisson())
#' predictive_interval(fit)
#' }
#'
Expand Down Expand Up @@ -3048,7 +3048,7 @@ control_params.brmsfit <- function(x, pars = NULL, ...) {
#' \dontrun{
#' # model with the treatment effect
#' fit1 <- brm(
#' count ~ log_Age_c + log_Base4_c + Trt_c,
#' count ~ zAge + zBase + Trt,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand All @@ -3058,7 +3058,7 @@ control_params.brmsfit <- function(x, pars = NULL, ...) {
#'
#' # model without the treatment effect
#' fit2 <- brm(
#' count ~ log_Age_c + log_Base4_c,
#' count ~ zAge + zBase,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand Down Expand Up @@ -3138,7 +3138,7 @@ bridge_sampler.brmsfit <- function(samples, ...) {
#' \dontrun{
#' # model with the treatment effect
#' fit1 <- brm(
#' count ~ log_Age_c + log_Base4_c + Trt_c,
#' count ~ zAge + zBase + Trt,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand All @@ -3147,7 +3147,7 @@ bridge_sampler.brmsfit <- function(samples, ...) {
#'
#' # model without the treatment effect
#' fit2 <- brm(
#' count ~ log_Age_c + log_Base4_c,
#' count ~ zAge + zBase,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand Down Expand Up @@ -3210,7 +3210,7 @@ bayes_factor.brmsfit <- function(x1, x2, log = FALSE, ...) {
#' \dontrun{
#' # model with the treatment effect
#' fit1 <- brm(
#' count ~ log_Age_c + log_Base4_c + Trt_c,
#' count ~ zAge + zBase + Trt,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand All @@ -3219,7 +3219,7 @@ bayes_factor.brmsfit <- function(x1, x2, log = FALSE, ...) {
#'
#' # model without the treatent effect
#' fit2 <- brm(
#' count ~ log_Age_c + log_Base4_c,
#' count ~ zAge + zBase,
#' data = epilepsy, family = negbinomial(),
#' prior = prior(normal(0, 1), class = b),
#' save_all_pars = TRUE
Expand Down
27 changes: 12 additions & 15 deletions R/datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' In addition, information on the risk variables age, sex and disease
#' type is provided.
#'
#' @format A dataframe of 76 observations containing
#' @format A data frame of 76 observations containing
#' information on the following 7 variables.
#' \describe{
#' \item{time}{The time to first or second recurrence of the infection,
Expand Down Expand Up @@ -54,7 +54,7 @@
#' Patients were asked to rate the clarity of leaflet instructions
#' accompanying each device, using a 4-point ordinal scale.
#'
#' @format A dataframe of 572 observations containing
#' @format A data frame of 572 observations containing
#' information on the following 5 variables.
#' \describe{
#' \item{subject}{The subject number}
Expand Down Expand Up @@ -98,37 +98,34 @@
#' therapy in epilepsy. Covariates are treatment,
#' 8-week baseline seizure counts, and age of the patients in years.
#'
#' @format A dataframe of 236 observations containing information
#' @format A data frame of 236 observations containing information
#' on the following 9 variables.
#' \describe{
#' \item{Age}{The age of the patients in years}
#' \item{Base}{The seizure count at 8-weeks baseline}
#' \item{Trt}{Either \code{0} or \code{1} indicating
#' if the patient received anti-convulsant therapy}
#' \item{log_Age_c}{The logarithm of Age centered around its mean}
#' \item{log_Base4_c}{The logarithm of Base divided by 4
#' (i.e. log(Base/4)) centered around its mean}
#' \item{Trt_c}{Trt centered around its mean}
#' \item{patient}{The patient number}
#' \item{visit}{The session number from \code{1} (first visit)
#' to \code{4} (last visit)}
#' \item{count}{The seizure count between two visits}
#' \item{patient}{The patient number}
#' \item{obs}{The observation number,
#' i.e. a unique identifier for each observation}
#' \item{obs}{The observation number, that is
#' a unique identifier for each observation}
#' \item{zAge}{Standardized \code{Age}}
#' \item{zBase}{Standardized \code{Base}}
#' }
#'
#' @examples
#' \dontrun{
#' ## poisson regression without random effects.
#' fit1 <- brm(count ~ log_Age_c + log_Base4_c * Trt_c,
#' fit1 <- brm(count ~ zAge + zBase * Trt,
#' data = epilepsy, family = poisson())
#' summary(fit1)
#' plot(fit1)
#'
#' ## poisson regression with random intercepts of patients and visits
#' ## as well as normal priors for fixed effects parameters.
#' fit2 <- brm(count ~ log_Age_c + log_Base4_c * Trt_c
#' + (1|patient) + (1|visit),
#' ## poisson regression with varying intercepts of patients
#' ## as well as normal priors for overall effects parameters.
#' fit2 <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson(),
#' prior = set_prior("normal(0,5)"))
#' summary(fit2)
Expand Down
2 changes: 1 addition & 1 deletion R/formula-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#' summary(fit3)
#'
#' ## Poisson model with truncated counts
#' fit4 <- brm(count | trunc(ub = 104) ~ log_Base4_c * Trt_c,
#' fit4 <- brm(count | trunc(ub = 104) ~ zBase * Trt,
#' data = epilepsy, family = poisson())
#' summary(fit4)
#' }
Expand Down
3 changes: 1 addition & 2 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ autocor <- function(object, ...) {
#'
#' @examples
#' \dontrun{
#' model <- brm(count ~ log_Age_c + log_Base4_c * Trt
#' + (1|patient) + (1|visit),
#' model <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = "poisson")
#'
#' # plot posterior intervals
Expand Down
4 changes: 2 additions & 2 deletions R/ggplot-themes.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ NULL
#' @examples
#' \dontrun{
#' # change default ggplot theme
#' theme_set(theme_black())
#' ggplot2::theme_set(theme_black())
#'
#' # change default bayesplot color scheme
#' bayesplot::color_scheme_set("viridisC")
#'
#' # fit a simple model
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt + (1|patient),
#' fit <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson(), chains = 2)
#' summary(fit)
#'
Expand Down
4 changes: 2 additions & 2 deletions R/kfold-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
#'
#' @examples
#' \dontrun{
#' fit1 <- brm(count ~ log_Age_c + log_Base4_c * Trt +
#' fit1 <- brm(count ~ zAge + zBase * Trt +
#' (1|patient) + (1|obs),
#' data = epilepsy, family = poisson())
#' # throws warning about some pareto k estimates being too high
Expand Down Expand Up @@ -242,7 +242,7 @@ kfold_internal <- function(x, K = 10, Ksub = NULL, folds = NULL,
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Base4_c * Trt + (1|patient),
#' fit <- brm(count ~ zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson())
#'
#' # perform k-fold cross validation
Expand Down
2 changes: 1 addition & 1 deletion R/loo-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ validate_models <- function(models, model_names, sub_names) {
#'
#' @examples
#' \dontrun{
#' fit1 <- brm(count ~ log_Age_c + log_Base4_c * Trt + (1|patient),
#' fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = poisson())
#' # throws warning about some pareto k estimates being too high
#' (loo1 <- loo(fit1))
Expand Down
3 changes: 1 addition & 2 deletions R/make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#' make_stancode(rating ~ treat + period + carry + (1|subject),
#' data = inhaler, family = "cumulative")
#'
#' make_stancode(count ~ log_Age_c + log_Base4_c * Trt_c
#' + (1|patient) + (1|visit),
#' make_stancode(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = "poisson")
#'
#' @export
Expand Down
3 changes: 1 addition & 2 deletions R/make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#' data = inhaler, family = "cumulative")
#' names(data1)
#'
#' data2 <- make_standata(count ~ log_Age_c + log_Base4_c * Trt_c
#' + (1|patient) + (1|visit),
#' data2 <- make_standata(count ~ zAge + zBase * Trt + (1|patient),
#' data = epilepsy, family = "poisson")
#' names(data2)
#'
Expand Down
32 changes: 16 additions & 16 deletions R/marginal_effects-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,48 +173,48 @@
#'
#' @examples
#' \dontrun{
#' fit <- brm(count ~ log_Age_c + log_Base4_c * Trt + (1 | patient),
#' fit <- brm(count ~ zAge + zBase * Trt + (1 | patient),
#' data = epilepsy, family = poisson())
#'
#' ## plot all marginal effects
#' plot(marginal_effects(fit), ask = FALSE)
#'
#' ## change colours to grey scale
#' me <- marginal_effects(fit, "log_Base4_c:Trt")
#' me <- marginal_effects(fit, "zBase:Trt")
#' plot(me, plot = FALSE)[[1]] +
#' scale_color_grey() +
#' scale_fill_grey()
#'
#' ## only plot the marginal interaction effect of 'log_Base4_c:Trt'
#' ## for different values for 'log_Age_c'
#' conditions <- data.frame(log_Age_c = c(-0.3, 0, 0.3))
#' plot(marginal_effects(fit, effects = "log_Base4_c:Trt",
#' ## only plot the marginal interaction effect of 'zBase:Trt'
#' ## for different values for 'zAge'
#' conditions <- data.frame(zAge = c(-1, 0, 1))
#' plot(marginal_effects(fit, effects = "zBase:Trt",
#' conditions = conditions))
#'
#' ## also incorporate random effects variance over patients
#' ## also add data points and a rug representation of predictor values
#' plot(marginal_effects(fit, effects = "log_Base4_c:Trt",
#' plot(marginal_effects(fit, effects = "zBase:Trt",
#' conditions = conditions, re_formula = NULL),
#' points = TRUE, rug = TRUE)
#'
#' ## change handling of two-way interactions
#' int_conditions <- list(
#' log_Base4_c = setNames(c(-2, 1, 0), c("b", "c", "a"))
#' zBase = setNames(c(-2, 1, 0), c("b", "c", "a"))
#' )
#' marginal_effects(fit, effects = "Trt:log_Base4_c",
#' marginal_effects(fit, effects = "Trt:zBase",
#' int_conditions = int_conditions)
#' marginal_effects(fit, effects = "Trt:log_Base4_c",
#' int_conditions = list(log_Base4_c = quantile))
#' marginal_effects(fit, effects = "Trt:zBase",
#' int_conditions = list(zBase = quantile))
#'
#' ## fit a model to illustrate how to plot 3-way interactions
#' fit3way <- brm(count ~ log_Age_c * log_Base4_c * Trt, data = epilepsy)
#' conditions <- make_conditions(fit3way, "log_Age_c")
#' fit3way <- brm(count ~ zAge * zBase * Trt, data = epilepsy)
#' conditions <- make_conditions(fit3way, "zAge")
#' marginal_effects(
#' fit3way, "log_Base4_c:Trt", conditions = conditions
#' fit3way, "zBase:Trt", conditions = conditions
#' )
#' ## only include points close to the specified values of log_Age_c
#' ## only include points close to the specified values of zAge
#' me <- marginal_effects(
#' fit3way, "log_Base4_c:Trt", conditions = conditions,
#' fit3way, "zBase:Trt", conditions = conditions,
#' select_points = 0.1
#' )
#' plot(me, points = TRUE)
Expand Down
Loading

0 comments on commit f03bea1

Please sign in to comment.