3

ここでちょっと困っています、助けてください。私はこのデータを持っています

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

しかし、それでも成功しません。

ポイントではなく線としてプロットされた予測値を取得するのを手伝ってくれる人はいますか?

4

3 に答える 3

6

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)
       )
于 2012-01-13T17:27:53.383 に答える
3

これは些細なことかもしれませんが、次のことを試してみてください。

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

" p" は点を追加し、" l" はそれらを破線で結び、" " はrデータを通じて線形モデルを当てはめます。type="r"単独では、データ ポイントを表示せずに回帰直線のみをプロットします。

于 2013-01-31T10:25:24.870 に答える
2

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

ここに画像の説明を入力

于 2012-01-13T17:19:02.873 に答える