Description Usage Arguments Details Author(s) See Also Examples
This function computes predictions and prediction intervals from a model fit by glmer2stan
. Predictions use the entire posterior for inference. Uncertainty due to both posterior probability and sampling distribution are included.
1 2 | stanpredict( stanfit , data , vary_prefix="vary_" , fixed_prefix="beta_" ,
probs=c(0.025,0.975) , nsims=1e4 )
|
fit |
|
data |
data frame of cases to use for prediction |
fixed_prefix |
Identifying prefix for fixed effects, in the samples |
vary_prefix |
Identifying prefix for varying effects, in the samples |
probs |
Prediction quantiles to report (default is 95 percent interval) |
nsims |
Number of simulations, for generating sampling distributions |
This function uses the posterior samples from a stanfit
object, fit with glmer2stan
, to simulate model predictions.
The returned value is a list with a named entry for each outcome variable in the model. Under each outcome variable is a list with: (1) mu
, expectation of mean; (2) mu.ci
, quantiles for the mean; and (3) obs.ci
, quantiles for the predicted sampling distribution.
When passing new data to stanpredict
, you can use a list with different length vectors. Short vectors will be grown to match the length of the longest vector, using repetition. For cluster variables, using a value of zero will be interpreted as omitting varying effects from predictions, generating predictions for average cases in the population. Otherwise, cluster variable values should be integers.
Richard McElreath
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # simple linear regression
data(cars)
m <- glmer2stan( dist ~ speed , data=cars )
speed.seq <- seq( from=min(cars$speed) , to=max(cars$speed) , length.out=20 )
pred.dist <- stanpredict( m , data=list( speed=speed.seq ) )$dist
plot( dist ~ speed , cars )
lines( speed.seq , pred.dist$mu )
lines( speed.seq , pred.dist$mu.ci[1,] , lty=2 )
lines( speed.seq , pred.dist$mu.ci[2,] , lty=2 )
# binomial example
data(UCBadmit)
m <- glmer2stan( cbind(admit,reject) ~ (1|dept) + male ,
data=UCBadmit , family="binomial" )
pred.admit <- stanpredict( m , data=UCBadmit )$admit
prop.admit <- UCBadmit$admit / UCBadmit$applications
plot( prop.admit , ylim=c(0,1) , pch=ifelse(UCBadmit$male==1,2,1) ,
ylab="probability admit" , xlab="case" )
lines( 1:12 , pred.admit$mu )
lines( 1:12 , pred.admit$mu.ci[1,] , lty=2 )
lines( 1:12 , pred.admit$mu.ci[2,] , lty=2 )
lines( 1:12 , pred.admit$obs.ci[1,] , lty=3 )
lines( 1:12 , pred.admit$obs.ci[2,] , lty=3 )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.