Description Usage Arguments Details Value References See Also Examples
Bayesian inference for linear modeling with regularizing priors on the model
parameters that are driven by prior beliefs about R^2, the proportion
of variance in the outcome attributable to the predictors. See
priors
for an explanation of this critical point.
stan_glm
with family="gaussian"
also estimates a linear
model with normallydistributed errors and allows for various other priors on
the coefficients.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  stan_aov(
formula,
data,
projections = FALSE,
contrasts = NULL,
...,
prior = R2(stop("'location' must be specified")),
prior_PD = FALSE,
algorithm = c("sampling", "meanfield", "fullrank"),
adapt_delta = NULL
)
stan_lm(
formula,
data,
subset,
weights,
na.action,
model = TRUE,
x = FALSE,
y = FALSE,
singular.ok = TRUE,
contrasts = NULL,
offset,
...,
prior = R2(stop("'location' must be specified")),
prior_intercept = NULL,
prior_PD = FALSE,
algorithm = c("sampling", "meanfield", "fullrank"),
adapt_delta = NULL
)
stan_lm.wfit(
x,
y,
w,
offset = NULL,
singular.ok = TRUE,
...,
prior = R2(stop("'location' must be specified")),
prior_intercept = NULL,
prior_PD = FALSE,
algorithm = c("sampling", "meanfield", "fullrank"),
adapt_delta = NULL
)
stan_lm.fit(
x,
y,
offset = NULL,
singular.ok = TRUE,
...,
prior = R2(stop("'location' must be specified")),
prior_intercept = NULL,
prior_PD = FALSE,
algorithm = c("sampling", "meanfield", "fullrank"),
adapt_delta = NULL
)

formula, data, subset 
Same as 
projections 
For 
... 
Further arguments passed to the function in the rstan
package ( 
prior 
Must be a call to 
prior_PD 
A logical scalar (defaulting to 
algorithm 
A string (possibly abbreviated) indicating the
estimation approach to use. Can be 
adapt_delta 
Only relevant if 
na.action, singular.ok, contrasts 
Same as 
model, offset, weights 
Same as 
x, y 
In 
prior_intercept 
Either Note: If using a dense representation of the design matrix
—i.e., if the 
w 
Same as in 
The stan_lm
function is similar in syntax to the
lm
function but rather than choosing the parameters to
minimize the sum of squared residuals, samples from the posterior
distribution are drawn using MCMC (if algorithm
is
"sampling"
). The stan_lm
function has a formulabased
interface and would usually be called by users but the stan_lm.fit
and stan_lm.wfit
functions might be called by other functions that
parse the data themselves and are analogous to lm.fit
and lm.wfit
respectively.
In addition to estimating sigma
— the standard deviation of the
normallydistributed errors — this model estimates a positive parameter
called logfit_ratio
. If it is positive, the marginal posterior
variance of the outcome will exceed the sample variance of the outcome
by a multiplicative factor equal to the square of fit_ratio
.
Conversely if logfit_ratio
is negative, then the model underfits.
Given the regularizing nature of the priors, a slight underfit is good.
Finally, the posterior predictive distribution is generated with the predictors fixed at their sample means. This quantity is useful for checking convergence because it is reasonably normally distributed and a function of all the parameters in the model.
The stan_aov
function is similar to aov
, but
does a Bayesian analysis of variance that is basically equivalent to
stan_lm
with dummy variables. stan_aov
has a somewhat
customized print
method that prints an ANOVAlike table in
addition to the output printed for stan_lm
models.
A stanreg object is returned
for stan_lm, stan_aov
.
A stanfit object (or a slightly modified
stanfit object) is returned if stan_lm.fit or stan_lm.wfit
is called directly.
Lewandowski, D., Kurowicka D., and Joe, H. (2009). Generating random correlation matrices based on vines and extended onion method. Journal of Multivariate Analysis. 100(9), 1989–2001.
The vignettes for stan_lm
and stan_aov
, which have more
thorough descriptions and examples.
http://mcstan.org/rstanarm/articles/
Also see stan_glm
, which — if family =
gaussian(link="identity")
— also estimates a linear model with
normallydistributed errors but specifies different priors.
1 2 3 4 5 6 7 8 9 10 11 12  op < options(contrasts = c("contr.helmert", "contr.poly"))
fit_aov < stan_aov(yield ~ block + N*P*K, data = npk,
prior = R2(0.5), seed = 12345)
options(op)
print(fit_aov)
(fit < stan_lm(mpg ~ wt + qsec + am, data = mtcars, prior = R2(0.75),
# the next line is only to make the example go fast enough
chains = 1, iter = 300, seed = 12345, refresh = 0))
plot(fit, "hist", pars = c("wt", "am", "qsec", "sigma"),
transformations = list(sigma = "log"))

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