library("ggplot2")
library("car")
library("MASS")

assurances <- read.table("Assurances.txt", header = TRUE)
head(assurances)
##       cost age    sex    bmi children smoker    region
## 1 19151.22  19 female 27.900        0    yes southwest
## 2  9146.12  18   male 33.770        1     no southeast
## 3 11774.53  28   male 33.000        3     no southeast
## 4 11539.90  33   male 22.705        0     no northwest
## 5 11100.11  32   male 28.880        0     no northwest
## 6 10027.75  31 female 25.740        0     no southeast
dim(assurances)
## [1] 2772    7
n <- dim(assurances)[1]

assurances$sex <- factor(assurances$sex)
assurances$smoker <- factor(assurances$smoker)
assurances$region <- factor(assurances$region)

(a) Statistiques descriptives

summary(assurances)
##       cost            age            sex            bmi           children    
##  Min.   : 4776   Min.   :18.00   female:1366   Min.   :15.96   Min.   :0.000  
##  1st Qu.:10863   1st Qu.:26.00   male  :1406   1st Qu.:26.22   1st Qu.:0.000  
##  Median :12692   Median :39.00                 Median :30.45   Median :1.000  
##  Mean   :13414   Mean   :39.11                 Mean   :30.70   Mean   :1.102  
##  3rd Qu.:15116   3rd Qu.:51.00                 3rd Qu.:34.77   3rd Qu.:2.000  
##  Max.   :24812   Max.   :64.00                 Max.   :53.13   Max.   :5.000  
##  smoker           region   
##  no :2208   northeast:658  
##  yes: 564   northwest:664  
##             southeast:766  
##             southwest:684  
##                            
## 
par(mfrow = c(2,2))
hist(assurances$cost, main = "Histogramme de cost", xlab = "cost")
hist(assurances$age, main = "Histogramme de age", xlab = "age")
hist(assurances$bmi, main = "Histogramme de bmi", xlab = "bmi")
hist(assurances$children, main = "Histogramme de children", xlab = "children")

par(mfrow = c(1,1))
x <- ggplot(assurances, aes(age, cost))
x + geom_point() + theme_minimal()

x <- ggplot(assurances, aes(bmi, cost))
x + geom_point() + theme_minimal()

x <- ggplot(assurances, aes(factor(children), cost))
x + geom_boxplot()

x <- ggplot(assurances, aes(sex, cost))
x + geom_boxplot()

x <- ggplot(assurances, aes(smoker, cost))
x + geom_boxplot()

x <- ggplot(assurances, aes(region, cost))
x + geom_boxplot()

x <- ggplot(assurances, aes(x = age, y = cost, color = smoker))
x + geom_point() + theme_bw()

x <- ggplot(assurances, aes(x = bmi, y = cost, color = smoker))
x + geom_point() + theme_bw()

x <- ggplot(assurances, aes(x = smoker, y = cost, fill = sex))
x + geom_boxplot()

x <- ggplot(assurances, aes(x = bmi, y = cost, color = age))
x + geom_jitter(width = .2)

(b) Multicolinéarité

cor(assurances[, c("age", "bmi", "children")])
##                 age          bmi     children
## age      1.00000000  0.113048451  0.037574294
## bmi      0.11304845  1.000000000 -0.001492284
## children 0.03757429 -0.001492284  1.000000000
modele.additif <- lm(cost ~ age + sex + bmi + children + smoker + region, data = assurances)
vif(modele.additif)
##              GVIF Df GVIF^(1/(2*Df))
## age      1.018201  1        1.009060
## sex      1.009875  1        1.004925
## bmi      1.114274  1        1.055592
## children 1.004017  1        1.002006
## smoker   1.013467  1        1.006711
## region   1.107600  3        1.017178

(c) Sélection de modèle

modele.max <- lm(cost ~ (age + sex + bmi + children + smoker + region)^2 +
                   I(age^2) + I(bmi^2), data = assurances)
modele.min <- lm(cost ~ 1, data = assurances)

Forward

modele.forward <- step(modele.min, direction = "forward",
                       scope = list(lower = modele.min, upper = modele.max))
