2017-03-26 31 views
10

我正在使用lme4軟件包運行glmer logit模型。我對各種兩種和三種互動效果及其解釋感興趣。爲了簡化,我只關心固定效應係數。glmer logit - 交互對概率尺度的影響(用`predict`複製'效果')

我設法提出了一個代碼來計算和繪製這些影響的對數尺度,但我很難將它們轉換爲預測的概率尺度。最終我想複製effects包的輸出。

該示例依賴於UCLA's data on cancer patients

library(lme4) 
library(ggplot2) 
library(plyr) 

getmode <- function(v) { 
    uniqv <- unique(v) 
    uniqv[which.max(tabulate(match(v, uniqv)))] 
} 

facmin <- function(n) { 
    min(as.numeric(levels(n))) 
} 

facmax <- function(x) { 
    max(as.numeric(levels(x))) 
} 

hdp <- read.csv("http://www.ats.ucla.edu/stat/data/hdp.csv") 

head(hdp) 
hdp <- hdp[complete.cases(hdp),] 

hdp <- within(hdp, { 
    Married <- factor(Married, levels = 0:1, labels = c("no", "yes")) 
    DID <- factor(DID) 
    HID <- factor(HID) 
    CancerStage <- revalue(hdp$CancerStage, c("I"="1", "II"="2", "III"="3", "IV"="4")) 
}) 

直到這裏,它是所有的數據管理,功能和我需要的軟件包。

m <- glmer(remission ~ CancerStage*LengthofStay + Experience + 
      (1 | DID), data = hdp, family = binomial(link="logit")) 
summary(m) 

這是模型。它需要一分鐘,並與下面的警告收斂:

Warning message: 
In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : 
    Model failed to converge with max|grad| = 0.0417259 (tol = 0.001, component 1) 

即使我不能肯定我是否應該擔心的警告,我用的是估計繪製感興趣的相互作用的平均邊際效應。首先,我準備將數據集輸入到predict函數中,然後使用固定效果參數計算邊際效應以及置信區間。

newdat <- expand.grid(
    remission = getmode(hdp$remission), 
    CancerStage = as.factor(seq(facmin(hdp$CancerStage), facmax(hdp$CancerStage),1)), 
    LengthofStay = seq(min(hdp$LengthofStay, na.rm=T),max(hdp$LengthofStay, na.rm=T),1), 
    Experience = mean(hdp$Experience, na.rm=T)) 

mm <- model.matrix(terms(m), newdat) 
newdat$remission <- predict(m, newdat, re.form = NA) 
pvar1 <- diag(mm %*% tcrossprod(vcov(m), mm)) 
cmult <- 1.96 

## lower and upper CI 
newdat <- data.frame(
    newdat, plo = newdat$remission - cmult*sqrt(pvar1), 
    phi = newdat$remission + cmult*sqrt(pvar1)) 

我相當有信心這些是對logit規模的正確估計,但也許我錯了。總之,這是劇情:

plot_remission <- ggplot(newdat, aes(LengthofStay, 
    fill=factor(CancerStage), color=factor(CancerStage))) + 
    geom_ribbon(aes(ymin = plo, ymax = phi), colour=NA, alpha=0.2) + 
    geom_line(aes(y = remission), size=1.2) + 
    xlab("Length of Stay") + xlim(c(2, 10)) + 
    ylab("Probability of Remission") + ylim(c(0.0, 0.5)) + 
    labs(colour="Cancer Stage", fill="Cancer Stage") + 
    theme_minimal() 

plot_remission 

我覺得現在OY規模Logit變換的規模衡量,而是它的意義,我想將它轉化爲預測概率。基於wikipedia,像exp(value)/(exp(value)+1)應該做的伎倆來達到預測的概率。雖然我可以做newdat$remission <- exp(newdat$remission)/(exp(newdat$remission)+1)我不知道我應該如何做到這一點的置信區間?

最終我想得到相同的情節effects包生成。那就是:

eff.m <- effect("CancerStage*LengthofStay", m, KR=T) 

eff.m <- as.data.frame(eff.m) 

plot_remission2 <- ggplot(eff.m, aes(LengthofStay, 
    fill=factor(CancerStage), color=factor(CancerStage))) + 
    geom_ribbon(aes(ymin = lower, ymax = upper), colour=NA, alpha=0.2) + 
    geom_line(aes(y = fit), size=1.2) + 
    xlab("Length of Stay") + xlim(c(2, 10)) + 
    ylab("Probability of Remission") + ylim(c(0.0, 0.5)) + 
    labs(colour="Cancer Stage", fill="Cancer Stage") + 
    theme_minimal() 

plot_remission2 

即使我可以只使用effects封裝,遺憾的是不帶很多,我不得不爲我自己的工作運行模式的編譯:

Error in model.matrix(mod2) %*% mod2$coefficients : 
    non-conformable arguments 
In addition: Warning message: 
In vcov.merMod(mod) : 
    variance-covariance matrix computed from finite-difference Hessian is 
not positive definite or contains NA values: falling back to var-cov estimated from RX 

解決可能會需要調整估計程序,目前我想避免這一程序。再加上,我也很好奇effects究竟在這裏做了什麼。 我將不勝感激任何關於如何調整我的初始語法以獲得預測概率的建議!

+1

