簡體   English   中英

lme4計算協方差的置信區間

[英]lme4 calculate confidence intervals of covariances

請參閱Ben Bolker 16/05/2016的答案,了解相應的解決方案。 OP下面。


我正在使用lme4安裝幾個多級模型。 我想報告隨機效應的方差和協方差,並自動化這個過程。

我知道我能得到的方差與as.data.frame(VarCorr(mymodel))我知道我能得到的置信區間與confint(mymodel) 顯然,我可以合並/綁定兩個表,並通過簡單地將confint()的輸出平方在適當的行和列confint()設置方差周圍的差異,但是如果不是,我還沒有找到一個令人信服的方法來計算協方差。用手。

confint的結果是:

conf <- NULL
a <- c(6.2,-0.4,2.2,1.5,-0.4,-0.5,2.8,-0.9,1.3,3.9)
b <- c(6.8,-0.2,2.5,2.5,0.1,0.2,4.8,-0.7,2.3,5)
conf <- data.frame(a,b,row.names = c("sd_(Intercept)|ID","cor_Time.(Intercept)|ID","sd_Time|ID","sd_(Intercept)|Group","cor_Time.(Intercept)|Group","cor_I(Time^2).(Intercept)|Group","sd_Time|Group","cor_I(Time^2).Time|Group","sd_I(Time^2)|Group","sigma"))
colnames(conf) <- c("2.5%","97.5%")
conf

如何自動執行各種乘法以獲得協方差

cov.time.intercept <- conf[1,2]*conf[1,1]*conf[1,3]

我試過分割標准偏差和相關性,創建“ID”,“時間”,“我(時間^ 2)”和“(攔截)”變量然后匹配兩列,但我沒有得到任何結果。 問題是,每次模型更改時,您可能會有不同數量的方差和協方差,以及不同的三角矩陣。

感謝您的任何幫助,

ķ。

解決了,謝謝你的貢獻。 我會更新最初的帖子。 可以使用此處提供的Snijders&Bosker數據集測試結果

導入

library(foreign)
chap12 <- read.dta(file = "<your path>/ch12.dta")

一個臨時模型:

snijders <- lmer(prox_pup ~ 1 + prox_sel + (1 + occ|teacher), data = chap12)

來源功能:

ExtractVarCovCI <- function(Model) {

v <- NULL
v <- as.data.frame(VarCorr(Model),order = "lower.tri") #Extract variances and covariances

conf <- confint(Model, parm  ="theta_", oldNames = F) #extract CIs

v.conf <- cbind(v,conf) #bind confidence intervals

covs <- as.data.frame(v.conf[!is.na(v[,3]),]) #separate variance from covariance components
vars <- as.data.frame(v.conf[is.na(v[,3]),]) #separate variance from covariance components
vars.sq <- vars[,6:7]^2 #calculate square of variance components
colnames(vars.sq) <- sub("[%]", "% sq.", colnames(vars.sq))

vars2 <- cbind(vars,vars.sq) #bind squares of variance components
covs$`2.5 % sq.` <- c(rep(NA,nrow(covs))) #create empty columns for later
covs$`97.5 % sq.` <- c(rep(NA,nrow(covs))) #create empty columns for later

lcovs <- length(row.names(covs)) #now we re-organise the table so that each covariance is below the variance of its variables
k <- NULL
for (i in seq(1:lcovs)) {
  k <- rbind(k,vars2[vars2$grp %in% covs[i,1] & vars2$var1 %in% covs[i,2],],vars2[vars2$grp %in% covs[i,1] & vars2$var1 %in% covs[i,3],],covs[i,])
}

k2 <- rbind(k,vars2["sigma",]) #bind the level-1 residuals at the end

k2.covrow <- grep("^cor",rownames(k2)) # isolate covariance row position
k2[k2.covrow,8] <- k2[k2.covrow,6]*k2[k2.covrow-1,6]*k2[k2.covrow-2,6] #calculate covariance 2.5%
k2[k2.covrow,9] <- k2[k2.covrow,7]*k2[k2.covrow-1,7]*k2[k2.covrow-2,7] #calculate covariance 97.5%

p <- NULL
p <- k2[,c(4,8:9)] #retain only the estimates and the confidence intervals
rownames(p) <- sub("^sd","var",rownames(p)) #now it's clear that we have proper variances and covariances
rownames(p) <- sub("^cor","cov",rownames(p)) #now it's clear that we have proper variances and covariances
colnames(p) <- c("Estimate", "2.5%", "97.5%")

return(p)
}

運行功能:

ExtractVarCovCI(snijders)

我的輸出是:

                               Estimate         2.5%       97.5%
var_(Intercept)|teacher      0.15617962  0.089020350  0.26130969
var_occ|teacher              0.01205317  0.002467408  0.02779329
cov_occ.(Intercept)|teacher -0.03883458 -0.014820577 -0.05887660
sigma                        0.04979762  0.034631759  0.07263837

