1) Histogram of miles per gallon
mpg <- mtcars$mpg
mean_mpg <- mean(mpg)
hist(mpg,
main = "Histogram of Miles per Gallon",
xlab = "Miles per Gallon (mpg)",
col = "lightgray",
border = "white")
abline(v = mean_mpg, col = "red", lwd = 2)

mean_mpg
## [1] 20.09062
# Written answer:
# The mpg distribution is centered around 20 mpg and is slightly right-skewed, with a few cars above 30 mpg
2) Boxplot of mpg by cylinders
boxplot(mpg ~ factor(mtcars$cyl),
main = "Miles per Gallon by Number of Cylinders",
xlab = "Number of Cylinders",
ylab = "Miles per Gallon (mpg)",
col = "lightgray")

# Written answer:
# Four-cylinder cars generally have the highest mpg values, six-cylinder cars
# are in the middle, and eight-cylinder cars generally have the lowest mpg values.
3) Scatterplot of weight and miles per gallon
plot(mtcars$wt, mtcars$mpg,
main = "Car Weight vs. Miles per Gallon",
xlab = "Weight (1000 lbs)",
ylab = "Miles per Gallon (mpg)",
pch = 19)
mpg_wt_model <- lm(mpg ~ wt, data = mtcars)
abline(mpg_wt_model, col = "red", lwd = 2)

summary(mpg_wt_model)
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5432 -2.3647 -0.1252 1.4096 6.8727
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
## wt -5.3445 0.5591 -9.559 1.29e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
## F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
# Written answer:
# There is a strong negative relationship between weight and mpg: as a car
# becomes heavier, its miles per gallon tends to decrease.
4) Bar plot of cylinder counts
cylinder_counts <- table(mtcars$cyl)
barplot(cylinder_counts,
main = "Number of Cars by Cylinder Group",
xlab = "Number of Cylinders",
ylab = "Number of Cars",
col = "lightgray")

cylinder_counts
##
## 4 6 8
## 11 7 14
# Written answer:
# The bar plot shows that 8-cylinder cars are the most common (14 cars),
# followed by 4-cylinder cars (11 cars) and 6-cylinder cars (7 cars).
5) Short written summary
# Written summary:
# The most important pattern is that fuel economy tends to decrease as cars
# become heavier and as the number of cylinders increases.
# Four-cylinder cars generally have the highest mpg, while eight-cylinder cars
# have the lowest mpg.
# The boxplot was the most useful plot for comparing mpg across cylinder groups.
# The scatterplot was the most useful plot for showing the relationship between
# the two numeric variables weight and mpg.
6) Normal distribution functions with mtcars mpg
mpg <- mtcars$mpg
mean_mpg <- mean(mpg)
sd_mpg <- sd(mpg)
mean_mpg
## [1] 20.09062
sd_mpg
## [1] 6.026948
prob_normal <- pnorm(25, mean = mean_mpg, sd = sd_mpg) -
pnorm(15, mean = mean_mpg, sd = sd_mpg)
empirical_prob <- mean(mpg >= 15 & mpg <= 25)
prob_normal
## [1] 0.5931861
empirical_prob
## [1] 0.65625
hist(mpg,
probability = TRUE,
main = "mpg with Fitted Normal Curve",
xlab = "Miles per Gallon (mpg)",
col = "lightgray",
border = "white")
curve(dnorm(x, mean = mean_mpg, sd = sd_mpg),
add = TRUE,
col = "red",
lwd = 2)

# Written answer:
# The fitted normal model gives P(15 <= mpg <= 25) of about 0.593.
# The observed proportion in mtcars is about 0.656, so the normal-model
# probability is somewhat lower than the actual proportion in the data.
7) Lognormal distribution functions with state income
income <- state.x77[, "Income"]
meanlog_income <- mean(log(income))
sdlog_income <- sd(log(income))
meanlog_income
## [1] 8.387963
sdlog_income
## [1] 0.1399314
prob_income <- plnorm(5000,
meanlog = meanlog_income,
sdlog = sdlog_income) -
plnorm(4000,
meanlog = meanlog_income,
sdlog = sdlog_income)
prob_income
## [1] 0.5710674
hist(income,
probability = TRUE,
main = "State Income with Fitted Lognormal Curve",
xlab = "Income",
col = "lightgray",
border = "white")
curve(dlnorm(x,
meanlog = meanlog_income,
sdlog = sdlog_income),
from = min(income),
to = max(income),
add = TRUE,
col = "red",
lwd = 2)

# Written answer:
# Under the fitted log-normal model, the probability that state income is
# between 4000 and 5000 is about 0.571.
8) Exponential distribution functions with waiting times
waiting <- faithful$waiting
rate_waiting <- 1 / mean(waiting)
rate_waiting
## [1] 0.01410496
prob_waiting <- pexp(80, rate = rate_waiting) -
pexp(60, rate = rate_waiting)
prob_waiting
## [1] 0.1054488
hist(waiting,
probability = TRUE,
main = "Old Faithful Waiting Times with Fitted Exponential Curve",
xlab = "Waiting Time (minutes)",
col = "lightgray",
border = "white")
curve(dexp(x, rate = rate_waiting),
from = 0,
to = max(waiting),
add = TRUE,
col = "red",
lwd = 2)

# Written answer:
# Under the fitted exponential model, the probability that the waiting time
# is between 60 and 80 minutes is about 0.105.
9) Central Limit Theorem using replicate()
set.seed(123)
means_5 <- replicate(
5000,
mean(sample(rivers, size = 5, replace = TRUE))
)
means_30 <- replicate(
5000,
mean(sample(rivers, size = 30, replace = TRUE))
)
par(mfrow = c(1, 2))
hist(means_5,
main = "Sample Means: n = 5",
xlab = "Sample Mean",
col = "lightgray",
border = "white")
hist(means_30,
main = "Sample Means: n = 30",
xlab = "Sample Mean",
col = "lightgray",
border = "white")

par(mfrow = c(1, 1))
# Written answer:
# When the sample size increases from 5 to 30, the sampling distribution of
# the mean becomes narrower and more approximately bell-shaped around the
# population mean showing the Central Limit Theorem.