date()
## [1] "Thu Oct 17 02:14:37 2013"
Due Date: October 17, 2013 Total Points: 30
1 The babyboom dataset (UsingR) contains the time of birth, sex, and birth weight for 44 babies born in one 24-hour period at a hospital in Brisbane, Australia.
a) Create side-by-side box plots of birth weight (grams) by gender. (2)
require(UsingR)
## Loading required package: UsingR Loading required package: MASS
require(ggplot2)
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:UsingR':
##
## movies
ggplot(babyboom, aes(x = gender, y = wt)) + geom_boxplot(fill = "green") + xlab("Gender") +
ylab("Weight In Grams")
b) Perform a t-test under the hypothesis that there is no difference in birth weight against the alternative hypothesis that girls weight less. What do you conclude? (5)
t.test(wt ~ gender, data = babyboom, alternative = "less")
##
## Welch Two Sample t-test
##
## data: wt by gender
## t = -1.421, df = 27.63, p-value = 0.08324
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf 48
## sample estimates:
## mean in group girl mean in group boy
## 3132 3375
Based on the p-value of .08 the evidence is suggestive but not conclusive enought to reject the alternative hypthesis.
2 The BushApproval dataset (UsingR) contains approval ratings (%) for George W. Bush from different polling outlets. Perform a t-test under the hypothesis that there is no difference in approval rating between Fox and UPenn versus the alternative that there is a difference. Hint: Subset the data first. The 'or' logical predicate is indicate by the vertical line | on your keyboard. (5)
t.test(approval ~ who, data = subset(BushApproval, who == "fox" | who == "upenn"))
##
## Welch Two Sample t-test
##
## data: approval by who
## t = 4.269, df = 46.21, p-value = 9.65e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 3.791 10.553
## sample estimates:
## mean in group fox mean in group upenn
## 65.67 58.50
3 The mtcars dataset contains the miles per gallon and whether or not the transmission is automatic (0 = automatic, 1 = manual) for 32 automobiles.
a) Plot a histogram of the miles per gallon over all cars. Use a bin width of 3 mpg. (3)
ggplot(mtcars, aes(x = mpg)) + geom_histogram(fill = "red", color = "black",
binwidth = 3) + ylab("Cars") + xlab("MPG")
b) Perform a Mann-Whitney-Wilcoxon test under the hypothesis that there is no difference in mpg between automatic and manual transmission cars without assuming they follow a normal distribution. The alternative is there is a difference. What do you conclude? (5)
wilcox.test(mpg ~ am, data = mtcars)
## Warning: cannot compute exact p-value with ties
##
## Wilcoxon rank sum test with continuity correction
##
## data: mpg by am
## W = 42, p-value = 0.001871
## alternative hypothesis: true location shift is not equal to 0
Because the p-value is less than .01 we can convincingly reject the null hypotheis.
4 The data set diamond (UsingR) contains data about the price of 48 diamond rings. The variable price records the price in Singapore dollars and the variable carat records the size of the diamond and you are interested in predicting price from carat size.
a) Make a scatter plot of carat versus price. (3)
ggplot(diamond, aes(x = carat, y = price)) + geom_point() + xlab("Carat") +
ylab("Price") + theme_bw()
b) Add a linear regression line to the plot. (3)
ggplot(diamond, aes(x = carat, y = price)) + geom_point() + xlab("Carat") +
ylab("Price") + theme_bw() + geom_smooth(method = lm, se = FALSE, col = "gold")
c) Use the model to predict the amount a 1/3 carat diamond ring would cost. (4)
model = lm(price ~ carat, data = diamond)
predict(model, data.frame(carat = 1/3))
## 1
## 980.7