2013-12-13 48 views
1

我有一個數據集,我正在用lme4擬合混合模型迴歸。繪製適當的混合模型迴歸斜率

dat <- structure(list(dv280 = c(41L, 68L, 0L, 6L, 20L, 30L, 8L, 1L, 
15L, NA, 59L, 5L, 21L, 41L, 11L, 14L, -2L, 20L, 25L, 33L, 32L, 
30L, 68L, 16L, 11L, -1L, 8L, 0L), v0 = c(55L, 90L, 30L, 23L, 
74L, 48L, 25L, 25L, 46L, NA, 60L, 69L, 55L, 41L, 34L, 41L, 53L, 
76L, 72L, 64L, 34L, 37L, 75L, 21L, 26L, 14L, 24L, 19L), treatment = structure(c(2L, 
1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 
2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L), .Label = c("hc", 
"nhc"), class = "factor"), cse = structure(c(2, 2, 6, 6, -4, 
-4, 5, 5, NA, NA, -4, -4, -3, -3, -2, -2, 3, 3, 2, 2, -4, -4, 
-7, -7, 4, 4, 2, 2), .Dim = c(28L, 1L)), pp = structure(c(1L, 
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 
9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L), .Label = c("j.1", 
"j.3", "j.6", "j.11", "j.13", "j.16", "j.17", "j.18", "j.19", 
"j.22", "j.24", "j.30", "j.32", "j.36"), class = "factor")), .Names = c("dv280", 
"v0", "treatment", "cse", "pp"), row.names = c(NA, 28L), class = "data.frame") 
head(dat) 
require(lme4) 
m <- lmer(dv280 ~ 1 + v0:treatment + cse + (0 + v0 | pp), data=dat, REML=TRUE) 
summary(m) 

接着我想繪製所得的配合,因此,使用的示例代碼從http://glmm.wikidot.com/faq我生成的原始值的預測。

newdat <- data.frame(
    v0=dat$v0, 
    treatment=dat$treatment, 
    cse=dat$cse, 
    dv280=0) 
newdat <- newdat[-c(9,10),] 
mm <- model.matrix(terms(m), newdat) 
newdat$dv280 <- mm %*% fixef(m) 
pvar1 <- diag(mm %*% tcrossprod(vcov(m), mm)) 
tvar1 <- pvar1 + VarCorr(m)$pp[1] 
newdat <- data.frame(newdat, plo=newdat$dv280 - 2 * sqrt(pvar1), 
        phi=newdat$dv280 + 2 * sqrt(pvar1), 
        tlo=newdat$dv280 - 2 * sqrt(tvar1), 
        thi=newdat$dv280 + 2 * sqrt(tvar1)) 

這是很容易與ggplot積:

p <- ggplot(data=newdat, mapping=aes(x=v0, y=dv280, colour=treatment)) + 
    geom_point() + 
    geom_smooth(method='lm', se=TRUE) + 
    scale_colour_discrete(guide=guide_legend(title.position='left', title.hjust=1)) 
p + .mytheme + coord_cartesian(xlim=c(-20,100)) +  
    geom_hline(yintercept=0, colour='gray35', linetype='dashed') + 
    geom_vline(xintercept=0, colour='gray35', linetype='dashed') 

但作爲圖像顯示,迴歸線由ggplot繪製的截距比迴歸估計截距關閉;至少這是ncc線的情況,該線清楚地變爲負截距,而共同估計的截距是0.54。

Plot

我的錯誤是容易的(我相信):使用不同的配合比的迴歸表示鑑於geom_smooth每個處理是獨立進行數據的同時,在不安裝的情況下面值geom_smooth結果模型。但是我不知道如何正確繪製線條。

+0

您忘記了添加示例代碼的屬性。 – Roland

+0

更新了問題。它是http://glmm.wikidot.com/faq。感謝您指出這一遺漏。 –

+0

[如何繪製混合模型的結果](http://stackoverflow.com/questions/9447329/how-to-plot-the-results-of-a-mixed-model) – ROLO

回答

1

您需要考慮cse的影響。如果我在newdat設置cse=0,線路經過攔截:

p <- ggplot(data=newdat, mapping=aes(x=v0, y=dv280, colour=treatment)) + 
    geom_point() + 
    geom_smooth(method='lm', se=TRUE, fullrange=TRUE) + 
    scale_colour_discrete(guide=guide_legend(title.position='left', title.hjust=1)) 
p + theme_bw() + coord_cartesian(xlim=c(-1,1), ylim=c(0.4,0.65)) +  
    geom_hline(yintercept=0, colour='gray35', linetype='dashed') + 
    geom_vline(xintercept=0, colour='gray35', linetype='dashed') 

enter image description here

需要注意的是,通常不建議在模型中包含的相互作用沒有相應的主效應。

編輯:

根據您的意見你可能要像這樣(與你原來的newdat):

p <- ggplot(data=newdat, mapping=aes(x=v0, y=dv280, ymin=tlo, ymax=thi, colour=treatment, fill=treatment)) + 
    geom_ribbon(alpha=0.2, aes(colour=NULL)) + 
    geom_point() + 
    geom_line() + 
    scale_colour_discrete(guide=guide_legend(title.position='left', title.hjust=1)) 
p + theme_bw() + 
    geom_hline(yintercept=0, colour='gray35', linetype='dashed') + 
    geom_vline(xintercept=0, colour='gray35', linetype='dashed') 

enter image description here

然而,這個情節似乎並不很有用的(特別是點之間的線),因爲它只描繪了dv280-v0-平面上的投影,並且您甚至無法以這種方式看到線性關係。

+0

感謝您的解決!但我不明白,爲什麼這是解決方案?現在我將cse(我們研究中的一個混淆器)設置爲零。你確實打算將它作爲一個實際的解決方案來正確地獲得線路(並因此適合兩個'newdat')? –

+0

這不是作爲「解決方案」,而是作爲解釋。你不能指望你的情節中的lm'合適的線條通過截取。 – Roland

+0

感覺很密集,但我不明白爲什麼當線條是基於迴歸導致特定的截距時,我不能指望線條通過截距。包含cse作爲協變量的迴歸;所以它的影響已經是迴歸方程的一部分。 –

相關問題