从R

时间:2019-04-09 21:00:02

标签: r bayesian lme4 p-value

我正在尝试提取贝叶斯p值(即,如果点估计为负,或者如果点估计为正,则大于0的估计比例)我从lmer模型中得出的估计值<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值。

1 个答案:

答案 0 :(得分: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)时所看到的结果。 Timeweight有影响。