ここでちょっと困っています、助けてください。私はこのデータを持っています
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 userR」で概説されています。
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
これまでのところすべて問題ありませんが、2 つの処理とのデータ ポイントに塗りつぶしの色を割り当てたいと考えています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
しかし、それでも成功しません。
ポイントではなく線としてプロットされた予測値を取得するのを手伝ってくれる人はいますか?