Data
setwd("/Users/vincentlefieux/Documents/DocsACADEMIQUE/PolysSlides/AdvancedRegression/Data/Cours/")
ozone <- read.table("ozone.txt",header=TRUE,sep=";",dec=",")
attach(ozone)
n <- dim(ozone)[1]
set.seed(1789)
sel <- sample(1:n,round(n*0.8))
ozone_train <- ozone[sel,]
ozone_test <- ozone[-sel,]
erreurs <- data.frame(method=character(0),RMSE=numeric(0),MAPE=numeric(0))
4 Ridge regression
library(MASS)
plot(lm.ridge(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,lambda=seq(0,20,0.01)))

select(lm.ridge(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,lambda=seq(0,20,0.01)))
## modified HKB estimator is 2.730106
## modified L-W estimator is 1.93314
## smallest value of GCV at 7.53
reg_ridge <- lm.ridge(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,lambda=7.53)
summary(reg_ridge)
## Length Class Mode
## coef 7 -none- numeric
## scales 7 -none- numeric
## Inter 1 -none- numeric
## lambda 1 -none- numeric
## ym 1 -none- numeric
## xm 7 -none- numeric
## GCV 1 -none- numeric
## kHKB 1 -none- numeric
## kLW 1 -none- numeric
pred_ridge <- as.matrix(data.frame(c=1,ozone_test[,c(3:8,12)]))%*%as.matrix(coef(reg_ridge))
res_ridge <- ozone_test$maxO3-t(pred_ridge)
rmse_ridge <- sqrt(mean((res_ridge)^2))
rmse_ridge
## [1] 12.61133
mape_ridge <- mean(abs(1-pred_ridge/ozone_test$maxO3))*100
mape_ridge
## [1] 10.60045
erreurs <- rbind(erreurs,data.frame(method="Ridge",RMSE=rmse_ridge,MAPE=mape_ridge))
5 LASSO regression
library(lars)
## Loaded lars 1.2
reg_lasso <- lars(data.matrix(ozone_train[,c(3:8,12)]),ozone_train[,2],type="lasso")
plot(reg_lasso)

summary(reg_lasso)
## LARS/LASSO
## Call: lars(x = data.matrix(ozone_train[, c(3:8, 12)]), y = ozone_train[,
## Call: 2], type = "lasso")
## Df Rss Cp
## 0 1 68691 226.7819
## 1 2 47989 133.9159
## 2 3 36648 83.9413
## 3 4 33791 72.8515
## 4 5 18431 4.4612
## 5 6 18164 5.2398
## 6 7 17896 6.0082
## 7 8 17894 8.0000
index_lasso <- seq(0,1,length=100)
cv <- cv.lars(data.matrix(ozone_train[,c(3:8,12)]),ozone_train[,2],K=4,type="lasso",index=index_lasso)

s_opt <- index_lasso[which.min(cv$cv)]
pred_lasso <- predict(reg_lasso,data.matrix(ozone_test[,c(3:8,12)]),s=s_opt,mode="fraction")
res_lasso <- ozone_test$maxO3-pred_lasso$fit
rmse_lasso <- sqrt(mean((res_lasso)^2))
rmse_lasso
## [1] 12.8278
mape_lasso <- mean(abs(1-pred_lasso$fit/ozone_test$maxO3))*100
mape_lasso
## [1] 10.84037
erreurs <- rbind(erreurs,data.frame(method="LASSO (LARS)",RMSE=rmse_lasso,MAPE=mape_lasso))
6 Nadaraya-Watson regression
reg_nw <- loess(ozone_train$maxO3~ozone_train$T12,degree=0)
plot(reg_nw)

