私はここで少し問題を抱えています。助けてください。 私は回帰直線を格子でプロットする
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
)
これまでのところ、すべてのものは大丈夫ですが、私はまた、データに、塗りつぶしの色を割り当てます2つの処置のポイント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
内タイプを配布するだけでなく、どちらかこの
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
それとなしの成功と同じようにpanel.xyplot
にデータをサブセット化しようとしまし。
ポイントではなく、予測値を線としてプロットするのに役立つ人はいますか?
ありがとうアーロン、それは仕事をしているようです... – matteo
格子apprachもうまく動作します... – matteo