Minggu 3 - Mat Aktuaria

Khusnia NK

2026-02-09

1. KONSEP DASAR DAN SIMULASI DATA

# ==============================================
# 1. KONSEP DASAR HARAPAN HIDUP
# ==============================================

# Install paket jika diperlukan
#if (!require("tidyverse")) install.packages("tidyverse")
#if (!require("ggplot2")) install.packages("ggplot2")

library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── 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
library(ggplot2)

# Set seed untuk reproduksibilitas
set.seed(123)

# Fungsi untuk membuat data simulasi harapan hidup
create_life_data <- function(n = 1000) {
  data.frame(
    id = 1:n,
    age = sample(0:100, n, replace = TRUE, prob = dnorm(0:100, mean = 50, sd = 20)),
    gender = sample(c("Male", "Female"), n, replace = TRUE, prob = c(0.48, 0.52)),
    smoker = sample(c("Yes", "No"), n, replace = TRUE, prob = c(0.3, 0.7)),
    income_group = sample(c("Low", "Middle", "High"), n, replace = TRUE),
    life_expectancy = rnorm(n, mean = 75, sd = 10)
  )
}

# Buat dataset
life_data <- create_life_data(1000)

# Tampilkan struktur data
head(life_data)
##   id age gender smoker income_group life_expectancy
## 1  1  57 Female     No       Middle        70.01529
## 2  2  74   Male     No         High        89.70661
## 3  3  39 Female     No       Middle        77.25039
## 4  4  81   Male     No         High        72.17466
## 5  5  86   Male     No       Middle        71.55582
## 6  6  49 Female     No          Low        65.73211

2. HARAPAN HIDUP LENGKAP (Complete Life Expectancy)

# ==============================================
# 2. HARAPAN HIDUP LENGKAP (Complete Life Expectancy)
# ==============================================

# Fungsi menghitung harapan hidup lengkap
calculate_complete_life_expectancy <- function(data) {
  
  # Hitung berdasarkan kelompok usia
  age_groups <- cut(data$age, breaks = seq(0, 100, by = 10), 
                   labels = paste0(seq(0, 90, by = 10), "-", seq(9, 99, by = 10)))
  
  results <- data %>%
    mutate(age_group = age_groups) %>%
    group_by(age_group) %>%
    summarize(
      n = n(),
      complete_expectancy = mean(life_expectancy - age, na.rm = TRUE),
      sd = sd(life_expectancy - age, na.rm = TRUE),
      min = min(life_expectancy - age, na.rm = TRUE),
      max = max(life_expectancy - age, na.rm = TRUE)
    )
  
  return(results)
}

# Hitung harapan hidup lengkap
complete_results <- calculate_complete_life_expectancy(life_data)
print("HARAPAN HIDUP LENGKAP (Complete Life Expectancy):")
## [1] "HARAPAN HIDUP LENGKAP (Complete Life Expectancy):"
print(complete_results)
## # A tibble: 11 × 6
##    age_group     n complete_expectancy    sd    min   max
##    <fct>     <int>               <dbl> <dbl>  <dbl> <dbl>
##  1 0-9          19              70.5   11.5   49.9  90.3 
##  2 10-19        46              57.5    8.13  40.1  74.5 
##  3 20-29        94              49.1   10.7   18.6  73.9 
##  4 30-39       176              38.0    9.73  10.8  68.2 
##  5 40-49       198              30.4   10.4    1.46 53.5 
##  6 50-59       194              19.8    9.57 -12.3  46.2 
##  7 60-69       128               8.88  10.3  -14.6  35.2 
##  8 70-79        88              -0.881 10.4  -29.4  20.8 
##  9 80-89        41             -10.2   10.4  -28.8  16.9 
## 10 90-99        15             -16.9    7.81 -31.1  -2.42
## 11 <NA>          1              58.1   NA     58.1  58.1
# Visualisasi
ggplot(complete_results, aes(x = age_group, y = complete_expectancy)) +
  geom_bar(stat = "identity", fill = "steelblue", alpha = 0.7) +
  geom_errorbar(aes(ymin = complete_expectancy - sd, 
                    ymax = complete_expectancy + sd), 
                width = 0.2) +
  labs(title = "Harapan Hidup Lengkap per Kelompok Usia",
       x = "Kelompok Usia",
       y = "Harapan Hidup Lengkap (tahun)") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

3. HARAPAN HIDUP RINGKAS (Curtate Life Expectancy)

# ==============================================
# 3. HARAPAN HIDUP RINGKAS (Curtate Life Expectancy)
# ==============================================

# Fungsi menghitung harapan hidup ringkas
calculate_curtate_life_expectancy <- function(data) {
  
  # Bulatkan ke bawah (floor) untuk mendapatkan tahun penuh
  results <- data %>%
    mutate(
      curtate_years = floor(life_expectancy - age),  # Tahun penuh yang diharapkan
      fractional_part = (life_expectancy - age) - curtate_years  # Bagian fraksional
    ) %>%
    group_by(gender) %>%
    summarize(
      n = n(),
      curtate_expectancy = mean(curtate_years, na.rm = TRUE),
      complete_expectancy = mean(life_expectancy - age, na.rm = TRUE),
      difference = complete_expectancy - curtate_expectancy
    )
  
  return(results)
}

# Hitung harapan hidup ringkas
curtate_results <- calculate_curtate_life_expectancy(life_data)
print("HARAPAN HIDUP RINGKAS (Curtate Life Expectancy):")
## [1] "HARAPAN HIDUP RINGKAS (Curtate Life Expectancy):"
print(curtate_results)
## # A tibble: 2 × 5
##   gender     n curtate_expectancy complete_expectancy difference
##   <chr>  <int>              <dbl>               <dbl>      <dbl>
## 1 Female   519               25.2                25.7      0.494
## 2 Male     481               24.9                25.4      0.504
# Visualisasi perbandingan
comparison_df <- curtate_results %>%
  pivot_longer(cols = c(curtate_expectancy, complete_expectancy), 
               names_to = "type", values_to = "expectancy")

