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.
Try:
This should work.
Since you didn't provide any data, here is a working example using the built-in mtcars dataset.
Some notes:
(1) It is a really bad idea to reference external data structures in the
formula=...
argument tolm(...)
. Instead, reference columns of a data frame referenced in thedata=...
argumennt, as above and as @mso points out.(2) You can specify the formula as @mso suggests, or you can use the
poly(...)
function withraw=TRUE
.(3) The
curve(...)
function takes an expression as its first argument, This expression has to have a variablex
, which will be populated automatically by values from the x-axis of the graph. So in this example, the expression is:which uses
predict(...)
on the model with a dataframe havingwt
(the predictor variable) given byx
.Try with ggplot:
Using mtcars data: