2012-02-25 105 views
20

我在R中,使用lme4結果以適應混合模型如何繪製的混合模型的

lmer(value~status+(1|experiment))) 

其中值是連續的,狀態(N/d/R)和實驗的因素,我也得到

Linear mixed model fit by REML 
Formula: value ~ status + (1 | experiment) 
    AIC BIC logLik deviance REMLdev 
29.1 46.98 -9.548 5.911 19.1 
Random effects: 
Groups  Name  Variance Std.Dev. 
experiment (Intercept) 0.065526 0.25598 
Residual    0.053029 0.23028 
Number of obs: 264, groups: experiment, 10 

Fixed effects: 
      Estimate Std. Error t value 
(Intercept) 2.78004 0.08448 32.91 
statusD  0.20493 0.03389 6.05 
statusR  0.88690 0.03583 24.76 

Correlation of Fixed Effects: 
     (Intr) statsD 
statusD -0.204  
statusR -0.193 0.476 

我想以圖形方式表示固定效果評估。然而,這些對象似乎沒有繪圖功能。有什麼方法可以用圖形描述固定效果嗎?

+0

見'coefplot'或'coefplot2 'CRAN上的軟件包。並且使用'data ='參數來構建您的模型擬合過程... – 2012-02-25 19:43:48

+1

不要認爲coefplot適用於混合模型。 – ECII 2012-02-25 19:49:23

+0

對不起,我的意思是'arm'軟件包中的'coefplot'函數(它有) – 2012-02-25 21:42:29

回答

9

以下是一些建議。

library(ggplot2) 
library(lme4) 
library(multcomp) 
# Creating datasets to get same results as question 
dataset <- expand.grid(experiment = factor(seq_len(10)), 
         status = factor(c("N", "D", "R"), 
         levels = c("N", "D", "R")), 
         reps = seq_len(10)) 
dataset$value <- rnorm(nrow(dataset), sd = 0.23) + 
        with(dataset, rnorm(length(levels(experiment)), 
         sd = 0.256)[experiment] + 
        ifelse(status == "D", 0.205, 
          ifelse(status == "R", 0.887, 0))) + 
        2.78 

# Fitting model 
model <- lmer(value~status+(1|experiment), data = dataset) 

# First possibility 
tmp <- as.data.frame(confint(glht(model, mcp(status = "Tukey")))$confint) 
tmp$Comparison <- rownames(tmp) 
ggplot(tmp, aes(x = Comparison, y = Estimate, ymin = lwr, ymax = upr)) + 
    geom_errorbar() + geom_point() 

# Second possibility 
tmp <- as.data.frame(confint(glht(model))$confint) 
tmp$Comparison <- rownames(tmp) 
ggplot(tmp, aes(x = Comparison, y = Estimate, ymin = lwr, ymax = upr)) + 
    geom_errorbar() + geom_point() 

# Third possibility 
model <- lmer(value ~ 0 + status + (1|experiment), data = dataset) 
tmp <- as.data.frame(confint(glht(model))$confint) 
tmp$Comparison <- rownames(tmp) 
ggplot(tmp, aes(x = Comparison, y = Estimate, ymin = lwr, ymax = upr)) + 
    geom_errorbar() + geom_point() 
+0

你能解釋在你的代碼中使用'glht'嗎?我讀到它是測試[一般線性假設](https://www.rdocumentation.org/packages/multcomp/versions/1.4-6/topics/glht)的函數,在這裏感覺有點不必要... 我也只是用一個更復雜的數據集/模型組合來測試它,它帶有多個固定效果,它不再給我'比較'的名字......有沒有辦法讓你的代碼更一般? – 2017-08-06 12:42:38

19

使用coefplot2(於r-鍛造):

從@Thierry盜仿真代碼:

set.seed(101) 
dataset <- expand.grid(experiment = factor(seq_len(10)), 
    status = factor(c("N", "D", "R"), levels = c("N", "D", "R")), 
    reps = seq_len(10)) 
X <- model.matrix(~status,dataset) 
dataset <- transform(dataset, 
    value=rnorm(nrow(dataset), sd = 0.23) + ## residual 
    rnorm(length(levels(experiment)), sd = 0.256)[experiment] + ## block effects 
    X %*% c(2.78,0.205,0.887)) ## fixed effects 

擬合模型:

library(lme4) 
model <- lmer(value~status+(1|experiment), data = dataset) 

簡介:

install.packages("coefplot2",repos="http://r-forge.r-project.org") 
library(coefplot2) 
coefplot2(model) 

編輯:與R-Forge的構建

我經常被遇到的問題。如果R-Forge的構建不工作這回退應該工作:

install.packages("coefplot2", 
    repos="http://www.math.mcmaster.ca/bolker/R", 
    type="source") 

注意,必須已經安裝了coda依賴。

+0

感謝您對本書的貢獻。我看到你模擬一個數據集,建立一個模型並使用coefplot2。但是我似乎無法在CRAN中找到coefplot2。這個包是否有另一個存儲庫? – ECII 2012-02-25 23:49:36

+0

是的 - 看看我上面的註釋,以及上面代碼中引用r-forge(我今天就是骨頭)的(編輯)'install.packages()'命令。它在r-forge上... – 2012-02-25 23:59:59

+0

我可以看到coefplot 2軟件包的當前狀態是:「無法構建」,無法將其安裝在R 2.15.2上。有進一步的發展被拋棄了嗎併爲此R vers。它可用嗎? – 2012-11-12 12:58:00

12

我喜歡係數置信區間的地塊,但它可能是考慮一些額外的地塊,瞭解固定效應有用..

從@Thierry盜模擬代碼:

library(ggplot2) 
library(lme4) 
library(multcomp) 
dataset <- expand.grid(experiment = factor(seq_len(10)), status = factor(c("N", "D", "R"), levels = c("N", "D", "R")), reps = seq_len(10)) 
dataset$value <- rnorm(nrow(dataset), sd = 0.23) + with(dataset, rnorm(length(levels(experiment)), sd = 0.256)[experiment] + ifelse(status == "D", 0.205, ifelse(status == "R", 0.887, 0))) + 2.78 
model <- lmer(value~status+(1|experiment), data = dataset) 

得到一個看數據的結構...看起來平衡..

library(plotrix); sizetree(dataset[,c(1,2)]) 

enter image description here

跟蹤固定效應之間的相關性可能很有趣,特別是如果您適合不同的相關結構。有一個在下面的鏈接提供了一些很酷的代碼...

http://hlplab.wordpress.com/2012/03/20/correlation-plot-matrices-using-the-ellipse-library/

my.plotcorr(
matrix(c(1,  .891, .891, 
     .891, 1,  .891, 
     .891, .891, 1), nrow=3) 
) 

very basic implementation of function

最後似乎在有關跨10個實驗的變化看以及跨越「的地位變化「在實驗中。當我在不平衡的數據上打破它時,我仍在爲此編寫代碼,但是這個想法是......

My2Boxes(m=4,f1=dataset$experiment,f2=dataset$status,x=dataset$value,color=c("red","yellow","green")) 

enter image description here

最後,已經從一點點我脫脂提到Piniero貝茨(2000)一書非常贊成格..所以,你可能會給一個鏡頭。也許像繪製的原始數據...

lattice::xyplot(value~status | experiment, groups=experiment, data=dataset, type=c('p','r'), auto.key=F) 

enter image description here

然後繪製擬合值...

lattice::xyplot(fitted(model)~status | experiment, groups=experiment, data=dataset, type=c('p','r'), auto.key=F) 

enter image description here

相關問題