ggplot(comparison_df, aes(x = gender, y = expectancy, fill = type)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  scale_fill_manual(values = c("complete_expectancy" = "#2E86C1", 
                               "curtate_expectancy" = "#F39C12"),
                    labels = c("Complete", "Curtate")) +
  labs(title = "Perbandingan Harapan Hidup Lengkap vs Ringkas",
       x = "Jenis Kelamin",
       y = "Harapan Hidup (tahun)",
       fill = "Tipe") +
  theme_minimal()

4. HUBUNGAN e_x, e_x° (Hubungan antara Complete dan Curtate)

# ==============================================
# 4. HUBUNGAN e_x DAN e_x° 
# (Hubungan antara Complete dan Curtate Life Expectancy)
# ==============================================

# Fungsi menghitung hubungan teoritis
calculate_life_expectancy_relationship <- function(data) {
  
  # Hitung untuk setiap usia
  relationship <- data %>%
    mutate(
      age_decade = floor(age/10)*10,  # Kelompokkan per dekade
      curtate = floor(life_expectancy - age),
      complete = life_expectancy - age,
      fractional = complete - curtate
    ) %>%
    group_by(age_decade) %>%
    summarize(
      n = n(),
      e_x_curtate = mean(curtate, na.rm = TRUE),        # e_x
      e_x_complete = mean(complete, na.rm = TRUE),      # e_x°
      mean_fractional = mean(fractional, na.rm = TRUE),
      # Hubungan teoritis: e_x° ≈ e_x + 0.5 (dengan asumsi distribusi uniform)
      theoretical_relationship = e_x_curtate + 0.5
    )
  
  return(relationship)
}

# Hitung hubungan
relationship_results <- calculate_life_expectancy_relationship(life_data)
print("HUBUNGAN e_x DAN e_x°:")
## [1] "HUBUNGAN e_x DAN e_x°:"
print(relationship_results)
## # A tibble: 10 × 6
##    age_decade     n e_x_curtate e_x_complete mean_fractional
##         <dbl> <int>       <dbl>        <dbl>           <dbl>
##  1          0    17      69.8         70.4             0.589
##  2         10    43      57.3         57.8             0.418
##  3         20    89      49.8         50.3             0.507
##  4         30   171      38.3         38.9             0.527
##  5         40   192      30.7         31.2             0.506
##  6         50   197      20.0         20.5             0.489
##  7         60   136      10.1         10.7             0.530
##  8         70    94      -0.628       -0.198           0.429
##  9         80    44      -9.66        -9.19            0.468
## 10         90    17     -16.9        -16.4             0.556
## # ℹ 1 more variable: theoretical_relationship <dbl>
# Visualisasi hubungan
ggplot(relationship_results, aes(x = age_decade)) +
  geom_line(aes(y = e_x_complete, color = "Actual e_x°"), linewidth = 1.5) +
  geom_line(aes(y = theoretical_relationship, color = "Theoretical e_x + 0.5"), 
            linewidth = 1, linetype = "dashed") +
  geom_point(aes(y = e_x_complete), size = 3) +
  scale_color_manual(values = c("Actual e_x°" = "#E74C3C", 
                                "Theoretical e_x + 0.5" = "#2ECC71")) +
  labs(title = "Hubungan antara Complete dan Curtate Life Expectancy",
       x = "Usia (dekade)",
       y = "Harapan Hidup (tahun)",
       color = "Keterangan") +
  theme_minimal() +
  theme(legend.position = "bottom")

5. APLIKASI DALAM ASURANSI JIWA

# ==============================================
# 5. APLIKASI DALAM ASURANSI JIWA
# ==============================================

# Fungsi untuk perhitungan premi sederhana
calculate_insurance_premium <- function(data, interest_rate = 0.05) {
  
  premium_calc <- data %>%
    mutate(
      # Probabilitas hidup (sederhana)
      survival_prob = 1 - pnorm(age, mean = 80, sd = 15),
      
      # Faktor diskon
      discount_factor = 1/(1 + interest_rate)^(life_expectancy - age),
      
      # Nilai sekarang (sederhana)
      present_value = 10000 * survival_prob * discount_factor,
      
      # Premi tahunan (disederhanakan)
      annual_premium = present_value / (life_expectancy - age)
    ) %>%
    group_by(gender, smoker) %>%
    summarize(
      n = n(),
      avg_age = mean(age),
      avg_life_expectancy = mean(life_expectancy),
      avg_premium = mean(annual_premium, na.rm = TRUE),
      min_premium = min(annual_premium, na.rm = TRUE),
      max_premium = max(annual_premium, na.rm = TRUE)
    )
  
  return(premium_calc)
}

# Hitung premi asuransi
insurance_results <- calculate_insurance_premium(life_data)
## `summarise()` has grouped output by 'gender'. You can override using the
## `.groups` argument.
print("APLIKASI ASURANSI JIWA - PERHITUNGAN PREMI:")
## [1] "APLIKASI ASURANSI JIWA - PERHITUNGAN PREMI:"
print(insurance_results)
## # A tibble: 4 × 8
## # Groups:   gender [2]
##   gender smoker     n avg_age avg_life_expectancy avg_premium min_premium
##   <chr>  <chr>  <int>   <dbl>               <dbl>       <dbl>       <dbl>
## 1 Female No       358    49.6                74.9        511.     -12693.
## 2 Female Yes      161    48.9                75.6       -748.    -137338.
## 3 Male   No       335    48.7                75.0       1742.      -9239.
## 4 Male   Yes      146    50.9                74.4        313.      -5973.
## # ℹ 1 more variable: max_premium <dbl>
# Visualisasi premi
ggplot(insurance_results, aes(x = gender, y = avg_premium, fill = smoker)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = min_premium, ymax = max_premium),
                position = position_dodge(0.9), width = 0.25) +
  scale_fill_manual(values = c("Yes" = "#E74C3C", "No" = "#2ECC71")) +
  labs(title = "Premi Asuransi Jiwa Tahunan Rata-rata",
       subtitle = "Berdasarkan Jenis Kelamin dan Status Merokok",
       x = "Jenis Kelamin",
       y = "Premi Tahunan (Rp)",
       fill = "Perokok?") +
  theme_minimal()

6. PENGUJIAN HIPOTESIS STATISTIK

# ==============================================
# ANALISIS HARAPAN HIDUP - KODE LENGKAP (FIXED)
# ==============================================

# 1. Setup dan Data
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("ggplot2")) install.packages("ggplot2")
library(tidyverse)
library(ggplot2)

set.seed(123)

create_life_data <- function(n = 1000) {
  data.frame(
    id = 1:n,
    age = sample(0:100, n, replace = TRUE, prob = dnorm(0:100, mean = 50, sd = 20)),
    gender = sample(c("Male", "Female"), n, replace = TRUE, prob = c(0.48, 0.52)),
    smoker = sample(c("Yes", "No"), n, replace = TRUE, prob = c(0.3, 0.7)),
    income_group = sample(c("Low", "Middle", "High"), n, replace = TRUE),
    life_expectancy = rnorm(n, mean = 75, sd = 10)
  )
}

life_data <- create_life_data(1000)

# 2. Complete Life Expectancy
calculate_complete_life_expectancy <- function(data) {
  age_groups <- cut(data$age, breaks = seq(0, 100, by = 10), 
                   labels = paste0(seq(0, 90, by = 10), "-", seq(9, 99, by = 10)))
  
  results <- data %>%
    mutate(age_group = age_groups) %>%
    group_by(age_group) %>%
    summarize(
      n = n(),
      complete_expectancy = mean(life_expectancy - age, na.rm = TRUE),
      sd = sd(life_expectancy - age, na.rm = TRUE)
    )
  return(results)
}

# 3. Curtate Life Expectancy
calculate_curtate_life_expectancy <- function(data) {
  results <- data %>%
    mutate(curtate_years = floor(life_expectancy - age)) %>%
    group_by(gender) %>%
    summarize(
      n = n(),
      curtate_expectancy = mean(curtate_years, na.rm = TRUE),
      complete_expectancy = mean(life_expectancy - age, na.rm = TRUE)
    )
  return(results)
}

# 4. Pengujian Hipotesis Sederhana
hypothesis_test_gender <- function(data) {
  cat("\n=== UJI PERBEDAAN GENDER ===\n")
  
  # Pisahkan data
  male <- data %>% filter(gender == "Male") %>% pull(life_expectancy)
  female <- data %>% filter(gender == "Female") %>% pull(life_expectancy)
  
  # Uji t sederhana
  test_result <- t.test(female, male)
  
  cat("Rata-rata Female:", round(mean(female), 2), "\n")
  cat("Rata-rata Male:", round(mean(male), 2), "\n")
  cat("Perbedaan:", round(mean(female) - mean(male), 2), "\n")
  cat("p-value:", round(test_result$p.value, 4), "\n")
  
  if(test_result$p.value < 0.05) {
    cat("KESIMPULAN: Ada perbedaan signifikan (p < 0.05)\n")
  } else {
    cat("KESIMPULAN: Tidak ada perbedaan signifikan\n")
  }
  
  return(test_result)
}

# 5. Visualisasi Sederhana
visualize_life_expectancy <- function(data) {
  # Plot 1: Distribusi Harapan Hidup
  p1 <- ggplot(data, aes(x = life_expectancy, fill = gender)) +
    geom_density(alpha = 0.5) +
    labs(title = "Distribusi Harapan Hidup per Gender",
         x = "Harapan Hidup (tahun)", y = "Densitas") +
    theme_minimal()
  
  # Plot 2: Harapan Hidup vs Usia
  p2 <- ggplot(data, aes(x = age, y = life_expectancy, color = smoker)) +
    geom_point(alpha = 0.3) +
    geom_smooth(method = "lm", se = FALSE) +
    labs(title = "Hubungan Usia dan Harapan Hidup",
         x = "Usia", y = "Harapan Hidup") +
    theme_minimal()
  
  # Plot 3: Complete vs Curtate
  data_summary <- data %>%
    mutate(
      complete = life_expectancy - age,
      curtate = floor(complete)
    ) %>%
    group_by(gender) %>%
    summarize(
      complete_mean = mean(complete),
      curtate_mean = mean(curtate)
    )
  
  p3 <- ggplot(data_summary %>% pivot_longer(cols = -gender), 
               aes(x = gender, y = value, fill = name)) +
    geom_bar(stat = "identity", position = "dodge") +
    labs(title = "Complete vs Curtate Life Expectancy",
         x = "Gender", y = "Tahun", fill = "Tipe") +
    theme_minimal()
  
  # Tampilkan semua plot
  print(p1)
  print(p2)
  print(p3)
}

