1 Libraries

library(car) #For VIF
library(leaps) #For variables selection

2 Data

setwd("/Users/vincentlefieux/Documents/DocsACADEMIQUE/PolysSlides/AdvancedRegression/Data/Cours/")
ozone <- read.table("ozone.txt",header=TRUE,sep=";",dec=",")
attach(ozone)

3 Simple linear regression

3.1 Estimation

reg1 <- lm(maxO3~T12)
reg1
## 
## Call:
## lm(formula = maxO3 ~ T12)
## 
## Coefficients:
## (Intercept)          T12  
##     -27.420        5.469
names(reg1)
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "xlevels"       "call"          "terms"         "model"
summary(reg1)
## 
## Call:
## lm(formula = maxO3 ~ T12)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -38.079 -12.735   0.257  11.003  44.671 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -27.4196     9.0335  -3.035    0.003 ** 
## T12           5.4687     0.4125  13.258   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.57 on 110 degrees of freedom
## Multiple R-squared:  0.6151, Adjusted R-squared:  0.6116 
## F-statistic: 175.8 on 1 and 110 DF,  p-value: < 2.2e-16
plot(T12,maxO3,xlab="T12",ylab="maxO3")
abline(reg1,col="red")

par(mfrow=c(2,2))
plot(reg1)

par(mfrow=c(1,1))
yhat1 <- reg1$fit
plot(maxO3,yhat1,xlab="maxO3",ylab="Fitted maxO3")
abline(a=0,b=1,col="red")

It’s also possible to use yhat1=reg1$fitted.values or yhat1=predict(reg1).

round(mean(reg1$residuals),digits=2)
## [1] 0
round(var(reg1$residuals),digits=2)
## [1] 305.84
boxplot(reg1$residuals,main="Residuals of reg1")

hist(reg1$residuals,main="Residuals of reg1")

Level of tests is 5%:

alpha <- 0.05

Dimensions are:

n <- dim(ozone)[1]
p <- 2

We compute studentized residuals:

rstudent1 <- rstudent(reg1)
threshold_stud1 <- qt(1-alpha/2,n-p-1)
plot(rstudent1,xlab="Observation",ylab="Studentized residuals")
abline(h=threshold_stud1,col="red")
abline(h=-threshold_stud1,col="red")

ids1 <- abs(rstudent1)>threshold_stud1
rstudent1[ids1]
##        17        25        34        79 
##  2.210261 -2.216162  2.395229  2.646881

We compute Cook distances:

infl1 <- influence.measures(reg1)
names(infl1)
## [1] "infmat" "is.inf" "call"
colnames(infl1$infmat)
## [1] "dfb.1_"  "dfb.T12" "dffit"   "cov.r"   "cook.d"  "hat"
cook1 <- infl1$infmat[,"cook.d"]
threshold_cook1 <- 4/(n-p)
plot(cook1,type="h",xlab="Observation",ylab="Cook distance")
abline(h=threshold_cook1,col="red")

idc1 <- cook1>threshold_cook1
cook1[idc1]
##         34         52         53         54         56         58 
## 0.04196648 0.03637132 0.04316194 0.05956720 0.05199605 0.03818800 
##         79 
## 0.09102743

We remove outliers:

to_remove1 <- (1:n)[ids1*idc1==1]
ozone1 <- ozone[-to_remove1,]
reg2 <- lm(maxO3~T12,data=ozone1)
summary(reg2)
## 
## Call:
## lm(formula = maxO3 ~ T12, data = ozone1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.241 -12.170   0.543  11.423  38.876 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -26.7493     8.6949  -3.076  0.00265 ** 
## T12           5.4013     0.3975  13.590  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16.72 on 108 degrees of freedom
## Multiple R-squared:  0.631,  Adjusted R-squared:  0.6276 
## F-statistic: 184.7 on 1 and 108 DF,  p-value: < 2.2e-16

3.2 Polynomial regression

reg3 <- lm(maxO3~T12+I(T12^2),data=ozone)
summary(reg3)
## 
## Call:
## lm(formula = maxO3 ~ T12 + I(T12^2), data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.401 -13.490  -1.266  11.555  45.061 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 76.74824   41.75410   1.838   0.0688 .
## T12         -3.87486    3.68286  -1.052   0.2951  
## I(T12^2)     0.20219    0.07922   2.552   0.0121 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.14 on 109 degrees of freedom
## Multiple R-squared:  0.6368, Adjusted R-squared:  0.6301 
## F-statistic: 95.54 on 2 and 109 DF,  p-value: < 2.2e-16

We remove non significant parameter:

