Exemples-Sélection de Modèles
Exemples-Sélection de Modèles
Exemples-Sélection de Modèles
19 07:45
as.Date, as.Date.numeric
as.array
>
>
>
> # 1. Données de base
> # ------------------
>
> # a) Lecture des données de base
>
> Bwages <- read.csv2(file(paste(ddpath,"Bwages.csv",sep=""),encoding="latin1"),
+ header=T,sep=";", dec=".")
> head(Bwages)
wage educ exper sex
1 313.8528 1 23 1
2 194.3780 1 15 0
3 426.1364 1 31 1
4 284.0909 1 32 1
5 318.1818 1 9 1
6 330.7895 1 15 0
>
> # b) Facteurs
>
> Bwages <- transform(Bwages,
+ educ = factor(educ, labels=c("1-bas", "2", "3", "4", "5-élevé")),
+ sex = factor(sex, labels=c("Femme", "Homme"))
+ )
>
> # 2. Analyse de régression
> # ------------------------
>
> # a) Formes structurelles
>
> f0 <- formula(log(wage) ~ 1.)
> f1 <- formula(log(wage) ~ educ + exper + I(exper^2))
> f2 <- update(f1, .~. + sex)
> f3 <- update(f1, .~. + sex*educ)
> f4 <- update(f1, .~. + sex*educ + sex*exper)
>
> # a) Estimation des modèles
Page 1 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
>
> # Modèle nul
>
> lm.fit0 <- lm(f0, data = Bwages, na.action = na.exclude)
> summary(lm.fit0)
Call:
lm(formula = f0, data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.55005 -0.24094 -0.01922 0.21156 1.52792
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.031735 0.009449 638.3 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # Modèle 1
>
> lm.fit1 <- lm(f1, data = Bwages, na.action = na.exclude)
> summary(lm.fit1)
Call:
lm(formula = f1, data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.60391 -0.15586 -0.00126 0.17378 1.07186
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.262e+00 3.809e-02 138.173 < 2e-16 ***
educ2 1.412e-01 3.408e-02 4.142 3.63e-05 ***
educ3 2.967e-01 3.283e-02 9.039 < 2e-16 ***
educ4 4.529e-01 3.372e-02 13.433 < 2e-16 ***
educ5-élevé 6.290e-01 3.401e-02 18.496 < 2e-16 ***
exper 3.530e-02 2.583e-03 13.666 < 2e-16 ***
I(exper^2) -4.999e-04 6.515e-05 -7.672 3.06e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # Modèle 2
>
> lm.fit2 <- lm(f2, data = Bwages, na.action = na.exclude)
> summary(lm.fit2)
Call:
lm(formula = f2, data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.51821 -0.15173 0.01222 0.16646 1.09148
Coefficients:
Page 2 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
>
> # Modèle 3
>
> lm.fit3 <- lm(f3, data = Bwages, na.action = na.exclude)
> summary(lm.fit3)
Call:
lm(formula = f3, data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.53173 -0.15037 0.00789 0.16645 1.07623
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.067e+00 6.299e-02 80.440 < 2e-16 ***
educ2 2.156e-01 6.762e-02 3.189 0.00146 **
educ3 4.456e-01 6.298e-02 7.076 2.30e-12 ***
educ4 6.190e-01 6.247e-02 9.908 < 2e-16 ***
educ5-élevé 7.719e-01 6.404e-02 12.053 < 2e-16 ***
exper 3.465e-02 2.539e-03 13.648 < 2e-16 ***
I(exper^2) -4.979e-04 6.391e-05 -7.790 1.26e-14 ***
sexHomme 2.741e-01 6.691e-02 4.096 4.43e-05 ***
educ2:sexHomme -9.324e-02 7.756e-02 -1.202 0.22951
educ3:sexHomme -1.821e-01 7.256e-02 -2.509 0.01220 *
educ4:sexHomme -1.912e-01 7.324e-02 -2.610 0.00914 **
educ5-élevé:sexHomme -1.726e-01 7.393e-02 -2.335 0.01970 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # Modèle 4
>
> lm.fit4 <- lm(f4, data = Bwages, na.action = na.exclude)
> summary(lm.fit4)
Call:
lm(formula = f4, data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.43944 -0.15304 0.00623 0.16474 1.09383
Coefficients:
Page 3 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
>
> # b) Statistiques AIC et BIC
>
> AIC(lm.fit0, lm.fit1, lm.fit2, lm.fit3, lm.fit4)
df AIC
lm.fit0 2 1193.2707
lm.fit1 8 509.4604
lm.fit2 9 456.5910
lm.fit3 13 454.0713
lm.fit4 14 450.3493
>
> # c) Tests de modèles
>
> # Tests de significativité des coefficients du modèle 1
>
> coeftest(lm.fit1) # t-test
t test of coefficients:
>
> # Tests d'ajustement général du modèle 1
>
> waldtest(lm.fit1, test = "F")
Page 4 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
Wald test
Calls:
lm.fit1: lm(formula = f1, data = Bwages, na.action = na.exclude)
lm.fit2: lm(formula = f2, data = Bwages, na.action = na.exclude)
lm.fit3: lm(formula = f3, data = Bwages, na.action = na.exclude)
lm.fit4: lm(formula = f4, data = Bwages, na.action = na.exclude)
======================================================================================
lm.fit1 lm.fit2 lm.fit3 lm.fit4
--------------------------------------------------------------------------------------
(Intercept) 5.262*** 5.193*** 5.067*** 5.120***
(0.038) (0.039) (0.063) (0.067)
educ: 2/1-bas 0.141*** 0.142*** 0.216** 0.209**
(0.034) (0.033) (0.068) (0.068)
educ: 3/1-bas 0.297*** 0.309*** 0.446*** 0.424***
(0.033) (0.032) (0.063) (0.064)
educ: 4/1-bas 0.453*** 0.481*** 0.619*** 0.595***
(0.034) (0.033) (0.062) (0.063)
educ: 5-élevé/1-bas 0.629*** 0.641*** 0.772*** 0.745***
(0.034) (0.033) (0.064) (0.065)
exper 0.035*** 0.034*** 0.035*** 0.033***
(0.003) (0.003) (0.003) (0.003)
I(exper^2) -0.000*** -0.000*** -0.000*** -0.001***
(0.000) (0.000) (0.000) (0.000)
sex: Homme/Femme 0.115*** 0.274*** 0.180*
Page 5 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
Step: AIC=-3236.35
log(wage) ~ educ
Step: AIC=-3613.9
log(wage) ~ educ + exper
Step: AIC=-3669.89
log(wage) ~ educ + exper + I(exper^2)
Step: AIC=-3722.76
log(wage) ~ educ + exper + I(exper^2) + sex
Page 6 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
Step: AIC=-3730.87
log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex
Call:
lm(formula = log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex,
data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.41148 -0.15152 0.00854 0.16322 1.11280
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.228e+00 3.998e-02 130.752 < 2e-16 ***
educ2 1.484e-01 3.343e-02 4.440 9.65e-06 ***
educ3 3.131e-01 3.220e-02 9.723 < 2e-16 ***
educ4 4.853e-01 3.324e-02 14.598 < 2e-16 ***
educ5-élevé 6.451e-01 3.335e-02 19.340 < 2e-16 ***
exper 3.239e-02 2.607e-03 12.428 < 2e-16 ***
I(exper^2) -5.216e-04 6.443e-05 -8.096 1.18e-15 ***
sexHomme 3.515e-02 2.956e-02 1.189 0.23462
exper:sexHomme 4.864e-03 1.532e-03 3.175 0.00153 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # b) Sélection backward
>
> stepreg.fit2 <- step(lm.fit4,
+ scope = list(upper =~., lower = ~1.),
+ direction = "backward", trace=T)
Start: AIC=-3729.01
log(wage) ~ educ + exper + I(exper^2) + sex + educ:sex + exper:sex
Step: AIC=-3730.87
log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex
Page 7 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
Call:
lm(formula = log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex,
data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.41148 -0.15152 0.00854 0.16322 1.11280
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.228e+00 3.998e-02 130.752 < 2e-16 ***
educ2 1.484e-01 3.343e-02 4.440 9.65e-06 ***
educ3 3.131e-01 3.220e-02 9.723 < 2e-16 ***
educ4 4.853e-01 3.324e-02 14.598 < 2e-16 ***
educ5-élevé 6.451e-01 3.335e-02 19.340 < 2e-16 ***
exper 3.239e-02 2.607e-03 12.428 < 2e-16 ***
I(exper^2) -5.216e-04 6.443e-05 -8.096 1.18e-15 ***
sexHomme 3.515e-02 2.956e-02 1.189 0.23462
exper:sexHomme 4.864e-03 1.532e-03 3.175 0.00153 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # c) Sélection forward-backward
>
> stepreg.fit3 <- step(lm.fit0,
+ scope = ~ I(exper^2) + sex*educ + sex*exper,
+ direction = "both", trace=T)
Start: AIC=-2986.08
log(wage) ~ 1
Step: AIC=-3236.35
log(wage) ~ educ
Step: AIC=-3613.9
log(wage) ~ educ + exper
Step: AIC=-3669.89
log(wage) ~ educ + exper + I(exper^2)
Page 8 sur 9
Exemples-Sélection de modèles 04.11.19 07:45
Step: AIC=-3722.76
log(wage) ~ educ + exper + I(exper^2) + sex
Step: AIC=-3730.87
log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex
Call:
lm(formula = log(wage) ~ educ + exper + I(exper^2) + sex + exper:sex,
data = Bwages, na.action = na.exclude)
Residuals:
Min 1Q Median 3Q Max
-1.41148 -0.15152 0.00854 0.16322 1.11280
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.228e+00 3.998e-02 130.752 < 2e-16 ***
educ2 1.484e-01 3.343e-02 4.440 9.65e-06 ***
educ3 3.131e-01 3.220e-02 9.723 < 2e-16 ***
educ4 4.853e-01 3.324e-02 14.598 < 2e-16 ***
educ5-élevé 6.451e-01 3.335e-02 19.340 < 2e-16 ***
exper 3.239e-02 2.607e-03 12.428 < 2e-16 ***
I(exper^2) -5.216e-04 6.443e-05 -8.096 1.18e-15 ***
sexHomme 3.515e-02 2.956e-02 1.189 0.23462
exper:sexHomme 4.864e-03 1.532e-03 3.175 0.00153 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>
Page 9 sur 9