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)
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
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="")
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
Question 4
Question 5
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%).
–
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%).
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.
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.
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.
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