Week 7 and Week 8
Week 7 and Week 8
R
To install R: Go to http://cran.r-project.org/
Documentations on R: http://cran.r-
project.org/doc/manuals
Based on S language
Rcmdr package
Combines R with an easy-to-use drop-down menu
Provides the lines of code that correspond to the
analyses carried out
Install and launch using library(Rcmdr)
Import Data
To import data from the file myfile.csv and to place
it within an object dat:
dat<- read.table(“myfile.csv”, sep=“,”, header=TRUE,
dec=“.”)
summary(dat)
160
maxO3
40
T9
15
15 30
T12
35
T15
15
6
Ne9
0
6
Ne12
0
6
Ne15
0
-8 0
Wx9
5
Wx12
-5
2
Wx15
-8
160
maxO3y
40
40 120 15 25 0 4 8 0 4 8 -5 5 40 120
Multiple Linear Regression (cont.)
Call:
lm(formula = maxO3 ~ ., data = ozone.m)
Residuals:
Min 1Q Median 3Q Max
-53.566 -8.727 -0.403 7.599 39.458
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 12.24442 13.47190 0.909 0.3656
T9 -0.01901 1.12515 -0.017 0.9866
T12 2.22115 1.43294 1.550 0.1243
T15 0.55853 1.14464 0.488 0.6266
Ne9 -2.18909 0.93824 -2.333 0.0216 *
Ne12 -0.42102 1.36766 -0.308 0.7588
Ne15 0.18373 1.00279 0.183 0.8550
Wx9 0.94791 0.91228 1.039 0.3013
Wx12 0.03120 1.05523 0.030 0.9765
Wx15 0.41859 0.91568 0.457 0.6486
maxO3y 0.35198 0.06289 5.597 1.88e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-140
-140
-130
-130
-120
bic
-120
-120
-110
-110
-97
T9
T12
T15
Ne9
Ne12
Ne15
Wx9
Wx12
Wx15
maxO3y
(Intercept)
Multiple Linear Regression (cont.)
Call:
lm(formula = maxO3 ~ T12 + Ne9 + Wx9 + maxO3y, data = ozone.m)
Residuals:
Min 1Q Median 3Q Max
-52.396 -8.377 -1.086 7.951 40.933
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 12.63131 11.00088 1.148 0.253443
T12 2.76409 0.47450 5.825 6.07e-08 ***
Ne9 -2.51540 0.67585 -3.722 0.000317 ***
Wx9 1.29286 0.60218 2.147 0.034055 *
maxO3y 0.35483 0.05789 6.130 1.50e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
2
Residuals
0
-2
-4
0 20 40 60 80 100
Index
100
80
60
40
wind
Analysis of Variance Table
Response: maxO3
Df Sum Sq Mean Sq F value Pr(>F)
wind 3 7586 2528.69 3.3881 0.02074 *
Residuals 108 80606 746.35
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
One-Way ANOVA (cont.)
# Conducting a residual analysis
res.aov1 <- rstudent(reg.aov1)
library(lattice)
mypanel <- function(...){
panel.xyplot(...)
panel.abline(h=c(-2,0,2),lty=c(3,2,3),...)
}
trellis.par.set(list(fontsize=list(point=5,text=8)))
xyplot(res.aov1~I(1:112)|wind,data=ozone,pch=20,ylim
=c(-3,3),panel=mypanel, ylab="Residuals",xlab="")
One-Way ANOVA (cont.)
0 20 40 60 80 100
South West
-1
-2
Residuals
East North
-1
-2
0 20 40 60 80 100
One-Way ANOVA (cont.)
# Interpreting the coefficients
summary(reg.aov1)
summary(lm(maxO3~C(wind,base=2),data=ozone))
summary(lm(maxO3~C(wind,sum),data=ozone))
Residuals:
Min 1Q Median 3Q Max
-60.600 -16.807 -7.365 11.478 81.300
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 105.600 8.639 12.223 <2e-16 ***
windNorth -19.471 9.935 -1.960 0.0526 .
windSouth -3.076 10.496 -0.293 0.7700
windWest -20.900 9.464 -2.208 0.0293 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residuals:
Min 1Q Median 3Q Max
-60.600 -16.807 -7.365 11.478 81.300
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 86.129 4.907 17.553 <2e-16 ***
C(wind, base = 2)1 19.471 9.935 1.960 0.0526 .
C(wind, base = 2)3 16.395 7.721 2.123 0.0360 *
C(wind, base = 2)4 -1.429 6.245 -0.229 0.8194
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residuals:
Min 1Q Median 3Q Max
-60.600 -16.807 -7.365 11.478 81.300
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 94.738 3.053 31.027 <2e-16 ***
C(wind, sum)1 10.862 6.829 1.590 0.1147
C(wind, sum)2 -8.609 4.622 -1.863 0.0652 .
C(wind, sum)3 7.786 5.205 1.496 0.1376
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residuals:
Min 1Q Median 3Q Max
-60.600 -16.807 -7.365 11.478 81.300
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 94.738 3.053 31.027 <2e-16 ***
wind1 10.862 6.829 1.590 0.1147
wind2 -8.609 4.622 -1.863 0.0652 .
wind3 7.786 5.205 1.496 0.1376
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1