Non-linear Analysis with R.
myFit=lm(outcome~predictor1 +I(predictor1^2),data.frame) myFit
- the quadratic is indicated by the power 2 (predictor1^2)
- As power has a meaning in this formula, the identity function (I) is used to protect it.
Call: lm(formula = medv ~ predictor1 + I(predictor1^2), data = data.frame) Coefficients: (Intercept) predictor1 I(predictor1 ^2) 42.86201 -2.33282 0.04355
A poly function in R is an easier way of fitting polynomials.
- 4 is the degree
Call: lm(formula = medv ~ predictor1 + I(predictor1^2), data = data.frame) Residuals: Min 1Q Median 3Q Max -15.2834 -3.8313 -0.5295 2.3095 25.4148 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 42.862007 0.872084 49.15 <2e-16 *** predictor1 -2.332821 0.123803 -18.84 <2e-16 *** I(predictor1^2) 0.043547 0.003745 11.63 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 5.524 on 503 degrees of freedom Multiple R-squared: 0.6407, Adjusted R-squared: 0.6393 F-statistic: 448.5 on 2 and 503 DF, p-value: < 2.2e-16
2.5 % 97.5 % (Intercept) 41.14863062 44.57538404 predictor1 -2.57605639 -2.08958581 I(predictor1^2) 0.03618883 0.05090495
Series of points
- The fitted model can't be plotted with the function abline anymore, because that only works when you've got a straight line fit. It will be plotted as a series of points with the points function.
- the first argument is the predictor
- the second argument are the fitted values from the model. fitted(myFit)
- pch is the plotting character
It means for each value of predictor, shows the fitted value from the model.