reg4 <- lm(maxO3~I(T12^2),data=ozone)
summary(reg4)
## 
## Call:
## lm(formula = maxO3 ~ I(T12^2), data = ozone)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -35.95 -12.69  -0.52  11.53  44.64 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.068401   4.459399   7.415 2.68e-11 ***
## I(T12^2)     0.119340   0.008662  13.777  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.15 on 110 degrees of freedom
## Multiple R-squared:  0.6331, Adjusted R-squared:  0.6298 
## F-statistic: 189.8 on 1 and 110 DF,  p-value: < 2.2e-16
plot(reg4)

We plot the poynomial regression:

newT12 <- data.frame(T12=seq(0,40,1))
pred.w.plim <- predict(reg4,newT12,interval="prediction")
pred.w.clim <- predict(reg4,newT12,interval="confidence")
matplot(newT12,cbind(pred.w.clim,pred.w.plim[,-1]),lty=c(1,2,2,3,3),col=c(1,2,2,3,3),type="l",xlab="T12",ylab="Prédiction")

predict(reg4,newT12,se.fit=TRUE)
## $fit
##         1         2         3         4         5         6         7 
##  33.06840  33.18774  33.54576  34.14246  34.97784  36.05190  37.36465 
##         8         9        10        11        12        13        14 
##  38.91607  40.70617  42.73495  45.00242  47.50856  50.25338  53.23689 
##        15        16        17        18        19        20        21 
##  56.45907  59.91993  63.61948  67.55770  71.73461  76.15019  80.80446 
##        22        23        24        25        26        27        28 
##  85.69740  90.82903  96.19934 101.80832 107.65599 113.74234 120.06737 
##        29        30        31        32        33        34        35 
## 126.63107 133.43346 140.47453 147.75428 155.27271 163.02982 171.02561 
##        36        37        38        39        40        41 
## 179.26008 187.73323 196.44506 205.39557 214.58476 224.01263 
## 
## $se.fit
##        1        2        3        4        5        6        7        8 
## 4.459399 4.451330 4.427136 4.386859 4.330570 4.258373 4.170414 4.066889 
##        9       10       11       12       13       14       15       16 
## 3.948054 3.814251 3.665927 3.503681 3.328316 3.140934 2.943062 2.736868 
##       17       18       19       20       21       22       23       24 
## 2.525487 2.313533 2.107894 1.918840 1.761231 1.654782 1.621102 1.676204 
##       25       26       27       28       29       30       31       32 
## 1.823153 2.052553 2.349690 2.700744 3.095097 3.525250 3.986054 4.473978 
##       33       34       35       36       37       38       39       40 
## 4.986568 5.522093 6.079305 6.657291 7.255367 7.873015 8.509835 9.165516 
##       41 
## 9.839810 
## 
## $df
## [1] 110
## 
## $residual.scale
## [1] 17.15139

4 Multiple linear regression

4.1 Model selection

The complete regression is:

reg_compl <- lm(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + Ne15 + maxO3v, 
##     data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -57.768  -7.845  -1.359   8.134  38.984 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 12.70548   13.10860   0.969  0.33467    
## T9          -0.63596    1.03462  -0.615  0.54011    
## T12          2.50600    1.39946   1.791  0.07625 .  
## T15          0.71381    1.13674   0.628  0.53142    
## Ne9         -2.76057    0.89157  -3.096  0.00252 ** 
## Ne12        -0.37193    1.34590  -0.276  0.78283    
## Ne15         0.09028    0.99934   0.090  0.92819    
## maxO3v       0.37774    0.06121   6.171 1.32e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.43 on 104 degrees of freedom
## Multiple R-squared:  0.7546, Adjusted R-squared:  0.738 
## F-statistic: 45.68 on 7 and 104 DF,  p-value: < 2.2e-16

We remove ne15:

reg_compl <- lm(maxO3~T9+T12+T15+Ne9+Ne12+maxO3v,data=ozone)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + maxO3v, data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -57.689  -7.796  -1.447   8.147  38.929 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 12.84916   12.95015   0.992  0.32338    
## T9          -0.62976    1.02745  -0.613  0.54124    
## T12          2.56018    1.25841   2.034  0.04443 *  
## T15          0.65787    0.94878   0.693  0.48960    
## Ne9         -2.76526    0.88585  -3.122  0.00232 ** 
## Ne12        -0.30796    1.13912  -0.270  0.78742    
## maxO3v       0.37752    0.06087   6.202 1.12e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.36 on 105 degrees of freedom
## Multiple R-squared:  0.7545, Adjusted R-squared:  0.7405 
## F-statistic:  53.8 on 6 and 105 DF,  p-value: < 2.2e-16

We remove ne12:

reg_compl <- lm(maxO3~T9+T12+T15+Ne9+maxO3v,data=ozone)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T9 + T12 + T15 + Ne9 + maxO3v, data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -57.246  -7.607  -1.295   8.285  38.477 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 11.28440   11.53397   0.978   0.3301    
## T9          -0.73127    0.95218  -0.768   0.4442    
## T12          2.66487    1.19211   2.235   0.0275 *  
## T15          0.66817    0.94386   0.708   0.4806    
## Ne9         -2.92578    0.65451  -4.470 1.97e-05 ***
## maxO3v       0.37960    0.06012   6.314 6.48e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.3 on 106 degrees of freedom
## Multiple R-squared:  0.7544, Adjusted R-squared:  0.7428 
## F-statistic: 65.11 on 5 and 106 DF,  p-value: < 2.2e-16

We remove T9:

reg_compl <- lm(maxO3~T12+T15+Ne9+maxO3v,data=ozone)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T12 + T15 + Ne9 + maxO3v, data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -56.068  -7.767  -1.605   8.446  40.187 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.13680   11.16838   0.818   0.4151    
## T12          2.23175    1.04826   2.129   0.0355 *  
## T15          0.62772    0.94058   0.667   0.5060    
## Ne9         -2.96393    0.65137  -4.550 1.42e-05 ***
## maxO3v       0.37019    0.05875   6.301 6.71e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.27 on 107 degrees of freedom
## Multiple R-squared:  0.753,  Adjusted R-squared:  0.7438 
## F-statistic: 81.55 on 4 and 107 DF,  p-value: < 2.2e-16

We remove T15:

reg_compl <- lm(maxO3~T12+Ne9+maxO3v,data=ozone)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T12 + Ne9 + maxO3v, data = ozone)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -56.385  -7.872  -1.941   7.899  41.513 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.76225   11.10038   0.879    0.381    
## T12          2.85308    0.48052   5.937 3.57e-08 ***
## Ne9         -3.02423    0.64342  -4.700 7.71e-06 ***
## maxO3v       0.37571    0.05801   6.477 2.85e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.23 on 108 degrees of freedom
## Multiple R-squared:  0.752,  Adjusted R-squared:  0.7451 
## F-statistic: 109.1 on 3 and 108 DF,  p-value: < 2.2e-16
p <- 3 # Number of selected variables

We compute studentized residuals:

rstudent2 <- rstudent(reg_compl)
threshold_stud2 <- qt(1-alpha/2,n-p-1)
plot(rstudent2,xlab="Observation",ylab="Studentized residuals")
abline(h=threshold_stud2,col="red")
abline(h=-threshold_stud2,col="red")

ids2 <- abs(rstudent2)>threshold_stud2
rstudent2[ids2]
##        34        52        58        79 
##  2.874822  2.998967 -4.451532  3.083156

We compute Cook distances:

infl2 <- influence.measures(reg_compl)
names(infl2)
## [1] "infmat" "is.inf" "call"
colnames(infl2$infmat)
## [1] "dfb.1_"   "dfb.T12"  "dfb.Ne9"  "dfb.mxO3" "dffit"    "cov.r"   
## [7] "cook.d"   "hat"
cook2 <- infl2$infmat[,"cook.d"]
threshold_cook2 <- 4/(n-p)
plot(cook2,type="h",xlab="Observation",ylab="Cook distance")
abline(h=threshold_cook2,col="red")

idc2 <- cook2>threshold_cook2
cook2[idc2]
##         18         34         52         58         79 
## 0.05362500 0.05401076 0.08422179 0.31669963 0.07849321

We remove outliers:

to_remove2 <- (1:n)[ids2*idc2==1]
to_remove2
## [1] 34 52 58 79
ozone2 <- ozone[-to_remove2,]
reg_compl <- lm(maxO3~T12+Ne9+maxO3v,data=ozone2)
summary(reg_compl)
## 
## Call:
## lm(formula = maxO3 ~ T12 + Ne9 + maxO3v, data = ozone2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -24.269  -6.958  -1.427   8.129  27.247 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 16.73671    8.98841   1.862   0.0654 .  
## T12          2.44303    0.39276   6.220 1.05e-08 ***
## Ne9         -3.57561    0.52140  -6.858 5.15e-10 ***
## maxO3v       0.41973    0.04807   8.732 4.51e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.41 on 104 degrees of freedom
## Multiple R-squared:  0.827,  Adjusted R-squared:  0.822 
## F-statistic: 165.7 on 3 and 104 DF,  p-value: < 2.2e-16

There is no colinearity problem, VIF are under 10:

vif(reg_compl)
##      T12      Ne9   maxO3v 
## 2.034850 1.498920 1.466548

4.2 Automatic variables selection

With the backward method:

