Learn R Programming

eeptools (version 0.3.1)

gelmansim: Generate prediction intervals for model functions

Description

Generate prediction intervals from R models following Gelman and Hill

Usage

gelmansim(mod, newdata, nsims, 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
nsims
Number of simulations per case
na.omit
Logical indicating whether to remove NAs from newdata

Value

  • A dataframe with newdata and prediction intervals

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.

References

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

Examples

Run this code
#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, nsims=200, na.omit=TRUE)
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, nsims=200, na.omit=TRUE)

Run the code above in your browser using DataLab