get_predictions: Get model predictions for specific conditions.

View source: R/predict.R

get_predictionsR Documentation

Get model predictions for specific conditions.

Description

Get model predictions for specific conditions.

Usage

get_predictions(
  model,
  cond = NULL,
  rm.ranef = TRUE,
  se = TRUE,
  sim.ci = FALSE,
  f = 1.96,
  return.n.posterior = 0,
  print.summary = getOption("itsadug_print")
)

Arguments

model

A gam object, produced by gam or bam.

cond

A named list of the values to use for the predictor terms. Variables omitted from this list will have the closest observed value to the median for continuous variables, or the reference level for factors.

rm.ranef

Logical: whether or not to remove random effects. Default is TRUE. Alternatively a vector with numbers (modelterms) of the random effect(s) to remove.

se

Logical: whether or not to return the confidence interval or standard error around the estimates.

sim.ci

Logical: Using simultaneous confidence intervals or not (default set to FALSE). The implementation of simultaneous CIs follows Gavin Simpson's blog of December 15, 2016: https://fromthebottomoftheheap.net/2016/12/15/simultaneous-interval-revisited/. This interval is calculated from simulations based. Please specify a seed (e.g., set.seed(123)) for reproducable results. In addition, make sure to specify at least 200 points for each smooth for the simulations when using simultaneous CI. Note: in contrast with Gavin Simpson's code, here the Bayesian posterior covariance matrix of the parameters is uncertainty corrected (unconditional=TRUE) to reflect the uncertainty on the estimation of smoothness parameters.

f

A number to scale the standard error. Defaults to 1.96, resulting in 95% confidence intervals. For 99% confidence intervals use a value of 2.58.

return.n.posterior

Numeric: N samples from the posterior distribution of the fitted model are returned. Default value is 0 (no samples returned). Only workes when sim.ci=TRUE.

print.summary

Logical: whether or not to print a summary of the values selected for each predictor. Default set to the print info messages option (see infoMessages).

Value

A data frame with estimates and optionally errors.

Author(s)

Jacolien van Rij

See Also

Other Model predictions: get_coefs(), get_difference(), get_fitted(), get_modelterm(), get_random()

Examples

data(simdat)

## Not run: 
m1 <- bam(Y ~ Group + s(Time, by=Group), data=simdat)

# Time value is automatically set:
pp <- get_predictions(m1, cond=list(Group='Adults'))
head(pp)

# Range of time values:
pp <- get_predictions(m1, 
    cond=list(Group='Adults', Time=seq(0,500,length=100)))
# plot:
emptyPlot(500, range(pp$fit), h=0)
plot_error(pp$Time, pp$fit, pp$CI, shade=TRUE, xpd=TRUE)

# Warning: also unrealistical values are possible
range(simdat$Time)
pp <- get_predictions(m1, 
    cond=list(Group='Adults', Time=seq(-500,0,length=100)))
# plot of predictions that are not supported by data:
emptyPlot(c(-500,0), range(pp$fit), h=0)
plot_error(pp$Time, pp$fit, pp$CI, shade=TRUE, xpd=TRUE) 

m2 <- bam(Y ~ Group + s(Time, by=Group)
    + s(Time, Subject, bs='fs', m=1), 
    data=simdat, discrete=TRUE)
# Simultaneous CI vs pointwise CI
# NOTE: USE AT LEST 200 DATAPOINTS FOR SIMULTANEOUS CI
pp <- get_predictions(m2, 
    cond=list(Group='Adults', Time=seq(0,2000,length=200)), 
    rm.ranef=TRUE, sim.ci=TRUE)
head(pp)
# plot:
emptyPlot(2000, range(pp$fit), h=0)
plot_error(pp$Time, pp$fit, pp$CI, shade=TRUE, xpd=TRUE)
plot_error(pp$Time, pp$fit, pp$sim.CI, shade=FALSE, col=2, xpd=TRUE)



## End(Not run)


itsadug documentation built on June 17, 2022, 5:05 p.m.