gpt4 book ai didi

r - 用格子绘制回归线

转载 作者:行者123 更新时间:2023-12-04 10:33:57 25 4
gpt4 key购买 nike

我在这里遇到了一些麻烦,请帮助我。
我有这个数据

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 的“Lattice Tricks for the 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

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

关于r - 用格子绘制回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com