date()
## [1] "Tue Oct 30 13:44:29 2012"
Due Date/Time: October 30, 2012, 1:45pm
The points per quesion are given in parentheses.
(1) The UScereal (MASS package) contains many variables regarding breakfast cereals. One variable is the amount of sugar per portion and another is shelf position (counting from the floor up). Create side-by-side box plots showing the distribution of sugar by shelf number.
require(MASS)
## Loading required package: MASS
require(ggplot2)
## Loading required package: ggplot2
head(UScereal)
## mfr calories protein fat sodium fibre carbo
## 100% Bran N 212.1 12.121 3.030 393.9 30.303 15.15
## All-Bran K 212.1 12.121 3.030 787.9 27.273 21.21
## All-Bran with Extra Fiber K 100.0 8.000 0.000 280.0 28.000 16.00
## Apple Cinnamon Cheerios G 146.7 2.667 2.667 240.0 2.000 14.00
## Apple Jacks K 110.0 2.000 0.000 125.0 1.000 11.00
## Basic 4 G 173.3 4.000 2.667 280.0 2.667 24.00
## sugars shelf potassium vitamins
## 100% Bran 18.18 3 848.48 enriched
## All-Bran 15.15 3 969.70 enriched
## All-Bran with Extra Fiber 0.00 3 660.00 enriched
## Apple Cinnamon Cheerios 13.33 1 93.33 enriched
## Apple Jacks 14.00 2 30.00 enriched
## Basic 4 10.67 3 133.33 enriched
ggplot(UScereal, aes(x = factor(shelf), y = sugars)) + geom_boxplot(col = "orange") +
xlab("Shelf Number") + ylab("Distribution of Sugar")
Perform a t test to determine if there is a significant difference in the amount of sugar in cereals on the first and second shelves. What do you conclude? (20)
head(USceral)
## Error: object 'USceral' not found
t.test(sugars ~ shelf)
## Error: object 'sugars' not found
(2) The data set USmelanoma (HSAUR2 package) contains male mortality counts per one million inhabitants by state along with the latitude and longitude centroid of the state. (40)
require(HSAUR2)
## Loading required package: HSAUR2
## Warning: package 'HSAUR2' was built under R version 2.15.2
## Loading required package: lattice
## Loading required package: scatterplot3d
a. Create a scatter plot of mortality versus latitude using latitude as the explanatory variable.
head(USmelanoma)
## mortality latitude longitude ocean
## Alabama 219 33.0 87.0 yes
## Arizona 160 34.5 112.0 no
## Arkansas 170 35.0 92.5 no
## California 182 37.5 119.5 yes
## Colorado 149 39.0 105.5 no
## Connecticut 159 41.8 72.8 yes
require(ggplot2)
m = ggplot(USmelanoma, aes(x = latitude, y = mortality)) + geom_point()
m
b. Add the linear regression line to your scatter plot.
m = m + geom_smooth(method = "lm", se = FALSE)
m
c. Regress mortality on latitude and interpret the value of the slope coefficient.
model = lm(mortality ~ latitude, data = USmelanoma)
summary(model)
##
## Call:
## lm(formula = mortality ~ latitude, data = USmelanoma)
##
## Residuals:
## Min 1Q Median 3Q Max
## -38.97 -13.18 0.97 12.01 43.94
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 389.189 23.812 16.34 < 2e-16 ***
## latitude -5.978 0.598 -9.99 3.3e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 19.1 on 47 degrees of freedom
## Multiple R-squared: 0.68, Adjusted R-squared: 0.673
## F-statistic: 99.8 on 1 and 47 DF, p-value: 3.31e-13
The value of the slope coefficient is -5.9776
d. Determine the sum of squared errors.
deviance(model)
## [1] 17173
e. Use density and box plots to examine the model assumptions. What do you conclude?
require(sm)
## Loading required package: sm
## Warning: package 'sm' was built under R version 2.15.2
## Package `sm', version 2.2-4.1 Copyright (C) 1997, 2000, 2005, 2007, 2008,
## A.W.Bowman & A.Azzalini Type help(sm) for summary information
confint(model)
## 2.5 % 97.5 %
## (Intercept) 341.285 437.094
## latitude -7.181 -4.774
ggplot(USmelanoma, aes(x = factor(latitude), y = mortality)) + geom_boxplot(col = "green") +
xlab("Latitude") + ylab("Mortality")
res = residuals(model)
sm.density(res, xlab = "model residuals", model = "normal")
The normality assumption does not seem to be suspect
(3) Davies and Goldsmith (1972) investigated the relationship between abrasion loss (abrasion) of samples of rubber (grams per hour) as a function of hardness (higher values indicate harder rubber) and tensile strength (kg/cm2 ). The data are in AbrasionLoss.txt. Input the data using AL = read.table(“http://myweb.fsu.edu/jelsner/AbrasionLoss.txt”, header=TRUE) (40)
a. Create a scatter plot matrix of the three variables. Based on the scatter of points in the plot of abrasion versus strength does it appear that tensile strength would be helpful in explaining abrasion loss?
AL = read.table("http://myweb.fsu.edu/jelsner/AbrasionLoss.txt", header = TRUE)
require(scatterplot3d)
pairs(AL, panel = panel.smooth)
There is not much correlation, providing cause that it would not be helpful in explaining abrasion loss
b. Regress abrasion loss on hardness and strength. What is the adjusted R squared value? Is strength an important explanatory variable after accounting for hardness?
model2 = lm(abrasion ~ hardness + strength, data = AL)
summary(model2)
##
## Call:
## lm(formula = abrasion ~ hardness + strength, data = AL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -79.38 -14.61 3.82 19.75 65.98
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 885.161 61.752 14.33 3.8e-14 ***
## hardness -6.571 0.583 -11.27 1.0e-11 ***
## strength -1.374 0.194 -7.07 1.3e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 36.5 on 27 degrees of freedom
## Multiple R-squared: 0.84, Adjusted R-squared: 0.828
## F-statistic: 71 on 2 and 27 DF, p-value: 1.77e-11
The adjusted value is 82% or .82
Strength is an important explanatory variable after accounting for hardness
c. On average how much additional abrasion is lost for every 1 kg/cm2 increase in tensile strength?
summary(model2)
##
## Call:
## lm(formula = abrasion ~ hardness + strength, data = AL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -79.38 -14.61 3.82 19.75 65.98
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 885.161 61.752 14.33 3.8e-14 ***
## hardness -6.571 0.583 -11.27 1.0e-11 ***
## strength -1.374 0.194 -7.07 1.3e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 36.5 on 27 degrees of freedom
## Multiple R-squared: 0.84, Adjusted R-squared: 0.828
## F-statistic: 71 on 2 and 27 DF, p-value: 1.77e-11
-1.3743
d. Check the correlations between the explanatory variables. Could collinearity be a problem for interpreting the model?
cor(AL)
## abrasion hardness strength
## abrasion 1.0000 -0.7377 -0.2984
## hardness -0.7377 1.0000 -0.2992
## strength -0.2984 -0.2992 1.0000
install.packages("psych")
## Installing package(s) into 'C:/Users/Marcus/Documents/R/win-library/2.15'
## (as 'lib' is unspecified)
## Error: trying to use CRAN without setting a mirror
require(psych)
## Loading required package: psych
## Warning: package 'psych' was built under R version 2.15.2
## Attaching package: 'psych'
## The following object(s) are masked from 'package:ggplot2':
##
## %+%
pairs.panels(AL)
No, colinearity does not seem to be a problem because none are above .64
e. Find the 95% prediction interval for the abrasion corresponding to a new rubber sample having a hardness of 60 units and a tensile strength of 200 kg/cm2.
predict(model2, data.frame(hardness = c(60), strength = c(200)), level = 0.95,
interval = "prediction")
## fit lwr upr
## 1 216 138.9 293.2