O'Reilly logo

R Statistical Application Development by Example Beginner's Guide by Prabhanjan Narayanachar Tattar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – understanding overfitting

Polynomial regression models are built using the lm function, as we saw earlier, with the option poly.

  1. Read the hypothetical dataset into R by using data(OF).
  2. Plot Y against X by using plot(OF$X, OF$Y,"b",col="red",xlab="X", ylab="Y").
  3. Fit the polynomial regression models of orders 1, 2, 3, 6, and 9, and add their fitted lines against the covariates X with the following code:
    lines(OF$X,lm(Y~poly(X,1,raw=TRUE),data=OF)$fitted.values,"b",col="green") lines(OF$X,lm(Y~poly(X,2,raw=TRUE),data=OF)$fitted.values,"b",col="wheat") lines(OF$X,lm(Y~poly(X,3,raw=TRUE),data=OF)$fitted.values,"b",col="yellow") lines(OF$X,lm(Y~poly(X,6,raw=TRUE),data=OF)$fitted.values,"b",col="orange") lines(OF$X,lm(Y~poly(X,9,raw=TRUE),data=OF)$fitted.values,"b",col="black") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required