summary(reg_nw)
## Call:
## loess(formula = ozone_train$maxO3 ~ ozone_train$T12, degree = 0)
##
## Number of Observations: 90
## Equivalent Number of Parameters: 2.15
## Residual Standard Error: 20.05
## Trace of smoother matrix: 2.31 (exact)
##
## Control settings:
## span : 0.75
## degree : 0
## family : gaussian
## surface : interpolate cell = 0.2
## normalize: TRUE
## parametric: FALSE
## drop.square: FALSE
pred_nw <- predict(reg_nw,ozone_test$T12)
res_nw <- ozone_test$maxO3-pred_nw
rmse_nw <- sqrt(mean((res_nw)^2))
rmse_nw
## [1] 20.55192
mape_nw <- mean(abs(1-pred_nw/ozone_test$maxO3))*100
mape_nw
## [1] 17.50319
erreurs <- rbind(erreurs,data.frame(method="Nadaraya-Watson",RMSE=rmse_nw,MAPE=mape_nw))
7 Local polynomials (degree 1)
reg_loess <- loess(ozone_train$maxO3~ozone_train$T12,degree=1)
plot(reg_loess)

summary(reg_loess)
## Call:
## loess(formula = ozone_train$maxO3 ~ ozone_train$T12, degree = 1)
##
## Number of Observations: 90
## Equivalent Number of Parameters: 3.46
## Residual Standard Error: 16.54
## Trace of smoother matrix: 4.01 (exact)
##
## Control settings:
## span : 0.75
## degree : 1
## family : gaussian
## surface : interpolate cell = 0.2
## normalize: TRUE
## parametric: FALSE
## drop.square: FALSE
pred_loess <- predict(reg_loess,ozone_test$T12)
res_loess <- ozone_test$maxO3-pred_loess
rmse_loess <- sqrt(mean((res_loess)^2))
rmse_loess
## [1] 13.99398
mape_loess <- mean(abs(1-pred_loess/ozone_test$maxO3))*100
mape_loess
## [1] 13.82895
erreurs <- rbind(erreurs,data.frame(method="Local polynomial (degree 1)",RMSE=rmse_loess,MAPE=mape_loess))
8 GAM regression
library(mgcv)
## Loading required package: nlme
## This is mgcv 1.8-18. For overview type 'help("mgcv-package")'.
reg_gam <- gam(maxO3~s(T12)+s(maxO3v),data=ozone_train)
plot(reg_gam)


summary(reg_gam)
##
## Family: gaussian
## Link function: identity
##
## Formula:
## maxO3 ~ s(T12) + s(maxO3v)
##
## Parametric coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 90.056 1.466 61.45 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Approximate significance of smooth terms:
## edf Ref.df F p-value
## s(T12) 7.894 8.652 13.53 5.64e-15 ***
## s(maxO3v) 2.037 2.568 7.83 0.000241 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## R-sq.(adj) = 0.75 Deviance explained = 77.7%
## GCV = 220.02 Scale est. = 193.3 n = 90
pred_gam <- predict(reg_gam,ozone_test[,c(3:8,12)])
res_gam <- ozone_test$maxO3-pred_gam
rmse_gam <- sqrt(mean((res_gam)^2))
rmse_gam
## [1] 12.60062
mape_gam <- mean(abs(1-pred_gam/ozone_test$maxO3))*100
mape_gam
## [1] 11.9212
erreurs <- rbind(erreurs,data.frame(method="GAM",RMSE=rmse_gam,MAPE=mape_gam))
9 Index regression
library(EDR)
## Loading required package: sm
## Package 'sm', version 2.2-5.4: type help(sm) for summary information
##
## Attaching package: 'sm'
## The following object is masked from 'package:MASS':
##
## muscle
reg_edr <- edr(data.matrix(ozone_train[,c(3:8,12)]),ozone_train[,2])
summary(reg_edr)
## Basis of the Effective Dimension Reduction Space, m= 1
##
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] -0.4634 0.6096 -0.1624 -0.09351 -0.5033 -0.3424 0.08885
## Cumulative sum of normalized eigenvalues of Bhat: 0.3996005 0.6734142
## $Rhat
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.4634133 0.6096016 -0.1624306 -0.09351359 -0.5033292 -0.3424479
## [,7]
## [1,] 0.08885201
##
## $cumlam
## [1] 0.3996005 0.6734142
##
## $loss1
## NULL
##
## $loss2
## NULL
pred_edr <- predict(reg_edr,data.matrix(ozone_test[,c(3:8,12)]))
res_edr <- ozone_test$maxO3-pred_edr$fhat
rmse_edr <- sqrt(mean((res_edr)^2))
rmse_edr
## [1] 17.43995
mape_edr <- mean(abs(1-pred_edr$fhat/ozone_test$maxO3))*100
mape_edr
## [1] 15.136
erreurs <- rbind(erreurs,data.frame(method="SIM",RMSE=rmse_edr,MAPE=mape_edr))
10 PLS regression
library(pls)
##
## Attaching package: 'pls'
## The following object is masked from 'package:stats':
##
## loadings
reg_pls <- plsr(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,ncomp=3,data=ozone_train,validation="CV")
summary(reg_pls)
## Data: X dimension: 90 7
## Y dimension: 90 1
## Fit method: kernelpls
## Number of components considered: 3
##
## VALIDATION: RMSEP
## Cross-validated using 10 random segments.
## (Intercept) 1 comps 2 comps 3 comps
## CV 27.94 21.31 15.79 15.68
## adjCV 27.94 21.24 15.74 15.62
##
## TRAINING: % variance explained
## 1 comps 2 comps 3 comps
## X 93.77 98.34 99.19
## maxO3 47.61 71.15 72.80
plot(reg_pls)

