Title: | Regression with Multiple Change Points |
---|---|
Description: | Flexible and informed regression with Multiple Change Points. 'mcp' can infer change points in means, variances, autocorrelation structure, and any combination of these, as well as the parameters of the segments in between. All parameters are estimated with uncertainty and prediction intervals are supported - also near the change points. 'mcp' supports hypothesis testing via Savage-Dickey density ratio, posterior contrasts, and cross-validation. 'mcp' is described in Lindeløv (submitted) <doi:10.31219/osf.io/fzqxv> and generalizes the approach described in Carlin, Gelfand, & Smith (1992) <doi:10.2307/2347570> and Stephens (1994) <doi:10.2307/2986119>. |
Authors: | Jonas Kristoffer Lindeløv [aut, cre] |
Maintainer: | Jonas Kristoffer Lindeløv <[email protected]> |
License: | GPL-2 |
Version: | 0.3.4 |
Built: | 2024-11-13 06:23:41 UTC |
Source: | CRAN |
Bernoulli family for mcp
bernoulli(link = "logit")
bernoulli(link = "logit")
link |
Link function. |
Takes an mcpfit
as input and computes information criteria using loo or
WAIC. Compare models using loo_compare
and loo_model_weights
.
more in loo
.
criterion(fit, criterion = "loo", ...) ## S3 method for class 'mcpfit' loo(x, ...) ## S3 method for class 'mcpfit' waic(x, ...)
criterion(fit, criterion = "loo", ...) ## S3 method for class 'mcpfit' loo(x, ...) ## S3 method for class 'mcpfit' waic(x, ...)
fit |
An |
criterion |
|
... |
Currently ignored |
x |
An |
a loo
or psis_loo
object.
loo(mcpfit)
: Computes loo on mcpfit objects
waic(mcpfit)
: Computes WAIC on mcpfit objects
Jonas Kristoffer Lindeløv [email protected]
# Define two models and sample them # options(mc.cores = 3) # Speed up sampling ex = mcp_example("intercepts") # Get some simulated data. model1 = list(y ~ 1 + x, ~ 1) model2 = list(y ~ 1 + x) # Without a change point fit1 = mcp(model1, ex$data) fit2 = mcp(model2, ex$data) # Compute LOO for each and compare (works for waic(fit) too) fit1$loo = loo(fit1) fit2$loo = loo(fit2) loo::loo_compare(fit1$loo, fit2$loo)
# Define two models and sample them # options(mc.cores = 3) # Speed up sampling ex = mcp_example("intercepts") # Get some simulated data. model1 = list(y ~ 1 + x, ~ 1) model2 = list(y ~ 1 + x) # Without a change point fit1 = mcp(model1, ex$data) fit2 = mcp(model2, ex$data) # Compute LOO for each and compare (works for waic(fit) too) fit1$loo = loo(fit1) fit2$loo = loo(fit2) loo::loo_compare(fit1$loo, fit2$loo)
mcpfit
for examplesThis was generated using mcp_examples("demo", sample = TRUE)
.
demo_fit
demo_fit
An mcpfit
object.
Exponential family for mcp
exponential(link = "identity")
exponential(link = "identity")
link |
Link function (Character). |
Expected Values from the Posterior Predictive Distribution
## S3 method for class 'mcpfit' fitted( object, newdata = NULL, summary = TRUE, probs = TRUE, rate = TRUE, prior = FALSE, which_y = "ct", varying = TRUE, arma = TRUE, nsamples = NULL, samples_format = "tidy", scale = "response", ... )
## S3 method for class 'mcpfit' fitted( object, newdata = NULL, summary = TRUE, probs = TRUE, rate = TRUE, prior = FALSE, which_y = "ct", varying = TRUE, arma = TRUE, nsamples = NULL, samples_format = "tidy", scale = "response", ... )
object |
An |
newdata |
A |
summary |
Summarise at each x-value |
probs |
Vector of quantiles. Only in effect when |
rate |
Boolean. For binomial models, plot on raw data ( |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
which_y |
What to plot on the y-axis. One of
|
varying |
One of:
|
arma |
Whether to include autoregressive effects.
|
nsamples |
Integer or |
samples_format |
One of "tidy" or "matrix". Controls the output format when |
scale |
One of
|
... |
Currently unused |
If summary = TRUE
: A tibble
with the posterior mean for each row in newdata
,
If newdata
is NULL
, the data in fit$data
is used.
If summary = FALSE
and samples_format = "tidy"
: A tidybayes
tibble
with all the posterior
samples (Ns
) evaluated at each row in newdata
(Nn
), i.e., with Ns x Nn
rows. If there are
varying effects, the returned data is expanded with the relevant levels for each row.
The return columns are:
Predictors from newdata
.
Sample descriptors: ".chain", ".iter", ".draw" (see the tidybayes
package for more), and "data_row" (newdata
rownumber)
Sample values: one column for each parameter in the model.
The estimate. Either "predict" or "fitted", i.e., the name of the type
argument.
If summary = FALSE
and samples_format = "matrix"
: An N_draws
X nrows(newdata)
matrix with fitted/predicted
values (depending on type
). This format is used by brms
and it's useful as yrep
in
bayesplot::ppc_*
functions.
Jonas Kristoffer Lindeløv [email protected]
pp_eval
predict.mcpfit
residuals.mcpfit
fitted(demo_fit) fitted(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. fitted(demo_fit, summary = FALSE) # Samples instead of summary. fitted(demo_fit, newdata = data.frame(time = c(-5, 20, 300)), # New data probs = c(0.025, 0.5, 0.975))
fitted(demo_fit) fitted(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. fitted(demo_fit, summary = FALSE) # Samples instead of summary. fitted(demo_fit, newdata = data.frame(time = c(-5, 20, 300)), # New data probs = c(0.025, 0.5, 0.975))
Returns posterior probabilities and Bayes Factors for flexible hypotheses involving
model parameters. The documentation for the argument hypotheses
below
shows examples of how to specify hypotheses, and read worked examples on the mcp website.
For directional hypotheses, hypothesis`` executes the hypothesis string in a
tidybayes“ environment and summerises the proportion of samples where
the expression evaluates to TRUE. For equals-hypothesis, a Savage-Dickey
ratio is computed. Savage-Dickey requires a prior too, so remember
mcp(..., sample = "both")
. This function is heavily inspired by the
'hypothesis' function from the 'brms' package.
hypothesis(fit, hypotheses, width = 0.95, digits = 3)
hypothesis(fit, hypotheses, width = 0.95, digits = 3)
fit |
An |
hypotheses |
String representation of a logical test involving model parameters. Takes R code that evaluates to TRUE or FALSE in a vectorized way. Directional hypotheses are specified using <, >, <=, or >=.
Hypotheses can also test equality using the equal sign (=). This runs a
Savage-Dickey test, i.e., the proportion by which the probability density
has increased from the prior to the posterior at a given value. Therefore,
it requires
|
width |
Float. The width of the highest posterior density interval (between 0 and 1). |
digits |
a non-null value for digits specifies the minimum number of significant digits to be printed in values. The default, NULL, uses getOption("digits"). (For the interpretation for complex numbers see signif.) Non-integer values will be rounded down, and only values greater than or equal to 1 and no greater than 22 are accepted. |
A data.frame with a row per hypothesis and the following columns:
hypothesis
is the hypothesis; often re-arranged to test against zero.
mean
is the posterior mean of the left-hand side of the hypothesis.
lower
is the lower bound of the (two-sided) highest-density interval of width width
.
upper
is the upper bound of ditto.
p
Posterior probability.
For "=" (Savage-Dickey), it is the BF converted to p.
For directional hypotheses, it is the proportion of samples that returns TRUE.
BF
Bayes Factor in favor of the hypothesis.
For "=" it is the Savage-Dickey density ratio.
For directional hypotheses, it is p converted to odds.
Jonas Kristoffer Lindeløv [email protected]
Inverse logit function
ilogit(eta)
ilogit(eta)
eta |
A vector of logits |
A vector with same length as eta
mcpfit
objectChecks if argument is an mcpfit
object
is.mcpfit(x)
is.mcpfit(x)
x |
An |
Logit function
logit(mu)
logit(mu)
mu |
A vector of probabilities (0.0 to 1.0) |
A vector with same length as mu
Given a model (a list of segment formulas), mcp
infers the posterior
distributions of the parameters of each segment as well as the change points
between segments. See more details and worked examples on the mcp website.
All segments must regress on the same x-variable. Change
points are forced to be ordered using truncation of the priors. You can run
fit = mcp(model, sample=FALSE)
to avoid sampling and the need for
data if you just want to get the priors (fit$prior
), the JAGS code
fit$jags_code
, or the R function to simulate data (fit$simulate
).
mcp( model, data = NULL, prior = list(), family = gaussian(), par_x = NULL, sample = "post", cores = 1, chains = 3, iter = 3000, adapt = 1500, inits = NULL, jags_code = NULL )
mcp( model, data = NULL, prior = list(), family = gaussian(), par_x = NULL, sample = "post", cores = 1, chains = 3, iter = 3000, adapt = 1500, inits = NULL, jags_code = NULL )
model |
A list of formulas - one for each segment. The first formula
has the format
|
data |
Data.frame or tibble in long format. |
prior |
Named list. Names are parameter names (
|
family |
One of |
par_x |
String (default: NULL). Only relevant if no segments contains slope (no hint at what x is). Set this, e.g., par_x = "time". |
sample |
One of
|
cores |
Positive integer or "all". Number of cores.
|
chains |
Positive integer. Number of chains to run. |
iter |
Positive integer. Number of post-warmup draws from each chain.
The total number of draws is |
adapt |
Positive integer. Also sometimes called "burnin", this is the number of samples used to reach convergence. Set lower for greater speed. Set higher if the chains haven't converged yet or look at tips, tricks, and debugging. |
inits |
A list if initial values for the parameters. This can be useful
if a model fails to converge. Read more in |
jags_code |
String. Pass JAGS code to |
Notes on priors:
Order restriction is automatically applied to cp_\* parameters using
truncation (e.g., T(cp_1, )
) so that they are in the correct order on the
x-axis UNLESS you do it yourself. The one exception is for dunif
distributions where you have to do it as above.
In addition to the model parameters, MINX
(minimum x-value), MAXX
(maximum x-value), SDX
(etc...), MINY
, MAXY
, and SDY
are also available when you set priors. They are used to set uninformative
default priors.
Use SD when you specify priors for dt, dlogis, etc. JAGS uses precision
but mcp
converts to precision under the hood via the sd_to_prec()
function. So you will see SDs in fit$prior
but precision ($1/SD^2)
in fit$jags_code
An mcpfit
object.
Jonas Kristoffer Lindeløv [email protected]
# Define the segments using formulas. A change point is estimated between each formula. model = list( response ~ 1, # Plateau in the first segment (int_1) ~ 0 + time, # Joined slope (time_2) at cp_1 ~ 1 + time # Disjoined slope (int_3, time_3) at cp_2 ) # Fit it and sample the prior too. # options(mc.cores = 3) # Uncomment to speed up sampling ex = mcp_example("demo") # Simulated data example demo_fit = mcp(model, data = ex$data, sample = "both") # See parameter estimates summary(demo_fit) # Visual inspection of the results plot(demo_fit) # Visualization of model fit/predictions plot_pars(demo_fit) # Parameter distributions pp_check(demo_fit) # Prior/Posterior predictive checks # Test a hypothesis hypothesis(demo_fit, "cp_1 > 10") # Make predictions fitted(demo_fit) predict(demo_fit) predict(demo_fit, newdata = data.frame(time = c(55.545, 80, 132))) # Compare to a one-intercept-only model (no change points) with default prior model_null = list(response ~ 1) fit_null = mcp(model_null, data = ex$data, par_x = "time") # fit another model here demo_fit$loo = loo(demo_fit) fit_null$loo = loo(fit_null) loo::loo_compare(demo_fit$loo, fit_null$loo) # Inspect the prior. Useful for prior predictive checks. summary(demo_fit, prior = TRUE) plot(demo_fit, prior = TRUE) # Show all priors. Default priors are added where you don't provide any print(demo_fit$prior) # Set priors and re-run prior = list( int_1 = 15, time_2 = "dt(0, 2, 1) T(0, )", # t-dist slope. Truncated to positive. cp_2 = "dunif(cp_1, 80)", # change point to segment 2 > cp_1 and < 80. int_3 = "int_1" # Shared intercept between segment 1 and 3 ) fit3 = mcp(model, data = ex$data, prior = prior) # Show the JAGS model demo_fit$jags_code
# Define the segments using formulas. A change point is estimated between each formula. model = list( response ~ 1, # Plateau in the first segment (int_1) ~ 0 + time, # Joined slope (time_2) at cp_1 ~ 1 + time # Disjoined slope (int_3, time_3) at cp_2 ) # Fit it and sample the prior too. # options(mc.cores = 3) # Uncomment to speed up sampling ex = mcp_example("demo") # Simulated data example demo_fit = mcp(model, data = ex$data, sample = "both") # See parameter estimates summary(demo_fit) # Visual inspection of the results plot(demo_fit) # Visualization of model fit/predictions plot_pars(demo_fit) # Parameter distributions pp_check(demo_fit) # Prior/Posterior predictive checks # Test a hypothesis hypothesis(demo_fit, "cp_1 > 10") # Make predictions fitted(demo_fit) predict(demo_fit) predict(demo_fit, newdata = data.frame(time = c(55.545, 80, 132))) # Compare to a one-intercept-only model (no change points) with default prior model_null = list(response ~ 1) fit_null = mcp(model_null, data = ex$data, par_x = "time") # fit another model here demo_fit$loo = loo(demo_fit) fit_null$loo = loo(fit_null) loo::loo_compare(demo_fit$loo, fit_null$loo) # Inspect the prior. Useful for prior predictive checks. summary(demo_fit, prior = TRUE) plot(demo_fit, prior = TRUE) # Show all priors. Default priors are added where you don't provide any print(demo_fit$prior) # Set priors and re-run prior = list( int_1 = 15, time_2 = "dt(0, 2, 1) T(0, )", # t-dist slope. Truncated to positive. cp_2 = "dunif(cp_1, 80)", # change point to segment 2 > cp_1 and < 80. int_3 = "int_1" # Shared intercept between segment 1 and 3 ) fit3 = mcp(model, data = ex$data, prior = prior) # Show the JAGS model demo_fit$jags_code
Get example models and data
mcp_example(name, sample = FALSE)
mcp_example(name, sample = FALSE)
name |
Name of the example. One of:
|
sample |
TRUE (run |
List with
model
: A list of formulas
data
: The simulated data
simulated
: The parameters used for simulating the data.
fit
: an mcpfit
if sample = TRUE
,
call
: the code to run the above.
Jonas Kristoffer Lindeløv [email protected]
ex = mcp_example("demo") plot(ex$data) # Plot data print(ex$simulated) # See true parameters used to simulate print(ex$call) # See how the data was simulated # Fit the model. Either... fit = mcp(ex$model, ex$data) plot(fit) ex_with_fit = mcp_example("demo", sample = TRUE) plot(ex_with_fit$fit)
ex = mcp_example("demo") plot(ex$data) # Plot data print(ex$simulated) # See true parameters used to simulate print(ex$call) # See how the data was simulated # Fit the model. Either... fit = mcp(ex$model, ex$data) plot(fit) ex_with_fit = mcp_example("demo", sample = TRUE) plot(ex_with_fit$fit)
mcpfit
of models fitted with the mcp packageModels fitted with the mcp
function are represented as
an mcpfit
object which contains the user input (model, data, family),
derived model characteristics (prior, parameter names, and jags code), and
the fit (prior and/or posterior mcmc samples).
See methods(class = "mcpfit")
for an overview of available methods.
User-provided information (see mcp
for more details):
model
A list of formulas, making up the model.
Provided by user. See mcp
for more details.
data
A data frame.
Provided by user. See mcp
for more details.
family
An mcpfamily
object.
Provided by user. See mcp
for more details.
prior
A named list.
Provided by user. See mcp
for more details.
mcmc_post
An mcmc.list
object with posterior samples.
mcmc_prior
An mcmc.list
object with prior samples.
mcmc_loglik
An mcmc.list
object with samples of log-likelihood.
pars
A list of character vectors of model parameter names.
jags_code
A string with jags code. Use cat(fit$jags_code)
to show it.
simulate
A method to simulate and predict data.
.other
Information that is used internally by mcp.
Parameterized as mu
(mean; poisson lambda) and size
(a shape parameter),
so you can do rnbinom(10, mu = 10, size = 1)
. Read more in the doc for rnbinom
,
negbinomial(link = "log")
negbinomial(link = "log")
link |
Link function (Character). |
Inverse probit function
phi(eta)
phi(eta)
eta |
A vector of probits |
A vector with same length as mu
Plot many types of plots of parameter estimates. See examples for typical use cases.
plot_pars( fit, pars = "population", regex_pars = character(0), type = "combo", ncol = 1, prior = FALSE )
plot_pars( fit, pars = "population", regex_pars = character(0), type = "combo", ncol = 1, prior = FALSE )
fit |
An |
pars |
Character vector. One of:
|
regex_pars |
Vector of regular expressions. This will typically just be the beginning of the parameter name(s), i.e., "^cp_" plots all change points, "^my_varying" plots all levels of a particular varying effect, and "^cp_|^my_varying" plots both. |
type |
String or vector of strings. Calls |
ncol |
Number of columns in plot. This is useful when you have many
parameters and only one plot |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
For other type
, it calls bayesplot::mcmc_type()
. Use these
directly on fit$mcmc_post
or fit$mcmc_prior
if you want finer
control of plotting, e.g., bayesplot::mcmc_dens(fit$mcmc_post)
. There
are also a number of useful plots in the coda package, i.e.,
coda::gelman.plot(fit$mcmc_post)
and coda::crosscorr.plot(fit$mcmc_post)
In any case, if you see a few erratic lines or parameter estimates, this is
a sign that you may want to increase argument 'adapt' and 'iter' in mcp
.
A ggplot2 object.
Jonas Kristoffer Lindeløv [email protected]
# Typical usage. demo_fit is an mcpfit object. plot_pars(demo_fit) ## Not run: # More options plot_pars(demo_fit, regex_pars = "^cp_") # Plot only change points plot_pars(demo_fit, pars = c("int_3", "time_3")) # Plot these parameters plot_pars(demo_fit, type = c("trace", "violin")) # Combine plots # Some plots only take pairs. hex is good to assess identifiability plot_pars(demo_fit, type = "hex", pars = c("cp_1", "time_2")) # Visualize the priors: plot_pars(demo_fit, prior = TRUE) # Useful for varying effects: # plot_pars(my_fit, pars = "varying", ncol = 3) # plot all varying effects # plot_pars(my_fit, regex_pars = "my_varying", ncol = 3) # plot all levels of a particular varying # Customize multi-column ggplots using "*" instead of "+" (patchwork) library(ggplot2) plot_pars(demo_fit, type = c("trace", "dens_overlay")) * theme_bw(10) ## End(Not run)
# Typical usage. demo_fit is an mcpfit object. plot_pars(demo_fit) ## Not run: # More options plot_pars(demo_fit, regex_pars = "^cp_") # Plot only change points plot_pars(demo_fit, pars = c("int_3", "time_3")) # Plot these parameters plot_pars(demo_fit, type = c("trace", "violin")) # Combine plots # Some plots only take pairs. hex is good to assess identifiability plot_pars(demo_fit, type = "hex", pars = c("cp_1", "time_2")) # Visualize the priors: plot_pars(demo_fit, prior = TRUE) # Useful for varying effects: # plot_pars(my_fit, pars = "varying", ncol = 3) # plot all varying effects # plot_pars(my_fit, regex_pars = "my_varying", ncol = 3) # plot all levels of a particular varying # Customize multi-column ggplots using "*" instead of "+" (patchwork) library(ggplot2) plot_pars(demo_fit, type = c("trace", "dens_overlay")) * theme_bw(10) ## End(Not run)
Plot prior or posterior model draws on top of data. Use plot_pars
to
plot individual parameter estimates.
## S3 method for class 'mcpfit' plot( x, facet_by = NULL, lines = 25, geom_data = "point", cp_dens = TRUE, q_fit = FALSE, q_predict = FALSE, rate = TRUE, prior = FALSE, which_y = "ct", arma = TRUE, nsamples = 2000, scale = "response", ... )
## S3 method for class 'mcpfit' plot( x, facet_by = NULL, lines = 25, geom_data = "point", cp_dens = TRUE, q_fit = FALSE, q_predict = FALSE, rate = TRUE, prior = FALSE, which_y = "ct", arma = TRUE, nsamples = 2000, scale = "response", ... )
x |
An |
facet_by |
String. Name of a varying group. |
lines |
Positive integer or |
geom_data |
String. One of "point", "line" (good for time-series), or FALSE (don not plot). |
cp_dens |
TRUE/FALSE. Plot posterior densities of the change point(s)?
Currently does not respect |
q_fit |
Whether to plot quantiles of the posterior (fitted value).
|
q_predict |
Same as |
rate |
Boolean. For binomial models, plot on raw data ( |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
which_y |
What to plot on the y-axis. One of
|
arma |
Whether to include autoregressive effects.
|
nsamples |
Integer or |
scale |
One of
|
... |
Currently ignored. |
plot()
uses fit$simulate()
on posterior samples. These represent the
(joint) posterior distribution.
A ggplot2 object.
Jonas Kristoffer Lindeløv [email protected]
# Typical usage. demo_fit is an mcpfit object. plot(demo_fit) plot(demo_fit, prior = TRUE) # The prior plot(demo_fit, lines = 0, q_fit = TRUE) # 95% HDI without lines plot(demo_fit, q_predict = c(0.1, 0.9)) # 80% prediction interval plot(demo_fit, which_y = "sigma", lines = 100) # The variance parameter on y # Show a panel for each varying effect # plot(fit, facet_by = "my_column") # Customize plots using regular ggplot2 library(ggplot2) plot(demo_fit) + theme_bw(15) + ggtitle("Great plot!")
# Typical usage. demo_fit is an mcpfit object. plot(demo_fit) plot(demo_fit, prior = TRUE) # The prior plot(demo_fit, lines = 0, q_fit = TRUE) # 95% HDI without lines plot(demo_fit, q_predict = c(0.1, 0.9)) # 80% prediction interval plot(demo_fit, which_y = "sigma", lines = 100) # The variance parameter on y # Show a panel for each varying effect # plot(fit, facet_by = "my_column") # Customize plots using regular ggplot2 library(ggplot2) plot(demo_fit) + theme_bw(15) + ggtitle("Great plot!")
Plot posterior (default) or prior (prior = TRUE
) predictive checks. This is convenience wrapper
around the bayesplot::ppc_*()
methods.
pp_check( object, type = "dens_overlay", facet_by = NULL, newdata = NULL, prior = FALSE, varying = TRUE, arma = TRUE, nsamples = 100, ... )
pp_check( object, type = "dens_overlay", facet_by = NULL, newdata = NULL, prior = FALSE, varying = TRUE, arma = TRUE, nsamples = 100, ... )
object |
An |
type |
One of |
facet_by |
Name of a column in data modeled as varying effect(s). |
newdata |
A |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
varying |
One of:
|
arma |
Whether to include autoregressive effects.
|
nsamples |
Number of draws. Note that you may want to use all data for summary geoms.
e.g., |
... |
Further arguments passed to |
A ggplot2
object for single plots. Enriched by patchwork
for faceted plots.
Jonas Kristoffer Lindeløv [email protected]
pp_check(demo_fit) pp_check(demo_fit, type = "ecdf_overlay") #pp_check(some_varying_fit, type = "loo_intervals", facet_by = "id")
pp_check(demo_fit) pp_check(demo_fit, type = "ecdf_overlay") #pp_check(some_varying_fit, type = "loo_intervals", facet_by = "id")
Samples from the Posterior Predictive Distribution
## S3 method for class 'mcpfit' predict( object, newdata = NULL, summary = TRUE, probs = TRUE, rate = TRUE, prior = FALSE, which_y = "ct", varying = TRUE, arma = TRUE, nsamples = NULL, samples_format = "tidy", ... )
## S3 method for class 'mcpfit' predict( object, newdata = NULL, summary = TRUE, probs = TRUE, rate = TRUE, prior = FALSE, which_y = "ct", varying = TRUE, arma = TRUE, nsamples = NULL, samples_format = "tidy", ... )
object |
An |
newdata |
A |
summary |
Summarise at each x-value |
probs |
Vector of quantiles. Only in effect when |
rate |
Boolean. For binomial models, plot on raw data ( |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
which_y |
What to plot on the y-axis. One of
|
varying |
One of:
|
arma |
Whether to include autoregressive effects.
|
nsamples |
Integer or |
samples_format |
One of "tidy" or "matrix". Controls the output format when |
... |
Currently unused |
If summary = TRUE
: A tibble
with the posterior mean for each row in newdata
,
If newdata
is NULL
, the data in fit$data
is used.
If summary = FALSE
and samples_format = "tidy"
: A tidybayes
tibble
with all the posterior
samples (Ns
) evaluated at each row in newdata
(Nn
), i.e., with Ns x Nn
rows. If there are
varying effects, the returned data is expanded with the relevant levels for each row.
The return columns are:
Predictors from newdata
.
Sample descriptors: ".chain", ".iter", ".draw" (see the tidybayes
package for more), and "data_row" (newdata
rownumber)
Sample values: one column for each parameter in the model.
The estimate. Either "predict" or "fitted", i.e., the name of the type
argument.
If summary = FALSE
and samples_format = "matrix"
: An N_draws
X nrows(newdata)
matrix with fitted/predicted
values (depending on type
). This format is used by brms
and it's useful as yrep
in
bayesplot::ppc_*
functions.
Jonas Kristoffer Lindeløv [email protected]
pp_eval
fitted.mcpfit
residuals.mcpfit
predict(demo_fit) # Evaluate at each demo_fit$data predict(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. predict(demo_fit, summary = FALSE) # Samples instead of summary. predict( demo_fit, newdata = data.frame(time = c(-5, 20, 300)), # Evaluate probs = c(0.025, 0.5, 0.975) )
predict(demo_fit) # Evaluate at each demo_fit$data predict(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. predict(demo_fit, summary = FALSE) # Samples instead of summary. predict( demo_fit, newdata = data.frame(time = c(-5, 20, 300)), # Evaluate probs = c(0.025, 0.5, 0.975) )
Shows a list in a more condensed format using str(list)
.
## S3 method for class 'mcplist' print(x, ...)
## S3 method for class 'mcplist' print(x, ...)
x |
An |
... |
Currently ignored |
Jonas Kristoffer Lindeløv [email protected]
Useful for print(fit$jags_code)
, print(mcp_demo$call)
, etc.
## S3 method for class 'mcptext' print(x, ...)
## S3 method for class 'mcptext' print(x, ...)
x |
Character, often with newlines. |
... |
Currently ignored. |
Jonas Kristoffer Lindeløv [email protected]
mytext = "line1 = 2\n line2 = 'horse'" class(mytext) = "mcptext" print(mytext)
mytext = "line1 = 2\n line2 = 'horse'" class(mytext) = "mcptext" print(mytext)
Probit function
probit(mu)
probit(mu)
mu |
A vector of probabilities (0.0 to 1.0) |
A vector with same length as mu
Equivalent to fitted(fit, ...) - fit$data[, fit$data$yvar]
(or fitted(fit, ...) - newdata[, fit$data$yvar]
),
but with fixed arguments for fitted
: rate = FALSE, which_y = 'ct', samples_format = 'tidy'
.
## S3 method for class 'mcpfit' residuals( object, newdata = NULL, summary = TRUE, probs = TRUE, prior = FALSE, varying = TRUE, arma = TRUE, nsamples = NULL, ... )
## S3 method for class 'mcpfit' residuals( object, newdata = NULL, summary = TRUE, probs = TRUE, prior = FALSE, varying = TRUE, arma = TRUE, nsamples = NULL, ... )
object |
An |
newdata |
A |
summary |
Summarise at each x-value |
probs |
Vector of quantiles. Only in effect when |
prior |
TRUE/FALSE. Plot using prior samples? Useful for |
varying |
One of:
|
arma |
Whether to include autoregressive effects.
|
nsamples |
Integer or |
... |
Currently unused |
Jonas Kristoffer Lindeløv [email protected]
pp_eval
fitted.mcpfit
predict.mcpfit
residuals(demo_fit) residuals(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. residuals(demo_fit, summary = FALSE) # Samples instead of summary.
residuals(demo_fit) residuals(demo_fit, probs = c(0.1, 0.5, 0.9)) # With median and 80% credible interval. residuals(demo_fit, summary = FALSE) # Samples instead of summary.
JAGS uses precision rather than SD. This function converts
dnorm(4.2, 1.3)
into dnorm(4.2, 1/1.3^2)
. It allows users to specify
priors using SD and then it's transformed for the JAGS code. It works for the
following distributions: dnorm|dt|dcauchy|ddexp|dlogis|dlnorm. In all of
these,
tau/sd is the second parameter.
sd_to_prec(prior_str)
sd_to_prec(prior_str)
prior_str |
String. A JAGS prior. Can be truncated, e.g.
|
A string
Jonas Kristoffer Lindeløv [email protected]
Summarise parameter estimates and model diagnostics.
## S3 method for class 'mcpfit' summary(object, width = 0.95, digits = 2, prior = FALSE, ...) fixef(object, width = 0.95, prior = FALSE, ...) ranef(object, width = 0.95, prior = FALSE, ...) ## S3 method for class 'mcpfit' print(x, ...)
## S3 method for class 'mcpfit' summary(object, width = 0.95, digits = 2, prior = FALSE, ...) fixef(object, width = 0.95, prior = FALSE, ...) ranef(object, width = 0.95, prior = FALSE, ...) ## S3 method for class 'mcpfit' print(x, ...)
object |
An |
width |
Float. The width of the highest posterior density interval (between 0 and 1). |
digits |
a non-null value for digits specifies the minimum number of significant digits to be printed in values. The default, NULL, uses getOption("digits"). (For the interpretation for complex numbers see signif.) Non-integer values will be rounded down, and only values greater than or equal to 1 and no greater than 22 are accepted. |
prior |
TRUE/FALSE. Summarise prior instead of posterior? |
... |
Currently ignored |
x |
An |
A data frame with parameter estimates and MCMC diagnostics.
OBS: The change point distributions are often not unimodal and symmetric so
the intervals can be deceiving Plot them using plot_pars(fit)
.
mean
is the posterior mean
lower
is the lower quantile of the highest-density interval (HDI) given in width
.
upper
is the upper quantile.
Rhat
is the Gelman-Rubin convergence diagnostic which is often taken to
be acceptable if < 1.1. It is computed using gelman.diag
.
n.eff
is the effective sample size computed using effectiveSize
.
Low effective sample sizes are also obvious as poor mixing in trace plots
(see plot_pars(fit)
). Read how to deal with such problems here
ts_err
is the time-series error, taking autoregressive correlation
into account. It is computed using spectrum0.ar
.
For simulated data, the summary contains two additional columns so that it is easy to inspect whether the model can recover the parameters. Run simulation and summary multiple times to get a sense of the robustness.
sim
is the value used to generate the data.
match
is "OK"
if sim
is contained in the HDI interval (lower
to
upper
).
fixef()
: Get population-level ("fixed") effects of an mcpfit
object.
ranef()
: Get varying ("random") effects of an mcpfit
object.
print(mcpfit)
: Print the posterior summary of an mcpfit
object.
Jonas Kristoffer Lindeløv [email protected]
# Typical usage summary(demo_fit) summary(demo_fit, width = 0.8, digits = 4) # Set HDI width # Get the results as a data frame results = summary(demo_fit) # Varying (random) effects # ranef(my_fit) # Summarise prior summary(demo_fit, prior = TRUE)
# Typical usage summary(demo_fit) summary(demo_fit, width = 0.8, digits = 4) # Set HDI width # Get the results as a data frame results = summary(demo_fit) # Varying (random) effects # ranef(my_fit) # Summarise prior summary(demo_fit, prior = TRUE)