log_lik.stanreg {rstanarm} | R Documentation |
Pointwise log-likelihood matrix
Description
For models fit using MCMC only, the log_lik
method returns the
by
pointwise log-likelihood matrix, where
is the size
of the posterior sample and
is the number of data points, or in the
case of the
stanmvreg
method (when called on stan_jm
model objects) an by
matrix where
is the number
of individuals.
Usage
## S3 method for class 'stanreg'
log_lik(object, newdata = NULL, offset = NULL, ...)
## S3 method for class 'stanmvreg'
log_lik(object, m = 1, newdata = NULL, ...)
## S3 method for class 'stanjm'
log_lik(object, newdataLong = NULL, newdataEvent = NULL, ...)
Arguments
object |
A fitted model object returned by one of the
rstanarm modeling functions. See |
newdata |
An optional data frame of new data (e.g. holdout data) to use
when evaluating the log-likelihood. See the description of |
offset |
A vector of offsets. Only required if |
... |
Currently ignored. |
m |
Integer specifying the number or name of the submodel |
newdataLong , newdataEvent |
Optional data frames containing new data
(e.g. holdout data) to use when evaluating the log-likelihood for a
model estimated using |
Value
For the stanreg
and stanmvreg
methods an by
matrix, where
is the size of the posterior sample and
is the number of data points. For the
stanjm
method
an by
matrix where
is the number of individuals.
Examples
if (.Platform$OS.type != "windows" || .Platform$r_arch != "i386") {
roaches$roach100 <- roaches$roach1 / 100
fit <- stan_glm(
y ~ roach100 + treatment + senior,
offset = log(exposure2),
data = roaches,
family = poisson(link = "log"),
prior = normal(0, 2.5),
prior_intercept = normal(0, 10),
iter = 500, # just to speed up example,
refresh = 0
)
ll <- log_lik(fit)
dim(ll)
all.equal(ncol(ll), nobs(fit))
# using newdata argument
nd <- roaches[1:2, ]
nd$treatment[1:2] <- c(0, 1)
ll2 <- log_lik(fit, newdata = nd, offset = c(0, 0))
head(ll2)
dim(ll2)
all.equal(ncol(ll2), nrow(nd))
}