2b. Buatlah tabel seperti Tabel 4.2, yang memberikan distribusi sampling dari Nȳ dan t ̂_yr, untuk sampel berukuran n = 3.

# Data yang diberikan
x <- c(13, 7, 11, 12, 4, 3, 11, 3, 5)
y <- c(10, 7, 13, 17, 8, 1, 15, 7, 4)
N <- length(x) # Total populasi
n <- 3         # Ukuran sampel

# Membuat semua kombinasi sampel berukuran 3
combinations <- combn(N, n)

# Menyimpan hasil
x_bar_S <- c()
y_bar_S <- c()
B_samples <- c()
t_SRS <- c()
t_yr <- c()

# Looping melalui setiap kombinasi
for (i in 1:ncol(combinations)) {
  sample_idx <- combinations[, i]
  
  # Ambil data sampel
  x_sample <- x[sample_idx]
  y_sample <- y[sample_idx]
  
  # Hitung rata-rata sampel
  x_bar <- mean(x_sample)
  y_bar <- mean(y_sample)
  
  # Hitung standar deviasi
  S_x <- sd(x_sample)
  S_y <- sd(y_sample)
  
  # Hitung korelasi
  if (S_x > 0 & S_y > 0) {
    R <- cor(x_sample, y_sample)
  } else {
    R <- 0
  }
  
  # Hitung koefisien regresi B
  if (S_x != 0) {
    B <- R * (S_y / S_x)
  } else {
    B <- 0
  }
  
  # Hitung estimasi t_SRS dan t_yr
  t_SRS_sample <- N * y_bar
  t_yr_sample <- t_SRS_sample + B * (N * (mean(x) - x_bar))
  
  # Simpan hasil
  x_bar_S <- c(x_bar_S, x_bar)
  y_bar_S <- c(y_bar_S, y_bar)
  B_samples <- c(B_samples, B)
  t_SRS <- c(t_SRS, t_SRS_sample)
  t_yr <- c(t_yr, t_yr_sample)
}

# Membuat data frame untuk hasil akhir
result <- data.frame(
  Sample = apply(combinations, 2, function(idx) paste0("{", paste(idx, collapse = ","), "}")),
  x_bar_S = x_bar_S,
  y_bar_S = y_bar_S,
  B = B_samples,
  t_SRS = t_SRS,
  t_yr = t_yr
)

