#Chapitre 9
require(BioStatR)
## Loading required package: BioStatR
#page 377
#Exercice 9.1
#1)
lauriers<-subset(Mesures,subset=(Mesures$espece=="laurier rose"))
plot(taille~masse,data=lauriers,pch=19)

#page 378
#3)
droite_lauriers<-lm(taille~masse,data=lauriers)
coef(droite_lauriers)
## (Intercept)       masse 
##    6.413523    1.700114
#4)
fitted(droite_lauriers)
##      181      182      183      184      185      186      187      188 
## 14.74408 16.95423 13.21398 12.02390 14.57407 15.93416 14.06404 17.12424 
##      189      190      191      192      193      194      195      196 
## 13.55400 13.04397 16.27419 14.40406 16.61421 17.46427 14.91409 15.76415 
##      197      198      199      200      201      202      203      204 
## 14.40406 16.10418 12.53393 15.59414 15.42413 14.91409 14.06404 13.89403 
##      205      206      207      208      209      210      211      212 
## 14.57407 14.06404 11.85389 14.40406 13.21398 16.27419 15.76415 13.89403 
##      213      214      215      216      217      218      219      220 
## 12.36392 13.89403 13.72401 13.38399 15.42413 14.40406 15.42413 14.40406 
##      221      222      223      224      225      226      227      228 
## 14.74408 13.38399 14.23405 14.57407 12.19391 12.19391 16.27419 14.57407 
##      229      230      231      232      233      234      235      236 
## 13.04397 12.19391 14.06404 12.02390 12.02390 12.53393 12.36392 12.87396 
##      237      238      239      240      241      242      243      244 
## 11.85389 12.87396 15.42413 16.27419 14.23405 11.85389 13.72401 11.00383 
##      245      246      247      248      249      250      251      252 
## 10.83382 10.49380 10.83382 11.85389 17.29426 12.19391 12.19391 11.00383
#page 379
#5)
abline(coef(droite_lauriers),col="red",lwd=2)

#6)
predict(droite_lauriers,(masse=4.8))
##        1 
## 14.57407
#fonctionne comme predict(droite_lauriers,list(masse=4.8))
#7)
residuals(droite_lauriers)[lauriers$masse==4.8]
##         185         205         224         228 
##  0.52592789  0.62592789 -0.07407211  0.52592789
#page 380
#8)
mean(lauriers$taille)
## [1] 13.91528
6.413523+1.700114*mean(lauriers$masse)
## [1] 13.91528
coef(droite_lauriers)[1]+coef(droite_lauriers)[2]*mean(lauriers$masse)
## (Intercept) 
##    13.91528
#9)
summary(droite_lauriers)
## 
## Call:
## lm(formula = taille ~ masse, data = lauriers)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1339 -0.8590  0.1310  0.9259  2.3060 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   6.4135     0.5924   10.83   <2e-16 ***
## masse         1.7001     0.1309   12.99   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.12 on 70 degrees of freedom
## Multiple R-squared:  0.7068, Adjusted R-squared:  0.7026 
## F-statistic: 168.8 on 1 and 70 DF,  p-value: < 2.2e-16
#page 381
#10)
anova(droite_lauriers)
## Analysis of Variance Table
## 
## Response: taille
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## masse      1 211.57 211.573  168.76 < 2.2e-16 ***
## Residuals 70  87.76   1.254                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#11)
summary(droite_lauriers)
## 
## Call:
## lm(formula = taille ~ masse, data = lauriers)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1339 -0.8590  0.1310  0.9259  2.3060 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   6.4135     0.5924   10.83   <2e-16 ***
## masse         1.7001     0.1309   12.99   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.12 on 70 degrees of freedom
## Multiple R-squared:  0.7068, Adjusted R-squared:  0.7026 
## F-statistic: 168.8 on 1 and 70 DF,  p-value: < 2.2e-16
#page 382
#12)
residus<-residuals(droite_lauriers)
shapiro.test(residus)
## 
##  Shapiro-Wilk normality test
## 
## data:  residus
## W = 0.96531, p-value = 0.04439
#page 383
plot(lauriers$masse,residus)

pdf("residusmasse.pdf")
plot(lauriers$masse,residus)
dev.off()
## quartz_off_screen 
##                 2
#Les résidus ont l'air corrects => homoscédasticité des erreurs ok et 
#absence d'effet systématique
#Approche par permutation valide

#13)
if(!("lmPerm" %in% rownames(installed.packages()))){install.packages("lmPerm")}
library(lmPerm)
lmp(taille~masse,lauriers)
## [1] "Settings:  unique SS : numeric variables centered"
## 
## Call:
## lmp(formula = taille ~ masse, data = lauriers)
## 
## Coefficients:
## (Intercept)        masse  
##       13.92         1.70
#page 384
perm_laurier<-lmp(taille~masse,lauriers,center=FALSE)
## [1] "Settings:  unique SS "
summary(perm_laurier)
## 
## Call:
## lmp(formula = taille ~ masse, data = lauriers, center = FALSE)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.1339 -0.8590  0.1309  0.9259  2.3060 
## 
## Coefficients:
##       Estimate Iter Pr(Prob)    
## masse      1.7 5000   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.12 on 70 degrees of freedom
## Multiple R-Squared: 0.7068,  Adjusted R-squared: 0.7026 
## F-statistic: 168.8 on 1 and 70 DF,  p-value: < 2.2e-16
#page 385
#14)
confint(droite_lauriers)
##                2.5 %   97.5 %
## (Intercept) 5.232099 7.594947
## masse       1.439098 1.961131
predict(droite_lauriers,list(masse=c(4.8)),interval="confidence")
##        fit      lwr      upr
## 1 14.57407 14.29212 14.85602
predict(droite_lauriers,list(masse=c(4.8)),interval="prediction")
##        fit      lwr      upr
## 1 14.57407 12.32318 16.82496
#page 386
#Exercice 9.2
#1)
bignones<-subset(Mesures5,subset=(Mesures5$espece=="bignone"))[,c(1,4)]
plot(masse~masse_sec,data=bignones,pch=19)