2012-01-13 4 views
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.サーカーによる「パワーユーザーのためのラティス・トリック」を概説

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にデータをサブセット化しようとしまし。

ポイントではなく、予測値を線としてプロットするのに役立つ人はいますか?

答えて

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 

enter image description here

xとyはそうサブセット化、SUBJのためのデータのサブセットですので、私はあなたの最初の試みで何が起こっているのかわからないんだけど、添え字1が動作していませんそれらは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) 
     ) 
+0

ありがとうアーロン、それは仕事をしているようです... – matteo

+0

格子apprachもうまく動作します... – matteo

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

enter image description here

+0

これは実際には簡単ですが、実際にはモデルに適合しないので、私はそのアプローチを使用できませんでした。 – matteo

+1

@matteo私はあなたが何を意味するか分かりません。あなたが提供するサンプルデータを使用すると、 'panel.lmline'を使って得られる適切な行は' lm'の出力を使って得られる行と同じです。とにかくモデル情報が必要な場合は、とにかくフィッティングを止めることはありません。私の要点は、プロット自体には必要ないということです。 – joran

+0

実際、ジョランは簡単ですが、モデルの回帰直線を実際に与えないため、このアプローチを使用することはできません。これらの回帰直線は、各パネルのデータをちょうど「目に合わせる」ものです。例えば、私の実際のケースでは、線は異なる傾きを持っていますが、傾きにはsubjの大きな影響はありませんが、切片に対してのみです... – matteo

2

これは些細なことかもしれないが、あなたが試すことがあります。

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

pは」「lは」破線でそれらを接続し、「rは」あなたのデータによる線形モデルをフィットし、ポイントを追加します。 type="r"だけでは、データポイントを表示せずに回帰直線のみをプロットします。

+0

ニースとシンプル。 – Ben

関連する問題