Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用格点法绘制回归线_R_Lattice - Fatal编程技术网

R 用格点法绘制回归线

R 用格点法绘制回归线,r,lattice,R,Lattice,我这里有点麻烦,请帮帮我。 我有这些数据 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)) 我还尝试在panel.xyplot中移动类型和分发类型,以及像

我这里有点麻烦,请帮帮我。 我有这些数据

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))
我还尝试在
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

plot仅对原始数据使用
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)
)
xyplot(var~temp|sub,
数据=原始数据,
面板=功能(x,y,…,下标){

填充这可能是
latticetextra
包的作业

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

这可能微不足道,但您可以尝试:

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

p
添加点,
l
用虚线连接点,
r
”通过数据拟合线性模型。
type=“r”
单独绘制回归线而不显示数据点。

确实更简单,但我不能使用这种方法,因为它实际上不适合模型…@matteo我不确定您的意思。使用您提供的示例数据,使用
面板.lmline
得到的拟合线与您使用的拟合线相同来自
lm
的输出。如果你需要其他地方的模型信息,没有任何东西可以阻止你去拟合它;我的观点是,你不需要它用于绘图本身。事实上,joran,这会更简单,但我不能使用这种方法,因为这实际上没有给出模型的回归线……那些回归线s只是“眼睛适合”通过每个面板中的数据。例如,在我的真实案例中,直线具有不同的斜率,而sub对斜率没有显著影响,但仅对截距有影响……希望这是有意义的,尽管其他模型可能不同,@joran对于模型具有
temp*subject
交互作用的特定情况是正确的;
panel.lmline
在每个面板的数据上使用
lm
拟合线性模型,其拟合值将与您的模型完全相同。绝对@Aaron,我认为我的示例具有误导性,对此表示歉意。
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)
)
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
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)
       )
xyplot(... , type=c("p","l","r"))