gelmansim: Generate prediction intervals for model functions

View source: R/modsims.R

gelmansimR Documentation

Generate prediction intervals for model functions

Description

Generate prediction intervals from R models following Gelman and Hill

Usage

gelmansim(mod, newdata, n.sims, na.omit = TRUE)

Arguments

mod

Name of a model object such as lm, glm, or merMod

newdata

Sets of new data to generate predictions for

n.sims

Number of simulations per case

na.omit

Logical indicating whether to remove NAs from newdata

Details

Currently gelmansim does not work for lm objects because of the way sim in the arm package handles variable names for these objects. It is recommended users use glm in these cases.

Value

A dataframe with newdata and prediction intervals

References

Modified from Gelman and Hill 2006. Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.

Examples

 #Examples of "sim" 
set.seed (1)
J <- 15
n <- J*(J+1)/2
group <- rep (1:J, 1:J)
mu.a <- 5
sigma.a <- 2
a <- rnorm (J, mu.a, sigma.a)
b <- -3
x <- rnorm (n, 2, 1)
sigma.y <- 6
y <- rnorm (n, a[group] + b*x, sigma.y)
u <- runif (J, 0, 3)
y123.dat <- cbind (y, x, group)
# Linear regression 
x1 <- y123.dat[,2]
y1 <- y123.dat[,1]
M1 <- glm (y1 ~ x1)

cases <- data.frame(x1 = seq(-2, 2, by=0.1))
sim.results <- gelmansim(M1, newdata=cases, n.sims=200, na.omit=TRUE)
## Not run: 

dat <- as.data.frame(y123.dat)
M2 <- glm (y1 ~ x1 + group, data=dat)

cases <- expand.grid(x1 = seq(-2, 2, by=0.1), 
                     group=seq(1, 14, by=2))

sim.results <- gelmansim(M2, newdata=cases, n.sims=200, na.omit=TRUE)
 

## End(Not run)

eeptools documentation built on June 7, 2023, 5:06 p.m.