# Tampilkan hasil
print(result)
##     Sample   x_bar_S   y_bar_S           B t_SRS      t_yr
## 1  {1,2,3} 10.333333 10.000000  0.64285714    90  74.57143
## 2  {1,2,4} 10.666667 11.333333  0.98387097   102  75.43548
## 3  {1,2,5}  8.000000  8.333333  0.26190476    75  74.21429
## 4  {1,2,6}  7.666667  6.000000  0.86842105    54  54.00000
## 5  {1,2,7} 10.333333 10.666667  0.71428571    96  78.85714
## 6  {1,2,8}  7.666667  8.000000  0.31578947    72  72.00000
## 7  {1,2,9}  8.333333  7.000000  0.69230769    63  58.84615
## 8  {1,3,4} 12.000000 13.333333 -1.50000000   120 178.50000
## 9  {1,3,5}  9.333333 10.333333  0.35074627    93  87.73881
## 10 {1,3,6}  9.000000  8.000000  1.07142857    72  59.14286
## 11 {1,3,7} 11.666667 12.666667 -2.00000000   114 186.00000
## 12 {1,3,8}  9.000000 10.000000  0.42857143    90  84.85714
## 13 {1,3,9}  9.666667  9.000000  0.92307692    81  64.38462
## 14 {1,4,5}  9.666667 11.666667  0.56849315   105  94.76712
## 15 {1,4,6}  9.333333  9.333333  1.24725275    84  65.29121
## 16 {1,4,7} 12.000000 14.000000 -2.50000000   126 223.50000
## 17 {1,4,8}  9.333333 11.333333  0.62087912   102  92.68681
## 18 {1,4,9} 10.000000 10.333333  1.15789474    93  68.68421
## 19 {1,5,6}  6.666667  6.333333  0.63186813    57  62.68681
## 20 {1,5,7}  9.333333 11.000000  0.42537313    99  92.61940
## 21 {1,5,8}  6.666667  8.333333  0.26923077    75  77.42308
## 22 {1,5,9}  7.333333  7.333333  0.42465753    66  67.27397
## 23 {1,6,7}  9.000000  8.666667  1.14285714    78  64.28571
## 24 {1,6,8}  6.333333  6.000000  0.60000000    54  61.20000
## 25 {1,6,9}  7.000000  5.000000  0.85714286    45  50.14286
## 26 {1,7,8}  9.000000 10.666667  0.50000000    96  90.00000
## 27 {1,7,9}  9.666667  9.666667  1.00000000    87  69.00000
## 28 {1,8,9}  7.000000  7.000000  0.42857143    63  65.57143
## 29 {2,3,4} 10.000000 12.333333  1.85714286   111  72.00000
## 30 {2,3,5}  7.333333  9.333333  0.75675676    84  86.27027
## 31 {2,3,6}  7.000000  7.000000  1.50000000    63  72.00000
## 32 {2,3,7}  9.666667 11.666667  1.75000000   105  73.50000
## 33 {2,3,8}  7.000000  9.000000  0.75000000    81  85.50000
## 34 {2,3,9}  7.666667  8.000000  1.50000000    72  72.00000
## 35 {2,4,5}  7.666667 10.666667  1.21428571    96  96.00000
## 36 {2,4,6}  7.333333  8.333333  1.78688525    75  80.36066
## 37 {2,4,7} 10.000000 13.000000  2.00000000   117  75.00000
## 38 {2,4,8}  7.333333 10.333333  1.14754098    93  96.44262
## 39 {2,4,9}  8.000000  9.333333  1.88461538    84  78.34615
## 40 {2,5,6}  4.666667  5.333333  1.07692308    48  77.07692
## 41 {2,5,7}  7.333333 10.000000  1.05405405    90  93.16216
## 42 {2,5,8}  4.666667  7.333333 -0.07692308    66  63.92308
## 43 {2,5,9}  5.333333  6.333333 -0.07142857    57  55.50000
## 44 {2,6,7}  7.000000  7.666667  1.75000000    69  79.50000
## 45 {2,6,8}  4.333333  5.000000  0.75000000    45  67.50000
## 46 {2,6,9}  5.000000  4.000000  1.50000000    36  72.00000
## 47 {2,7,8}  7.000000  9.666667  1.00000000    87  93.00000
## 48 {2,7,9}  7.666667  8.666667  1.85714286    78  78.00000
## 49 {2,8,9}  5.000000  6.000000  0.00000000    54  54.00000
## 50 {3,4,5}  9.000000 12.666667  0.97368421   114 102.31579
## 51 {3,4,6}  8.666667 10.333333  1.67123288    93  77.95890
## 52 {3,4,7} 11.333333 15.000000  3.00000000   135  36.00000
## 53 {3,4,8}  8.666667 12.333333  0.97260274   111 102.24658
## 54 {3,4,9}  9.333333 11.333333  1.73255814   102  76.01163
## 55 {3,5,6}  6.000000  7.333333  1.21052632    66  84.15789
## 56 {3,5,7}  8.666667 12.000000  0.85714286   108 100.28571
## 57 {3,5,8}  6.000000  9.333333  0.73684211    84  95.05263
## 58 {3,5,9}  6.666667  8.333333  0.98837209    75  83.89535
## 59 {3,6,7}  8.333333  9.666667  1.62500000    87  77.25000
## 60 {3,6,8}  5.666667  7.000000  1.12500000    63  83.25000
## 61 {3,6,9}  6.333333  6.000000  1.50000000    54  72.00000
## 62 {3,7,8}  8.333333 11.666667  0.87500000   105  99.75000
## 63 {3,7,9}  9.000000 10.666667  1.66666667    96  76.00000
## 64 {3,8,9}  6.333333  8.000000  0.92307692    72  83.07692
## 65 {4,5,6}  6.333333  8.666667  1.52739726    78  96.32877
## 66 {4,5,7}  9.000000 13.333333  1.07894737   120 107.05263
## 67 {4,5,8}  6.333333 10.666667  1.11643836    96 109.39726
## 68 {4,5,9}  7.000000  9.666667  1.39473684    87  95.36842
## 69 {4,6,7}  8.666667 11.000000  1.76712329    99  83.09589
## 70 {4,6,8}  6.000000  8.333333  1.44444444    75  96.66667
## 71 {4,6,9}  6.666667  7.333333  1.79850746    66  82.18657
## 72 {4,7,8}  8.666667 13.000000  1.06849315   117 107.38356
## 73 {4,7,9}  9.333333 12.000000  1.84883721   108  80.26744
## 74 {4,8,9}  6.666667  9.333333  1.30597015    84  95.75373
## 75 {5,6,7}  6.000000  8.000000  1.47368421    72  94.10526
## 76 {5,6,8}  3.333333  5.333333  4.00000000    48 204.00000
## 77 {5,6,9}  4.000000  4.333333  1.50000000    39  88.50000
## 78 {5,7,8}  6.000000 10.000000  1.00000000    90 105.00000
## 79 {5,7,9}  6.666667  9.000000  1.29069767    81  92.61628
## 80 {5,8,9}  4.000000  6.333333 -1.50000000    57   7.50000
## 81 {6,7,8}  5.666667  7.666667  1.37500000    69  93.75000
## 82 {6,7,9}  6.333333  6.666667  1.76923077    60  81.23077
## 83 {6,8,9}  3.666667  4.000000  0.00000000    36  36.00000
## 84 {7,8,9}  6.333333  8.666667  1.19230769    78  92.30769