# 6. Latihan Mandiri Sederhana
simple_exercise <- function(data) {
  cat("\n=== LATIHAN MANDIRI ===\n")
  
  # Soal 1: Hitung rata-rata per kelompok
  cat("\n1. Rata-rata Harapan Hidup per Kelompok:\n")
  summary1 <- data %>%
    group_by(gender, smoker) %>%
    summarize(
      count = n(),
      mean_life = mean(life_expectancy),
      mean_remaining = mean(life_expectancy - age),
      .groups = "drop"
    )
  print(summary1)
  
  # Soal 2: Analisis sederhana
  cat("\n2. Analisis Dasar:\n")
  cat("   Total observasi:", nrow(data), "\n")
  cat("   Rata-rata usia:", round(mean(data$age), 2), "\n")
  cat("   Rata-rata harapan hidup:", round(mean(data$life_expectancy), 2), "\n")
  cat("   Rata-rata sisa hidup:", round(mean(data$life_expectancy - data$age), 2), "\n")
  
  # Soal 3: Perhitungan premi sederhana
  cat("\n3. Estimasi Premi Asuransi Sederhana:\n")
  premium_est <- data %>%
    group_by(gender) %>%
    summarize(
      avg_age = mean(age),
      avg_remaining = mean(life_expectancy - age),
      # Premi sederhana: semakin muda, semakin murah
      premium_index = 1000 / (100 - avg_age) * avg_remaining
    )
  print(premium_est)
}

# 7. FUNGSI UTAMA YANG PASTI BERJALAN
run_simple_analysis <- function() {
  cat("\n", strrep("=", 50), "\n")
  cat("ANALISIS HARAPAN HIDUP - VERSI SIMPLE\n")
  cat(strrep("=", 50), "\n")
  
  # 1. Buat data
  cat("\n1. MEMUAT DATA...\n")
  data <- create_life_data(500)
  cat("   Sampel:", nrow(data), "observasi\n")
  
  # 2. Statistik dasar
  cat("\n2. STATISTIK DASAR:\n")
  cat("   Usia rata-rata:", round(mean(data$age), 1), "tahun\n")
  cat("   Harapan hidup rata-rata:", round(mean(data$life_expectancy), 1), "tahun\n")
  cat("   Sisa hidup rata-rata:", round(mean(data$life_expectancy - data$age), 1), "tahun\n")
  
  # 3. Complete vs Curtate
  cat("\n3. PERBANDINGAN COMPLETE vs CURTATE:\n")
  data <- data %>%
    mutate(
      complete = life_expectancy - age,
      curtate = floor(complete)
    )
  
  comparison <- data %>%
    summarize(
      mean_complete = mean(complete),
      mean_curtate = mean(curtate),
      difference = mean_complete - mean_curtate
    )
  print(comparison)
  
  # 4. Uji hipotesis
  cat("\n4. PENGUJIAN HIPOTESIS:\n")
  test_result <- hypothesis_test_gender(data)
  
  # 5. Visualisasi
  cat("\n5. MEMBUAT VISUALISASI...\n")
  visualize_life_expectancy(data)
  
  # 6. Latihan
  cat("\n6. LATIHAN:\n")
  simple_exercise(data)
  
  cat("\n", strrep("=", 50), "\n")
  cat("ANALISIS SELESAI\n")
  cat(strrep("=", 50), "\n")
  
  return(list(
    data = data,
    comparison = comparison,
    test_result = test_result
  ))
}

# ==============================================
# JALANKAN ANALISIS
# ==============================================

# Versi sederhana yang pasti berjalan
results <- run_simple_analysis()
## 
##  ================================================== 
## ANALISIS HARAPAN HIDUP - VERSI SIMPLE
## ================================================== 
## 
## 1. MEMUAT DATA...
##    Sampel: 500 observasi
## 
## 2. STATISTIK DASAR:
##    Usia rata-rata: 50.7 tahun
##    Harapan hidup rata-rata: 74.7 tahun
##    Sisa hidup rata-rata: 24 tahun
## 
## 3. PERBANDINGAN COMPLETE vs CURTATE:
##   mean_complete mean_curtate difference
## 1      23.97151       23.468  0.5035058
## 
## 4. PENGUJIAN HIPOTESIS:
## 
## === UJI PERBEDAAN GENDER ===
## Rata-rata Female: 74.44 
## Rata-rata Male: 75 
## Perbedaan: -0.56 
## p-value: 0.5168 
## KESIMPULAN: Tidak ada perbedaan signifikan
## 
## 5. MEMBUAT VISUALISASI...

## `geom_smooth()` using formula = 'y ~ x'

## 
## 6. LATIHAN:
## 
## === LATIHAN MANDIRI ===
## 
## 1. Rata-rata Harapan Hidup per Kelompok:
## # A tibble: 4 × 5
##   gender smoker count mean_life mean_remaining
##   <chr>  <chr>  <int>     <dbl>          <dbl>
## 1 Female No       192      74.3           23.1
## 2 Female Yes       80      74.8           24.5
## 3 Male   No       169      75.0           24.1
## 4 Male   Yes       59      74.9           25.7
## 
## 2. Analisis Dasar:
##    Total observasi: 500 
##    Rata-rata usia: 50.72 
##    Rata-rata harapan hidup: 74.69 
##    Rata-rata sisa hidup: 23.97 
## 
## 3. Estimasi Premi Asuransi Sederhana:
## # A tibble: 2 × 4
##   gender avg_age avg_remaining premium_index
##   <chr>    <dbl>         <dbl>         <dbl>
## 1 Female    51.0          23.5          479.
## 2 Male      50.4          24.6          496.
## 
##  ================================================== 
## ANALISIS SELESAI
## ==================================================
# Tampilkan hasil
print(results$comparison)
##   mean_complete mean_curtate difference
## 1      23.97151       23.468  0.5035058
print(results$test_result)
## 
##  Welch Two Sample t-test
## 
## data:  female and male
## t = -0.64883, df = 486.98, p-value = 0.5168
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.254894  1.135369
## sample estimates:
## mean of x mean of y 
##  74.43825  74.99802
exercise_life_expectancy <- function() {
  cat("\n=== LATIHAN MANDIRI ===\n")
  
  # Buat dataset baru untuk latihan
  exercise_data <- create_life_data(500)
  
  # SOAL 1: Hitung harapan hidup lengkap per kelompok pendapatan
  cat("\nSOAL 1: Hitung Complete Life Expectancy per Income Group\n")
  soal1 <- exercise_data %>%
    group_by(income_group) %>%
    summarize(
      n = n(),
      complete_expectancy = mean(life_expectancy - age, na.rm = TRUE),
      sd = sd(life_expectancy - age, na.rm = TRUE)
    )
  print(soal1)
  
  # SOAL 2: Uji apakah kelompok pendapatan tinggi memiliki harapan hidup lebih panjang
  cat("\nSOAL 2: Uji perbedaan harapan hidup antar kelompok pendapatan\n")
  high_income <- exercise_data %>% filter(income_group == "High")
  low_income <- exercise_data %>% filter(income_group == "Low")
  
  t_test_income <- t.test(high_income$life_expectancy, 
                          low_income$life_expectancy,
                          alternative = "greater")
  
  cat("H0: μ_high ≤ μ_low\n")
  cat("H1: μ_high > μ_low\n")
  cat("p-value:", t_test_income$p.value, "\n")
  cat("Kesimpulan:", ifelse(t_test_income$p.value < 0.05, 
                           "Pendapatan tinggi berhubungan dengan harapan hidup lebih panjang",
                           "Tidak cukup bukti perbedaan signifikan"), "\n")
  
  # SOAL 3: Hitung hubungan antara Complete dan Curtate
  cat("\nSOAL 3: Analisis hubungan e_x dan e_x°\n")
  exercise_data <- exercise_data %>%
    mutate(
      curtate = floor(life_expectancy - age),
      complete = life_expectancy - age,
      difference = complete - curtate
    )
  
  relationship_summary <- exercise_data %>%
    summarize(
      mean_curtate = mean(curtate),
      mean_complete = mean(complete),
      mean_difference = mean(difference),
      theoretical_difference = 0.5,  # Asumsi distribusi uniform
      actual_vs_theoretical = mean_difference - theoretical_difference
    )
  print(relationship_summary)
  
  # SOAL 4: Analisis pengaruh interaksi gender dan smoking
  cat("\nSOAL 4: Analisis interaksi Gender × Smoking\n")
  interaction_analysis <- exercise_data %>%
    group_by(gender, smoker) %>%
    summarize(
      n = n(),
      mean_life = mean(life_expectancy),
      mean_remaining = mean(life_expectancy - age),
      sd_life = sd(life_expectancy)
    )
  print(interaction_analysis)
  
  # Visualisasi interaksi
  p <- ggplot(interaction_analysis, 
              aes(x = interaction(gender, smoker), 
                  y = mean_life, 
                  fill = gender)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(ymin = mean_life - sd_life, 
                      ymax = mean_life + sd_life),
                  width = 0.2, position = position_dodge(0.9)) +
    labs(title = "Interaksi Gender dan Smoking terhadap Harapan Hidup",
         x = "Kombinasi Gender-Smoking",
         y = "Rata-rata Harapan Hidup (tahun)") +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
  print(p)
  
  return(list(
    soal1 = soal1,
    t_test_income = t_test_income,
    relationship = relationship_summary,
    interaction = interaction_analysis
  ))
}

# Jalankan latihan mandiri
exercise_results <- exercise_life_expectancy()
## 
## === LATIHAN MANDIRI ===
## 
## SOAL 1: Hitung Complete Life Expectancy per Income Group
## # A tibble: 3 × 4
##   income_group     n complete_expectancy    sd
##   <chr>        <int>               <dbl> <dbl>
## 1 High           168                24.8  20.5
## 2 Low            168                25.0  20.4
## 3 Middle         164                28.6  23.6
## 
## SOAL 2: Uji perbedaan harapan hidup antar kelompok pendapatan
## H0: μ_high ≤ μ_low
## H1: μ_high > μ_low
## p-value: 0.1320049 
## Kesimpulan: Tidak cukup bukti perbedaan signifikan 
## 
## SOAL 3: Analisis hubungan e_x dan e_x°
##   mean_curtate mean_complete mean_difference theoretical_difference
## 1       25.622      26.09804       0.4760433                    0.5
##   actual_vs_theoretical
## 1           -0.02395667
## 
## SOAL 4: Analisis interaksi Gender × Smoking
## `summarise()` has grouped output by 'gender'. You can override using the
## `.groups` argument.
## # A tibble: 4 × 6
## # Groups:   gender [2]
##   gender smoker     n mean_life mean_remaining sd_life
##   <chr>  <chr>  <int>     <dbl>          <dbl>   <dbl>
## 1 Female No       181      75.5           26.4   10.2 
## 2 Female Yes       73      77.0           27.0   11.0 
## 3 Male   No       163      73.9           25.5   11.3 
## 4 Male   Yes       83      74.0           25.8    9.41

exercise_results
## $soal1
## # A tibble: 3 × 4
##   income_group     n complete_expectancy    sd
##   <chr>        <int>               <dbl> <dbl>
## 1 High           168                24.8  20.5
## 2 Low            168                25.0  20.4
## 3 Middle         164                28.6  23.6
## 
## $t_test_income
## 
##  Welch Two Sample t-test
## 
## data:  high_income$life_expectancy and low_income$life_expectancy
## t = 1.1189, df = 328.33, p-value = 0.132
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
##  -0.6108233        Inf
## sample estimates:
## mean of x mean of y 
##  75.12684  73.83886 
## 
## 
## $relationship
##   mean_curtate mean_complete mean_difference theoretical_difference
## 1       25.622      26.09804       0.4760433                    0.5
##   actual_vs_theoretical
## 1           -0.02395667
## 
## $interaction
## # A tibble: 4 × 6
## # Groups:   gender [2]
##   gender smoker     n mean_life mean_remaining sd_life
##   <chr>  <chr>  <int>     <dbl>          <dbl>   <dbl>
## 1 Female No       181      75.5           26.4   10.2 
## 2 Female Yes       73      77.0           27.0   11.0 
## 3 Male   No       163      73.9           25.5   11.3 
## 4 Male   Yes       83      74.0           25.8    9.41
# 1. Setup dan Data
#if (!require("tidyverse")) install.packages("tidyverse")
#if (!require("ggplot2")) install.packages("ggplot2")
library(tidyverse)
library(ggplot2)

set.seed(123)

# Fungsi buat data sederhana
create_simple_life_data <- function(n = 500) {
  data.frame(
    id = 1:n,
    age = sample(20:80, n, replace = TRUE),
    gender = sample(c("Male", "Female"), n, replace = TRUE, prob = c(0.48, 0.52)),
    smoker = sample(c("Yes", "No"), n, replace = TRUE, prob = c(0.3, 0.7)),
    life_expectancy = rnorm(n, mean = 75, sd = 10)
  )
}

