library(tidyverse)
library(lattice)
library(ggplot2)

Question 1

Auto = read.table("http://www.stat.uchicago.edu/~yibi/s220/Auto.txt", header=T)
Auto$origin = factor(Auto$origin, labels=c("American", "European", "Japanese"))
Auto$cylinders = as.factor(Auto$cylinders)
  1. The engine displacement and horsepower variables form a positive linear relationship, although, it is a weak relationship because there is a weak association between the two variables and large amount of scatter.
ggplot(Auto, aes(x = displacement, y = horsepower)) + geom_point()  +                         
  geom_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(Auto, aes(x = displacement, y = horsepower, color = cylinders)) + geom_point()

Question 2

cdc = read.table("https://www.stat.uchicago.edu/~yibi/s220/labs/data/cdc.dat", h=T)
cdc$genhlth = ordered(cdc$genhlth, levels=c("excellent", "very good", "good", "fair", "poor"))
cdc$bmi = with(cdc, 703.7*weight/height^2)
cdc$BMIrange = cut(cdc$bmi, breaks=c(0, 18.5, 25, 30, Inf), right=FALSE)
cdc$BMIrange = factor(cdc$BMIrange, labels=c("Underweight", "Healthy", "Overweight", "Obese"))
addmargins(xtabs(~ genhlth + BMIrange, data=cdc))
##            BMIrange
## genhlth     Underweight Healthy Overweight Obese   Sum
##   excellent          98    2496       1599   464  4657
##   very good         116    3050       2624  1182  6972
##   good              119    2088       2087  1381  5675
##   fair               52     629        695   643  2019
##   poor               25     204        221   227   677
##   Sum               410    8467       7226  3897 20000
  1. 3897 / 20000 = 19.48%
  1. 4657 / 20000 = 23.28%
  2. 2496 / 4657 = 53.59%
  3. 204 / 677 = 30.13%
library(ggplot2)
ggplot(cdc, aes(x=genhlth, fill=BMIrange)) + geom_bar() +
labs(x="Self-Rated General Health", fill="BMI Range")

ggplot(cdc, aes(x=genhlth, fill=BMIrange)) + geom_bar(position="fill") +
labs(x="Self-Rated General Health", fill="BMI Range")

par(mai=c(.4,.4,.1,.01), mgp=c(1.8,.5,0), las=1) # to reduce the plot margins
mosaicplot(xtabs(~ genhlth + BMIrange, data=cdc),
ylab="BMI Range",
xlab="Self-Rated General Health", color=TRUE, main="")

  1. Those who are of excellent general health are less likely to be obese and those who are obese are more likely to be of poor general health.
  1. The two variables are not independent but associated because there is a trend between BMIrange and General Health where those are at the ends of the BMIrange (underweight and obese) are proportional of poor health than those of in the healthy BMI range.
  1. Smoke100 is more independent with BMIrange because there seems to be no correlation between whether people smoke or not and their BMI.
par(mai=c(.4,.4,.1,.01), mgp=c(1.8,.5,0), las=1)
mosaicplot(xtabs(~ exerany + BMIrange, data=cdc),
ylab="BMI Range",
xlab="Any Exercise in Past Month?", color=TRUE, main="")

mosaicplot(xtabs(~ smoke100 + BMIrange, data=cdc),
ylab="BMI Range",
xlab="Smoked 100+ Cigarettes Lifetime?", color=TRUE, main="")

Question 3

  1. The left plot shows that more females participated in the survey.
  2. The left plot is more helpful in showing that the same amount of female and male participants said they have no stress.
  3. The right plot is more helpful in showing that a greater percentage of the female participants had stress that affected their grades.

Question 4

  1. Observational study comparing those who take oral contraceptives and those who do not. The response variable is whether someone has cervical cancer or not.
  2. Those who are on the younger and older side of the scale are less sexually active and would not take oral contraceptives as much as those in the middle age range, also those who are younger are less likely to have cervical cancer early in life compared to those older so it would bias if this confounding factor was not accounted for.
  3. The data would show a positive relationship between age and cervical cancer because older women are most likely to use oral contraception and have cervical cancer than younger women.
  4. Women who use the pill are likely more sexually active than those who do not use the pill, so they have a higher chance of getting a sexually transmitted disease that leads to cervical cancer than those who are not as sexually active. Because of this we cannot conclude that using oral contraceptives increases the risk of cervical cancer because researchers forgot to account for this confounding factor.

Question 5

  1. Small Kidney Stones Open Surgery: 81 / (81 + 6) = 93.1% PCNL: 234 / (234 + 36) = 86.67%

    Large Kidney Stones Open Surgery: 192 / (192 + 71) = 73% PCNL: 55 / (55 + 25) = 68.75%

When only small kidney stones are considered, open surgery is more effective (93.1%) than PCNL (86.67%). When only large kidney stones are considered, open surgery is still more effective (73%) than PCNL (68.75%).

  1. –

    Treatment Success Failure

    Open Surgery 273 78

    PCNL 289 61

Open Surgery: 273 / (273 + 78) = 77.78% PCNL: 289 / (289 + 61) = 82.57%

When the confounding variable is not accounted for than the results flip and show that PCNL is more effective (82.57%) than Open Surgery (77.78%).

  1. Open Surgery: (81 + 6) / (81 + 6 + 234 + 36) = 24.37% PCNL: (234 + 36) / (81 + 6 + 234 + 36) = 75.63%

    PCNL is more commonly used for small stones.

  1. Open Surgery: (192 + 71) / (192 + 71 + 55 + 25) = 76.68% PCNL: (55 + 25) / (192 + 71 + 55 + 25) = 23.32%

    Open Surgery is more commonly used for large stones.

  2. Small Kidney Stones: (81 + 234) / (81 + 6 + 234 + 36) = 88.23% Large Kidney Stones: (192 + 55) / (192 + 71 + 55 + 25) = 72.01%

    Small kidney stones treatment had a better success rate.

  3. Since PNCL is used 3 times more than open surgery for small stones and small kidney stones treatment had a better success rate than large kidney stones treatment, without the confounding factor of kidney stone size, the data is going to show that PCNL is more effective than open surgery even though when you add the confounding factor it flips.

Question 6

  1. Yes, because the two groups were randomly assigned to the medication and placebo, which could establish causality.
  2. Yes, the two blocks are smokers that live with another smoker and smokers that do not live with another smoker.
  3. Yes, as long as the sample size was randomly selected you can claim this because it could be generalized to the population.