Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot quadratic regression in R?

Tags:

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 do I plot the curve on the graph am confused.

To get graph:

 plot(listOfDataFrames1$avgTime~listOfDataFrames1$betaexit)

But curve?


Is there any to do it without manually copying the values? Like mso suggested though it works.

like image 323
Abhishek Bhatia Avatar asked Nov 16 '14 16:11

Abhishek Bhatia


2 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 is 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 a really bad idea to reference external data structures in the formula=... argument to lm(...). Instead, reference columns of a data frame referenced in the data=... argumennt, as above and as @mso points out.

(2) You can specify the formula as @mso suggests, or you can use the poly(...) function with raw=TRUE.

(3) The curve(...) function takes an expression as its first argument, This expression has to have a variable x, which will be populated automatically by values from the x-axis of the graph. So in this example, the expression is:

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

which uses predict(...) on the model with a dataframe having wt (the predictor variable) given by x.

like image 50
jlhoward Avatar answered Sep 18 '22 18:09

jlhoward


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

like image 20
rnso Avatar answered Sep 22 '22 18:09

rnso