Code R.

Transcription

Code R.
chem= read.table("exemples/chemicalProcess.txt", header=T)
chem
temperature concentration conversion
1
200.00
15.00
43
2
250.00
15.00
78
3
200.00
25.00
69
4
250.00
25.00
73
5
189.65
20.00
48
6
260.35
20.00
76
7
225.00
12.93
65
8
225.00
27.07
74
9
225.00
20.00
76
10
225.00
20.00
79
11
225.00
20.00
83
12
225.00
20.00
81
pairs( chem, pch=16 )
plot( conversion ~ temperature, data=chem, pch=16 )
chem1.lm = lm( conversion ~ temperature, data=chem )
summary( chem1.lm )
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -18.0127
31.9003 -0.565
0.5848
temperature
0.3930
0.1412
2.783
0.0193 *
--Residual standard error: 9.984 on 10 degrees of freedom
Multiple R-squared: 0.4365,
Adjusted R-squared: 0.3802
F-statistic: 7.748 on 1 and 10 DF, p-value: 0.01934
##graphique de résidus
##créer une fonction pour faire ces graphiques
plotResidus= function( data.lm, titre="" )
{
par( mfrow=c(2,1) )
plot( fitted( data.lm ), resid( data.lm ), pch=16, cex=1.5, main=titre,
xlab="VALEURS PREDITES", ylab="RESIDUS" )
abline(h=0)
qqnorm( resid( data.lm ), pch=16, xlab="QUANTILES THEORIQUES",
ylab="QUANTILES EMPIRIQUES" )
qqline( resid( data.lm ) )
par( mfrow = c(1,1) )
}
plotResidus( chem1.lm )
##relation quadratique
chem1.ss = smooth.spline( chem$temperature, chem$conversion, df=5 )
plot( conversion ~ temperature, data=chem, pch=16 )
lines( spline(chem1.ss$x, chem1.ss$y), lty=2, lwd=2 )
##graphique de la variable ajoutée: concentration ajustée par température
err.conv.temp = resid( chem1.ss )
#régression de concentration en fonction de température
conc.ss = smooth.spline( chem$temperature, chem$concentration, df=5 )
plot( concentration ~ temperature, data=chem, pch=16 )
lines( spline(conc.ss$x, conc.ss$y), lty=2, lwd=2 )
err.conc.temp = resid( conc.ss )
chem2.ss = smooth.spline( err.conv.temp ~ err.conc.temp, df = 5 )
par(mfrow=c(2,2))
plot( err.conv.temp ~ err.conc.temp, pch=16 )
lines( spline(chem2.ss$x, chem2.ss$y), lty=2, lwd=2 )
##graphique de la variable ajoutée: température ajustée par concentration
chem3.ss= smooth.spline( chem$concentration, chem$conversion, df=5 )
err.conv.conc = resid( chem3.ss )
#régression de température en fonction de concentration
temp.ss = smooth.spline( chem$concentration, chem$temperature, df=5 )
err.temp.conc = resid( temp.ss )
chem4.ss = smooth.spline( err.conv.conc ~ err.temp.conc, df = 5 )
library(mgcv)
chem.mgcv = gam( conversion ~ s(temperature,k=3) + s(concentration,k=3), data= chem )
summary( chem.mgcv)
Family: gaussian
Link function: identity
Formula:
conversion ~ s(temperature, k = 3) + s(concentration, k = 3)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
70.417
1.828
38.53 9.9e-10 ***
--Approximate significance of smooth terms:
edf Ref.df
F p-value
s(temperature)
1.930 1.995 15.847 0.00195 **
s(concentration) 1.741 1.933 3.699 0.10971
--R-sq.(adj) =
GCV = 65.63
0.751
Deviance explained = 83.4%
Scale est. = 40.082
n = 12
par( mfrow=c(2,1))
plot( chem.mgcv )
####################
##qraphiques
par(mfrow=c(3,2))
plot( err.conv.conc ~ err.temp.conc, pch=16, main="VARIABLE AJOUTEE: TEMPERATURE")
lines( spline(chem4.ss$x, chem4.ss$y), lty=2, lwd=2 )
plot( err.conv.temp ~ err.conc.temp, pch=16, main="VARIABLE AJOUTEE: CONCENTRATION" )
lines( spline(chem2.ss$x, chem2.ss$y), lty=2, lwd=2 )
plot( chem.mgcv )
##ANOVA
anova(chem.mgcv)
Family: gaussian
Link function: identity
Formula:
conversion ~ s(temperature, k = 3) + s(concentration, k = 3)
Approximate significance of smooth terms:
edf Ref.df
F p-value
s(temperature)
1.930 1.995 15.847 0.00195
s(concentration) 1.741 1.933 3.699 0.10971
#chem.mgcv$null.deviance
#chem.mgcv$df.null
chem.mgcv$deviance
chem.mgcv$df.residual
chem.mgcv$deviance
[1] 293.7428
chem.mgcv$df.residual
[1] 7.328615
chem.mgcv.temp = gam( conversion ~ s(temperature, k=3), data= chem )
summary( chem.mgcv.temp)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
70.417
2.303
30.57 1.58e-10 ***
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df
F p-value
s(temperature) 1.85 1.977 9.808
0.007 **
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) =
GCV = 83.498
0.604
Deviance explained = 67.1%
Scale est. = 63.669
n = 12
chem.mgcv.temp$deviance
[1] 582.5931
chem.mgcv.temp$df.residual
[1] 9.150275
chem.mgcv.conc = gam( conversion ~ s(concentration, k=3), data= chem )
summary( chem.mgcv.conc)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
70.417
3.682
19.12 3.32e-09 ***
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df
F p-value
s(concentration)
1
1 0.874
0.372
R-sq.(adj) =
GCV = 195.21
-0.0116
Deviance explained = 8.04%
Scale est. = 162.67
n = 12
##test d'hypothèse
F.conc = ( chem.mgcv.temp$deviance - chem.mgcv$deviance)/( chem.mgcv.temp$df.residual -chem.mgcv$df.resid…
F.conc = F.conc / ( chem.mgcv$deviance / chem.mgcv$df.residual )
[1] 3.956036
##valeur-p
1 - pf( F.conc,
[1] 0.07078389
chem.mgcv.temp$df.residual -chem.mgcv$df.residual, chem.mgcv$df.residual )
F.temp = ( chem.mgcv.conc$deviance - chem.mgcv$deviance)/( chem.mgcv.conc$df.residual -chem.mgcv$df.resid…
F.temp = F.temp / ( chem.mgcv$deviance / chem.mgcv$df.residual )
[1] 12.44914
F.temp = ( chem.mgcv.conc$deviance - chem.mgcv$deviance)/( sum(chem.mgcv$edf) - sum(chem.mgcv.conc$edf))
F.temp = F.temp / ( chem.mgcv$deviance / chem.mgcv$df.residual )
[1] 12.44914
##remarque:
chem.mgcv$deviance / chem.mgcv$df.residual
##valeur-p
1 - pf( F.temp,
[1] 0.003289315
est égal à
chem.mgcv$sig2
chem.mgcv.conc$df.residual -chem.mgcv$df.residual, chem.mgcv$df.residual )

Documents pareils