Eco06
Eco06

Reputation: 541

lattice xyplot - add a regression line to part of the data

I have a question about xyplot from lattice library in R.

Here is the example of the data:

set.seed(4)
mydata <- data.frame(x.data = rnorm(50),
                 y.data = rnorm(50),
                 type = rep(c("A","B"), 50))
head(mydata)

mod <- lm(x.data ~ y.data*type, data= mydata)
summary(mod)

xyplot(y.data + fitted(mod) ~ x.data, groups= type, data= mydata, auto.key=F)

How would it be possible to add a regression line only to type A data and only in the fitted(mod) part of the plot. Is it possible to have the regression line only from the min to max value?

I hoped that the result could look somethnig like this: enter image description here

Thank you for the help. I really appreciate it.

Upvotes: 0

Views: 432

Answers (1)

user3710546
user3710546

Reputation:

library(lattice)
library(latticeExtra)

set.seed(4)
mydata <- data.frame(x.data = rnorm(50),
                     y.data = rnorm(50),
                     type = rep(c("A","B"), 50))
head(mydata)

mod <- lm(x.data ~ y.data*type, data= mydata)
p1 <- predict(mod, newdata = data.frame(y.data = range(mydata$y.data), type = "A"))

xyplot(y.data + fitted(mod) ~ x.data, groups= type, data= mydata, auto.key=F) +
  layer(panel.lines(range(mydata$x.data), p1, col = "black", lty = 1), rows = 1, columns = 2)

enter image description here

Upvotes: 1

Related Questions