2012-01-13 281 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