从R中的lmer模型中提取贝叶斯p值

问题描述 投票:1回答:1

我试图从lmer模型I中提取贝叶斯p值(即,如果点估计为负,或者点估计为正,估计的比例<0),则估计的比例> 0)取得了。我理解“p值”本身就是频率论者,但我要求贝叶斯p值来安抚评论家(similar to this user)。

出于可重复性的目的,我使用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值的正确格式是什么?

有一个与original package/code一起发布的例子,但我的模型没有设置为他们的模型。

我不需要使用这个包,并且很乐意从我的1000次模拟中计算它。在这种情况下,我需要知道如何计算有多少估计低于/高于零。该数字/ 1000(估计总数)将是贝叶斯p值。

r bayesian lme4 p-value
1个回答
1
投票

要提取贝叶斯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

© www.soinside.com 2019 - 2024. All rights reserved.