2c. Gambarkan histogram dari distribusi sampling t ̂_yr . Bandingkan histogram ini dengan histogram dari distribusi sampling Nȳ.

# Data
x <- c(13, 7, 11, 12, 4, 3, 11, 3, 5)
y <- c(10, 7, 13, 17, 8, 1, 15, 7, 4)

# Total t_x dan N (jumlah unit)
tx <- sum(x)
N <- length(y)

# Fungsi untuk menghitung B, t_yr, dan N * y_bar
calculate_B_tyr_Nybar <- function(sample_indices) {
  x_sample <- x[sample_indices]
  y_sample <- y[sample_indices]
  
  x_bar_sample <- mean(x_sample)
  y_bar_sample <- mean(y_sample)
  
  # Menghitung B
  B <- y_bar_sample / x_bar_sample
  
  # Menghitung t_hat_yr
  t_hat_yr <- B * tx
  
  # Menghitung N * y_bar
  N_y_bar <- N * y_bar_sample
  
  return(c(x_bar_sample, y_bar_sample, B, t_hat_yr, N_y_bar))
}

# Paket yang diperlukan
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
# Fungsi untuk menghitung B, t_yr, dan N * y_bar
calculate_B_tyr_Nybar <- function(sample_indices) {
  x_sample <- x[sample_indices]
  y_sample <- y[sample_indices]
  
  x_bar_sample <- mean(x_sample)
  y_bar_sample <- mean(y_sample)
  
  # Menghitung B
  B <- y_bar_sample / x_bar_sample
  
  # Menghitung t_hat_yr
  t_hat_yr <- B * tx
  
  # Menghitung N * y_bar
  N_y_bar <- N * y_bar_sample
  
  return(c(x_bar_sample, y_bar_sample, B, t_hat_yr, N_y_bar))
}

# Generate all combinations of samples of size 3
library(combinat)
## 
## Attaching package: 'combinat'
## The following object is masked from 'package:utils':
## 
##     combn
samples <- combn(1:N, 3)

# Tabel distribusi
results <- data.frame(Sample = character(), 
                      x_bar = numeric(), 
                      y_bar = numeric(), 
                      B = numeric(), 
                      t_hat_yr = numeric(), 
                      N_y_bar = numeric())

# Mengisi tabel dengan perhitungan dari setiap sampel
for (i in 1:ncol(samples)) {
  sample_indices <- samples[, i]
  calculation <- calculate_B_tyr_Nybar(sample_indices)
  
  results <- rbind(results, data.frame(Sample = paste(sample_indices, collapse = ","),
                                       x_bar = calculation[1],
                                       y_bar = calculation[2],
                                       B = calculation[3],
                                       t_hat_yr = calculation[4],
                                       N_y_bar = calculation[5]))
}

# Plot histogram untuk t_hat_yr
hist_tyr <- ggplot(results, aes(x = t_hat_yr)) +
  geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
  labs(title = "Histogram Distribusi Sampling t̂_yr", x = "t̂_yr", y = "Frequency") +
  theme_minimal()

# Plot histogram untuk N_y_bar
hist_Nybar <- ggplot(results, aes(x = N_y_bar)) +
  geom_histogram(binwidth = 5, fill = "lightgreen", color = "black") +
  labs(title = "Histogram Distribusi Sampling Nȳ", x = "Nȳ", y = "Frequency") +
  theme_minimal()

# Menampilkan kedua histogram secara bersamaan
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.3.3
grid.arrange(hist_tyr, hist_Nybar, ncol = 2)