gelmansim | R Documentation |
Generate prediction intervals from R models following Gelman and Hill
gelmansim(mod, newdata, n.sims, na.omit = TRUE)
mod |
Name of a model object such as |
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 |
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.
A dataframe with newdata and prediction intervals
Modified from Gelman and Hill 2006. Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.
#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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.