如何仅在 rstanarm 中的一个特定预测变量上指定信息先验
How to specify informative prior only on one specific predictor in rstanarm
我目前正在尝试使用 rstanarm 拟合贝叶斯多级模型。
我预计——而且文献中有证据——我的主要预测因子的系数在 0.15 到 0.65 之间。因此,我想设置一个信息先验但仅针对此变量,并为其他变量保留信息较弱的默认值。
到目前为止我有:
stan_glmer(isei_r ~ 1 + maxisei_cntr + agea + as.factor(gender) + as.factor(emp_status) + (1 + maxisei_cntr | country),
data = ess,
seed = 349,
prior = normal(0.40, 0.25, autoscale=F))
但是通过这种方式,它先于我的所有协变量应用信息。是否可以只为一个预测变量指定信息先验?
谢谢
是的,但您需要传递一个大小等于系数数量(不包括截距)的先前位置和/或尺度的矢量。因此,在您的示例中,它可能类似于:
stan_glmer(..., prior = normal(location = c(0, 0.4, 0, 0),
scale = c(10, 0.25, 10, 10))
这是我用来自动执行此过程的方法:
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)
Parameter
Median
89% CI
pd
% in ROPE
Rhat
ESS
Prior
(Intercept)
2.40
[ 1.97, 2.79]
100%
0%
1.000
2556.00
Normal (5.84 +- 2.07)
Speciesversicolor
-0.95
[-1.31, -0.61]
100%
0%
1.002
1274.00
Normal (-1.00 +- 5.00)
Speciesvirginica
-1.39
[-1.86, -0.93]
100%
0%
1.003
1285.00
Normal (-1.50 +- 5.00)
Sepal.Width
0.43
[ 0.29, 0.55]
100%
0%
1.001
1803.00
Normal (0.00 +- 10.00)
Petal.Length
0.77
[ 0.67, 0.88]
100%
0%
1.002
1359.00
Normal (1.00 +- 8.00)
我目前正在尝试使用 rstanarm 拟合贝叶斯多级模型。 我预计——而且文献中有证据——我的主要预测因子的系数在 0.15 到 0.65 之间。因此,我想设置一个信息先验但仅针对此变量,并为其他变量保留信息较弱的默认值。 到目前为止我有:
stan_glmer(isei_r ~ 1 + maxisei_cntr + agea + as.factor(gender) + as.factor(emp_status) + (1 + maxisei_cntr | country),
data = ess,
seed = 349,
prior = normal(0.40, 0.25, autoscale=F))
但是通过这种方式,它先于我的所有协变量应用信息。是否可以只为一个预测变量指定信息先验?
谢谢
是的,但您需要传递一个大小等于系数数量(不包括截距)的先前位置和/或尺度的矢量。因此,在您的示例中,它可能类似于:
stan_glmer(..., prior = normal(location = c(0, 0.4, 0, 0),
scale = c(10, 0.25, 10, 10))
这是我用来自动执行此过程的方法:
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)
Parameter | Median | 89% CI | pd | % in ROPE | Rhat | ESS | Prior |
---|---|---|---|---|---|---|---|
(Intercept) | 2.40 | [ 1.97, 2.79] | 100% | 0% | 1.000 | 2556.00 | Normal (5.84 +- 2.07) |
Speciesversicolor | -0.95 | [-1.31, -0.61] | 100% | 0% | 1.002 | 1274.00 | Normal (-1.00 +- 5.00) |
Speciesvirginica | -1.39 | [-1.86, -0.93] | 100% | 0% | 1.003 | 1285.00 | Normal (-1.50 +- 5.00) |
Sepal.Width | 0.43 | [ 0.29, 0.55] | 100% | 0% | 1.001 | 1803.00 | Normal (0.00 +- 10.00) |
Petal.Length | 0.77 | [ 0.67, 0.88] | 100% | 0% | 1.002 | 1359.00 | Normal (1.00 +- 8.00) |