date()
## [1] "Tue Oct 15 15:16:22 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)
library(UsingR)
## Warning: package 'UsingR' was built under R version 3.0.2
## Loading required package: MASS
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.0.2
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:UsingR':
##
## movies
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.0.2
babyboom = as.data.frame(babyboom)
BBl = melt(babyboom[, -1])
## Using gender as id variables
ggplot(BBl, aes(x = gender, y = value)) + geom_boxplot() + xlab("") + ylab("birth weight (in grams)") +
theme_bw()
b) Perform a t-test under the hypothesis that there is no difference in birth weight against the alternative hypothesis that girls weigh less. What do you conclude? (5)
t.test(value ~ gender, data = BBl)
##
## Welch Two Sample t-test
##
## data: value by gender
## t = -0.466, df = 77.27, p-value = 0.6425
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -710.5 441.0
## sample estimates:
## mean in group girl mean in group boy
## 1953 2087
The p-value being greater than .05, there is no evidence to reject the null hypothesis.
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 indicated by the vertical line | on your keyboard. (5)
require(UsingR)
Bush.approval = as.data.frame(BushApproval)
bush.subset = subset(Bush.approval, Bush.approval$who == "fox" | Bush.approval$who ==
"upenn")
t.test(approval ~ who, data = bush.subset)
##
## 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)
mtcars = as.data.frame(mtcars)
ggplot(mtcars, aes(mpg)) + geom_histogram(binwidth = 3) + xlab("miles per gallon")
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
There is convincing evidence to reject the null hypothesis.
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)
require(UsingR)
diamond = as.data.frame(diamond)
ggplot(diamond, aes(x = carat, y = price)) + geom_point() + xlab("Carat") +
ylab("Price (in Singapore dollars")
b) Add a linear regression line to the plot. (3)
ggplot(diamond, aes(x = carat, y = price)) + geom_point() + xlab("Carat") +
ylab("Price (in Singapore dollars") + geom_smooth(method = lm, se = FALSE)
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 = 0.33))
## 1
## 968.3