使用lattice绘制回归线

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)

我希望能用lattice绘制结果,并通过我的模型预测的回归线进行拟合。为此,我使用了D. Sarkar在“Lattice技巧for the power useR”中概述的方法。请保留HTML标记。
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
       )

到目前为止一切都很好,但我还想为两种处理方式和的数据点指定填充颜色。
所以我写了这段代码,它运行良好,但当绘制回归线时,似乎面板函数无法识别类型...
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中的类型,并在其中对数据进行子集处理,如下所示:

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

但是也没有成功。

有人能帮我将预测值绘制成一条线而不是点吗?

3个回答

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)
       )

3

这可能很简单,但您可以尝试一下:


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

"p" 代表添加点,"l" 代表用虚线连接这些点,"r" 可以通过你的数据拟合出一条线性模型。当使用 type="r" 时,只会绘制回归线而不显示数据点。

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


确实这样会更简单,但我不能采用那种方法,因为它实际上不适合这个模型... - matteo
1
@matteo 我不确定你的意思。使用您提供的示例数据,使用 panel.lmline 得到的拟合线与使用 lm 的输出得到的拟合线相同。如果您需要在其他地方使用模型信息,没有任何阻止您进行拟合;我的观点是您不需要为了绘图本身而需要它。 - joran
确实,Joran,那样做会更简单,但我不能使用那种方法,因为那并没有真正给出模型的回归线...这些回归线只是通过每个面板中的数据“凭眼观察”拟合而成。例如,在我的实际情况中,这些线具有不同的斜率,而在斜率上没有显著的主体影响,但仅对截距有影响...希望这有意义。 - matteo
1
虽然其他模型可能不同,但在这种特定情况下,@joran是正确的,因为该模型具有“temp*subject”交互作用; panel.lmline在每个面板中使用lm拟合线性模型,其拟合值与您的模型完全相同。 - Aaron left Stack Overflow
绝对的,@Aaron,我认为我的例子有误导性,为此道歉。 - matteo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接