## Start:  AIC=45373.65
## cost ~ 1
## 
##            Df  Sum of Sq        RSS   AIC
## + smoker    1 2.3691e+10 1.1893e+10 42338
## + age       1 3.4405e+09 3.2144e+10 45094
## + I(age^2)  1 3.2512e+09 3.2333e+10 45110
## + bmi       1 1.7766e+09 3.3807e+10 45234
## + I(bmi^2)  1 1.7368e+09 3.3847e+10 45237
## + region    3 3.8036e+08 3.5204e+10 45350
## + children  1 1.4519e+08 3.5439e+10 45364
## + sex       1 8.9018e+07 3.5495e+10 45369
## <none>                   3.5584e+10 45374
## 
## Step:  AIC=42337.58
## cost ~ smoker
## 
##            Df  Sum of Sq        RSS   AIC
## + age       1 3875877459 8.0167e+09 41246
## + I(age^2)  1 3696472854 8.1961e+09 41308
## + bmi       1 1630899773 1.0262e+10 41931
## + I(bmi^2)  1 1559573048 1.0333e+10 41950
## + children  1  122554340 1.1770e+10 42311
## + region    3   60036765 1.1833e+10 42330
## + sex       1   10548460 1.1882e+10 42337
## <none>                   1.1893e+10 42338
## 
## Step:  AIC=41246.34
## cost ~ smoker + age
## 
##              Df  Sum of Sq        RSS   AIC
## + bmi         1 1125170406 6891511946 40829
## + I(bmi^2)    1 1100047751 6916634601 40839
## + children    1   76169047 7940513305 41222
## + region      3   73475038 7943207314 41227
## + I(age^2)    1   23600684 7993081668 41240
## <none>                     8016682352 41246
## + sex         1    3029821 8013652531 41247
## + age:smoker  1      21176 8016661176 41248
## 
## Step:  AIC=40829.12
## cost ~ smoker + age + bmi
## 
##              Df Sum of Sq        RSS   AIC
## + children    1  79656364 6811855581 40799
## + region      3  82417241 6809094705 40802
## + I(age^2)    1  33489720 6858022226 40818
## + sex         1  10622655 6880889291 40827
## <none>                    6891511946 40829
## + I(bmi^2)    1    463171 6891048775 40831
## + age:smoker  1    327072 6891184874 40831
## + age:bmi     1    227909 6891284037 40831
## + bmi:smoker  1     32607 6891479338 40831
## 
## Step:  AIC=40798.89
## cost ~ smoker + age + bmi + children
## 
##                   Df Sum of Sq        RSS   AIC
## + region           3  84229954 6727625627 40770
## + sex              1  11623528 6800232053 40796
## + I(age^2)         1  10799122 6801056459 40796
## + bmi:children     1   6616487 6805239095 40798
## <none>                         6811855581 40799
## + children:smoker  1   2008017 6809847565 40800
## + I(bmi^2)         1    465880 6811389701 40801
## + age:bmi          1    376625 6811478956 40801
## + age:children     1    160540 6811695041 40801
## + age:smoker       1    142977 6811712604 40801
## + bmi:smoker       1     49536 6811806046 40801
## 
## Step:  AIC=40770.4
## cost ~ smoker + age + bmi + children + region
## 
##                   Df Sum of Sq        RSS   AIC
## + sex              1  11678868 6715946758 40768
## + I(age^2)         1  10805099 6716820528 40768
## + age:region       3  16454396 6711171231 40770
## + bmi:children     1   6426404 6721199223 40770
## <none>                         6727625627 40770
## + children:smoker  1   2055236 6725570391 40772
## + I(bmi^2)         1    560929 6727064698 40772
## + age:bmi          1    255602 6727370024 40772
## + age:children     1    218765 6727406861 40772
## + bmi:smoker       1    185664 6727439963 40772
## + age:smoker       1     76977 6727548650 40772
## + bmi:region       3   4419508 6723206119 40775
## + children:region  3   3555665 6724069962 40775
## + smoker:region    3   3307185 6724318442 40775
## 
## Step:  AIC=40767.58
## cost ~ smoker + age + bmi + children + region + sex
## 
##                   Df Sum of Sq        RSS   AIC
## + I(age^2)         1  10706404 6705240354 40765
## + age:region       3  16458255 6699488503 40767
## + sex:children     1   6702878 6709243880 40767
## + bmi:children     1   6332963 6709613796 40767
## <none>                         6715946758 40768
## + children:smoker  1   1804517 6714142242 40769
## + age:sex          1   1527456 6714419303 40769
## + sex:smoker       1   1069158 6714877600 40769
## + sex:bmi          1    702893 6715243865 40769
## + I(bmi^2)         1    569345 6715377414 40769
## + sex:region       3  10218111 6705728647 40769
## + bmi:smoker       1    400226 6715546533 40769
## + age:children     1    362374 6715584384 40769
## + age:bmi          1    172976 6715773783 40770
## + age:smoker       1    102605 6715844153 40770
## + bmi:region       3   5026808 6710919950 40772
## + children:region  3   3622084 6712324675 40772
## + smoker:region    3   3212186 6712734573 40772
## 
## Step:  AIC=40765.16
## cost ~ smoker + age + bmi + children + region + sex + I(age^2)
## 
##                   Df Sum of Sq        RSS   AIC
## + age:region       3  16607736 6688632618 40764
## + bmi:children     1   6295927 6698944427 40765
## + sex:children     1   6251027 6698989327 40765
## <none>                         6705240354 40765
## + age:sex          1   1796303 6703444051 40766
## + children:smoker  1   1713131 6703527223 40766
## + sex:smoker       1    989019 6704251335 40767
## + sex:bmi          1    597242 6704643112 40767
## + sex:region       3  10241153 6694999201 40767
## + I(bmi^2)         1    517594 6704722760 40767
## + bmi:smoker       1    444037 6704796317 40767
## + age:children     1    319850 6704920504 40767
## + age:smoker       1    137261 6705103093 40767
## + age:bmi          1      2835 6705237519 40767
## + bmi:region       3   5087974 6700152380 40769
## + children:region  3   3269768 6701970586 40770
## + smoker:region    3   3050312 6702190042 40770
## 
## Step:  AIC=40764.28
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region
## 
##                   Df Sum of Sq        RSS   AIC
## + sex:children     1   5458301 6683174316 40764
## + bmi:children     1   5357034 6683275584 40764
## <none>                         6688632618 40764
## + age:sex          1   1960958 6686671660 40765
## + children:smoker  1   1194467 6687438150 40766
## + sex:region       3  10621897 6678010721 40766
## + age:bmi          1    827352 6687805265 40766
## + sex:smoker       1    757153 6687875464 40766
## + I(bmi^2)         1    703769 6687928849 40766
## + sex:bmi          1    603617 6688029000 40766
## + bmi:smoker       1    462102 6688170515 40766
## + age:children     1    420084 6688212534 40766
## + age:smoker       1     12118 6688620500 40766
## + bmi:region       3   5215443 6683417175 40768
## + smoker:region    3   3334816 6685297802 40769
## + children:region  3   3157028 6685475590 40769
## 
## Step:  AIC=40764.02
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex
## 
##                   Df Sum of Sq        RSS   AIC
## + bmi:children     1   5466478 6677707838 40764
## <none>                         6683174316 40764
## + age:sex          1   2227548 6680946769 40765
## + children:smoker  1   1599552 6681574764 40765
## + sex:region       3  10835239 6672339078 40766
## + sex:smoker       1    758652 6682415664 40766
## + age:bmi          1    715036 6682459280 40766
## + sex:bmi          1    619207 6682555110 40766
## + I(bmi^2)         1    576277 6682598040 40766
## + bmi:smoker       1    346936 6682827380 40766
## + age:children     1    238658 6682935658 40766
## + age:smoker       1      1317 6683173000 40766
## + bmi:region       3   5454294 6677720023 40768
## + smoker:region    3   3503685 6679670631 40769
## + children:region  3   3019454 6680154862 40769
## 
## Step:  AIC=40763.75
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex + bmi:children
## 
##                   Df Sum of Sq        RSS   AIC
## <none>                         6677707838 40764
## + age:sex          1   2034618 6675673220 40765
## + children:smoker  1   1464410 6676243428 40765
## + age:bmi          1    875496 6676832343 40765
## + sex:region       3  10432510 6667275329 40765
## + sex:bmi          1    735957 6676971882 40765
## + age:children     1    720578 6676987260 40765
## + I(bmi^2)         1    697462 6677010377 40765
## + sex:smoker       1    600846 6677106992 40766
## + bmi:smoker       1    195681 6677512158 40766
## + age:smoker       1        48 6677707790 40766
## + bmi:region       3   5713777 6671994062 40767
## + children:region  3   3446755 6674261084 40768
## + smoker:region    3   3240470 6674467369 40768

Backward

modele.backward <- step(modele.max, direction = "backward",
                        scope = list(lower = modele.min, upper = modele.max))
## Start:  AIC=40792.52
## cost ~ (age + sex + bmi + children + smoker + region)^2 + I(age^2) + 
##     I(bmi^2)
## 
##                   Df Sum of Sq        RSS   AIC
## - smoker:region    3   2524721 6648435354 40788
## - children:region  3   3708221 6649618854 40788
## - bmi:region       3   8322979 6654233612 40790
## - sex:region       3   9495513 6655406145 40790
## - age:smoker       1      9478 6645920111 40791
## - bmi:smoker       1    184342 6646094975 40791
## - sex:smoker       1    483846 6646394479 40791
## - age:bmi          1    545208 6646455841 40791
## - sex:bmi          1    670474 6646581107 40791
## - age:children     1    807910 6646718543 40791
## - children:smoker  1   1238051 6647148684 40791
## - age:sex          1   1868026 6647778659 40791
## - I(bmi^2)         1   3854094 6649764727 40792
## <none>                         6645910633 40793
## - sex:children     1   5982153 6651892786 40793
## - age:region       3  15687128 6661597761 40793
## - bmi:children     1   6150521 6652061154 40793
## - I(age^2)         1   8951735 6654862368 40794
## 
## Step:  AIC=40787.58
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:smoker + 
##     age:region + sex:bmi + sex:children + sex:smoker + sex:region + 
##     bmi:children + bmi:smoker + bmi:region + children:smoker + 
##     children:region
## 
##                   Df Sum of Sq        RSS   AIC
## - children:region  3   3687608 6652122962 40783
## - bmi:region       3   7890397 6656325751 40785
## - bmi:smoker       1     25472 6648460826 40786
## - age:smoker       1     33012 6648468366 40786
## - sex:smoker       1    444056 6648879410 40786
## - age:bmi          1    545075 6648980429 40786
## - sex:bmi          1    656111 6649091465 40786
## - sex:region       3  10460791 6658896145 40786
## - age:children     1    996207 6649431561 40786
## - children:smoker  1   1474156 6649909510 40786
## - age:sex          1   1707922 6650143276 40786
## - I(bmi^2)         1   3562028 6651997382 40787
## <none>                         6648435354 40788
## - sex:children     1   5788502 6654223856 40788
## - age:region       3  15484148 6663919502 40788
## - bmi:children     1   6327563 6654762917 40788
## - I(age^2)         1   9147061 6657582415 40789
## 
## Step:  AIC=40783.11
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:smoker + 
##     age:region + sex:bmi + sex:children + sex:smoker + sex:region + 
##     bmi:children + bmi:smoker + bmi:region + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - bmi:region       3   7637876 6659760838 40780
## - bmi:smoker       1      7452 6652130414 40781
## - age:smoker       1     34399 6652157361 40781
## - sex:smoker       1    398046 6652521008 40781
## - age:bmi          1    459717 6652582680 40781
## - sex:bmi          1    695382 6652818344 40781
## - age:children     1    862162 6652985124 40781
## - sex:region       3  10634529 6662757491 40782
## - children:smoker  1   1474290 6653597252 40782
## - age:sex          1   1741549 6653864511 40782
## - I(bmi^2)         1   3442122 6655565084 40783
## <none>                         6652122962 40783
## - age:region       3  15356662 6667479624 40784
## - sex:children     1   5867747 6657990709 40784
## - bmi:children     1   5974080 6658097042 40784
## - I(age^2)         1   9636136 6661759099 40785
## 
## Step:  AIC=40780.29
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:smoker + 
##     age:region + sex:bmi + sex:children + sex:smoker + sex:region + 
##     bmi:children + bmi:smoker + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - age:smoker       1     26400 6659787238 40778
## - bmi:smoker       1     26757 6659787595 40778
## - sex:smoker       1    359816 6660120654 40778
## - age:bmi          1    559100 6660319938 40779
## - I(bmi^2)         1    777839 6660538677 40779
## - sex:region       3  10632442 6670393280 40779
## - age:children     1   1028276 6660789113 40779
## - sex:bmi          1   1070641 6660831479 40779
## - children:smoker  1   1623907 6661384745 40779
## - age:sex          1   2103459 6661864297 40779
## <none>                         6659760838 40780
## - bmi:children     1   5481030 6665241868 40781
## - sex:children     1   5729510 6665490348 40781
## - age:region       3  15373696 6675134534 40781
## - I(age^2)         1   9578883 6669339721 40782
## 
## Step:  AIC=40778.3
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:region + 
##     sex:bmi + sex:children + sex:smoker + sex:region + bmi:children + 
##     bmi:smoker + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - bmi:smoker       1     31707 6659818945 40776
## - sex:smoker       1    355050 6660142288 40776
## - age:bmi          1    558882 6660346121 40777
## - I(bmi^2)         1    782063 6660569301 40777
## - age:children     1   1019028 6660806266 40777
## - sex:region       3  10664570 6670451808 40777
## - sex:bmi          1   1056104 6660843342 40777
## - children:smoker  1   1601002 6661388240 40777
## - age:sex          1   2080136 6661867374 40777
## <none>                         6659787238 40778
## - bmi:children     1   5485767 6665273005 40779
## - sex:children     1   5750152 6665537390 40779
## - age:region       3  15528520 6675315759 40779
## - I(age^2)         1   9565771 6669353009 40780
## 
## Step:  AIC=40776.32
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:region + 
##     sex:bmi + sex:children + sex:smoker + sex:region + bmi:children + 
##     children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - sex:smoker       1    389251 6660208196 40774
## - age:bmi          1    567003 6660385948 40775
## - I(bmi^2)         1    773312 6660592257 40775
## - age:children     1   1019320 6660838265 40775
## - sex:bmi          1   1067401 6660886346 40775
## - sex:region       3  10694004 6670512949 40775
## - children:smoker  1   1611300 6661430245 40775
## - age:sex          1   2099258 6661918204 40775
## <none>                         6659818945 40776
## - bmi:children     1   5551326 6665370272 40777
## - sex:children     1   5798429 6665617374 40777
## - age:region       3  15518917 6675337863 40777
## - I(age^2)         1   9550190 6669369136 40778
## 
## Step:  AIC=40774.48
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:bmi + age:children + age:region + 
##     sex:bmi + sex:children + sex:region + bmi:children + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - age:bmi          1    609977 6660818173 40773
## - I(bmi^2)         1    846995 6661055191 40773
## - age:children     1    991897 6661200093 40773
## - sex:bmi          1   1047148 6661255344 40773
## - sex:region       3  10862029 6671070225 40773
## - children:smoker  1   1512221 6661720418 40773
## - age:sex          1   2140364 6662348560 40773
## <none>                         6660208196 40774
## - bmi:children     1   5685307 6665893503 40775
## - sex:children     1   5791743 6665999939 40775
## - age:region       3  15762924 6675971120 40775
## - I(age^2)         1   9583625 6669791821 40776
## 
## Step:  AIC=40772.73
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     I(bmi^2) + age:sex + age:children + age:region + sex:bmi + 
##     sex:children + sex:region + bmi:children + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - I(bmi^2)         1   1005174 6661823347 40771
## - age:children     1   1010046 6661828219 40771
## - sex:bmi          1   1121198 6661939371 40771
## - sex:region       3  10758118 6671576291 40771
## - children:smoker  1   1561686 6662379859 40771
## - age:sex          1   2236729 6663054902 40772
## <none>                         6660818173 40773
## - age:region       3  15156008 6675974181 40773
## - bmi:children     1   5570846 6666389019 40773
## - sex:children     1   5899326 6666717499 40773
## - I(age^2)         1  10299298 6671117472 40775
## 
## Step:  AIC=40771.15
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:sex + age:children + age:region + sex:bmi + sex:children + 
##     sex:region + bmi:children + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - age:children     1    953915 6662777262 40770
## - sex:bmi          1    972936 6662796284 40770
## - sex:region       3  10610400 6672433747 40770
## - children:smoker  1   1694625 6663517973 40770
## - age:sex          1   2159152 6663982499 40770
## <none>                         6661823347 40771
## - age:region       3  14903568 6676726915 40771
## - bmi:children     1   5397290 6667220638 40771
## - sex:children     1   6089842 6667913189 40772
## - I(age^2)         1  10366685 6672190032 40773
## 
## Step:  AIC=40769.55
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:sex + age:region + sex:bmi + sex:children + sex:region + 
##     bmi:children + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - sex:region       3  10389810 6673167072 40768
## - sex:bmi          1   1020139 6663797402 40768
## - children:smoker  1   1647399 6664424661 40768
## - age:sex          1   2144478 6664921741 40768
## <none>                         6662777262 40770
## - bmi:children     1   4855934 6667633196 40770
## - age:region       3  14791249 6677568512 40770
## - sex:children     1   6451045 6669228308 40770
## - I(age^2)         1  10420483 6673197746 40772
## 
## Step:  AIC=40767.87
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:sex + age:region + sex:bmi + sex:children + bmi:children + 
##     children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - sex:bmi          1   1141367 6674308439 40766
## - children:smoker  1   1453870 6674620942 40766
## - age:sex          1   2256073 6675423145 40767
## <none>                         6673167072 40768
## - age:region       3  14501199 6687668271 40768
## - bmi:children     1   5274893 6678441965 40768
## - sex:children     1   6258866 6679425938 40768
## - I(age^2)         1  10412481 6683579553 40770
## 
## Step:  AIC=40766.34
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:sex + age:region + sex:children + bmi:children + children:smoker
## 
##                   Df Sum of Sq        RSS   AIC
## - children:smoker  1   1364781 6675673220 40765
## - age:sex          1   1934989 6676243428 40765
## <none>                         6674308439 40766
## - age:region       3  14496428 6688804867 40766
## - bmi:children     1   5150037 6679458476 40766
## - sex:children     1   6198272 6680506711 40767
## - I(age^2)         1  10539606 6684848045 40769
## 
## Step:  AIC=40764.91
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:sex + age:region + sex:children + bmi:children
## 
##                Df  Sum of Sq        RSS   AIC
## - age:sex       1 2.0346e+06 6.6777e+09 40764
## <none>                       6.6757e+09 40765
## - bmi:children  1 5.2735e+06 6.6809e+09 40765
## - age:region    3 1.5055e+07 6.6907e+09 40765
## - sex:children  1 5.8227e+06 6.6815e+09 40765
## - I(age^2)      1 1.0642e+07 6.6863e+09 40767
## - smoker        1 2.3560e+10 3.0235e+10 44950
## 
## Step:  AIC=40763.75
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:region + sex:children + bmi:children
## 
##                Df  Sum of Sq        RSS   AIC
## <none>                       6.6777e+09 40764
## - age:region    3 1.4890e+07 6.6926e+09 40764
## - bmi:children  1 5.4665e+06 6.6832e+09 40764
## - sex:children  1 5.5677e+06 6.6833e+09 40764
## - I(age^2)      1 1.0372e+07 6.6881e+09 40766
## - smoker        1 2.3558e+10 3.0236e+10 44948

Stepwise

modele.stepwise <- step(modele.min, direction = "both",
                        scope = list(lower = modele.min, upper = modele.max))
## Start:  AIC=45373.65
## cost ~ 1
## 
##            Df  Sum of Sq        RSS   AIC
## + smoker    1 2.3691e+10 1.1893e+10 42338
## + age       1 3.4405e+09 3.2144e+10 45094
## + I(age^2)  1 3.2512e+09 3.2333e+10 45110
## + bmi       1 1.7766e+09 3.3807e+10 45234
## + I(bmi^2)  1 1.7368e+09 3.3847e+10 45237
## + region    3 3.8036e+08 3.5204e+10 45350
## + children  1 1.4519e+08 3.5439e+10 45364
## + sex       1 8.9018e+07 3.5495e+10 45369
## <none>                   3.5584e+10 45374
## 
## Step:  AIC=42337.58
## cost ~ smoker
## 
##            Df  Sum of Sq        RSS   AIC
## + age       1 3.8759e+09 8.0167e+09 41246
## + I(age^2)  1 3.6965e+09 8.1961e+09 41308
## + bmi       1 1.6309e+09 1.0262e+10 41931
## + I(bmi^2)  1 1.5596e+09 1.0333e+10 41950
## + children  1 1.2255e+08 1.1770e+10 42311
## + region    3 6.0037e+07 1.1833e+10 42330
## + sex       1 1.0548e+07 1.1882e+10 42337
## <none>                   1.1893e+10 42338
## - smoker    1 2.3691e+10 3.5584e+10 45374
## 
## Step:  AIC=41246.34
## cost ~ smoker + age
## 
##              Df  Sum of Sq        RSS   AIC
## + bmi         1 1.1252e+09 6.8915e+09 40829
## + I(bmi^2)    1 1.1000e+09 6.9166e+09 40839
## + children    1 7.6169e+07 7.9405e+09 41222
## + region      3 7.3475e+07 7.9432e+09 41227
## + I(age^2)    1 2.3601e+07 7.9931e+09 41240
## <none>                     8.0167e+09 41246
## + sex         1 3.0298e+06 8.0137e+09 41247
## + age:smoker  1 2.1176e+04 8.0167e+09 41248
## - age         1 3.8759e+09 1.1893e+10 42338
## - smoker      1 2.4127e+10 3.2144e+10 45094
## 
## Step:  AIC=40829.12
## cost ~ smoker + age + bmi
## 
##              Df  Sum of Sq        RSS   AIC
## + children    1 7.9656e+07 6.8119e+09 40799
## + region      3 8.2417e+07 6.8091e+09 40802
## + I(age^2)    1 3.3490e+07 6.8580e+09 40818
## + sex         1 1.0623e+07 6.8809e+09 40827
## <none>                     6.8915e+09 40829
## + I(bmi^2)    1 4.6317e+05 6.8910e+09 40831
## + age:smoker  1 3.2707e+05 6.8912e+09 40831
## + age:bmi     1 2.2791e+05 6.8913e+09 40831
## + bmi:smoker  1 3.2607e+04 6.8915e+09 40831
## - bmi         1 1.1252e+09 8.0167e+09 41246
## - age         1 3.3701e+09 1.0262e+10 41931
## - smoker      1 2.3974e+10 3.0866e+10 44983
## 
## Step:  AIC=40798.89
## cost ~ smoker + age + bmi + children
## 
##                   Df  Sum of Sq        RSS   AIC
## + region           3 8.4230e+07 6.7276e+09 40770
## + sex              1 1.1624e+07 6.8002e+09 40796
## + I(age^2)         1 1.0799e+07 6.8011e+09 40796
## + bmi:children     1 6.6165e+06 6.8052e+09 40798
## <none>                          6.8119e+09 40799
## + children:smoker  1 2.0080e+06 6.8098e+09 40800
## + I(bmi^2)         1 4.6588e+05 6.8114e+09 40801
## + age:bmi          1 3.7662e+05 6.8115e+09 40801
## + age:children     1 1.6054e+05 6.8117e+09 40801
## + age:smoker       1 1.4298e+05 6.8117e+09 40801
## + bmi:smoker       1 4.9536e+04 6.8118e+09 40801
## - children         1 7.9656e+07 6.8915e+09 40829
## - bmi              1 1.1287e+09 7.9405e+09 41222
## - age              1 3.3258e+09 1.0138e+10 41899
## - smoker           1 2.3953e+10 3.0764e+10 44976
## 
## Step:  AIC=40770.4
## cost ~ smoker + age + bmi + children + region
## 
##                   Df  Sum of Sq        RSS   AIC
## + sex              1 1.1679e+07 6.7159e+09 40768
## + I(age^2)         1 1.0805e+07 6.7168e+09 40768
## + age:region       3 1.6454e+07 6.7112e+09 40770
## + bmi:children     1 6.4264e+06 6.7212e+09 40770
## <none>                          6.7276e+09 40770
## + children:smoker  1 2.0552e+06 6.7256e+09 40772
## + I(bmi^2)         1 5.6093e+05 6.7271e+09 40772
## + age:bmi          1 2.5560e+05 6.7274e+09 40772
## + age:children     1 2.1876e+05 6.7274e+09 40772
## + bmi:smoker       1 1.8566e+05 6.7274e+09 40772
## + age:smoker       1 7.6977e+04 6.7275e+09 40772
## + bmi:region       3 4.4195e+06 6.7232e+09 40775
## + children:region  3 3.5557e+06 6.7241e+09 40775
## + smoker:region    3 3.3072e+06 6.7243e+09 40775
## - region           3 8.4230e+07 6.8119e+09 40799
## - children         1 8.1469e+07 6.8091e+09 40802
## - bmi              1 1.1332e+09 7.8609e+09 41200
## - age              1 3.2966e+09 1.0024e+10 41874
## - smoker           1 2.3801e+10 3.0528e+10 44961
## 
## Step:  AIC=40767.58
## cost ~ smoker + age + bmi + children + region + sex
## 
##                   Df  Sum of Sq        RSS   AIC
## + I(age^2)         1 1.0706e+07 6.7052e+09 40765
## + age:region       3 1.6458e+07 6.6995e+09 40767
## + sex:children     1 6.7029e+06 6.7092e+09 40767
## + bmi:children     1 6.3330e+06 6.7096e+09 40767
## <none>                          6.7159e+09 40768
## + children:smoker  1 1.8045e+06 6.7141e+09 40769
## + age:sex          1 1.5275e+06 6.7144e+09 40769
## + sex:smoker       1 1.0692e+06 6.7149e+09 40769
## + sex:bmi          1 7.0289e+05 6.7152e+09 40769
## + I(bmi^2)         1 5.6934e+05 6.7154e+09 40769
## + sex:region       3 1.0218e+07 6.7057e+09 40769
## + bmi:smoker       1 4.0023e+05 6.7155e+09 40769
## + age:children     1 3.6237e+05 6.7156e+09 40769
## + age:bmi          1 1.7298e+05 6.7158e+09 40770
## + age:smoker       1 1.0260e+05 6.7158e+09 40770
## - sex              1 1.1679e+07 6.7276e+09 40770
## + bmi:region       3 5.0268e+06 6.7109e+09 40772
## + children:region  3 3.6221e+06 6.7123e+09 40772
## + smoker:region    3 3.2122e+06 6.7127e+09 40772
## - region           3 8.4285e+07 6.8002e+09 40796
## - children         1 8.2508e+07 6.7985e+09 40799
## - bmi              1 1.1410e+09 7.8569e+09 41201
## - age              1 3.2821e+09 9.9980e+09 41869
## - smoker           1 2.3731e+10 3.0447e+10 44956
## 
## Step:  AIC=40765.16
## cost ~ smoker + age + bmi + children + region + sex + I(age^2)
## 
##                   Df  Sum of Sq        RSS   AIC
## + age:region       3 1.6608e+07 6.6886e+09 40764
## + bmi:children     1 6.2959e+06 6.6989e+09 40765
## + sex:children     1 6.2510e+06 6.6990e+09 40765
## <none>                          6.7052e+09 40765
## + age:sex          1 1.7963e+06 6.7034e+09 40766
## + children:smoker  1 1.7131e+06 6.7035e+09 40766
## + sex:smoker       1 9.8902e+05 6.7043e+09 40767
## + sex:bmi          1 5.9724e+05 6.7046e+09 40767
## + sex:region       3 1.0241e+07 6.6950e+09 40767
## + I(bmi^2)         1 5.1759e+05 6.7047e+09 40767
## + bmi:smoker       1 4.4404e+05 6.7048e+09 40767
## + age:children     1 3.1985e+05 6.7049e+09 40767
## + age:smoker       1 1.3726e+05 6.7051e+09 40767
## + age:bmi          1 2.8350e+03 6.7052e+09 40767
## - I(age^2)         1 1.0706e+07 6.7159e+09 40768
## - sex              1 1.1580e+07 6.7168e+09 40768
## + bmi:region       3 5.0880e+06 6.7002e+09 40769
## + children:region  3 3.2698e+06 6.7020e+09 40770
## + smoker:region    3 3.0503e+06 6.7022e+09 40770
## - children         1 5.9336e+07 6.7646e+09 40788
## - region           3 8.4288e+07 6.7895e+09 40794
## - age              1 1.3462e+08 6.8399e+09 40818
## - bmi              1 1.1457e+09 7.8510e+09 41200
## - smoker           1 2.3721e+10 3.0427e+10 44956
## 
## Step:  AIC=40764.28
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region
## 
##                   Df  Sum of Sq        RSS   AIC
## + sex:children     1 5.4583e+06 6.6832e+09 40764
## + bmi:children     1 5.3570e+06 6.6833e+09 40764
## <none>                          6.6886e+09 40764
## - age:region       3 1.6608e+07 6.7052e+09 40765
## + age:sex          1 1.9610e+06 6.6867e+09 40765
## + children:smoker  1 1.1945e+06 6.6874e+09 40766
## + sex:region       3 1.0622e+07 6.6780e+09 40766
## + age:bmi          1 8.2735e+05 6.6878e+09 40766
## + sex:smoker       1 7.5715e+05 6.6879e+09 40766
## + I(bmi^2)         1 7.0377e+05 6.6879e+09 40766
## + sex:bmi          1 6.0362e+05 6.6880e+09 40766
## + bmi:smoker       1 4.6210e+05 6.6882e+09 40766
## + age:children     1 4.2008e+05 6.6882e+09 40766
## + age:smoker       1 1.2118e+04 6.6886e+09 40766
## - I(age^2)         1 1.0856e+07 6.6995e+09 40767
## - sex              1 1.1583e+07 6.7002e+09 40767
## + bmi:region       3 5.2154e+06 6.6834e+09 40768
## + smoker:region    3 3.3348e+06 6.6853e+09 40769
## + children:region  3 3.1570e+06 6.6855e+09 40769
## - children         1 5.6050e+07 6.7447e+09 40785
## - bmi              1 1.1581e+09 7.8467e+09 41205
## - smoker           1 2.3583e+10 3.0271e+10 44947
## 
## Step:  AIC=40764.02
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex
## 
##                   Df  Sum of Sq        RSS   AIC
## + bmi:children     1 5.4665e+06 6.6777e+09 40764
## <none>                          6.6832e+09 40764
## - children:sex     1 5.4583e+06 6.6886e+09 40764
## - age:region       3 1.5815e+07 6.6990e+09 40765
## + age:sex          1 2.2275e+06 6.6809e+09 40765
## + children:smoker  1 1.5996e+06 6.6816e+09 40765
## + sex:region       3 1.0835e+07 6.6723e+09 40766
## + sex:smoker       1 7.5865e+05 6.6824e+09 40766
## + age:bmi          1 7.1504e+05 6.6825e+09 40766
## + sex:bmi          1 6.1921e+05 6.6826e+09 40766
## + I(bmi^2)         1 5.7628e+05 6.6826e+09 40766
## + bmi:smoker       1 3.4694e+05 6.6828e+09 40766
## + age:children     1 2.3866e+05 6.6829e+09 40766
## + age:smoker       1 1.3170e+03 6.6832e+09 40766
## - I(age^2)         1 1.0415e+07 6.6936e+09 40766
## + bmi:region       3 5.4543e+06 6.6777e+09 40768
## + smoker:region    3 3.5037e+06 6.6797e+09 40769
## + children:region  3 3.0195e+06 6.6802e+09 40769
## - bmi              1 1.1579e+09 7.8411e+09 41205
## - smoker           1 2.3555e+10 3.0239e+10 44946
## 
## Step:  AIC=40763.75
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex + bmi:children
## 
##                   Df  Sum of Sq        RSS   AIC
## <none>                          6.6777e+09 40764
## - age:region       3 1.4890e+07 6.6926e+09 40764
## - bmi:children     1 5.4665e+06 6.6832e+09 40764
## - children:sex     1 5.5677e+06 6.6833e+09 40764
## + age:sex          1 2.0346e+06 6.6757e+09 40765
## + children:smoker  1 1.4644e+06 6.6762e+09 40765
## + age:bmi          1 8.7550e+05 6.6768e+09 40765
## + sex:region       3 1.0433e+07 6.6673e+09 40765
## + sex:bmi          1 7.3596e+05 6.6770e+09 40765
## + age:children     1 7.2058e+05 6.6770e+09 40765
## + I(bmi^2)         1 6.9746e+05 6.6770e+09 40765
## + sex:smoker       1 6.0085e+05 6.6771e+09 40766
## + bmi:smoker       1 1.9568e+05 6.6775e+09 40766
## + age:smoker       1 4.8000e+01 6.6777e+09 40766
## - I(age^2)         1 1.0372e+07 6.6881e+09 40766
## + bmi:region       3 5.7138e+06 6.6720e+09 40767
## + children:region  3 3.4468e+06 6.6743e+09 40768
## + smoker:region    3 3.2405e+06 6.6745e+09 40768
## - smoker           1 2.3558e+10 3.0236e+10 44948
formula(modele.forward)
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex + bmi:children
formula(modele.backward)
## cost ~ age + sex + bmi + children + smoker + region + I(age^2) + 
##     age:region + sex:children + bmi:children
formula(modele.stepwise)
## cost ~ smoker + age + bmi + children + region + sex + I(age^2) + 
##     age:region + children:sex + bmi:children
extractAIC(modele.forward, k = 2)[2]
## [1] 40763.75
extractAIC(modele.backward, k = 2)[2]
## [1] 40763.75
extractAIC(modele.stepwise, k = 2)[2]
## [1] 40763.75
modele.retenu <- modele.stepwise
summary(modele.retenu)
## 
## Call:
## lm(formula = cost ~ smoker + age + bmi + children + region + 
##     sex + I(age^2) + age:region + children:sex + bmi:children, 
##     data = assurances)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5841.3 -1057.0    47.2  1081.0  6638.7 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         5562.9316   361.0265  15.409  < 2e-16 ***
## smokeryes           7303.7768    74.0585  98.622  < 2e-16 ***
## age                  102.1166    14.9362   6.837 9.93e-12 ***
## bmi                  104.8036     6.8140  15.381  < 2e-16 ***
## children            -100.5789   128.7842  -0.781  0.43488    
## regionnorthwest     -411.6447   254.2379  -1.619  0.10553    
## regionsoutheast     -805.2898   245.8442  -3.276  0.00107 ** 
## regionsouthwest     -491.6113   254.0440  -1.935  0.05307 .  
## sexmale             -210.7982    80.0466  -2.633  0.00850 ** 
## I(age^2)              -0.3753     0.1814  -2.069  0.03860 *  
## age:regionnorthwest    7.7586     6.1133   1.269  0.20451    
## age:regionsoutheast   12.4697     5.8789   2.121  0.03400 *  
## age:regionsouthwest    0.7899     6.0883   0.130  0.89678    
## children:sexmale      74.0695    48.8534   1.516  0.12959    
## bmi:children           6.0949     4.0570   1.502  0.13313    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1556 on 2757 degrees of freedom
## Multiple R-squared:  0.8123, Adjusted R-squared:  0.8114 
## F-statistic: 852.5 on 14 and 2757 DF,  p-value: < 2.2e-16
p <- length(coef(modele.retenu)) - 1

(d) Résidus et leviers

h <- hatvalues(modele.retenu)
range(h)
## [1] 0.002380277 0.039741677
sum(h)
## [1] 15
residus.bruts <- resid(modele.retenu)
residus.standardises <- rstandard(modele.retenu)
residus.studentises <- rstudent(modele.retenu)

summary(cbind(residus.standardises, residus.studentises))
##  residus.standardises residus.studentises
##  Min.   :-3.76129     Min.   :-3.770296  
##  1st Qu.:-0.68154     1st Qu.:-0.681471  
##  Median : 0.03038     Median : 0.030372  
##  Mean   :-0.00002     Mean   :-0.000045  
##  3rd Qu.: 0.69724     3rd Qu.: 0.697178  
##  Max.   : 4.27915     Max.   : 4.292651
plot(h, xlab = "Numero d'observation", ylab = "Leviers")
abline(h = 3*(p+1)/n, lty = 2, col = "red")

plot(residus.standardises, xlab = "Numero d'observation", ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(h = 3, lty = 2, col = "green")
abline(h = -3, lty = 2, col = "green")

plot(residus.studentises, xlab = "Numero d'observation", ylab = "Residus studentises")
abline(h = 0, col = "red")
abline(h = 3, lty = 2, col = "green")
abline(h = -3, lty = 2, col = "green")

plot(h, residus.standardises, xlab = "Leviers", ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(v = 3*(p+1)/n, lty = 2, col = "red")

(indices.h <- which(as.vector(h) > 3*(p+1)/n))
## [1]  167  439  641 1086 1553 1825 2027 2472
which(abs(as.vector(residus.standardises)) > 3)
##  [1]   84  298  540  694  709  771 1520 1800 1979 2078 2095
which(abs(as.vector(residus.studentises)) > 3)
##  [1]   84  298  540  694  709  771 1520 1800 1979 2078 2095

(e) Validation des hypothèses

Linéarité

scatter.smooth(residus.standardises ~ assurances$age,
               xlab = "age", ylab = "Residus standardises")

scatter.smooth(residus.standardises ~ assurances$bmi,
               xlab = "bmi", ylab = "Residus standardises")

scatter.smooth(residus.standardises ~ assurances$children,
               xlab = "children", ylab = "Residus standardises")

Variance constante

plot(fitted(modele.retenu), residus.standardises,
     xlab = "Valeurs predites", ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(h = 1.96, lty = 2, col = "green")
abline(h = -1.96, lty = 2, col = "green")

Indépendance

plot(residus.standardises, xlab = "Numero d'observation",
     ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(h = 1.96, lty = 2, col = "green")
abline(h = -1.96, lty = 2, col = "green")

plot(residus.standardises[2:n], residus.standardises[1:(n-1)],
     xlab = "Residus standardises precedents", ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(v = 0, col = "red")

Normalité

hist(residus.standardises, freq = FALSE,
     xlab = "Residus standardises", ylab = "Histogramme", main = "")
axe.x <- seq(-4, 4, 0.01)
axe.densite <- dnorm(seq(-4, 4, 0.01))
lines(axe.x, axe.densite, col = "green")

qqnorm(residus.standardises, xlab = "Quantiles theoriques",
       ylab = "Quantiles empiriques", main = "")
qqline(residus.standardises)

Transformation de Box-Cox

bc <- boxcox(modele.retenu, lambda = seq(-2, 3, length = 101))

(lambda <- bc$x[which.max(bc$y)])
## [1] 1.05
assurances.trans <- assurances
assurances.trans$cost <- assurances$cost^lambda
modele.trans <- lm(formula(modele.retenu), data = assurances.trans)
summary(modele.trans)
## 
## Call:
## lm(formula = formula(modele.retenu), data = assurances.trans)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9969.3 -1774.3    67.3  1820.9 11194.1 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          8410.5650   608.4846  13.822  < 2e-16 ***
## smokeryes           12413.8213   124.8203  99.454  < 2e-16 ***
## age                   169.6470    25.1740   6.739 1.93e-11 ***
## bmi                   176.1954    11.4845  15.342  < 2e-16 ***
## children             -180.1333   217.0567  -0.830  0.40667    
## regionnorthwest      -689.4088   428.5000  -1.609  0.10776    
## regionsoutheast     -1358.5692   414.3530  -3.279  0.00106 ** 
## regionsouthwest      -815.7435   428.1731  -1.905  0.05686 .  
## sexmale              -352.5696   134.9129  -2.613  0.00902 ** 
## I(age^2)               -0.6007     0.3057  -1.965  0.04949 *  
## age:regionnorthwest    12.9859    10.3036   1.260  0.20766    
## age:regionsoutheast    21.1768     9.9085   2.137  0.03267 *  
## age:regionsouthwest     1.0006    10.2614   0.098  0.92233    
## children:sexmale      125.4042    82.3390   1.523  0.12787    
## bmi:children           10.6422     6.8379   1.556  0.11974    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2623 on 2757 degrees of freedom
## Multiple R-squared:  0.8145, Adjusted R-squared:  0.8135 
## F-statistic: 864.5 on 14 and 2757 DF,  p-value: < 2.2e-16
residus.standardises.trans <- rstandard(modele.trans)

plot(fitted(modele.trans), residus.standardises.trans,
     xlab = "Valeurs predites", ylab = "Residus standardises")
abline(h = 0, col = "red")
abline(h = 1.96, lty = 2, col = "green")
abline(h = -1.96, lty = 2, col = "green")

qqnorm(residus.standardises.trans, xlab = "Quantiles theoriques",
       ylab = "Quantiles empiriques", main = "")
qqline(residus.standardises.trans)

(f) Observations influentes

d1 <- cooks.distance(modele.retenu)
max(d1)
## [1] 0.01331256
qf(0.5, p+1, n-(p+1))
## [1] 0.9561561
indices.d1 <- which(as.vector(d1) > qf(0.5, p+1, n-(p+1)))

d2 <- dffits(modele.retenu)
max(abs(d2))
## [1] 0.447407
3*sqrt(p/(n-p))
## [1] 0.2137411
indices.d2 <- which(as.vector(abs(d2)) > 3*sqrt(p/(n-p)))

d3 <- dfbetas(modele.retenu)
max(abs(d3))
## [1] 0.3397856
3/sqrt(n)
## [1] 0.05698029
d3.matrice <- abs(d3) > 3/sqrt(n)
indices.d3 <- which(as.vector(rowSums(d3.matrice)) > 0)

d4 <- covratio(modele.retenu)
max(abs(1-d4))
## [1] 0.08443621
3*(p+1)/n
## [1] 0.01623377
indices.d4 <- which(as.vector(abs(1-d4)) > 3*(p+1)/n)

indices.d1
## integer(0)
length(indices.d2)
## [1] 23
length(indices.d3)
## [1] 320
length(indices.d4)
## [1] 105
(indices <- intersect(intersect(indices.d2, indices.d3), indices.d4))
##  [1]   26   84  298  540  694  771  781  846 1120 1520 1722 1800 1825 1874 1979
## [16] 2285
modele.sans.obs.influentes <- lm(formula(modele.retenu), data = assurances[-indices, ])

data.frame(coef.avec = coef(modele.retenu),
           coef.sans = coef(modele.sans.obs.influentes))
##                        coef.avec    coef.sans
## (Intercept)         5562.9316257 5695.0676346
## smokeryes           7303.7767506 7308.4275189
## age                  102.1166419   95.3033875
## bmi                  104.8035560  103.6809585
## children            -100.5789247 -123.9831189
## regionnorthwest     -411.6446849 -449.6914251
## regionsoutheast     -805.2898205 -792.1578668
## regionsouthwest     -491.6113102 -459.3255734
## sexmale             -210.7982092 -204.4617067
## I(age^2)              -0.3753328   -0.2881425
## age:regionnorthwest    7.7585626    8.6378602
## age:regionsoutheast   12.4696588   13.1624352
## age:regionsouthwest    0.7899005    0.7895369
## children:sexmale      74.0694828   81.7288060
## bmi:children           6.0949284    7.1702517
data.frame(s.e.avec = sqrt(diag(vcov(modele.retenu))),
           s.e.sans = sqrt(diag(vcov(modele.sans.obs.influentes))))
##                        s.e.avec    s.e.sans
## (Intercept)         361.0265001 353.0903691
## smokeryes            74.0584597  72.3837328
## age                  14.9362467  14.6139921
## bmi                   6.8139667   6.6800289
## children            128.7842498 127.7140269
## regionnorthwest     254.2379041 248.3759238
## regionsoutheast     245.8441809 240.2275719
## regionsouthwest     254.0439744 248.4840398
## sexmale              80.0466014  78.3077113
## I(age^2)              0.1813751   0.1775186
## age:regionnorthwest   6.1133351   5.9689553
## age:regionsoutheast   5.8789457   5.7485976
## age:regionsouthwest   6.0883294   5.9555948
## children:sexmale     48.8534162  47.9965851
## bmi:children          4.0570478   4.0467626