Build observed and set values โ€‹โ€‹from linear regression using xyplot () from the lattice package

I can create simple graphics. I would like to observe and predict values โ€‹โ€‹(from linear regression) on the same chart. I plan to say Yvariable vs Xvariable . There is only 1 predictor and only 1 answer. How can I add a linear regression curve to the same graph?

So, to conclude, you need help with:

  • displaying evidence and predicting how
  • regression chart
+4
source share
2 answers

Here is one of the options for the observed and predicted values โ€‹โ€‹in one graph as points. Itโ€™s easier to get a regression line from the observed points, which I illustrate the second

Some dummy data first

 set.seed(1) x <- runif(50) y <- 2.5 + (3 * x) + rnorm(50, mean = 2.5, sd = 2) dat <- data.frame(x = x, y = y) 

Install our model

 mod <- lm(y ~ x, data = dat) 

Combine the output of the model and observe x in one object for plott

 res <- stack(data.frame(Observed = dat$y, Predicted = fitted(mod))) res <- cbind(res, x = rep(dat$x, 2)) head(res) 

Loading grids and graphics

 require("lattice") xyplot(values ~ x, data = res, group = ind, auto.key = TRUE) 

The resulting plot should look something like this:

enter image description here

To get only the regression line from the observed data, and the regression model is a simple straight line model according to the one I'm showing, then you can get around most of this and just build with

 xyplot(y ~ x, data = dat, type = c("p","r"), col.line = "red") 

(i.e. you donโ€™t even have to fit the model or create new data to plot)

The resulting plot should look like this:

enter image description here

An alternative to the first example, which can be used with everything that gives coefficients for the regression line, is to write your own panel functions - not as scary as it seems

 xyplot(y ~ x, data = dat, col.line = "red", panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(coef = coef(mod), ...) ## using mod from earlier } ) 

This gives the graph from Figure 2 above, but manually.

Assuming you did this with a caret , then

 mod <- train(y ~ x, data = dat, method = "lm", trControl = trainControl(method = "cv")) xyplot(y ~ x, data = dat, col.line = "red", panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(coef = coef(mod$finalModel), ...) ## using mod from caret } ) 

It will produce a graph in the same way as in Figure 2 above.

+12
source

Another option is to use panel.lmlineq from latticeExtra .

 library(latticeExtra) set.seed(0) xsim <- rnorm(50, mean = 3) ysim <- (0 + 2 * xsim) * (1 + rnorm(50, sd = 0.3)) ## basic use as a panel function xyplot(ysim ~ xsim, panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.lmlineq(x, y, adj = c(1,0), lty = 1,xol.text='red', col.line = "blue", digits = 1,r.squared =TRUE) }) 

enter image description here

+3
source

Source: https://habr.com/ru/post/1489328/


All Articles