2012-06-20 44 views
3

我有一個廣義混合效應模型如下面:點陣圖的隨機效應

d <- data.frame(
    g = sample(c("A","B","C","D","E"), 250, replace=TRUE), 
    x = runif(250, max=100), 
    y = sample(c(0,1), 250, replace=TRUE) 
) 

require(lme4) 

fm <- lmer(y ~ x + (1 + x | g), data=d, family=binomial) 

我想繪製使用dotplot截距的但沒有繪製x的隨機斜率成分的隨機效應。我的問題是,我似乎無法弄清楚如何訪問只是攔截組件,而不是隨機斜坡。

例如,我要的是該地塊的左側:

dotplot(ranef(fm, postVar=TRUE)) 

enter image description here

使用dotplot(ranef(fm, postVar=TRUE)$g[,2])不給我我想要的東西,即使我認爲它應該!我看過str(fm),但沒有看到任何能幫助我更接近的東西。

任何幫助和提示將不勝感激!

回答

2

這應該讓你非常接近。我一直有意將lme4對象的支持添加到coefplot,所以也許這是我的催化劑。

theRan <- ranef(fm, postVar=TRUE) 
pv <- attr(theRan$g, "postVar") 
se <- pv[1, 1, ] 
theIntercepts <- theRan$g[, 1, drop=F] 
theFrame <- cbind(theIntercepts, se) 
names(theFrame)[1] <- "Intercept" 
theFrame$Low <- with(theFrame, Intercept - 2 * se) 
theFrame$High <- with(theFrame, Intercept + 2 * se) 
theFrame$Variable <- rownames(theFrame) 
ggplot(theFrame, aes(y=Intercept, x=reorder(Variable, Intercept))) + geom_linerange(aes(ymin=Low, ymax=High), colour="black") + geom_point(, colour="blue") + coord_flip() + labs(y="Intercept", x=NULL) 
+0

謝謝,這是一個很好的答案! – smillig

2

你只需要一點更聰明有關刪除的隨機效應,其他列:

re <- ranef(fm,postVar = TRUE) 
re$g$x <- NULL 
dotplot(re) 

的原因,你的其它方法不起作用的是,被分派的dotplot方法仍預計其輸入到看起來像一個元素列表,就像它會來自ranef。所以你只需要進入並移除那個違規的柱子,但是保持結構的其餘部分不變。

+0

該死......貼在編輯時... – John

2

你沒有看str()的正確的東西。

re <- ranef(fm, postVar = TRUE) 
str(re) 

它向您顯示一個列表,其中第一項包含您想要的數據框。我只是從它中刪除了$ x列。

re[[1]]$x <- NULL 
3

你幾乎沒有在原始代碼:

dotplot(ranef(fm, postVar=TRUE))$g[1] 

一個額外的小費釋放的尺度爲每個情節:

dotplot(ranef(fm, postVar=TRUE), 
     scales = list(x =list(relation = 'free')))