Problem 1a: Interpretation: the mean and the median are not very close to each other in this data set,and it does not appear to skew any specific direction. The data is somewhat conglomerated in the middle of the graph with a high peak at the far left section of the Histogram.
bird_lengths <- c(14.5, 11.9, 18.8, 19.1, 17.9, 13.4, 18.1, 18.4, 14.2, 12.4, 14.3, 12.2)
hist(bird_lengths,
col = "green",
border = "purple",
main = "Histogram of Bird Body Lengths",
xlab = "Body Length (cm)",
ylab = "Frequency")
Problem 1b: Interpretation: There does not seem to be a clear corolation
from the weight of the bird to the number of eggs hatched. Even though
the heaviest bird produced the least amount of eggs, the other two
categories do not show a significant difference in their error bars that
supports that trend.
weight <- c(42.5, 40.3, 61.2, 63.8, 62.7, 39.2, 52.6, 52.9, 31.8, 40.8, 50.2, 42.8)
eggs <- c(3, 3, 3, 3, 2, 3, 4, 4, 3, 4, 4, 3)
data <- data.frame(weight, eggs)
boxplot(weight ~ eggs, data = data, col = rainbow(length(unique(data$eggs))),
xlab = "Number of Eggs", ylab = "Weight of Each Bird",
main = "Stratified Boxplot of Bird Weight by Number of Eggs")
Problem 1c: Interpretation: The majority of the data seems to fall
withing interquartile range 1 and 3. The median also sits in the middle
of the data, and the outlier bars to not reach incredibly far outside
the plot. The mean also falls fairly close to the median in this set of
data.
wingspan <- c(35.2, 34.6, 39.2, 42.3, 41.2, 33.1, 40.3, 38.3, 34.3, 32.3, 35.2, 30.3)
boxplot(wingspan,
col = "orange",
border = "navyblue",
main = "Boxplot of Wingspan Data",
xlab = "Birds",
ylab = "Wingspan (cm)")
Problem 2a:
p_H <- 0.2
p_T <- 0.8
coin_flips <- sample(c("H", "T"), 10, replace = TRUE, prob = c(p_H, p_T))
head_freq <- sum(coin_flips == "H") / length(coin_flips)
tail_freq <- sum(coin_flips == "T") / length(coin_flips)
print(paste("Possible outcomes:", paste(c("Heads", "Tails"), collapse = ", ")))
## [1] "Possible outcomes: Heads, Tails"
print(paste("Relative frequency of Heads:", head_freq))
## [1] "Relative frequency of Heads: 0.1"
print(paste("Relative frequency of Tails:", tail_freq))
## [1] "Relative frequency of Tails: 0.9"
Problem 2b:
p_H <- 0.2
num_flips <- 1000
coin_flips <- rbinom(num_flips, 1, p_H)
relative_frequency <- cumsum(coin_flips) / seq_along(coin_flips)
plot(relative_frequency, type = "l", col = "blue",
xlab = "Number of Flips", ylab = "Relative Frequency of Heads",
main = "Convergence of Relative Frequency for Landing Heads")
Problem 3a and 3b:
values <- c(0, 2, 4, 6)
probabilities <- c(0.3, 0.2, 0.2, 0.3)
sample_X <- sample(values, 1000, replace = TRUE, prob = probabilities)
prob_X_geq_3 <- sum(sample_X >= 3) / length(sample_X)
prob_X_lt_4 <- sum(sample_X < 4) / length(sample_X)
cat("P(X ≥ 3) =", prob_X_geq_3, "\n")
## P(X ≥ 3) = 0.49
cat("P(X < 4) =", prob_X_lt_4, "\n")
## P(X < 4) = 0.51
Problem 3c and 3d:
values <- c(0, 2, 4, 6)
probabilities <- c(0.3, 0.2, 0.2, 0.3)
sample_x <- sample(values, 1000, replace = TRUE, prob = probabilities)
expected_value <- sum(values * probabilities)
variance <- sum(probabilities * (values - expected_value)^2)
standard_deviation <- sqrt(variance)
cat("Expected Value (E[X]):", expected_value, "\n")
## Expected Value (E[X]): 3
cat("Standard Deviation (s[X]):", standard_deviation, "\n")
## Standard Deviation (s[X]): 2.408319
Problem 3e:
values <- c(0, 2, 4, 6)
probabilities <- c(0.3, 0.2, 0.2, 0.3)
sample_x <- sample(values, 1000, replace = TRUE, prob = probabilities)
barplot(table(sample_x),
col = rainbow(length(values)),
xlab = "Values of the data set",
ylab = "Frequency",
main = "Barplot of the given sample")
Problem 4a and 4b:
n <- 8
p <- 0.5
sample_y <- rbinom(20000, size = n, prob = p)
prob_Y_5 <- sum(sample_y == 5) / length(sample_y)
prob_Y_4_7 <- sum(sample_y >= 4 & sample_y < 7) / length(sample_y)
cat("Probability P(Y = 5):", prob_Y_5, "\n")
## Probability P(Y = 5): 0.221
cat("Probability P(4 ≤ Y < 7):", prob_Y_4_7, "\n")
## Probability P(4 ≤ Y < 7): 0.59915
Problem 4c: Due to the large sample size, the probability converges to a vlue close to what we calculated before in the Direct calculations. The values are extremely close to each other due to the sample size of 20000.
sample_size <- 20000
n <- 8
p <- 0.5
sample_y <- rbinom(sample_size, size = n, prob = p)
prob_Y_5 <- sum(sample_y == 5) / sample_size
prob_Y_4_7 <- sum(sample_y >= 4 & sample_y < 7) / sample_size
theoretical_prob_Y_5 <- dbinom(5, size = n, prob = p)
theoretical_prob_Y_4_7 <- sum(dbinom(4:6, size = n, prob = p))
cat("Recalculated Probability P(Y = 5):", prob_Y_5, "\n")
## Recalculated Probability P(Y = 5): 0.22125
cat("Recalculated Probability P(4 ≤ Y < 7):", prob_Y_4_7, "\n")
## Recalculated Probability P(4 ≤ Y < 7): 0.6012
Problem 4d:
y <- rbinom(20000, size = 8, prob = 0.5)
barplot(table(y), col = rainbow(length(unique(y))),
xlab = "Values in data set", ylab = "Frequency",
main = "Barplot of Sample of data given")
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.