[英]Extracting the Bayesian p-value from lmer model in R
我正在尝试从lmer
模型中提取贝叶斯 p 值(即,如果点估计为负,则估计的比例 > 0,或者在点估计为正的情况下,估计的比例 < 0) )取得了。 我知道“p 值”本质上是常客,但我需要贝叶斯 p 值来安抚审阅者( 类似于此用户)。
出于可重复性的目的,我使用 R 中的数据集来说明我的问题。 数据集:
library(datasets)
data(ChickWeight) #importing data from base R
summary(ChickWeight)
weight Time Chick Diet
Min. : 35.0 Min. : 0.00 13 : 12 1:220
1st Qu.: 63.0 1st Qu.: 4.00 9 : 12 2:120
Median :103.0 Median :10.00 20 : 12 3:120
Mean :121.8 Mean :10.72 10 : 12 4:118
3rd Qu.:163.8 3rd Qu.:16.00 17 : 12
Max. :373.0 Max. :21.00 19 : 12
(Other):506
我的真实数据既有连续的也有离散的预测变量以及对个人身份的随机影响。
创建lmer
模型:
install.packages("lme4", dependencies=TRUE)
library(lme4)
m1<-lmer(weight ~ Time + Diet+ (1|Chick), data=ChickWeight)
summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula: weight ~ Time + Diet + (1 | Chick)
Data: ChickWeight
REML criterion at convergence: 5584
Scaled residuals:
Min 1Q Median 3Q Max
-3.0591 -0.5779 -0.1182 0.4962 3.4515
Random effects:
Groups Name Variance Std.Dev.
Chick (Intercept) 525.4 22.92
Residual 799.4 28.27
Number of obs: 578, groups: Chick, 50
Fixed effects:
Estimate Std. Error t value
(Intercept) 11.2438 5.7887 1.942
Time 8.7172 0.1755 49.684
Diet2 16.2100 9.4643 1.713
Diet3 36.5433 9.4643 3.861
Diet4 30.0129 9.4708 3.169
Correlation of Fixed Effects:
(Intr) Time Diet2 Diet3
Time -0.307
Diet2 -0.550 -0.015
Diet3 -0.550 -0.015 0.339
Diet4 -0.550 -0.011 0.339 0.339
与ChickWeight
数据集不同,我的真实数据集具有正负两种估计。
然后我想从我的模型m1
提取 95% 的可信区间:
install.packages(c("MCMCglmm", "arm"), dependencies=TRUE)
library(MCMCglmm)
library(arm)
sm1<-sim(m1,1000)
smfixef=sm1@fixef #fixed effects
smranef=sm1@ranef #random effects
smfixef=as.mcmc(smfixef)
posterior.mode(smfixef) #extract estimates for fixed effects
(Intercept) Time Diet2 Diet3 Diet4
10.489143 8.800899 16.761983 31.684341 28.037318
HPDinterval(smfixef) ##extract 95% credible intervals for fixed effects
lower upper
(Intercept) -0.05392775 21.960966
Time 8.38244319 9.064171
Diet2 -0.46587564 34.061686
Diet3 17.90445947 53.817409
Diet4 11.17259787 48.467258
attr(,"Probability")
[1] 0.95
现在我想得到贝叶斯 p 值:
install.packages("conting", dependencies=TRUE)
library(conting)
bayespval(object=sm1, n.burnin = 0, thin = 1, statistic = "X2")
#this last line is the line I am having trouble with
Error: $ operator not defined for this S4 class
通过我如何设置我的模型m1
,为每个估计提取贝叶斯 p 值的正确格式是什么?
有一个使用原始包/代码发布的示例,但我的模型没有按照他们的模型进行设置。
我不需要使用这个包,并且很乐意从我的 1000 次模拟中计算它。 在这种情况下,我需要知道如何计算有多少估计值低于/高于零。 该数字 / 1000(估计的总数)将是贝叶斯 p 值。
要提取贝叶斯 p 值(即,如果点估计为负,则估计的比例 > 0,或者在点估计为正的情况下,估计的比例 < 0 )您可以提取每个模拟的点估计然后除以模拟次数。
要使用ChickWeight
数据集和上述模型执行此操作,您需要:
library(datasets)
data(ChickWeight)
m1<-lmer(weight ~ Time + Diet+ (1|Chick), data=ChickWeight)
sm1<-sim(m1,1000)
smfixef=sm1@fixef
smfixef=as.mcmc(smfixef) #this has the 1000 simulations in it for the fixed effects
as.mcmc(smfixef)
Markov Chain Monte Carlo (MCMC) output:
Start = 1
End = 1000
Thinning interval = 1
(Intercept) Time Diet2 Diet3 Diet4
[1,] 17.52609243 8.381517 7.47169881 46.442343 19.7164997 #simulation 1
[2,] 16.52854430 8.859378 8.83279931 29.017547 25.4610474 #simulation 2
[3,] 4.00702870 8.830302 29.68309621 47.459395 35.1939344 #simulation 3
[4,] 16.44162722 8.599929 15.87393285 31.946265 33.7513144 #simulation 4
[5,] 21.07173579 8.596701 1.81909415 28.934133 19.0499998 #simulation 5
etc.
然后对于每一列,您可以编码哪些模拟高于或低于零:
p_Time=if_else(smfixef[,2]>0, 1,0) #Time variable (i.e., 2nd column)
由于Time
变量的点估计值为正,因此您需要计算该变量的估计值低于零的次数:
sum_p_Time=sum(p_Time<1)
> sum_p_Time
0
在这种情况下,它表示所有估计值都高于零,因此贝叶斯 p 值 < 0.001。 这支持了我们仅查看点估计和 95% 可信区间(即Time
估计为 8.80 和 95% 可信区间为 (8.38, 9.06))时所看到的情况。在这两种情况下,我们都看到强烈支持Time
有影响在weight
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.