data("mtcars")
attach(mtcars)
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Les données ont été extraites du magazine américain Motor Trend et comprennent la consommation de carburant et 10 aspects de la conception et des performances automobiles pour 32 automobiles

DESCRIPTION [, 1] mpg Miles/(US) gallon la distance en km, qu’une voiture peut parcourir par gallon de carburant. [, 2] cyl Number of cylinders [, 3] disp Displacement (cu.in.)est le volume balayé par le déplacement d’une pièce mobile dans une chambre hermétiquement close pour un mouvement unitaire. [, 4] hp Gross horsepower puissance brute [, 5] drat Rear axle ratio Rapport de transmision pont arrière [, 6] wt Weight (1000 lbs) poid [, 7] qsec 1/4 mile time temps de parcours de 400m [, 8] vs Engine (0 = V-shaped, 1 = straight) [, 9] am Transmission (0 = automatic, 1 = manual) [,10] gear Number of forward gears Nombre de vitesses avant [,11] carb Nombre de carburateurs

plot(mtcars$mpg~mtcars$wt)

model=lm(mpg~wt, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
plot(mtcars$mpg~mtcars$wt)
abline(model,col="red",lwd=2)

passons aux diagnostique du model

plot(model)

ici l’ideal est que la ligne rouge soit le plus horizontale possible!! ce qui n’est pas encore le cas visiblement !!! mais sa n’empêche que le modèle soit expliqué a 74% le mpg

                               passons now a un model polynomial
modelP2=lm(mpg~ +wt+I(wt^2), data = mtcars)
summary(modelP2)
## 
## Call:
## lm(formula = mpg ~ +wt + I(wt^2), data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.483 -1.998 -0.773  1.462  6.238 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  49.9308     4.2113  11.856 1.21e-12 ***
## wt          -13.3803     2.5140  -5.322 1.04e-05 ***
## I(wt^2)       1.1711     0.3594   3.258  0.00286 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.651 on 29 degrees of freedom
## Multiple R-squared:  0.8191, Adjusted R-squared:  0.8066 
## F-statistic: 65.64 on 2 and 29 DF,  p-value: 1.715e-11

cool une amelioration ! Le medel explique a 80.66% le mpg daignostiquons le

plot(modelP2)

la courbe tend vers une ligne horizontale nettement mieux que la précédante

plot(mtcars$mpg~mtcars$wt)
abline(model,col="green",lwd=2)
wt.seq=seq(min(wt),max(wt), by=0.01)
mpg.predict=predict.lm(modelP2,data.frame(wt=wt.seq))
lines(wt.seq ,mpg.predict,col="red",lwd=5)

nettement mieux le vert nesparrrrr

nous continuons avec une équation d’ordre 3

modelP3=lm(mpg~wt+I(wt^2)+I(wt^3), data = mtcars)
summary(modelP3)
## 
## Call:
## lm(formula = mpg ~ wt + I(wt^2) + I(wt^3), data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.506 -1.999 -0.768  1.490  6.188 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  48.40370   15.58379   3.106  0.00431 **
## wt          -11.82598   15.46346  -0.765  0.45081   
## I(wt^2)       0.68938    4.74034   0.145  0.88541   
## I(wt^3)       0.04594    0.45070   0.102  0.91954   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.697 on 28 degrees of freedom
## Multiple R-squared:  0.8191, Adjusted R-squared:  0.7997 
## F-statistic: 42.27 on 3 and 28 DF,  p-value: 1.585e-10

oups le model d’ordre 3 explique moin bien que le precedant d’ordre 2

plot(mtcars$mpg~mtcars$wt)
abline(model,col="red",lwd=2)
wt.seq=seq(min(wt),max(wt), by=0.01)
mpg.predict=predict.lm(modelP2,data.frame(wt=wt.seq))
lines(wt.seq ,mpg.predict,col="green")


mpg.predict1=predict.lm(modelP3,data.frame(wt=wt.seq))
lines(wt.seq ,mpg.predict1,col="blue")

quasiment identique nespas !!! mais le model d’ordre 2 est plus pertinant

evoluons

modelP4=lm(mpg~poly(wt,4),data = mtcars)
summary(modelP4)
## 
## Call:
## lm(formula = mpg ~ poly(wt, 4), data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.2158 -2.0436 -0.7734  1.2710  5.7726 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   20.0906     0.4817  41.709  < 2e-16 ***
## poly(wt, 4)1 -29.1157     2.7248 -10.685 3.36e-11 ***
## poly(wt, 4)2   8.6358     2.7248   3.169  0.00378 ** 
## poly(wt, 4)3   0.2749     2.7248   0.101  0.92039    
## poly(wt, 4)4  -1.7891     2.7248  -0.657  0.51701    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.725 on 27 degrees of freedom
## Multiple R-squared:  0.822,  Adjusted R-squared:  0.7956 
## F-statistic: 31.17 on 4 and 27 DF,  p-value: 9.211e-10

toujours pas mieux

  utilisation d'anova pour la comparaison des models
  
anova(model,modelP2,modelP3,modelP4)
## Analysis of Variance Table
## 
## Model 1: mpg ~ wt
## Model 2: mpg ~ +wt + I(wt^2)
## Model 3: mpg ~ wt + I(wt^2) + I(wt^3)
## Model 4: mpg ~ poly(wt, 4)
##   Res.Df    RSS Df Sum of Sq       F   Pr(>F)   
## 1     30 278.32                                 
## 2     29 203.75  1    74.576 10.0443 0.003779 **
## 3     28 203.67  1     0.076  0.0102 0.920390   
## 4     27 200.47  1     3.201  0.4311 0.517008   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

même d’apres l’anova le model le plus significatif est le deuxième

sur ceux je vous remercie..............................................