我在這裏有點麻煩,請幫助我。 我有這個數據繪製迴歸線與格子
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=1
和trt=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
但沒有成功。
任何人都可以幫助我得到預測值繪製爲一條線,而不是點?
謝謝阿龍,這似乎是做的工作... – matteo 2012-01-13 17:36:09
格點評價工作也很好... – matteo 2012-01-13 17:39:53