这是我用来自动化此过程的方法:
data = iris
formula = Sepal.Length ~ Species + Sepal.Width + Petal.Length
# get the coefficient terms, including expanded dummy columns for factors
formula_terms = colnames(
model.matrix(
formula,
data=data
)
)
# remove intercept (it can specified separately)
formula_terms = setdiff(formula_terms, '(Intercept)')
# create named vectors mapping some of the coefficients to priors
priors_location = c(
'Speciesversicolor'=-1,
'Speciesvirginica'=-1.5,
'Petal.Length'=1
)
priors_scale = c(
'Speciesversicolor'=5,
'Speciesvirginica'=5,
'Petal.Length'=8
)
现在,定义一个辅助函数,它可以放在别处:
priors_or_default_for = function(formula_terms, priors, default) {
as.array(
sapply(
formula_terms,
function(term) ifelse(term %in% names(priors), priors[term], default)
)
)
}
并用它来创建先验向量:
location = priors_or_default_for(formula_terms, priors_location, default=0)
scale = priors_or_default_for(formula_terms, priors_scale, default=10)
fit = rstanarm::stan_glm(
formula,
data=data,
prior=normal(
location=location,
scale=scale
)
)
# (recommended) check that the priors were properly assigned
parameters::parameters(fit)
范围 |
中位数 |
89% 置信度 |
PD |
绳子中的百分比 |
拉特 |
ESS |
事先的 |
(截距) |
2.40 |
[ 1.97, 2.79] |
100% |
0% |
1.000 |
2556.00 |
正常 (5.84 +- 2.07) |
杂色种 |
-0.95 |
[-1.31, -0.61] |
100% |
0% |
1.002 |
1274.00 |
正常 (-1.00 +- 5.00) |
种属处女 |
-1.39 |
[-1.86, -0.93] |
100% |
0% |
1.003 |
1285.00 |
正常 (-1.50 +- 5.00) |
隔板宽度 |
0.43 |
[ 0.29, 0.55] |
100% |
0% |
1.001 |
1803.00 |
正常 (0.00 +- 10.00) |
花瓣长度 |
0.77 |
[ 0.67, 0.88] |
100% |
0% |
1.002 |
1359.00 |
正常 (1.00 +- 8.00) |