2012-01-13 94 views
3

我在這裏有點麻煩,請幫助我。 我有這個數據繪製迴歸線與格子

set.seed(4) 
mydata <- data.frame(var = rnorm(100), 
        temp = rnorm(100), 
        subj = as.factor(rep(c(1:10),5)), 
        trt = rep(c("A","B"), 50)) 

而這種模式適合他們

lm <- lm(var ~ temp * subj, data = mydata) 

我想格子繪製的結果和擬合迴歸線的,預測與我的模型,通過他們。要做到這一點,我用這種方法,概括「對於電力用戶萊迪思技巧」由D.薩卡

temp_rng <- range(mydata$temp, finite = TRUE) 

grid <- expand.grid(temp = do.breaks(temp_rng, 30), 
        subj = unique(mydata$subj), 
        trt = unique(mydata$trt)) 

model <- cbind(grid, var = predict(lm, newdata = grid)) 

orig <- mydata[c("var","temp","subj","trt")] 

combined <- make.groups(original = orig, model = model) 


xyplot(var ~ temp | subj, 
     data = combined, 
     groups = which, 
     type = c("p", "l"), 
     distribute.type = TRUE 
     ) 

到目前爲止一切都很好,但我也想將填充顏色分配給數據兩個處理點分別爲trt=1trt=2

所以我寫這一段代碼,工作正常,但是當它涉及到繪製迴歸線,似乎型不被面板功能的認可......

my.fill <- c("black", "grey") 

plot <- with(combined, 
     xyplot(var ~ temp | subj, 
       data = combined, 
       group = combined$which, 
       type = c("p", "l"), 
       distribute.type = TRUE, 
       panel = function(x, y, ..., subscripts){ 
        fill <- my.fill[combined$trt[subscripts]] 
        panel.xyplot(x, y, pch = 21, fill = my.fill, col = "black") 
        }, 
      key = list(space = "right", 
        text = list(c("trt1", "trt2"), cex = 0.8), 
        points = list(pch = c(21), fill = c("black", "grey")), 
        rep = FALSE) 
        ) 
    ) 
plot 

我以前也嘗試過移動式和內panel.xyplot分佈類型,以及在它panel.xyplot任子集劃分的數據這樣

plot <- with(combined, 
     xyplot(var ~ temp | subj, 
       data = combined, 
       panel = function(x, y, ..., subscripts){ 
        fill <- my.fill[combined$trt[subscripts]] 
        panel.xyplot(x[combined$which=="original"], y[combined$which=="original"], pch = 21, fill = my.fill, col = "black") 
        panel.xyplot(x[combined$which=="model"], y[combined$which=="model"], type = "l", col = "black") 
        }, 
      key = list(space = "right", 
        text = list(c("trt1", "trt2"), cex = 0.8), 
        points = list(pch = c(21), fill = c("black", "grey")), 
        rep = FALSE) 
        ) 
    ) 
plot 

但沒有成功。

任何人都可以幫助我得到預測值繪製爲一條線,而不是點?

回答

6

這可能是爲latticeExtra包工作。

library(latticeExtra) 
p1 <- xyplot(var ~ temp | subj, data=orig, panel=function(..., subscripts) { 
    fill <- my.fill[combined$trt[subscripts]] 
    panel.xyplot(..., pch=21, fill=my.fill, col="black") 
}) 
p2 <- xyplot(var ~ temp | subj, data=model, type="l") 
p1+p2 

enter image description here

我不知道發生了什麼事情在你的第一次嘗試,但一個與標不工作,因爲x和y是SUBJ的數據的一個子集,因此子集他們使用基於combined的矢量將不會按照您認爲的方式工作。試試這個。

xyplot(var ~ temp | subj, groups=which, data = combined, 
     panel = function(x, y, groups, subscripts){ 
     fill <- my.fill[combined$trt[subscripts]] 
     g <- groups[subscripts] 
     panel.points(x[g=="original"], y[g=="original"], pch = 21, 
         fill = my.fill, col = "black") 
     panel.lines(x[g=="model"], y[g=="model"], col = "black") 
     }, 
     key = list(space = "right", 
     text = list(c("trt1", "trt2"), cex = 0.8), 
     points = list(pch = c(21), fill = c("black", "grey")), 
     rep = FALSE) 
     ) 
+0

謝謝阿龍,這似乎是做的工作... – matteo 2012-01-13 17:36:09

+0

格點評價工作也很好... – matteo 2012-01-13 17:39:53

2

可能更容易簡單地使用panel.lmline功能上只是你的原始數據:

xyplot(var ~ temp | subj, 
     data = orig, 
     panel = function(x,y,...,subscripts){ 
      fill <- my.fill[orig$trt[subscripts]] 
      panel.xyplot(x, y, pch = 21, fill = my.fill,col = "black") 
      panel.lmline(x,y,col = "salmon") 
     }, 
     key = list(space = "right", 
        text = list(c("trt1", "trt2"), cex = 0.8), 
        points = list(pch = c(21), fill = c("black", "grey")), 
        rep = FALSE) 
) 

enter image description here

+0

的確很簡單,但我不能使用這種方法,因爲這樣做實際上不適合模型... – matteo 2012-01-13 17:23:16

+1

@matteo我不確定你的意思。使用您提供的示例數據,使用'panel.lmline'獲得的擬合線與使用'lm'的輸出得到的線相同。如果你需要其他地方的模型信息,那麼無論如何都沒有阻止你去適應它。我的觀點是,你不需要它的情節本身。 – joran 2012-01-13 17:27:11

+0

事實上,古蘭經,這會更簡單,但我不能使用這種方法,因爲這實際上並沒有給出模型的迴歸線......這些迴歸線只是通過每個面板中的數據「適合眼睛」。例如,在我的實際情況中,線條具有不同的斜率,而主體對斜率沒有顯着影響,但僅限於截距......希望它有意義 – matteo 2012-01-13 17:31:21

2

這可能是微不足道的,但你可以嘗試:

xyplot(... , type=c("p","l","r")) 

p補充說:」點「l」他們用虛線連接,「r」適合通過你的數據的線性模型。僅顯示type="r"僅繪製迴歸線而不顯示數據點。

+0

好又簡單。 – Ben 2015-04-16 23:19:23