Description Usage Arguments Value Note See Also Examples
View source: R/predictive_error.R
This is a convenience function for computing y  yrep
(insample, for observed y) or y  ytilde
(outofsample, for new or heldout y). The method for stanreg objects
calls posterior_predict
internally, whereas the method for
objects with class "ppd"
accepts the matrix returned by
posterior_predict
as input and can be used to avoid multiple calls to
posterior_predict
.
1 2 3 4 5 6 7 8 9 10 11 12 13  ## S3 method for class 'stanreg'
predictive_error(
object,
newdata = NULL,
draws = NULL,
re.form = NULL,
seed = NULL,
offset = NULL,
...
)
## S3 method for class 'ppd'
predictive_error(object, y, ...)

object 
Either a fitted model object returned by one of the
rstanarm modeling functions (a stanreg
object) or, for the 
newdata, draws, seed, offset, re.form 
Optional arguments passed to

... 
Currently ignored. 
y 
For the 
A draws
by nrow(newdata)
matrix. If newdata
is
not specified then it will be draws
by nobs(object)
.
The Note section in posterior_predict
about
newdata
for binomial models also applies for
predictive_error
, with one important difference. For
posterior_predict
if the lefthand side of the model formula is
cbind(successes, failures)
then the particular values of
successes
and failures
in newdata
don't matter, only
that they add to the desired number of trials. This is not the case
for predictive_error
. For predictive_error
the particular
value of successes
matters because it is used as y when
computing the error.
posterior_predict
to draw
from the posterior predictive distribution without computing predictive
errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  if (!exists("example_model")) example(example_model)
err1 < predictive_error(example_model, draws = 50)
hist(err1)
# Using newdata with a binomial model
formula(example_model)
nd < data.frame(
size = c(10, 20),
incidence = c(5, 10),
period = factor(c(1,2)),
herd = c(1, 15)
)
err2 < predictive_error(example_model, newdata = nd, draws = 10, seed = 1234)
# stanreg vs ppd methods
fit < stan_glm(mpg ~ wt, data = mtcars, iter = 300)
preds < posterior_predict(fit, seed = 123)
all.equal(
predictive_error(fit, seed = 123),
predictive_error(preds, y = fit$y)
)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.