How do I plot a quadratic regression in R?

The following code generates a qudaratic regression in R.

 lm.out3 = lm(listOfDataFrames1$avgTime ~ listOfDataFrames1$betaexit + I(listOfDataFrames1$betaexit^2) + I(listOfDataFrames1$betaexit^3))

 summary(lm.out3)

Call:
lm(formula = listOfDataFrames1$avgTime ~ listOfDataFrames1$betaexit + 
    I(listOfDataFrames1$betaexit^2) + I(listOfDataFrames1$betaexit^3))

Residuals:
    Min      1Q  Median      3Q     Max 
-14.168  -2.923  -1.435   2.459  28.429 

Coefficients:
                                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)                        199.41      11.13  17.913  < 2e-16 ***
listOfDataFrames1$betaexit       -3982.03     449.49  -8.859 1.14e-12 ***
I(listOfDataFrames1$betaexit^2)  32630.86    5370.27   6.076 7.87e-08 ***
I(listOfDataFrames1$betaexit^3) -93042.90   19521.05  -4.766 1.15e-05 ***
---
Signif. codes:  0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1

Residual standard error: 7.254 on 63 degrees of freedom
Multiple R-squared:  0.9302,    Adjusted R-squared:  0.9269 
F-statistic: 279.8 on 3 and 63 DF,  p-value: < 2.2e-16

      

But how to make a graph of a curve on a graph, I am confused.

To get a graph:

 plot(listOfDataFrames1$avgTime~listOfDataFrames1$betaexit)

      

But the curve?


Is it possible to do this without manually copying the values? As mso suggested, although it works.

+3


source to share


3 answers


This should work.

# not tested
lm.out3 = lm(avgTime ~ poly(betaexit,3,raw=TRUE),listofDataFrames3)
plot(avgTime~betaexit,listofDataDFrames3)
curve(predict(lm.out3,newdata=data.frame(betaexit=x)),add=T)

      

Since you didn't provide any data, here's a working example using the built-in mtcars dataset.

fit <- lm(mpg~poly(wt,3,raw=TRUE),mtcars)
plot(mpg~wt,mtcars)
curve(predict(fit,newdata=data.frame(wt=x)),add=T)

      

Some notes:



(1) It is really a bad idea to refer to external data structures in an argument formula=...

to lm(...)

. Instead, the pivot columns of the data=...

dataframe that are referenced in the argumennt as above and as @mso points out.

(2) You can specify the formula as @mso, or you can use the function poly(...)

with raw=TRUE

.

(3) The function curve(...)

takes an expression as its first argument. This expression must have a variable x

that will be automatically filled with values ​​from the x-axis of the graph. So in this example the expression:

predict(fit,newdata=data.frame(wt=x))

      

which uses predict(...)

for a model with a dataframe having wt

(predictor variable) given x

.

+5


source


Try with ggplot:

library(ggplot)
ggplot(listOfDataFrames1, aes(x=betaexit, y=avgTime)) + geom_point()+stat_smooth(se=F)

      

Using mtcars data:



ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()+stat_smooth(se=F, method='lm', formula=y~poly(x,3))

      

enter image description here

+3


source


Try:

with(listOfDataFrames1, plot(betaexit, avgTime))
with(listOfDataFrames1, lines(betaexit, 199-3982*betaexit+32630*betaexit^2-93042*betaexit^3))

      

+1


source







All Articles