R - Non-linear Effect Analysis

> Procedural Languages > R

1 - About

3 - Prerequisites

4 - Steps

4.1 - Linear Model

4.1.1 - Power

myFit=lm(outcome~predictor1 +I(predictor1^2),data.frame)
myFit

where:

  • 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  
Advertising

4.1.2 - Poly function

A poly function in R is an easier way of fitting polynomials.

myFit=lm(outcome~poly(predictor,4))

where:

  • 4 is the degree

4.2 - Summary

Summary Statistics

summary(myFit)
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

4.3 - Confidence Interval

Confidence Interval

confint(myFit)
                     2.5 %       97.5 %
(Intercept)    41.14863062  44.57538404
predictor1     -2.57605639  -2.08958581
I(predictor1^2) 0.03618883   0.05090495

4.4 - Plot

# attach the variables. ie the named variables are available in our data space.
# It just makes it easier for making nice looking plots.
attach(data.frame)
 
# 1 frame, single plane layout
par(mfrow=c(1,1))
 
# Plot
plot(outcome~predictor)

4.4.1 - 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.
points(predictor,fitted(myFit),col="red",pch=20)

where:

  • 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.

Advertising