regselect1 <- regsubsets(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone,method="backward")
summary(regselect1)
## Subset selection object
## Call: regsubsets.formula(maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + Ne15 + 
##     maxO3v, data = ozone, method = "backward")
## 7 Variables  (and intercept)
##        Forced in Forced out
## T9         FALSE      FALSE
## T12        FALSE      FALSE
## T15        FALSE      FALSE
## Ne9        FALSE      FALSE
## Ne12       FALSE      FALSE
## Ne15       FALSE      FALSE
## maxO3v     FALSE      FALSE
## 1 subsets of each size up to 7
## Selection Algorithm: backward
##          T9  T12 T15 Ne9 Ne12 Ne15 maxO3v
## 1  ( 1 ) " " "*" " " " " " "  " "  " "   
## 2  ( 1 ) " " "*" " " " " " "  " "  "*"   
## 3  ( 1 ) " " "*" " " "*" " "  " "  "*"   
## 4  ( 1 ) "*" "*" " " "*" " "  " "  "*"   
## 5  ( 1 ) "*" "*" "*" "*" " "  " "  "*"   
## 6  ( 1 ) "*" "*" "*" "*" "*"  " "  "*"   
## 7  ( 1 ) "*" "*" "*" "*" "*"  "*"  "*"
plot(regselect1)
plot(regselect1,scale="bic")

plot(regselect1,scale="Cp")

plot(regselect1,scale="adjr2")

With the forward method:

regselect2 <- regsubsets(maxO3~T9+T12+T15+Ne9+Ne12+Ne15+maxO3v,data=ozone,method="forward")
summary(regselect2)
## Subset selection object
## Call: regsubsets.formula(maxO3 ~ T9 + T12 + T15 + Ne9 + Ne12 + Ne15 + 
##     maxO3v, data = ozone, method = "forward")
## 7 Variables  (and intercept)
##        Forced in Forced out
## T9         FALSE      FALSE
## T12        FALSE      FALSE
## T15        FALSE      FALSE
## Ne9        FALSE      FALSE
## Ne12       FALSE      FALSE
## Ne15       FALSE      FALSE
## maxO3v     FALSE      FALSE
## 1 subsets of each size up to 7
## Selection Algorithm: forward
##          T9  T12 T15 Ne9 Ne12 Ne15 maxO3v
## 1  ( 1 ) " " "*" " " " " " "  " "  " "   
## 2  ( 1 ) " " "*" " " " " " "  " "  "*"   
## 3  ( 1 ) " " "*" " " "*" " "  " "  "*"   
## 4  ( 1 ) "*" "*" " " "*" " "  " "  "*"   
## 5  ( 1 ) "*" "*" "*" "*" " "  " "  "*"   
## 6  ( 1 ) "*" "*" "*" "*" "*"  " "  "*"   
## 7  ( 1 ) "*" "*" "*" "*" "*"  "*"  "*"
plot(regselect2)
plot(regselect2,scale="bic")

plot(regselect2,scale="Cp")

plot(regselect2,scale="adjr2")

4.3 Predictive power of reg_compl

We partition the sample, estimate the model on 80% of the data set and calculate the forecast error on the 20% left.

n <- dim(ozone)[1]
sel <- sample(1:n,round(n*0.8))
ozone_estim <- ozone[sel,]
ozone_pred <- ozone[-sel,]
reg_compl_tronc <- lm(maxO3~T12+Ne9+maxO3v-1,data=ozone_estim)
summary(reg_compl_tronc)
## 
## Call:
## lm(formula = maxO3 ~ T12 + Ne9 + maxO3v - 1, data = ozone_estim)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -56.030  -7.660  -0.101   8.758  39.649 
## 
## Coefficients:
##        Estimate Std. Error t value Pr(>|t|)    
## T12      3.0902     0.2963  10.429  < 2e-16 ***
## Ne9     -2.2288     0.4784  -4.659 1.14e-05 ***
## maxO3v   0.3909     0.0648   6.033 3.83e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.99 on 87 degrees of freedom
## Multiple R-squared:  0.979,  Adjusted R-squared:  0.9782 
## F-statistic:  1350 on 3 and 87 DF,  p-value: < 2.2e-16
pred <- predict(reg_compl_tronc,ozone_pred)
res_pred <- ozone_pred$maxO3-pred

We can use for example the RMSE (Root Mean Square Error): \[ \operatorname{RMSE}=\sqrt{\frac{1}{n}\sum_{i=1}^{n}\left( y_{i}-\widehat{y}_{i}\right) ^{2}}. \]

or the MAPE (Mean Average Percentage Error): \[ \operatorname{MAPE}=\frac{1}{n}\sum_{i=1}^{n}\left\vert\frac{y_{i}-\widehat{y}_{i}}{y_{i}}\right\vert\times100. \]

rmse <- sqrt(mean((res_pred)^2))
round(rmse,digits=2)
## [1] 15.42
mape <- mean(abs(1-pred/ozone_pred$maxO3))*100
round(mape,digits=2)
## [1] 15.06