# 2. Fungsi utama yang sederhana
simple_life_analysis <- function() {
  cat("==================================================\n")
  cat("ANALISIS HARAPAN HIDUP - VERSI SIMPLE\n")
  cat("==================================================\n\n")
  
  # Buat data
  cat("1. MEMBUAT DATA...\n")
  data <- create_simple_life_data(300)
  cat("   Jumlah data:", nrow(data), "\n")
  cat("   Variabel: age, gender, smoker, life_expectancy\n\n")
  
  # Hitung complete dan curtate
  cat("2. PERHITUNGAN HARAPAN HIDUP\n")
  data$complete <- data$life_expectancy - data$age
  data$curtate <- floor(data$complete)
  
  # Statistik dasar
  cat("   Rata-rata usia:", round(mean(data$age), 1), "\n")
  cat("   Rata-rata harapan hidup:", round(mean(data$life_expectancy), 1), "\n")
  cat("   Complete Life Expectancy (e_x°):", round(mean(data$complete), 2), "\n")
  cat("   Curtate Life Expectancy (e_x):", round(mean(data$curtate), 2), "\n")
  cat("   Perbedaan (e_x° - e_x):", round(mean(data$complete) - mean(data$curtate), 2), "\n")
  cat("   (Teori: e_x° ≈ e_x + 0.5)\n\n")
  
  # 3. Pengujian hipotesis
  cat("3. PENGUJIAN HIPOTESIS\n")
  
  # Hipotesis 1: Perbedaan gender
  cat("   Hipotesis 1: Apakah perempuan hidup lebih lama?\n")
  male_le <- data$life_expectancy[data$gender == "Male"]
  female_le <- data$life_expectancy[data$gender == "Female"]
  
  t_test_gender <- t.test(female_le, male_le)
  cat("   Rata-rata Female:", round(mean(female_le), 2), "\n")
  cat("   Rata-rata Male:", round(mean(male_le), 2), "\n")
  cat("   p-value:", round(t_test_gender$p.value, 4), "\n")
  
  if (t_test_gender$p.value < 0.05) {
    cat("   KESIMPULAN: Signifikan (p < 0.05) - Perempuan hidup lebih lama\n")
  } else {
    cat("   KESIMPULAN: Tidak signifikan - Tidak cukup bukti\n")
  }
  cat("\n")
  
  # Hipotesis 2: Pengaruh merokok
  cat("   Hipotesis 2: Apakah merokok mengurangi harapan hidup?\n")
  smoker_le <- data$life_expectancy[data$smoker == "Yes"]
  nonsmoker_le <- data$life_expectancy[data$smoker == "No"]
  
  t_test_smoke <- t.test(nonsmoker_le, smoker_le, alternative = "greater")
  cat("   Rata-rata Non-Smoker:", round(mean(nonsmoker_le), 2), "\n")
  cat("   Rata-rata Smoker:", round(mean(smoker_le), 2), "\n")
  cat("   p-value:", round(t_test_smoke$p.value, 4), "\n")
  
  if (t_test_smoke$p.value < 0.05) {
    cat("   KESIMPULAN: Signifikan - Merokok mengurangi harapan hidup\n")
  } else {
    cat("   KESIMPULAN: Tidak signifikan\n")
  }
  cat("\n")
  
  # 4. Visualisasi
  cat("4. VISUALISASI DATA\n")
  
  # Plot 1: Distribusi harapan hidup
  p1 <- ggplot(data, aes(x = life_expectancy, fill = gender)) +
    geom_histogram(alpha = 0.7, bins = 20, position = "identity") +
    labs(title = "Distribusi Harapan Hidup per Gender",
         x = "Harapan Hidup (tahun)",
         y = "Frekuensi") +
    theme_minimal()
  
  # Plot 2: Complete vs Curtate
  summary_data <- data.frame(
    Type = c("Complete (e_x°)", "Curtate (e_x)"),
    Value = c(mean(data$complete), mean(data$curtate))
  )
  
  p2 <- ggplot(summary_data, aes(x = Type, y = Value, fill = Type)) +
    geom_bar(stat = "identity") +
    labs(title = "Perbandingan Complete vs Curtate Life Expectancy",
         x = "Tipe Harapan Hidup",
         y = "Rata-rata (tahun)") +
    theme_minimal()
  
  # Plot 3: Hubungan usia dan sisa hidup
  p3 <- ggplot(data, aes(x = age, y = complete)) +
    geom_point(aes(color = smoker), alpha = 0.6) +
    geom_smooth(method = "lm", se = FALSE, color = "red") +
    labs(title = "Hubungan Usia dan Sisa Harapan Hidup",
         x = "Usia",
         y = "Sisa Harapan Hidup (tahun)") +
    theme_minimal()
  
  print(p1)
  print(p2)
  print(p3)
  
  # 5. Latihan sederhana
  cat("5. LATIHAN MANDIRI\n")
  cat("   a. Hitung harapan hidup per kelompok gender dan smoker:\n")
  
  exercise_table <- data %>%
    group_by(gender, smoker) %>%
    summarize(
      n = n(),
      mean_age = mean(age),
      mean_life = mean(life_expectancy),
      mean_remaining = mean(complete),
      .groups = "drop"
    )
  
  print(exercise_table)
  
  cat("\n   b. Hitung premi asuransi sederhana (asumsi):\n")
  cat("      Premi = 1,000,000 / (100 - usia) * sisa_hidup\n")
  
  data$premium <- 1000000 / (100 - data$age) * data$complete
  premium_summary <- data %>%
    group_by(gender, smoker) %>%
    summarize(
      avg_premium = mean(premium),
      .groups = "drop"
    )
  
  print(premium_summary)
  
  # 6. Ringkasan
  cat("\n==================================================\n")
  cat("RINGKASAN\n")
  cat("==================================================\n")
  cat("1. Complete Life Expectancy:", round(mean(data$complete), 2), "tahun\n")
  cat("2. Curtate Life Expectancy:", round(mean(data$curtate), 2), "tahun\n")
  cat("3. Selisih:", round(mean(data$complete) - mean(data$curtate), 2), "tahun\n")
  cat("4. Harapan hidup tertinggi:", 
      ifelse(mean(female_le) > mean(male_le), "Perempuan", "Laki-laki"), "\n")
  cat("5. Premi tertinggi:", 
      premium_summary$gender[which.max(premium_summary$avg_premium)], 
      "yang", premium_summary$smoker[which.max(premium_summary$avg_premium)], "\n")
  cat("==================================================\n")
  
  # Return hasil
  return(list(
    data = data,
    t_test_gender = t_test_gender,
    t_test_smoke = t_test_smoke,
    summary = exercise_table,
    premium = premium_summary
  ))
}

# ==============================================
# JALANKAN ANALISIS
# ==============================================

# Jalankan fungsi utama
hasil <- simple_life_analysis()
## ==================================================
## ANALISIS HARAPAN HIDUP - VERSI SIMPLE
## ==================================================
## 
## 1. MEMBUAT DATA...
##    Jumlah data: 300 
##    Variabel: age, gender, smoker, life_expectancy
## 
## 2. PERHITUNGAN HARAPAN HIDUP
##    Rata-rata usia: 50.3 
##    Rata-rata harapan hidup: 74.4 
##    Complete Life Expectancy (e_x°): 24.1 
##    Curtate Life Expectancy (e_x): 23.58 
##    Perbedaan (e_x° - e_x): 0.51 
##    (Teori: e_x° ≈ e_x + 0.5)
## 
## 3. PENGUJIAN HIPOTESIS
##    Hipotesis 1: Apakah perempuan hidup lebih lama?
##    Rata-rata Female: 73.56 
##    Rata-rata Male: 75.39 
##    p-value: 0.1067 
##    KESIMPULAN: Tidak signifikan - Tidak cukup bukti
## 
##    Hipotesis 2: Apakah merokok mengurangi harapan hidup?
##    Rata-rata Non-Smoker: 74.67 
##    Rata-rata Smoker: 73.84 
##    p-value: 0.2471 
##    KESIMPULAN: Tidak signifikan
## 
## 4. VISUALISASI DATA

## `geom_smooth()` using formula = 'y ~ x'