msepcv_pls <- MSEP(reg_pls,estimate=c("train","CV"))
plot(msepcv_pls,type="l",legendpos="topright",main="Validation croisée")

comp_opt <- which.min(msepcv_pls$val["CV",,])-1
reg_pls <- plsr(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,ncomp=comp_opt,data=ozone_train)
summary(reg_pls)
## Data: X dimension: 90 7
## Y dimension: 90 1
## Fit method: kernelpls
## Number of components considered: 3
## TRAINING: % variance explained
## 1 comps 2 comps 3 comps
## X 93.77 98.34 99.19
## maxO3 47.61 71.15 72.80
pred_pls <- predict(reg_pls,ozone_test[,c(3:8,12)])
res_pls <- ozone_test$maxO3-pred_pls
rmse_pls <- sqrt(mean((res_pls)^2))
rmse_pls
## [1] 14.86144
mape_pls <- mean(abs(1-pred_pls/ozone_test$maxO3))*100
mape_pls
## [1] 13.51327
erreurs <- rbind(erreurs,data.frame(method="PLS",RMSE=rmse_pls,MAPE=mape_pls))
11 CART regression
library(rpart)
library(rpart.plot)
reg_cart <- rpart(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train)
summary(reg_cart)
## Call:
## rpart(formula = maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + Ne15 +
## maxO3v, data = ozone_train)
## n= 90
##
## CP nsplit rel error xerror xstd
## 1 0.61271599 0 1.0000000 1.0128666 0.16502717
## 2 0.08460423 1 0.3872840 0.5052521 0.07706505
## 3 0.04602492 2 0.3026798 0.4476081 0.07059852
## 4 0.03237008 3 0.2566549 0.4519370 0.06966568
## 5 0.01366992 4 0.2242848 0.3901173 0.06571495
## 6 0.01000000 5 0.2106149 0.4002184 0.06528087
##
## Variable importance
## T12 T15 T9 maxO3v Ne12 Ne9
## 26 24 21 12 9 9
##
## Node number 1: 90 observations, complexity param=0.612716
## mean=90.05556, MSE=763.2302
## left son=2 (66 obs) right son=3 (24 obs)
## Primary splits:
## T12 < 23.45 to the left, improve=0.6127160, (0 missing)
## T15 < 25.8 to the left, improve=0.5930173, (0 missing)
## T9 < 19.65 to the left, improve=0.4911226, (0 missing)
## maxO3v < 116.5 to the left, improve=0.3878578, (0 missing)
## Ne12 < 4.5 to the right, improve=0.3550840, (0 missing)
## Surrogate splits:
## T15 < 24.95 to the left, agree=0.978, adj=0.917, (0 split)
## T9 < 19.55 to the left, agree=0.956, adj=0.833, (0 split)
## maxO3v < 115 to the left, agree=0.844, adj=0.417, (0 split)
## Ne9 < 1.5 to the right, agree=0.822, adj=0.333, (0 split)
## Ne12 < 1.5 to the right, agree=0.822, adj=0.333, (0 split)
##
## Node number 2: 66 observations, complexity param=0.04602492
## mean=77.01515, MSE=202.4998
## left son=4 (50 obs) right son=5 (16 obs)
## Primary splits:
## maxO3v < 91 to the left, improve=0.2365498, (0 missing)
## Ne12 < 4.5 to the right, improve=0.2066556, (0 missing)
## T12 < 17.25 to the left, improve=0.1812109, (0 missing)
## Ne9 < 3.5 to the right, improve=0.1532984, (0 missing)
## Ne15 < 4.5 to the right, improve=0.1079424, (0 missing)
## Surrogate splits:
## Ne9 < 1.5 to the right, agree=0.773, adj=0.062, (0 split)
##
## Node number 3: 24 observations, complexity param=0.08460423
## mean=125.9167, MSE=551.5764
## left son=6 (11 obs) right son=7 (13 obs)
## Primary splits:
## T12 < 26.6 to the left, improve=0.4390088, (0 missing)
## T15 < 28.1 to the left, improve=0.2947235, (0 missing)
## maxO3v < 116.5 to the left, improve=0.2870686, (0 missing)
## T9 < 22.15 to the left, improve=0.1645254, (0 missing)
## Ne12 < 2.5 to the right, improve=0.1616495, (0 missing)
## Surrogate splits:
## T15 < 28.1 to the left, agree=0.958, adj=0.909, (0 split)
## T9 < 21.45 to the left, agree=0.875, adj=0.727, (0 split)
## Ne9 < 6.5 to the right, agree=0.667, adj=0.273, (0 split)
## Ne12 < 3.5 to the right, agree=0.625, adj=0.182, (0 split)
## maxO3v < 106.5 to the left, agree=0.625, adj=0.182, (0 split)
##
## Node number 4: 50 observations, complexity param=0.03237008
## mean=73.1, MSE=158.29
## left son=8 (41 obs) right son=9 (9 obs)
## Primary splits:
## Ne12 < 4.5 to the right, improve=0.2809431, (0 missing)
## T12 < 17.25 to the left, improve=0.1922224, (0 missing)
## Ne9 < 6.5 to the right, improve=0.1623468, (0 missing)
## Ne15 < 4.5 to the right, improve=0.1617606, (0 missing)
## maxO3v < 68.5 to the left, improve=0.1390606, (0 missing)
## Surrogate splits:
## T12 < 22.45 to the left, agree=0.86, adj=0.222, (0 split)
## Ne9 < 2.5 to the right, agree=0.86, adj=0.222, (0 split)
## Ne15 < 2.5 to the right, agree=0.86, adj=0.222, (0 split)
## T15 < 23.8 to the left, agree=0.84, adj=0.111, (0 split)
##
## Node number 5: 16 observations
## mean=89.25, MSE=143.0625
##
## Node number 6: 11 observations
## mean=109, MSE=280.7273
##
## Node number 7: 13 observations
## mean=140.2308, MSE=333.716
##
## Node number 8: 41 observations, complexity param=0.01366992
## mean=69.97561, MSE=111.0482
## left son=16 (7 obs) right son=17 (34 obs)
## Primary splits:
## T12 < 17.25 to the left, improve=0.2062380, (0 missing)
## Ne9 < 7.5 to the right, improve=0.1354375, (0 missing)
## maxO3v < 61 to the left, improve=0.1221956, (0 missing)
## T9 < 15.35 to the left, improve=0.1219994, (0 missing)
## Ne15 < 4.5 to the right, improve=0.1171860, (0 missing)
## Surrogate splits:
## T15 < 16.25 to the left, agree=0.951, adj=0.714, (0 split)
## T9 < 15 to the left, agree=0.878, adj=0.286, (0 split)
## maxO3v < 57.5 to the left, agree=0.854, adj=0.143, (0 split)
##
## Node number 9: 9 observations
## mean=87.33333, MSE=126.4444
##
## Node number 16: 7 observations
## mean=59.42857, MSE=151.102
##
## Node number 17: 34 observations
## mean=72.14706, MSE=75.18426
rpart.plot(reg_cart)

