401 lab6

advertisement
401 lab6
#1. Return to the class "Depth of Cut" data set and example. Now treat "pulse
s" as a quantitative variable and consider analysis based on an approximately
linear relationship:
๐‘‘๐‘’๐‘๐‘กโ„Ž ≈ ๐›ฝ0 + ๐›ฝ1 ∗ ๐‘๐‘ข๐‘™๐‘ ๐‘’๐‘ 
#a) Use R to make a plot with sample means for 100, 500, and 1000 pulses plot
ted against number of pulses. Connect the consecutive (in terms of number of
pulses) plotted points with line segments.
#Here is some R code for the class "Depth of Cut" example
#First is code from Lab 5 ignoring the quantitative nature of the factor "pul
ses"
pulses<-c(rep("A(100)",4),rep("B(500)",4),rep("C(1000)",4))
depth<-c(7.4,8.6,5.6,8.0,24.2,29.5,26.5,23.8,33.4,37.5,35.9,34.8)
Depth<-data.frame(depth,pulses)
Depth
##
##
##
##
##
##
##
##
##
##
##
##
##
depth
1
7.4
2
8.6
3
5.6
4
8.0
5
24.2
6
29.5
7
26.5
8
23.8
9
33.4
10 37.5
11 35.9
12 34.8
pulses
A(100)
A(100)
A(100)
A(100)
B(500)
B(500)
B(500)
B(500)
C(1000)
C(1000)
C(1000)
C(1000)
plot(depth ~ pulses,data=Depth)
plot(as.factor(pulses),depth)
summary(Depth)
##
##
##
##
##
##
##
depth
Min.
: 5.60
1st Qu.: 8.45
Median :25.35
Mean
:22.93
3rd Qu.:33.75
Max.
:37.50
pulses
A(100) :4
B(500) :4
C(1000):4
aggregate(Depth$depth,by=list(Depth$pulses),mean)
##
Group.1
x
## 1 A(100) 7.4
## 2 B(500) 26.0
## 3 C(1000) 35.4
aggregate(Depth$depth,by=list(Depth$pulses),sd)
##
Group.1
x
## 1 A(100) 1.296148
## 2 B(500) 2.619160
## 3 C(1000) 1.733974
depth.aov<-aov(depth ~ pulses,data=Depth)
summary(depth.aov)
##
##
##
##
##
Df Sum Sq Mean Sq F value
Pr(>F)
pulses
2 1624.4
812.2
211 2.75e-08 ***
Residuals
9
34.6
3.8
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Now some code making use of the quantitative nature of the factor pulses and
a straight line
#model for how mean depth of cut changes with number of pulses
npulses<-c(rep(100,4),rep(500,4),rep(1000,4))
plot(npulses,depth,xlim=c(0,1100),ylim=c(0,40))
#Then add the 12 data pairs to the plot. How "linear" does the plot appear to
be?
The plot below looks quite linear.
nDepth<-data.frame(npulses,depth)
#b) Evaluate the sample correlation between the pulses and depth variables.
cor(nDepth)
##
npulses
depth
## npulses 1.000000 0.958367
## depth
0.958367 1.000000
#Give us the correlation of npulses and depth, and sample correlation between
the pulses and depth variables is almost 1,indicating a linear relationship b
etween these two variables.
#c) Add the least squares line to the plot in a).
#plot the mean values of each depth, seems like there is a linear relationshi
p among the data, and abline function
plot(c(100,500,1000),c(7.4,26.0,34.8),type="l",xlim=c(0,1100),ylim=c(0,40))
points(npulses,depth,xlim=c(0,1100),ylim=c(0,40))
abline(lm(depth~npulses),xlim=c(0,1100),ylim=c(0,40))
#d) Use the lm() to get details of the least squares fit. What is the value o
f the coefficient of determination ( 2 R ) here?
# lm(depth~npulses) which means fit depth with npulses
lm.out1<-lm(depth~npulses)
lm.out1
##
##
##
##
##
##
##
Call:
lm(formula = depth ~ npulses)
Coefficients:
(Intercept)
6.60984
npulses
0.03061
summary(lm.out1)
##
##
##
##
##
##
##
##
##
##
Call:
lm(formula = depth ~ npulses)
Residuals:
Min
1Q Median
-4.071 -2.307 -1.193
3Q
1.987
Max
7.587
Coefficients:
Estimate Std. Error t value Pr(>|t|)
##
##
##
##
##
##
##
##
(Intercept) 6.609836
1.868845
3.537 0.00538 **
npulses
0.030607
0.002884 10.614 9.18e-07 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.678 on 10 degrees of freedom
Multiple R-squared: 0.9185, Adjusted R-squared: 0.9103
F-statistic: 112.7 on 1 and 10 DF, p-value: 9.185e-07
plot(lm.out1)
aov(lm.out1)
## Call:
##
aov(formula = lm.out1)
##
##
##
##
##
##
##
##
Terms:
npulses Residuals
Sum of Squares 1523.7985 135.2682
Deg. of Freedom
1
10
Residual standard error: 3.677882
Estimated effects may be unbalanced
#Here is some code for Exercise 4 of Section 4.1 Vardeman and Jobe page 140
speed<-c(rep(800,4),rep(700,4),rep(600,4),rep(500,4),rep(400,4))
life<-c(1.00,.90,.74,.66,1.00,1.20,1.50,1.60,2.35,2.65,3.00,3.60,6.40,7.80,9.
80,16.50,21.50,24.50,26.00,33.00)
plot(speed,life)
plot(log(speed),log(life))#this will make the points looks like in a straight
line
lm.out2<-lm(life~speed)
lm.out2
##
##
##
##
##
##
##
Call:
lm(formula = life ~ speed)
Coefficients:
(Intercept)
44.07500
speed
-0.05965
summary(lm.out2)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Call:
lm(formula = life ~ speed)
Residuals:
Min
1Q Median
-7.850 -4.835 -0.770
3Q
Max
4.325 12.785
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 44.075000
5.368434
8.210 1.69e-07 ***
speed
-0.059650
0.008709 -6.849 2.08e-06 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.508 on 18 degrees of freedom
## Multiple R-squared: 0.7227, Adjusted R-squared: 0.7073
## F-statistic: 46.91 on 1 and 18 DF, p-value: 2.076e-06
plot(lm.out2)
lm.out2$coef
## (Intercept)
##
44.07500
speed
-0.05965
lm.out2$coef[1]
## (Intercept)
##
44.075
lm.out2$coef[2]
##
speed
## -0.05965
plot(speed,life)
abline(lm(life~speed))
lm.out3<-lm(log(life)~log(speed))
lm.out3
##
##
##
##
##
##
##
Call:
lm(formula = log(life) ~ log(speed))
Coefficients:
(Intercept)
log(speed)
34.344
-5.186
summary(lm.out3)
##
## Call:
## lm(formula = log(life) ~ log(speed))
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Residuals:
Min
1Q
Median
-0.37234 -0.19180 -0.03964
3Q
0.12321
Max
0.68618
Coefficients:
Estimate Std. Error t value
(Intercept) 34.3441
1.4823
23.17
log(speed)
-5.1857
0.2326 -22.29
--Signif. codes: 0 '***' 0.001 '**' 0.01
Pr(>|t|)
7.49e-15 ***
1.47e-14 ***
'*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.2548 on 18 degrees of freedom
Multiple R-squared: 0.965, Adjusted R-squared: 0.9631
F-statistic:
497 on 1 and 18 DF, p-value: 1.467e-14
plot(lm.out3)
lm.out3$coef
## (Intercept)
##
34.344106
log(speed)
-5.185674
lm.out3$coef[1]
## (Intercept)
##
34.34411
lm.out3$coef[2]
## log(speed)
## -5.185674
plot(speed,life)
curve(exp(lm.out3$coef[1])*x^lm.out3$coef[2],add=TRUE)
##log(life)=a+b*log(speed), then life=exp(a)*(speed)^b
##and here a=lm.out3$coef[1], b=lm.out3$coef[2], speed=x
Download