English 中文(简体)
plot plot积回归线
原标题:plotting regression line in with lattice
  • 时间:2012-01-13 16:36:08
  •  标签:
  • r
  • lattice

I m having a bit of a trouble here, please help me. I have this data

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

and this model that fits them

lm  <- lm(var ~ temp * subj, data = mydata)

我想用lattice来规划结果,并用我的模型来预测回归线。 为了做到这一点,Im采用这种办法,概述了D. Sarkar的“电力使用压力三角”。

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=1trt=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

I ve also tried to move type and distribute type within panel.xyplot, as well as subsetting the data in it panel.xyplot like this

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

但没有成功。

任何人都能够帮助我获得被划为一线而不是点的预计价值?

最佳回答

这可能是<代码>latice的工作。 附加

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

“entergraph

I m not sure what s going on in your first attempt, but the one with the subscripts isn t working because x and y are a subset of the data for subj, so subsetting them using a vector based on combined won t work the way you think it will. Try this instead.

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)
       )
问题回答

This may be trivial, but you may try:

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

p”增加点,“l<>/code>将其与条块连接起来,“r通过您的数据适合其行距模型。 仅凭“代码”类型=“r”,就只能划定回归线,而不显示数据点。

简单使用<代码>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)
)

“enterography





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签