現在我們有一個方差 - 協方差表,它使用非標准化的隨機效應及其上限和下限置信區間。 我相信有更好的方法可以做到這一點,但這是一個開始......

ķ。

請注意, lme4摘要中隨機效應的標准偏差不是方差的標准誤差! 這只是方差的平方根!

如果您需要對隨機效應的方差進行置信區間,那么您需要profile()可能性。 ?lme4::profile

你的計算似乎給出了合理的答案,但它沒有意義 (對我而言;我隨時准備好被糾正/開悟......)。 假設cov = corr*var1*var2 假設ci(.)是數量的(下限或上限)置信限。 ci(cov) = ci(corr)*ci(var1)*ci(var2)並不是真的有趣的是你得到了合理的答案;我認為當數量大致不相關時,這很有可能發生...)如果你有每個分量的方差和它們之間的協方差(我不是指隨機效應方差和協方差本身,而是它們的采樣方差/協方差)你可以使用delta方法近似地傳播它們,但是這些很難得到(見這里 )。

據我所知,做到這一點的“正確”方法是在方差 - 協方差量表上進行似然概率計算而不是標准偏差 - 相關量表。 這在以前是不可能的,但它現在(與Github上的開發版本)。

安裝最新版本:

library(remotes) ## for install_github (or library(devtools))
install_github("lme4/lme4")

預賽:

chap12 <- foreign::read.dta(file = "ch12.dta")
library(lme4)
snijders <- lmer(prox_pup ~ 1 + prox_sel + (1 + occ|teacher),
                 data = chap12)

as.data.frame(VarCorr(snijders))
##        grp        var1 var2        vcov      sdcor
## 1  teacher (Intercept) <NA>  0.15617962  0.3951957
## 2  teacher         occ <NA>  0.01205317  0.1097869
## 3  teacher (Intercept)  occ -0.03883458 -0.8950676
## 4 Residual        <NA> <NA>  0.04979762  0.2231538

我們在比較結果時必須要小心,因為我們將很快使用的profile.merMod會自動(並且默默地!)將默認REML的擬合轉換為最大似然擬合(因為基於REML的配置文件可能在統計上有點冒險) ; 然而,它看起來並沒有產生巨大的差異。

s2 <- refitML(snijders)
as.data.frame(VarCorr(s2))
##        grp        var1 var2        vcov      sdcor
## 1  teacher (Intercept) <NA>  0.15426049  0.3927601
## 2  teacher         occ <NA>  0.01202631  0.1096645
## 3  teacher (Intercept)  occ -0.03884427 -0.9018483
## 4 Residual        <NA> <NA>  0.04955549  0.2226106

p.sd <- profile(s2,which="theta_",
              signames=FALSE)
p.vcov <- profile(s2,which="theta_",prof.scale="varcov",
              signames=FALSE)

我們收到一些關於非單調輪廓的警告......

confint(p.vcov)
##                                    2.5 %     97.5 %
## var_(Intercept)|teacher      0.08888931  0.26131067
## cov_occ.(Intercept)|teacher -0.07553263 -0.01589043
## var_occ|teacher              0.00000000  0.02783863
## sigma                        0.03463184  0.07258777

如果我們檢查相關(sd / variance)元素的平方怎么辦?

confint(p.sd)[c(1,3,4),]^2
##                              2.5 %     97.5 %
## sd_(Intercept)|teacher 0.089089363 0.26130970
## sd_occ|teacher         0.002467408 0.02779329
## sigma                  0.034631759 0.07263869

除了occ方差的下界之外,這些匹配得很好; 它們也符合您上面的結果。 然而,協方差結果(我聲稱這是很困難的)給了我(-0.0755,-0.0159),對你來說(-0.0588,-0.0148),大約有20%的差異。 這可能不是什么大問題,取決於你想要做什么。

我們也試試蠻力:

sumfun <- function(x) {
    vv <- as.data.frame(VarCorr(x),order="lower.tri")[,"vcov"]
    ## cheating a bit here, using internal lme4 naming functions ...
    return(setNames(vv,
       c(lme4:::tnames(x,old=FALSE,prefix=c("var","cov")),
         "sigmasq")))
}

cc <- confint(s2,method="boot",nsim=1000,FUN=sumfun,seed=101,
        .progress="txt", PBargs=list(style=3))
## .progress/PBargs just cosmetic ...

##                                    2.5 %      97.5 %
## var_(Intercept)|teacher      0.079429623  0.24053633
## cov_occ.(Intercept)|teacher -0.067063911 -0.01479572
## var_occ|teacher              0.002733402  0.02378310
## sigmasq                      0.031952508  0.06736664

這里的“黃金標准”似乎在我的個人資料結果和結果之間:協方差的下限是-0.067,而-0.0755(個人資料)或-0.0588。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM