How to change confidence line colors when using `matlines` for prediction graph?

I draw the logarithmic regression line of best fit, as well as confidence intervals around this line. The code I use works quite well, but I would prefer the confidence intervals to be "gray" (rather than the default "red" and "green"). Unfortunately, I see no way to isolate them when indicating color changes. I would like to

  • for the regression line: lty = 1, col = "black" ;
  • for confidence intervals: lty=2, col = "gray" .

How can i achieve this? my code is:

 R6cl <- lm(log(R6$y) ~ R6$x) pR6cl <- predict(R6cl, interval="confidence") plot(R6$x, log(R6$y), type = "p") matlines(x = R6$x, y = log(R6$y), lwd = 2, lty = 1, col = "black") 

which produces:

enter image description here

+1
source share
2 answers

col , lty and lwd vectorized. you can use

 R6cl <- lm(log(y) ~ x, data = R6) ## don't use $ in formula pR6cl <- predict(R6cl, interval = "confidence") plot(log(y) ~ x, data = R6) ## Read `?plot.formula` matlines(R6$x, pR6cl, lwd = 2, lty = c(1, 2, 2), col = c(1, 2, 2)) 

You can check the last digit in piecewise regression with a quadratic polynomial and a straight line that smoothly connects at the break point , for which this code will generate.

If you don’t know why I recommend using $ in the model formula, read Predict () - Maybe I don’t understand it .


Notification to other readers

The OP has a dataset where x sorted. If your x not sorted, make sure you sort it first. See A Useless Plot for Building Polynomial Regression Predictions Using lm () in R for more details.

+1
source

What about:

 a = 1:10 b = c(2,1,2,4,5,5,3,7,4,10) R6cl <- lm(log(b)~a) pR6cl <- predict(R6cl, interval = "confidence") plot(a, log(b), type = "p") lines(a, pR6cl[,1], lty = 1, col = "black") lines(a, pR6cl[,2], lty = 2, col = "gray") lines(a, pR6cl[,3], lty = 2, col = "gray") 

What gives:

plot

+1
source

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


All Articles