install.packages(“tidyverse”)

1.1. a, b, c

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ages <- c(2, 5, 6, 12, 14, 15, 15, 16, 18, 19, 20, 22, 23, 25, 27, 28, 30, 32, 33, 35, 36, 36, 37, 38, 39, 40, 40, 41, 42, 43, 43, 44, 44, 45, 45, 46, 47, 47, 48, 49, 50, 51, 56, 57, 58, 59, 59, 60, 62, 63, 65, 65, 67, 69, 71, 75, 78, 80, 82, 84, 90, 96)

#a. Display the data in a frequency table.
table(ages)
## ages
##  2  5  6 12 14 15 16 18 19 20 22 23 25 27 28 30 32 33 35 36 37 38 39 40 41 42 
##  1  1  1  1  1  2  1  1  1  1  1  1  1  1  1  1  1  1  1  2  1  1  1  2  1  1 
## 43 44 45 46 47 48 49 50 51 56 57 58 59 60 62 63 65 67 69 71 75 78 80 82 84 90 
##  2  2  2  1  2  1  1  1  1  1  1  1  2  1  1  1  2  1  1  1  1  1  1  1  1  1 
## 96 
##  1
ages_frame <- as.data.frame(ages)
#b. Display the data in a histogram.
ggplot2::ggplot(ages_frame, ggplot2::aes(x = ages)) + ggplot2::geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

#c. Describe the shape of the distribution.
# The distribution is slightly right-skewed because the data extend farther toward the older ages.

1.6. b, d

goats <- c(56, 32, 60, 59, 74, 65, 44, 51, 58, 51, 66, 49)
goats_frame <- as.data.frame(goats)
#b. Illustrate the observations with a histogram.
ggplot2::ggplot(goats_frame, ggplot2::aes(x = goats)) + ggplot2::geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

#d. What proportion of the measurements lie in the interval x_bar +- 2s

lower <- mean(goats) - 2 * sd(goats)
upper <- mean(goats) + 2 * sd(goats)

length(goats[goats >= lower & goats <= upper]) / length(goats)
## [1] 0.9166667

1.7 Done by hand

# Sample mean = (9+8+12+5+3+6+13)/7 = 56/7 = 8
# mean = 8
#sample sd = sqrt((9-8)^ + (8-8)^2 + (12-8)^2 + (5-8)^2 + (3-8)^2 + (6-8)^2 + (13-8)^2 / 7-1)
# sd = sqrt(80/7-1) = 3.65
# sd = 3.65

1.11

scores <- c(81, 89, 69, 72, 91, 58, 69, 66, 60, 67, 95, 83, 84, 68, 53, 76, 63, 74, 72, 68, 79, 81, 81, 86, 72, 79, 83, 73, 58, 73, 81, 77, 92, 87, 48, 49, 89, 88, 97, 80)

#a. 25th percentile
quantile(scores, 0.25)
## 25% 
##  68
#b. 50th percentile
quantile(scores, 0.50)
##  50% 
## 76.5
#c. 64th percentile
quantile(scores, 0.64)
## 64% 
##  81
#d. 75th percentile
quantile(scores, 0.75)
##   75% 
## 83.25
#e. 82nd percentile
quantile(scores, 0.82)
##   82% 
## 86.98
#f. 90th percentile
quantile(scores, 0.90)
##  90% 
## 89.2

1.12

weights <- c(113, 127, 131, 149, 174, 248)

#a. median
median(weights)
## [1] 140
#b. 70th percentile
quantile(weights, 0.70)
##   70% 
## 161.5
#c. mean
mean(weights)
## [1] 157
#d. variance and standard deviation
var(weights)
## [1] 2429.2
sd(weights)
## [1] 49.28692
#e. range
max(weights) - min(weights)
## [1] 135

1.18

heights <- c(67, 67, 67, 60, 68, 64, 69, 71, 67, 67, 66, 63, 67, 62, 66, 70, 67, 61, 68, 67, 68, 64, 69, 67, 70, 72, 61, 67, 69, 68, 69, 72, 66, 67, 66, 67, 69, 64, 64, 63, 68, 66, 65, 60, 70, 65, 68, 66, 61, 65)


#a. Compute the median and the quartiles.
median(heights)
## [1] 67
Q1 <- quantile(weights, 0.25)
Q2 <- quantile(weights, 0.50)
Q3 <-quantile(weights, 0.75)

#b. Find the range and interquartile range.
IQR(heights)
## [1] 3
#c. Construct a boxplot.
heights_frame <- as.data.frame(heights)
ggplot2::ggplot(heights_frame, ggplot2::aes(x = heights)) + ggplot2::geom_boxplot()

#d. Draw a dot diagram.
ggplot2::ggplot(heights_frame, ggplot2::aes(x = heights)) + ggplot2::geom_dotplot()
## Bin width defaults to 1/30 of the range of the data. Pick better value with
## `binwidth`.

#e. Is there an outlier?
boxplot(heights)$out

## [1] 60 60

1.22

#a. Compare the two distributions by describing the shapes.
# Midterm 1 has the distribution slightly left-skewed, and has one outlier. Whereas midterm 2 has the distribution very left-skewed as there are several outliers. 

#b. Overall, did the students perform better on the second midterm?
# Yes, students performed better on the second midterm as the median score increased from about 74 on Midterm 1 to about 84 on Midterm 2.

#c. Which exam has a larger difference between the mean and the median? For this exam, is the mean or median larger? Why?
# Midterm 2 has the larger difference between the mean and median. This is because the mean is smaller than the median because midterm 2 has several outliers that pull the mean downward, while the median is not affected by these extreme values.

#d. Which exam has a larger standard deviation of the scores?
# Midterm 2 has the larger standard deviation. However, both exams have a similar overall spread, midterm 2 has several outliers in the lower score section which increase the variability of the scores.