从 stan 分布线性模型中提取 sigma 值并将其添加到数据框
Extract and add to the data frame the values of sigma from a stan distributional linear model
鉴于下面的示例数据 sampleDT
和 brms
模型 brm.fit
和 brm.fit.distr
,我想:
estimate, extract and add to the data frame the values of the standard
deviations for each observation from the distributional model
brm.fit.distr
.
我可以使用 brm.fit
执行此操作,但是当我使用 brm.fit.distr
时我的方法失败了。
示例数据
sampleDT<-structure(list(id = 1:10, N = c(10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L), A = c(62L, 96L, 17L, 41L, 212L, 143L, 143L,
143L, 73L, 73L), B = c(3L, 1L, 0L, 2L, 170L, 21L, 0L, 33L, 62L,
17L), C = c(0.05, 0.01, 0, 0.05, 0.8, 0.15, 0, 0.23, 0.85, 0.23
), employer = c(1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L), F = c(0L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), G = c(1.94, 1.19, 1.16,
1.16, 1.13, 1.13, 1.13, 1.13, 1.12, 1.12), H = c(0.14, 0.24,
0.28, 0.28, 0.21, 0.12, 0.17, 0.07, 0.14, 0.12), dollar.wage_1 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_2 = c(1.93,
1.18, 3.15, 3.15, 1.12, 1.12, 2.12, 1.12, 1.11, 1.11), dollar.wage_3 = c(1.95,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.13, 1.13), dollar.wage_4 = c(1.94,
1.18, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_5 = c(1.94,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_6 = c(1.94,
1.18, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_7 = c(1.94,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_8 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_9 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_10 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12)), row.names = c(NA,
-10L), class = "data.frame")
我的模特
library(brms)
brm.fit <-brm(dollar.wage_1 ~ A + B + C + employer + F + G + H,
data=sampleDT, iter = 4000, family = gaussian())
brm.fit.distr <-brm(bf(dollar.wage_1 ~ A + B + C + employer + F + G + H,
sigma ~ A + B + C + employer + F + G + H),
data=sampleDT, iter = 4000, family = gaussian())
我对 brm.fit
的方法和对 brm.fit.distr
的尝试
sampleDT$sd_brm_fit<-summary(brm.fit)$spec_pars[1] //this works
sampleDT$sd_brm_fit_distr<-summary(brm.fit.distr)$spec_pars[1] //this does not work
在此先感谢您的帮助。
正如贝叶斯模型所预期的那样,有不同的方式来看待不确定性的程度。所以,首先,我们不再有一个参数sigma
;相反,
中有几个标准偏差参数
summary(brm.fit.distr)$fixed
尤其是
exp(summary(brm.fit.distr)$fixed[, 1])[grep("sigma", rownames(summary(brm.fit.distr)$fixed))]
# sigma_Intercept sigma_A sigma_B sigma_C sigma_employer
# 1.17043390 0.99913160 1.01382623 0.28655150 1.06713923
# sigma_F sigma_G sigma_H
# 0.50428952 0.87669186 0.01203015
我使用 exp
来生成数字 positive。
现在我们可以看看作为不确定性的综合衡量标准
predict(brm.fit.distr)[, 2]
请注意,这些是随机的 (!)在某些情况下,这些数字非常大
predict(brm.fit.distr)[, 2]
# [1] 34.620936 4.456770 2.837869 1.727396 107.116980 2.238100 2.350523 3.037880
# [9] 6.266055 2.517457
但我们有,例如,
sampleDT[5, 1:5]
# id N A B C
# 5 5 10 212 170 0.8
因此 A
和 B
的值非常大。同样你可以看看
predict(brm.fit)[, 2]
# [1] 5.203937 4.846928 4.960600 4.827138 4.937323 4.625976 5.122794 4.767257 4.862458 4.219394
也是随机的。
鉴于下面的示例数据 sampleDT
和 brms
模型 brm.fit
和 brm.fit.distr
,我想:
estimate, extract and add to the data frame the values of the standard deviations for each observation from the distributional model
brm.fit.distr
.
我可以使用 brm.fit
执行此操作,但是当我使用 brm.fit.distr
时我的方法失败了。
示例数据
sampleDT<-structure(list(id = 1:10, N = c(10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L), A = c(62L, 96L, 17L, 41L, 212L, 143L, 143L,
143L, 73L, 73L), B = c(3L, 1L, 0L, 2L, 170L, 21L, 0L, 33L, 62L,
17L), C = c(0.05, 0.01, 0, 0.05, 0.8, 0.15, 0, 0.23, 0.85, 0.23
), employer = c(1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L), F = c(0L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), G = c(1.94, 1.19, 1.16,
1.16, 1.13, 1.13, 1.13, 1.13, 1.12, 1.12), H = c(0.14, 0.24,
0.28, 0.28, 0.21, 0.12, 0.17, 0.07, 0.14, 0.12), dollar.wage_1 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_2 = c(1.93,
1.18, 3.15, 3.15, 1.12, 1.12, 2.12, 1.12, 1.11, 1.11), dollar.wage_3 = c(1.95,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.13, 1.13), dollar.wage_4 = c(1.94,
1.18, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_5 = c(1.94,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_6 = c(1.94,
1.18, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_7 = c(1.94,
1.19, 3.16, 3.16, 1.14, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_8 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_9 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12), dollar.wage_10 = c(1.94,
1.19, 3.16, 3.16, 1.13, 1.13, 2.13, 1.13, 1.12, 1.12)), row.names = c(NA,
-10L), class = "data.frame")
我的模特
library(brms)
brm.fit <-brm(dollar.wage_1 ~ A + B + C + employer + F + G + H,
data=sampleDT, iter = 4000, family = gaussian())
brm.fit.distr <-brm(bf(dollar.wage_1 ~ A + B + C + employer + F + G + H,
sigma ~ A + B + C + employer + F + G + H),
data=sampleDT, iter = 4000, family = gaussian())
我对 brm.fit
的方法和对 brm.fit.distr
sampleDT$sd_brm_fit<-summary(brm.fit)$spec_pars[1] //this works
sampleDT$sd_brm_fit_distr<-summary(brm.fit.distr)$spec_pars[1] //this does not work
在此先感谢您的帮助。
正如贝叶斯模型所预期的那样,有不同的方式来看待不确定性的程度。所以,首先,我们不再有一个参数sigma
;相反,
summary(brm.fit.distr)$fixed
尤其是
exp(summary(brm.fit.distr)$fixed[, 1])[grep("sigma", rownames(summary(brm.fit.distr)$fixed))]
# sigma_Intercept sigma_A sigma_B sigma_C sigma_employer
# 1.17043390 0.99913160 1.01382623 0.28655150 1.06713923
# sigma_F sigma_G sigma_H
# 0.50428952 0.87669186 0.01203015
我使用 exp
来生成数字 positive。
现在我们可以看看作为不确定性的综合衡量标准
predict(brm.fit.distr)[, 2]
请注意,这些是随机的 (!)在某些情况下,这些数字非常大
predict(brm.fit.distr)[, 2]
# [1] 34.620936 4.456770 2.837869 1.727396 107.116980 2.238100 2.350523 3.037880
# [9] 6.266055 2.517457
但我们有,例如,
sampleDT[5, 1:5]
# id N A B C
# 5 5 10 212 170 0.8
因此 A
和 B
的值非常大。同样你可以看看
predict(brm.fit)[, 2]
# [1] 5.203937 4.846928 4.960600 4.827138 4.937323 4.625976 5.122794 4.767257 4.862458 4.219394
也是随机的。