
Last chance! 50% off unlimited learning
Sale ends in
stan_glm(formula, family = gaussian(), data, weights, subset, na.action = NULL, offset = NULL, model = TRUE, x = FALSE, y = TRUE, contrasts = NULL, ..., prior = normal(), prior_intercept = normal(), prior_ops = prior_options(), prior_PD = FALSE, algorithm = c("sampling", "optimizing", "meanfield", "fullrank"), adapt_delta = NULL, QR = FALSE, sparse = FALSE)
stan_glm.nb(formula, data, weights, subset, na.action = NULL, offset = NULL, model = TRUE, x = FALSE, y = TRUE, contrasts = NULL, link = "log", ..., prior = normal(), prior_intercept = normal(), prior_ops = prior_options(), prior_PD = FALSE, algorithm = c("sampling", "optimizing", "meanfield", "fullrank"), adapt_delta = NULL, QR = FALSE)
stan_glm.fit(x, y, weights = rep(1, NROW(x)), offset = rep(0, NROW(x)), family = gaussian(), ..., prior = normal(), prior_intercept = normal(), prior_ops = prior_options(), group = list(), prior_PD = FALSE, algorithm = c("sampling", "optimizing", "meanfield", "fullrank"), adapt_delta = NULL, QR = FALSE, sparse = FALSE)
glm
.glm
, except negative binomial GLMs
are also possible using the neg_binomial_2
family object.glm
, but
rarely specified.glm
.stan_glm, stan_glm.nb
, logical scalars indicating whether to
return the design matrix and response vector. In stan_glm.fit
,
a design matrix and response vector.sampling
, vb
, or
optimizing
), corresponding to the estimation method
named by algorithm
. For example, if algorithm
is
"sampling"
it is possibly to specify iter
, chains
,
cores
, refresh
, etc.prior
can be a call to normal
, student_t
,
cauchy
, hs
or hs_plus
. See priors
for
details. To to omit a prior ---i.e., to use a flat (improper) uniform
prior--- set prior
to NULL
.prior_intercept
can be a call to normal
, student_t
or
cauchy
. See priors
for details. To to omit a prior
---i.e., to use a flat (improper) uniform prior--- set
prior_intercept
to NULL
. (Note: if a dense
representation of the design matrix is utilized ---i.e., if the
sparse
argument is left at its default value of FALSE
--- then
the prior distribution for the intercept is set so it applies to the value
when all predictors are centered.)NULL
to omit a prior on the dispersion and see
prior_options
otherwise.FALSE
) indicating
whether to draw from the prior predictive distribution instead of
conditioning on the outcome."sampling"
for MCMC (the
default), "optimizing"
for optimization, "meanfield"
for
variational inference with independent normal distributions, or
"fullrank"
for variational inference with a multivariate normal
distribution. See rstanarm-package
for more details on the
estimation algorithms. NOTE: not all fitting functions support all four
algorithms.algorithm="sampling"
. See
adapt_delta
for details.FALSE
) but if TRUE
applies a scaled qr
decomposition to the design matrix,
$X = Q* R*$, where
$Q* = Q (n-1)^0.5$ and
$R* = (n-1)^(-0.5) R$. The coefficients
relative to $Q*$ are obtained and then premultiplied by the
inverse of $R*$ to obtain coefficients relative to the
original predictors, $X$. These transformations do not change the
likelihood of the data but are recommended for computational reasons when
there are multiple predictors. However, because the coefficients relative
to $Q*$ are not very interpretable it is hard to specify an
informative prior. Setting QR=TRUE
is therefore only recommended
if you do not have an informative prior for the regression coefficients.FALSE
) indicating
whether to use a sparse representation of the design (X) matrix.
Setting this to TRUE
will likely be twice as slow, even if the
design matrix has a considerable number of zeros, but it may allow the
model to be estimated when the computer has too little RAM to
utilize a dense design matrix. If TRUE
, the the design matrix
is not centered (since that would destroy the sparsity) and it is
not possible to specify both QR = TRUE
and sparse = TRUE
.stan_glm.nb
only, the link function to use. See
neg_binomial_2
.mkReTrms
to
indicate the group-specific part of the model. In addition, this list must
have elements for the regularization
, concentration
shape
, and scale
components of a decov
prior for the covariance matrices among the group-specific coefficients.stan_glm
function is similar in syntax to
glm
but rather than performing maximum likelihood
estimation of generalized linear models, full Bayesian estimation is
performed (if algorithm
is "sampling"
) via MCMC. The Bayesian
model adds independent priors on the coefficients of the GLM. The
stan_glm
function calls the workhorse stan_glm.fit
function,
but it is also possible to call the latter directly.
The stan_glm.nb
function, which takes the extra argument
link
, is a simple wrapper for stan_glm
with family =
neg_binomial_2(link)
.
stanreg-methods
and
glm
.The various vignettes for stan_glm
.
if (!grepl("^sparc", R.version$platform)) {
### Linear regression
fit <- stan_glm(mpg / 10 ~ ., data = mtcars, QR = TRUE,
algorithm = "fullrank") # for speed only
plot(fit, ci_level = 0.5)
plot(fit, ci_level = 0.5, pars = "beta")
### Logistic regression
data(lalonde, package = "arm")
dat <- within(lalonde, {
re74_1k <- re74 / 1000
re75_1k <- re75 / 1000
})
t7 <- student_t(df = 7)
fmla <- treat ~ re74_1k + re75_1k + educ + black + hisp +
married + nodegr + u74 + u75
fit2 <- stan_glm(fmla, data = dat, family = binomial(link="logit"),
prior = t7, prior_intercept = t7,
algorithm = "fullrank") # for speed only
plot(fit2, pars = c("black", "hisp", "nodegr", "u74", "u75"),
ci_level = 0.67, outer_level = 1, show_density = TRUE)
pp_check(fit2, check = "resid")
pp_check(fit2, check = "test", test = "mean")
}
## Not run:
# ### Poisson regression (example from help("glm"))
# counts <- c(18,17,15,20,10,20,25,13,12)
# outcome <- gl(3,1,9)
# treatment <- gl(3,3)
# fit3 <- stan_glm(counts ~ outcome + treatment, family = poisson(link="log"),
# prior = normal(0, 1), prior_intercept = normal(0, 5))
# plot(fit3, fill_color = "skyblue4", est_color = "maroon")
#
# ### Gamma regression (example from help("glm"))
# clotting <- data.frame(log_u = log(c(5,10,15,20,30,40,60,80,100)),
# lot1 = c(118,58,42,35,27,25,21,19,18),
# lot2 = c(69,35,26,21,18,16,13,12,12))
# fit4 <- stan_glm(lot1 ~ log_u, data = clotting, family = Gamma)
# print(fit4, digits = 2)
# fit5 <- update(fit4, formula = lot2 ~ log_u)
# ## End(Not run)
Run the code above in your browser using DataLab