簡體   English   中英

用格子繪制回歸線

[英]plotting regression line in with lattice

我在這里遇到麻煩,請幫幫我。 我有這個數據

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. Sarkar撰寫的“用於power useR的格子技巧”

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

但也沒有成功。

誰能幫我把預測值繪制成一條線而不是點?

這可能是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

在此處輸入圖片說明

我不確定您的第一次嘗試是怎么回事,但是帶有下標的操作不起作用,因為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)
       )

這可能很簡單,但是您可以嘗試:

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

p ”添加點,“ l ”用虛線連接它們,“ r ”通過數據擬合線性模型。 type="r"會僅繪制回歸線而不會顯示數據點。

panel.lmline原始數據使用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)
)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM