Vous êtes sur la page 1sur 5

Régression linéaire simple

1. Données et statistiques descriptive


> y<-c(-0.6,7.9,10.5,15.4,20.3,23.8,28.8,34.7,39.1,45.4)
> x<-seq(from=0,to=45,by=5)
> cor(x,y)
[1] 0.9971962

*****Test de présence de corrélation entre X et Y

> cor.test(x,y)

Pearson's product-moment correlation

data: x and y
t = 37.6914, df = 8, p-value = 2.695e-10
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.9877220 0.9993621
sample estimates:
cor
0.9971962

**** t=37.69 df=8 p-value= 2.695*10-10 <5% donc on accepte l’hypothèse


alternative H1 selon laquelle les deux variables sont corrélées.

> var(x)
[1] 229.1667
> var(y)
[1] 215.1334
> ecarttypex<-sqrt(var(x))
> ecarttypey<-sqrt(var(y))
> ecarttypex
[1] 15.13825
> ecarttypey<-sqrt(var(y))
> ecarttypey
[1] 14.66743
> plot(x,y)

1
2. Régression avec constante
> model<-lm(y~x)
> names(model)
[1] "coefficients" "residuals" "effects" "rank"
"fitted.values"
[6] "assign" "qr" "df.residual" "xlevels" "call"
[11] "terms" "model"
> coef<-model$coefficients
> coef
(Intercept) x
0.7909091 0.9661818
> resid<-model$residuals
> summary(model)
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.3909 -0.8168 0.0700 0.1682 2.2782

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.79091 0.68424 1.156 0.281
x 0.96618 0.02563 37.691 2.69e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.164 on 8 degrees of freedom
Multiple R-squared: 0.9944, Adjusted R-squared: 0.9937
F-statistic: 1421 on 1 and 8 DF, p-value: 2.695e-10
> anova<-anova(model)
> anova
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 1925.36 1925.36 1420.6 2.695e-10 ***
Residuals 8 10.84 1.36
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

*** graphique pour les prévisions et les résidus issus du modèle


> par(mfrow=c(2,2))
> plot(model)

2
> pred<-model$fitted.values
> pred
1 2 3 4 5 6 7
0.7909091 5.6218182 10.4527273 15.2836364 20.1145455 24.9454545 29.7763636
8 9 10
34.6072727 39.4381818 44.2690909
> summary(pred)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.7909 11.6600 22.5300 22.5300 33.4000 44.2700
> plot(pred)
> par(mfrow=c(1,1))
> plot(pred)

*** Test de normalité


> ks.test(resid,pnorm,0,1.164)
One-sample Kolmogorov-Smirnov test
data: resid
D = 0.2367, p-value = 0.5528
alternative hypothesis: two-sided
> ks.test(resid,pnorm,0,1.164) $p.value
[1] 0.5528367

3
3. Régression sans constante
> model1<-lm(y~x-1)
> names(model1)
[1] "coefficients" "residuals" "effects" "rank"
[5] "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
> coef<-model1$coefficients
> coef
x
0.9911579
> resid<-model1$residuals
> summary(model1)

Call:
lm(formula = y ~ x - 1)

Residuals:
Min 1Q Median 3Q Max
-0.9789 -0.5866 0.2432 0.5745 2.9442

Coefficients:
Estimate Std. Error t value Pr(>|t|)
x 0.99116 0.01405 70.56 1.17e-13 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.186 on 9 degrees of freedom


Multiple R-squared: 0.9982, Adjusted R-squared: 0.998
F-statistic: 4979 on 1 and 9 DF, p-value: 1.166e-13
> anova<-anova(model1)
> anova
Analysis of Variance Table

Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 6999.6 6999.6 4978.8 1.166e-13 ***
Residuals 9 12.7 1.4
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> par(mfrow=c(2,2))
> plot(model1)

4
> pred<-model1$fitted.values
> pred
1 2 3 4 5 6
2.142730e-14 4.955789e+00 9.911579e+00 1.486737e+01 1.982316e+01 2.477895e+01
7 8 9 10
2.973474e+01 3.469053e+01 3.964632e+01 4.460211e+01
> summary(pred)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 11.15 22.30 22.30 33.45 44.60
> plot(pred)
> par(mfrow=c(1,1))
> plot(pred)

Vous aimerez peut-être aussi