## 5. LATIHAN MANDIRI
##    a. Hitung harapan hidup per kelompok gender dan smoker:
## # A tibble: 4 × 6
##   gender smoker     n mean_age mean_life mean_remaining
##   <chr>  <chr>  <int>    <dbl>     <dbl>          <dbl>
## 1 Female No       119     51.0      73.5           22.5
## 2 Female Yes       42     50.4      73.9           23.5
## 3 Male   No        88     52.1      76.3           24.2
## 4 Male   Yes       51     45.6      73.8           28.2
## 
##    b. Hitung premi asuransi sederhana (asumsi):
##       Premi = 1,000,000 / (100 - usia) * sisa_hidup
## # A tibble: 4 × 3
##   gender smoker avg_premium
##   <chr>  <chr>        <dbl>
## 1 Female No         385756.
## 2 Female Yes        378156.
## 3 Male   No         408068.
## 4 Male   Yes        443939.
## 
## ==================================================
## RINGKASAN
## ==================================================
## 1. Complete Life Expectancy: 24.1 tahun
## 2. Curtate Life Expectancy: 23.58 tahun
## 3. Selisih: 0.51 tahun
## 4. Harapan hidup tertinggi: Laki-laki 
## 5. Premi tertinggi: Male yang Yes 
## ==================================================
hasil
## $data
##      id age gender smoker life_expectancy     complete curtate      premium
## 1     1  50 Female     No        60.34790  10.34790216      10   206958.043
## 2     2  34 Female     No        58.76100  24.76100388      24   375166.725
## 3     3  70 Female     No        76.92297   6.92296879       6   230765.626
## 4     4  33   Male     No        92.44520  59.44520141      59   887241.812
## 5     5  22 Female    Yes        75.78813  53.78813039      53   689591.415
## 6     6  61 Female     No        70.31052   9.31051523       9   238731.160
## 7     7  69   Male    Yes        57.71167 -11.28832991     -12  -364139.674
## 8     8  73   Male    Yes        85.65154  12.65154397      12   468575.703
## 9     9  62 Female    Yes        79.80282  17.80281766      17   468495.202
## 10   10  56 Female    Yes        76.37499  20.37499039      20   463067.963
## 11   11  71   Male     No        82.88957  11.88957450      11   409985.328
## 12   12  33   Male    Yes        66.72920  33.72920084      33   503420.908
## 13   13  73 Female    Yes        77.43944   4.43944151       4   164423.760
## 14   14  44 Female    Yes        65.00137  21.00136975      21   375024.460
## 15   15  45   Male     No        86.41415  41.41415006      41   752984.546
## 16   16  46 Female     No        55.17088   9.17088250       9   169831.157
## 17   17  24 Female     No        74.75493  50.75493252      50   667828.059
## 18   18  70   Male     No        72.91848   2.91847532       2    97282.511
## 19   19  46 Female    Yes        65.12951  19.12950593      19   354250.110
## 20   20  76 Female     No        70.92484  -5.07516207      -6  -211465.086
## 21   21  47   Male     No        69.87220  22.87219594      22   431550.867
## 22   22  76   Male    Yes        64.88440 -11.11560354     -12  -463150.147
## 23   23  28 Female    Yes        57.93598  29.93597943      29   415777.492
## 24   24  48   Male     No        92.81877  44.81876899      44   861899.404
## 25   25  54   Male    Yes        59.55739   5.55739433       5   120812.920
## 26   26  27 Female     No        73.13191  46.13190846      46   631943.951
## 27   27  45 Female     No        73.73328  28.73327766      28   522423.230
## 28   28  26   Male    Yes        64.19582  38.19581503      38   516159.663
## 29   29  61   Male    Yes        76.74957  15.74956963      15   403835.119
## 30   30  28   Male    Yes        63.09004  35.09004255      35   487361.702
## 31   31  38   Male     No        74.45754  36.45753660      36   588024.784
## 32   32  55 Female     No        58.15334   3.15334352       3    70074.300
## 33   33  33 Female    Yes        68.85397  35.85396933      35   535133.871
## 34   34  36   Male    Yes        78.67083  42.67083316      42   666731.768
## 35   35  62 Female     No        50.42657 -11.57342826     -12  -304563.902
## 36   36  58 Female     No        72.95665  14.95664659      14   356110.633
## 37   37  72 Female     No        80.58336   8.58336203       8   306548.644
## 38   38  31   Male    Yes        78.24211  47.24210607      47   684668.204
## 39   39  34 Female     No        81.97737  47.97737006      47   726929.849
## 40   40  51 Female     No        64.77427  13.77426937      13   281107.538
## 41   41  61   Male     No        73.31729  12.31729241      12   315828.011
## 42   42  64 Female    Yes        67.40511   3.40510856       3    94586.349
## 43   43  26   Male    Yes        72.47091  46.47090763      46   627985.238
## 44   44  28 Female     No        84.52680  56.52679991      56   785094.443
## 45   45  60 Female     No        72.24585  12.24585102      12   306146.275
## 46   46  29   Male    Yes        62.67194  33.67193961      33   474252.671
## 47   47  42   Male    Yes        77.37504  35.37503584      35   609914.411
## 48   48  46   Male    Yes        85.51259  39.51259289      39   731714.683
## 49   49  79 Female     No        74.44542  -4.55457945      -5  -216884.736
## 50   50  72 Female     No        69.62323  -2.37676867      -3   -84884.596
## 51   51  26   Male     No        73.50382  47.50382315      47   641943.556
## 52   52  72 Female    Yes        70.90086  -1.09914409      -2   -39255.146
## 53   53  46 Female    Yes        75.17542  29.17541600      29   540285.481
## 54   54  51   Male     No        82.51041  31.51041104      31   643069.613
## 55   55  57 Female    Yes        78.62371  21.62371124      21   502877.006
## 56   56  44 Female     No        69.14851  25.14851130      25   449080.559
## 57   57  53   Male     No        83.15959  30.15959483      30   641693.507
## 58   58  48 Female    Yes        72.33063  24.33063406      24   467896.809
## 59   59  24 Female     No        70.28279  46.28279337      46   608984.123
## 60   60  27 Female     No        73.88195  46.88194869      46   642218.475
## 61   61  31 Female     No        76.38753  45.38752680      45   657790.244
## 62   62  32 Female     No        76.08582  44.08581793      44   648320.852
## 63   63  37 Female     No        63.74970  26.74969883      26   424598.394
## 64   64  52   Male     No        76.20595  24.20595490      24   504290.727
## 65   65  76 Female     No        76.49818   0.49817935       0    20757.473
## 66   66  46   Male     No        73.68602  27.68601659      27   512704.011
## 67   67  44   Male     No        80.81616  36.81615900      36   657431.411
## 68   68  57   Male    Yes        75.53327  18.53326870      18   431006.249
## 69   69  40 Female     No        60.66523  20.66523332      20   344420.555
## 70   70  34 Female     No        52.77009  18.77009350      18   284395.356
## 71   71  60   Male     No        99.38003  39.38003047      39   984500.762
## 72   72  66   Male    Yes        84.41265  18.41265109      18   541548.562
## 73   73  45   Male    Yes        75.98949  30.98949299      30   563445.327
## 74   74  79 Female    Yes        69.06550  -9.93450389     -10  -473071.614
## 75   75  50   Male    Yes        68.04705  18.04705064      18   360941.013
## 76   76  35   Male    Yes        80.70353  45.70353337      45   703131.283
## 77   77  71   Male     No        65.57792  -5.42208165      -6  -186968.333
## 78   78  49 Female     No        71.67901  22.67901060      22   444686.482
## 79   79  25 Female     No        80.62611  55.62611271      55   741681.503
## 80   80  62 Female     No        64.00915   2.00914549       2    52872.250
## 81   81  27 Female     No        76.55948  49.55948423      49   678897.044
## 82   82  41   Male     No        85.31787  44.31787176      44   751150.369
## 83   83  41 Female     No        83.43324  42.43324032      42   719207.463
## 84   84  58   Male     No        78.29015  20.29014833      20   483098.770
## 85   85  50   Male     No        56.18321   6.18321289       6   123664.258
## 86   86  67 Female     No        71.29311   4.29311220       4   130094.309
## 87   87  36 Female     No        65.53461  29.53460861      29   461478.260
## 88   88  73 Female     No        72.81591  -0.18409405      -1    -6818.298
## 89   89  69 Female     No        68.92894  -0.07106249      -1    -2292.339
## 90   90  68 Female     No        56.09366 -11.90633913     -12  -372073.098
## 91   91  53   Male     No        74.50164  21.50163749      21   457481.649
## 92   92  23   Male    Yes        60.88209  37.88208506      37   491975.131
## 93   93  32 Female     No        63.70845  31.70845313      31   466300.781
## 94   94  24   Male     No        81.97000  57.96999800      57   762763.132
## 95   95  73   Male    Yes        78.17461   5.17460726       5   191652.121
## 96   96  70 Female     No        66.58064  -3.41936393      -4  -113978.798
## 97   97  44 Female     No        81.06060  37.06060494      37   661796.517
## 98   98  71   Male     No        81.56468  10.56468109      10   364299.348
## 99   99  41 Female     No        75.73776  34.73776168      34   588775.622
## 100 100  44 Female     No        90.35197  46.35197483      46   827713.836
## 101 101  51   Male     No        79.13916  28.13916298      28   574268.632
## 102 102  65 Female     No        84.39767  19.39767493      19   554219.284
## 103 103  44   Male     No        72.23882  28.23882168      28   504264.673
## 104 104  42 Female     No        68.07489  26.07489035      26   449567.075
## 105 105  54 Female    Yes        90.36321  36.36321087      36   790504.584
## 106 106  59 Female     No        76.72731  17.72731485      17   432373.533
## 107 107  67 Female     No        66.32396  -0.67603807      -1   -20486.002
## 108 108  49   Male     No        70.14097  21.14096778      21   414528.780
## 109 109  31 Female     No        59.28478  28.28477806      28   409924.320
## 110 110  50 Female    Yes        76.72144  26.72144429      26   534428.886
## 111 111  76 Female     No        76.63136   0.63136413       0    26306.839
## 112 112  65   Male     No        84.14156  19.14155854      19   546901.673
## 113 113  49   Male    Yes        76.70645  27.70644565      27   543263.640
## 114 114  54   Male    Yes        81.82201  27.82201448      27   604826.402
## 115 115  33 Female    Yes        69.40793  36.40793154      36   543401.963
## 116 116  48   Male     No        68.55563  20.55563070      20   395300.590
## 117 117  51   Male    Yes        72.60513  21.60512723      21   440920.964
## 118 118  26 Female    Yes        91.82444  65.82444093      65   889519.472
## 119 119  22 Female     No        72.61187  50.61186926      50   648870.119
## 120 120  42 Female    Yes        78.27631  36.27631386      36   625453.687
## 121 121  73   Male     No        64.14768  -8.85231827      -9  -327863.639
## 122 122  77   Male     No        77.51060   0.51059551       0    22199.805
## 123 123  34   Male    Yes        88.98075  54.98075147      54   833041.689
## 124 124  40 Female     No        71.73383  31.73382877      31   528897.146
## 125 125  56   Male    Yes        85.55361  29.55361234      29   671673.008
## 126 126  27 Female     No        66.93572  39.93572018      39   547064.660
## 127 127  70   Male     No        66.84404  -3.15596411      -4  -105198.804
## 128 128  29 Female    Yes        67.98522  38.98521580      38   549087.546
## 129 129  69 Female    Yes        60.42355  -8.57645431      -9  -276659.816
## 130 130  61 Female     No        69.37705   8.37705273       8   214796.224
## 131 131  63 Female     No        94.17803  31.17803049      31   842649.473
## 132 132  53 Female    Yes        72.51345  19.51345063      19   415179.801
## 133 133  29   Male     No        88.80644  59.80643747      59   842344.190
## 134 134  41   Male     No        76.20607  35.20607089      35   596713.066
## 135 135  31 Female     No        72.39518  41.39518012      41   599930.147
## 136 136  39   Male     No        78.44850  39.44849936      39   646696.711
## 137 137  65   Male     No        66.95393   1.95392956       1    55826.559
## 138 138  36   Male    Yes        86.08556  50.08556092      50   782586.889
## 139 139  65 Female     No        63.96921  -1.03079161      -2   -29451.189
## 140 140  73 Female     No        83.01130  10.01130423      10   370789.046
## 141 141  54   Male    Yes        77.38551  23.38550624      23   508380.571
## 142 142  59   Male     No        76.38502  17.38501860      17   424024.844
## 143 143  65 Female     No        82.26280  17.26280300      17   493222.943
## 144 144  70 Female     No        66.41004  -3.58995725      -4  -119665.242
## 145 145  49 Female     No        62.72416  13.72416064      13   269101.189
## 146 146  34 Female     No        72.08145  38.08144599      38   576991.606
## 147 147  43   Male     No        87.12737  44.12736728      44   774164.338
## 148 148  68   Male     No        80.77182  12.77182206      12   399119.439
## 149 149  42   Male    Yes        97.80428  55.80428421      55   962142.831
## 150 150  62 Female     No        64.95890   2.95889915       2    77865.767
## 151 151  26 Female     No        76.05948  50.05948239      50   676479.492
## 152 152  48   Male     No       105.57125  57.57125296      57  1107139.480
## 153 153  34 Female     No        75.19743  41.19742651      41   624203.432
## 154 154  42 Female     No        83.91496  41.91495753      41   722671.682
## 155 155  45 Female     No        83.49443  38.49443442      38   699898.808
## 156 156  57   Male     No        91.10905  34.10904824      34   793233.680
## 157 157  65 Female     No        75.55300  10.55299946      10   301514.270
## 158 158  51 Female     No        87.05396  36.05396448      36   735795.193
## 159 159  26   Male     No        57.53716  31.53715509      31   426177.771
## 160 160  46 Female     No        67.48127  21.48126956      21   397801.288
## 161 161  61   Male     No        75.77622  14.77622246      14   378877.499
## 162 162  24 Female    Yes        69.09895  45.09894856      45   593407.218
## 163 163  25   Male    Yes        77.24218  52.24218285      52   696562.438
## 164 164  35 Female     No        57.84396  22.84396057      22   351445.547
## 165 165  43   Male    Yes        80.35228  37.35227760      37   655303.116
## 166 166  51   Male     No        70.07461  19.07460532      19   389277.660
## 167 167  40 Female     No        70.37173  30.37172723      30   506195.454
## 168 168  74   Male     No        73.49644  -0.50356069      -1   -19367.719
## 169 169  30 Female     No        69.15030  39.15029581      39   559289.940
## 170 170  55 Female     No        57.73492   2.73491803       2    60775.956
## 171 171  63   Male    Yes        81.04025  18.04024959      18   487574.313
## 172 172  65 Female     No        75.26877  10.26876990      10   293393.426
## 173 173  79   Male    Yes        44.53033 -34.46966569     -35 -1641412.652
## 174 174  80 Female     No        86.07528   6.07527710       6   303763.855
## 175 175  38   Male     No        68.08325  30.08324879      30   485213.690
## 176 176  44   Male    Yes        76.95710  32.95709640      32   588519.579
## 177 177  58 Female     No        77.92254  19.92254428      19   474346.292
## 178 178  73 Female     No        64.48104  -8.51896381      -9  -315517.178
## 179 179  45   Male     No        59.90306  14.90305601      14   270964.655
## 180 180  28 Female     No        90.32718  62.32718319      62   865655.322
## 181 181  26   Male    Yes        94.23694  68.23693661      68   922120.765
## 182 182  53   Male    Yes        80.98443  27.98443438      27   595413.497
## 183 183  67 Female     No        76.66663   9.66663054       9   292928.198
## 184 184  32 Female     No        76.37199  44.37198837      44   652529.241
## 185 185  38   Male     No        78.13710  40.13709510      40   647372.502
## 186 186  75 Female    Yes        72.26401  -2.73598781      -3  -109439.512
## 187 187  71   Male     No        85.48433  14.48432867      14   499459.609
## 188 188  66 Female     No        81.93806  15.93805852      15   468766.427
## 189 189  58 Female     No        69.04932  11.04932014      11   263079.051
## 190 190  23   Male     No        76.23842  53.23841943      53   691408.045
## 191 191  20   Male     No        75.37522  55.37522092      55   692190.261
## 192 192  59 Female     No        75.65903  16.65903233      16   406317.862
## 193 193  49 Female     No        85.28351  36.28351215      36   711441.415
## 194 194  49 Female     No        69.85139  20.85139277      20   408850.839
## 195 195  71 Female     No        81.23276  10.23276425      10   352853.940
## 196 196  44 Female     No        76.73953  32.73953094      32   584634.481
## 197 197  35 Female    Yes        74.65219  39.65219115      39   610033.710
## 198 198  43   Male     No        84.80824  41.80823565      41   733477.818
## 199 199  73 Female     No        76.38592   3.38592210       3   125404.522
## 200 200  30   Male    Yes        64.03830  34.03830494      34   486261.499
## 201 201  67 Female     No        62.16291  -4.83708808      -5  -146578.427
## 202 202  39 Female     No        71.57481  32.57480909      32   534013.264
## 203 203  59 Female     No        67.83601   8.83601292       8   215512.510
## 204 204  22   Male     No        62.60655  40.60654733      40   520596.761
## 205 205  48 Female    Yes        68.41491  20.41491398      20   392594.500
## 206 206  55   Male    Yes        56.73686   1.73686495       1    38596.999
## 207 207  71 Female     No        70.81396  -0.18603923      -1    -6415.146
## 208 208  63 Female     No        71.33796   8.33795989       8   225350.267
## 209 209  41   Male     No        72.05999  31.05998887      31   526440.489
## 210 210  68 Female     No        74.76877   6.76877367       6   211524.177
## 211 211  61   Male     No        78.62399  17.62399384      17   451897.278
## 212 212  78 Female     No        70.33286  -7.66714383      -8  -348506.538
## 213 213  39 Female     No        77.98514  38.98513610      38   639100.592
## 214 214  30 Female     No        65.47292  35.47291853      35   506755.979
## 215 215  76 Female    Yes        81.67541   5.67540579       5   236475.241
## 216 216  74 Female    Yes        75.87239   1.87238539       1    72014.823
## 217 217  27   Male     No        73.90791  46.90790552      46   642574.048
## 218 218  65   Male     No        68.22508   3.22507803       3    92145.087
## 219 219  40 Female     No        91.98823  51.98822656      51   866470.443
## 220 220  64   Male     No        78.85831  14.85831002      14   412730.834
## 221 221  21 Female    Yes        77.46757  56.46757029      56   714779.371
## 222 222  62   Male     No        49.13068 -12.86931756     -13  -338666.252
## 223 223  32   Male    Yes        55.36897  23.36896980      23   343661.321
## 224 224  65 Female     No        74.38695   9.38695172       9   268198.620
## 225 225  25   Male    Yes        83.95253  58.95252538      58   786033.672
## 226 226  76 Female     No        74.71941  -1.28058771      -2   -53357.821
## 227 227  27   Male     No        65.00253  38.00252983      38   520582.600
## 228 228  63 Female    Yes        73.40396  10.40396039      10   281188.119
## 229 229  51   Male     No        69.72306  18.72306352      18   382103.337
## 230 230  77   Male     No        77.16156   0.16156456       0     7024.546
## 231 231  55 Female     No        65.81045  10.81045020      10   240232.227
## 232 232  64   Male    Yes        65.99405   1.99405345       1    55390.374
## 233 233  80 Female    Yes        77.88609  -2.11391209      -3  -105695.605
## 234 234  33   Male     No        84.28885  51.28884794      51   765505.193
## 235 235  72 Female     No        91.73047  19.73047212      19   704659.719
## 236 236  35   Male     No        77.85514  42.85514424      42   659309.911
## 237 237  42   Male     No        71.27418  29.27417559      29   504727.165
## 238 238  80 Female    Yes        60.66117 -19.33882756     -20  -966941.378
## 239 239  52   Male     No        66.15532  14.15532080      14   294902.517
## 240 240  59   Male     No        86.24183  27.24182941      27   664434.864
## 241 241  59 Female     No        67.77194   8.77194203       8   213949.806
## 242 242  29 Female     No        79.62665  50.62664957      50   713051.402
## 243 243  44 Female    Yes        71.87446  27.87446470      27   497758.298
## 244 244  27   Male    Yes        83.48254  56.48254497      56   773733.493
## 245 245  37   Male    Yes        74.14680  37.14680423      37   589631.813
## 246 246  80   Male     No        67.84655 -12.15345095     -13  -607672.548
## 247 247  72   Male     No        67.63304  -4.36696068      -5  -155962.881
## 248 248  28   Male    Yes        74.92777  46.92777361      46   651774.633
## 249 249  26   Male     No        82.00478  56.00477870      56   756821.334
## 250 250  26 Female     No        91.26392  65.26392345      65   881944.911
## 251 251  77 Female    Yes        91.24661  14.24660897      14   619417.781
## 252 252  80 Female     No        89.18685   9.18685299       9   459342.649
## 253 253  79   Male     No        65.09507 -13.90493419     -14  -662139.724
## 254 254  29   Male     No        81.13368  52.13368245      52   734277.218
## 255 255  43 Female     No        70.11174  27.11173769      27   475644.521
## 256 256  73   Male     No        70.77413  -2.22587281      -3   -82439.734
## 257 257  42   Male     No        77.03246  35.03245676      35   604007.875
## 258 258  45 Female     No        89.95230  44.95229991      44   817314.544
## 259 259  62   Male     No        62.14715   0.14714847       0     3872.328
## 260 260  52   Male     No        96.46510  44.46510413      44   926356.336
## 261 261  76   Male     No        73.37354  -2.62645541      -3  -109435.642
## 262 262  80   Male     No        69.76618 -10.23382144     -11  -511691.072
## 263 263  48   Male     No        83.43447  35.43446866      35   681432.090
## 264 264  70   Male    Yes        71.98692   1.98691666       1    66230.555
## 265 265  29 Female     No        85.87842  56.87841508      56   801104.438
## 266 266  72   Male    Yes        75.40935   3.40934820       3   121762.436
## 267 267  73   Male     No        77.86535   4.86535252       4   180198.241
## 268 268  32 Female     No        74.95887  42.95886593      42   631748.028
## 269 269  62 Female     No        81.86813  19.86813204      19   522845.580
## 270 270  30   Male    Yes        73.55320  43.55320420      43   622188.631
## 271 271  44 Female     No        68.87922  24.87922065      24   444271.797
## 272 272  71   Male     No        81.30565  10.30565378      10   355367.372
## 273 273  45   Male    Yes        63.07297  18.07297427      18   328599.532
## 274 274  26   Male     No        83.34967  57.34967448      57   774995.601
## 275 275  44 Female     No        80.97328  36.97328115      36   660237.163
## 276 276  42   Male    Yes        63.19187  21.19187309      21   365377.122
## 277 277  45 Female    Yes        81.67472  36.67471782      36   666813.051
## 278 278  51 Female     No        66.21800  15.21800461      15   310571.523
## 279 279  39   Male     No        85.97471  46.97471316      46   770077.265
## 280 280  80   Male     No        64.08962 -15.91038436     -16  -795519.218
## 281 281  43   Male     No        74.07024  31.07024469      31   545092.012
## 282 282  76   Male     No        59.90417 -16.09583205     -17  -670659.669
## 283 283  28 Female    Yes        72.36108  44.36108052      44   616126.118
## 284 284  60   Male     No        80.98658  20.98657541      20   524664.385
## 285 285  56 Female     No       108.04330  52.04329844      52  1182802.237
## 286 286  42 Female    Yes        84.47105  42.47104778      42   732259.444
## 287 287  33   Male    Yes        62.84598  29.84598475      29   445462.459
## 288 288  65 Female     No        83.92175  18.92175371      18   540621.535
## 289 289  25 Female     No        58.38580  33.38579630      33   445143.951
## 290 290  46   Male     No        72.96350  26.96350040      26   499324.082
## 291 291  20 Female    Yes        84.57070  64.57070086      64   807133.761
## 292 292  45 Female    Yes        52.27288   7.27287786       7   132234.143
## 293 293  61   Male     No        82.24725  21.24725141      21   544801.318
## 294 294  68 Female    Yes        83.85334  15.85334027      15   495416.883
## 295 295  77 Female     No        66.61089 -10.38911117     -11  -451700.486
## 296 296  36 Female     No        73.42818  37.42818060      37   584815.322
## 297 297  48   Male     No        97.57235  49.57234977      49   953314.419
## 298 298  45 Female    Yes        67.63177  22.63176822      22   411486.695
## 299 299  46   Male    Yes        76.42436  30.42436401      30   563414.148
## 300 300  40 Female    Yes        73.32863  33.32863299      33   555477.217
## 
## $t_test_gender
## 
##  Welch Two Sample t-test
## 
## data:  female_le and male_le
## t = -1.6184, df = 282.01, p-value = 0.1067
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.066506  0.396827
## sample estimates:
## mean of x mean of y 
##  73.55824  75.39308 
## 
## 
## $t_test_smoke
## 
##  Welch Two Sample t-test
## 
## data:  nonsmoker_le and smoker_le
## t = 0.68489, df = 179.48, p-value = 0.2471
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
##  -1.174109       Inf
## sample estimates:
## mean of x mean of y 
##  74.66577  73.83548 
## 
## 
## $summary
## # A tibble: 4 × 6
##   gender smoker     n mean_age mean_life mean_remaining
##   <chr>  <chr>  <int>    <dbl>     <dbl>          <dbl>
## 1 Female No       119     51.0      73.5           22.5
## 2 Female Yes       42     50.4      73.9           23.5
## 3 Male   No        88     52.1      76.3           24.2
## 4 Male   Yes       51     45.6      73.8           28.2
## 
## $premium
## # A tibble: 4 × 3
##   gender smoker avg_premium
##   <chr>  <chr>        <dbl>
## 1 Female No         385756.
## 2 Female Yes        378156.
## 3 Male   No         408068.
## 4 Male   Yes        443939.
# Akses hasil spesifik
cat("\n\n=== HASIL DETAIL ===\n")
## 
## 
## === HASIL DETAIL ===
cat("Data Test Gender - p-value:", hasil$t_test_gender$p.value, "\n")
## Data Test Gender - p-value: 0.1066952
cat("Data Test Smoking - p-value:", hasil$t_test_smoke$p.value, "\n")
## Data Test Smoking - p-value: 0.2471491