我認爲如果你做這樣的事情,你的圖會更容易閱讀:'ggplot(n (aes(ymin = plo,ymax = phi),color = NA,alpha = 0.2)+ geom_line(aes(aes ylab(「緩解的概率」)+ 實驗室(color =「Cancer Stage」,fill =「Cancer Stage」)+ theme_minimal(「y = remission」,size = 1.2)+ xlab(「Stay of Length」)+ )' – eipi10

+0

你絕對應該擔心收斂警告。 –

+0

我真的不明白爲什麼這是一個不可能的問題來回答......我所要求的東西有些不清楚嗎? – eborbath

回答

4

要獲得與您的問題中提供的effect函數類似的結果,您只需將預測值和置信區間的邊界從logit縮放比例轉換爲原始縮放比例的邊界即可: exp(x)/(1+exp(x))

這種轉變可以在基礎R來完成與plogis功能:

> a <- 1:5 
> plogis(a) 
[1] 0.7310586 0.8807971 0.9525741 0.9820138 0.9933071 
> exp(a)/(1+exp(a)) 
[1] 0.7310586 0.8807971 0.9525741 0.9820138 0.9933071 

因此,使用色帶的置信帶,而不是點線使用建議從@ eipi10(我也覺得這個演示更具可讀性) :

ggplot(newdat, aes(LengthofStay, fill=factor(CancerStage), color=factor(CancerStage))) + 
     geom_ribbon(aes(ymin = plogis(plo), ymax = plogis(phi)), colour=NA, alpha=0.2) + 
     geom_line(aes(y = plogis(remission)), size=1.2) + 
     xlab("Length of Stay") + xlim(c(2, 10)) + 
     ylab("Probability of Remission") + ylim(c(0.0, 0.5)) + 
     labs(colour="Cancer Stage", fill="Cancer Stage") + 
     theme_minimal() 

enter image description here

的結果是相同的(具有effects_3.1-2lme4_1.1-13):

> compare <- merge(newdat, eff.m) 
> compare[, c("remission", "plo", "phi")] <- 
+  sapply(compare[, c("remission", "plo", "phi")], plogis) 
> head(compare) 
    CancerStage LengthofStay remission Experience  plo  phi  fit  se  lower  upper 
1   1   10 0.20657613 17.64129 0.12473504 0.3223392 0.20657613 0.3074726 0.12473625 0.3223368 
2   1   2 0.35920425 17.64129 0.27570456 0.4522040 0.35920425 0.1974744 0.27570598 0.4522022 
3   1   4 0.31636299 17.64129 0.26572506 0.3717650 0.31636299 0.1254513 0.26572595 0.3717639 
4   1   6 0.27642711 17.64129 0.22800277 0.3307300 0.27642711 0.1313108 0.22800360 0.3307290 
5   1   8 0.23976445 17.64129 0.17324422 0.3218821 0.23976445 0.2085896 0.17324530 0.3218805 
6   2   10 0.09957493 17.64129 0.06218598 0.1557113 0.09957493 0.2609519 0.06218653 0.1557101 
> compare$remission-compare$fit 
[1] 8.604228e-16 1.221245e-15 1.165734e-15 1.054712e-15 9.714451e-16 4.718448e-16 1.221245e-15 1.054712e-15 8.326673e-16 
[10] 6.383782e-16 4.163336e-16 7.494005e-16 6.383782e-16 5.689893e-16 4.857226e-16 2.567391e-16 1.075529e-16 1.318390e-16 
[19] 1.665335e-16 2.081668e-16 

信心邊界之間的差別較高,但仍然很小:

> compare$plo-compare$lower 
[1] -1.208997e-06 -1.420235e-06 -8.815678e-07 -8.324261e-07 -1.076016e-06 -5.481007e-07 -1.429258e-06 -8.133438e-07 -5.648821e-07 
[10] -5.806940e-07 -5.364281e-07 -1.004792e-06 -6.314904e-07 -4.007381e-07 -4.847205e-07 -3.474783e-07 -1.398476e-07 -1.679746e-07 
[19] -1.476577e-07 -2.332091e-07 

但是,如果使用正態分佈cmult <- qnorm(0.975)的實際位數,而不是cmult <- 1.96我獲得非常對於這些邊界也有小差異:

> compare$plo-compare$lower 
[1] 5.828671e-16 9.992007e-16 9.992007e-16 9.436896e-16 7.771561e-16 3.053113e-16 9.992007e-16 8.604228e-16 6.938894e-16 
[10] 5.134781e-16 2.289835e-16 4.718448e-16 4.857226e-16 4.440892e-16 3.469447e-16 1.006140e-16 3.382711e-17 6.765422e-17 
[19] 1.214306e-16 1.283695e-16 
+0

謝謝!這有助於很多!不幸的是,雖然這兩個地塊之間還是有一點差距,但是我將它們帶到了相同的比例,以便在曲線中可見(我添加了「xlim」和「ylim」)。您還可以看到與例如比較< - merge(newdat,eff.m) head(compare) compare $ remission-compare $ fit'確實,在這個例子中,差異非常小,但我想知道偏差來自哪裏,所以我可以在我的研究中消除它。 PS:我編輯了這些情節並添加了「plyr」包。感謝您的回答! – eborbath

+0

請參閱編輯的回覆。我無法複製任何重大差異。也許在軟件包版本上有所不同?注意,你還應該在你的代碼中加入'library(effects)',並刪除你的第一個plot的ylim'(這個圖是在logit尺度上的,所以0,0.5的限制超出了圖的範圍) – Gilles

+0

感謝你澄清這個! – eborbath