# \donttest{
# 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
# }
Run the code above in your browser using DataLab