rpart.plot(rpart(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,control=rpart.control(cp=0.005)))

rpart.plot(rpart(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,control=rpart.control(cp=0.5)))

printcp(reg_cart)
##
## Regression tree:
## rpart(formula = maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + Ne15 +
## maxO3v, data = ozone_train)
##
## Variables actually used in tree construction:
## [1] maxO3v Ne12 T12
##
## Root node error: 68691/90 = 763.23
##
## n= 90
##
## CP nsplit rel error xerror xstd
## 1 0.612716 0 1.00000 1.01287 0.165027
## 2 0.084604 1 0.38728 0.50525 0.077065
## 3 0.046025 2 0.30268 0.44761 0.070599
## 4 0.032370 3 0.25665 0.45194 0.069666
## 5 0.013670 4 0.22428 0.39012 0.065715
## 6 0.010000 5 0.21061 0.40022 0.065281
plotcp(reg_cart)

node_opt <- which.min(reg_cart$cptable[,"xerror"])
cp_opt <- reg_cart$cptable[node_opt,"CP"]
reg_cart_opt <- rpart(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,control=rpart.control(cp=cp_opt),data=ozone_train)
pred_cart <- predict(reg_cart,ozone_test[,c(3:8,12)])
res_cart <- ozone_test$maxO3-pred_cart
rmse_cart <- sqrt(mean((res_cart)^2))
rmse_cart
## [1] 12.69259
mape_cart <- mean(abs(1-pred_cart/ozone_test$maxO3))*100
mape_cart
## [1] 12.27956
erreurs <- rbind(erreurs,data.frame(method="CART",RMSE=rmse_cart,MAPE=mape_cart))
11 Random forests regression
library(randomForest)
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
reg_rf <- randomForest(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone_train,importance=TRUE)
pred_rf <- predict(reg_rf,ozone_test[,c(3:8,12)])
res_rf <- ozone_test$maxO3-pred_rf
rmse_rf <- sqrt(mean((res_rf)^2))
rmse_rf
## [1] 11.90259
mape_rf <- mean(abs(1-pred_rf/ozone_test$maxO3))*100
mape_rf
## [1] 11.29602
erreurs <- rbind(erreurs,data.frame(method="Random forests",RMSE=rmse_rf,MAPE=mape_rf))
Summary
library(knitr)
kable(erreurs,digits=2,format="markdown")
| Linear (simple) |
16.24 |
14.76 |
| Linear (polynomial) |
15.14 |
13.99 |
| Linear (multiple) |
13.06 |
10.91 |
| Ridge |
12.61 |
10.60 |
| LASSO (LARS) |
12.83 |
10.84 |
| Nadaraya-Watson |
20.55 |
17.50 |
| Local polynomial (degree 1) |
13.99 |
13.83 |
| GAM |
12.60 |
11.92 |
| SIM |
17.44 |
15.14 |
| PLS |
14.86 |
13.51 |
| CART |
12.69 |
12.28 |
| Random forests |
11.90 |
11.30 |