# ==============================================================================
# SCRIPT R: UJI VALIDITAS DAN RELIABILITAS INSTRUMEN ANGKET (SKALA LIKERT)
# ==============================================================================


library(readxl)
## Warning: package 'readxl' was built under R version 4.5.2
library(psych)
## Warning: package 'psych' was built under R version 4.5.3
data_raw  <- read_excel("C:/Users/ASUS/Downloads/Persepsi dan Pemahaman Mahasiswa Universitas Palangka Raya Terhadap Kebakaran Hutan dan Lahan Serta Penanganannya di Kalimantan Tengah (Responses).xlsx")

# 3. Ekstraksi 22 Kolom Pertanyaan (Kolom 6 hingga 27)
data_items <- data_raw[, 6:27]

# Konversi teks Likert ke numerik secara otomatis jika data berformat teks
likert_map <- c(
  "Sangat Tidak Setuju" = 1,
  "Tidak Setuju"        = 2,
  "Netral"              = 3,
  "Setuju"              = 4,
  "Sangat Setuju"       = 5
)

for(i in 1:ncol(data_items)){
  if(is.character(data_items[[i]])){
    data_items[[i]] <- as.numeric(likert_map[data_items[[i]]])
  }
}

# 4. Penamaan Kolom Sesuai Kode Butir Pernyataan pada Kisi-Kisi Instrumen
colnames(data_items) <- c(
  "A1.1", "A2.1", "A3.1", "B1.1", "B2.1", "B3.1", 
  "C1.1", "C2.1", "C3.1", "D1.1", "D2.1", "A1.2", 
  "A2.2", "A3.2", "B1.2", "B2.2", "B3.2", "C1.2", 
  "C2.2", "C3.2", "D1.2", "D2.2"
)

# 5. Pengelompokan Item Berdasarkan 4 Aspek Kisi-Kisi
items_A <- c("A1.1", "A1.2", "A2.1", "A2.2", "A3.1", "A3.2") # Persepsi Karhutla
items_B <- c("B1.1", "B1.2", "B2.1", "B2.2", "B3.1", "B3.2") # Pemahaman Karhutla
items_C <- c("C1.1", "C1.2", "C2.1", "C2.2", "C3.1", "C3.2") # Persepsi Perkuliahan Online
items_D <- c("D1.1", "D1.2", "D2.1", "D2.2")                # Dampak Karhutla thd Perkuliahan

# Hitung Skor Total Per Aspek
data_items$Total_A <- rowSums(data_items[, items_A])
data_items$Total_B <- rowSums(data_items[, items_B])
data_items$Total_C <- rowSums(data_items[, items_C])
data_items$Total_D <- rowSums(data_items[, items_D])

# 6. Fungsi Uji Validitas Pearson Product-Moment
uji_validitas <- function(df, item_list, col_total, alpha = 0.05) {
  N <- nrow(df)
  df_val <- N - 2
  r_tabel <- qt(1 - alpha/2, df_val) / sqrt(df_val + (qt(1 - alpha/2, df_val))^2)
  
  hasil <- data.frame(
    Kode_Item = character(),
    r_Hitung  = numeric(),
    r_Tabel   = numeric(),
    p_Value   = numeric(),
    Keputusan = character(),
    stringsAsFactors = FALSE
  )
  
  for (item in item_list) {
    test <- cor.test(df[[item]], df[[col_total]], method = "pearson")
    r_hit <- unname(test$estimate)
    p_val <- test$p.value
    status <- if(r_hit > r_tabel && p_val < alpha) "VALID" else "TIDAK VALID"
    
    hasil <- rbind(hasil, data.frame(
      Kode_Item = item,
      r_Hitung  = round(r_hit, 4),
      r_Tabel   = round(r_tabel, 4),
      p_Value   = round(p_val, 4),
      Keputusan = status
    ))
  }
  return(hasil)
}

# ------------------------------------------------------------------------------
# HASIL ANALISIS
# ------------------------------------------------------------------------------

# 1. Output Uji Validitas Per Aspek
cat("--- ASPEK A: Persepsi Mahasiswa terhadap Karhutla ---\n")
## --- ASPEK A: Persepsi Mahasiswa terhadap Karhutla ---
print(uji_validitas(data_items, items_A, "Total_A"))
##   Kode_Item r_Hitung r_Tabel p_Value Keputusan
## 1      A1.1   0.9826  0.4132       0     VALID
## 2      A1.2   0.9552  0.4132       0     VALID
## 3      A2.1   0.9826  0.4132       0     VALID
## 4      A2.2   0.9704  0.4132       0     VALID
## 5      A3.1   0.9750  0.4132       0     VALID
## 6      A3.2   0.8670  0.4132       0     VALID
cat("\n--- ASPEK B: Pemahaman Mahasiswa mengenai Karhutla ---\n")
## 
## --- ASPEK B: Pemahaman Mahasiswa mengenai Karhutla ---
print(uji_validitas(data_items, items_B, "Total_B"))
##   Kode_Item r_Hitung r_Tabel p_Value Keputusan
## 1      B1.1   0.8173  0.4132       0     VALID
## 2      B1.2   0.8680  0.4132       0     VALID
## 3      B2.1   0.8964  0.4132       0     VALID
## 4      B2.2   0.9383  0.4132       0     VALID
## 5      B3.1   0.8189  0.4132       0     VALID
## 6      B3.2   0.8894  0.4132       0     VALID
cat("\n--- ASPEK C: Persepsi Mahasiswa terhadap Perkuliahan Online ---\n")
## 
## --- ASPEK C: Persepsi Mahasiswa terhadap Perkuliahan Online ---
print(uji_validitas(data_items, items_C, "Total_C"))
##   Kode_Item r_Hitung r_Tabel p_Value Keputusan
## 1      C1.1   0.8458  0.4132       0     VALID
## 2      C1.2   0.8242  0.4132       0     VALID
## 3      C2.1   0.8123  0.4132       0     VALID
## 4      C2.2   0.7824  0.4132       0     VALID
## 5      C3.1   0.9466  0.4132       0     VALID
## 6      C3.2   0.7824  0.4132       0     VALID
cat("\n--- ASPEK D: Dampak Karhutla terhadap Perkuliahan ---\n")
## 
## --- ASPEK D: Dampak Karhutla terhadap Perkuliahan ---
print(uji_validitas(data_items, items_D, "Total_D"))
##   Kode_Item r_Hitung r_Tabel p_Value Keputusan
## 1      D1.1   0.9400  0.4132  0.0000     VALID
## 2      D1.2   0.5691  0.4132  0.0046     VALID
## 3      D2.1   0.8442  0.4132  0.0000     VALID
## 4      D2.2   0.7371  0.4132  0.0001     VALID
# 2. Output Uji Reliabilitas Cronbach's Alpha
cat("\n--- REKAPITULASI UJI RELIABILITAS (CRONBACH'S ALPHA) ---\n")
## 
## --- REKAPITULASI UJI RELIABILITAS (CRONBACH'S ALPHA) ---
alpha_A <- psych::alpha(data_items[, items_A], warnings = FALSE)$total$raw_alpha
## Warning in log(det(m.inv.r)): NaNs produced
## The determinant of the smoothed correlation was zero.
## This means the objective function is not defined for the null model either.
## The Chi square is thus based upon observed correlations.
alpha_B <- psych::alpha(data_items[, items_B], warnings = FALSE)$total$raw_alpha
alpha_C <- psych::alpha(data_items[, items_C], warnings = FALSE)$total$raw_alpha
## Warning in cor.smooth(r): Matrix was not positive definite, smoothing was done
## In factor.stats, I could not find the RMSEA upper bound . Sorry about that
## In smc, smcs < 0 were set to .0
## In smc, smcs < 0 were set to .0
## In smc, smcs < 0 were set to .0
## In smc, smcs < 0 were set to .0
## In smc, smcs < 0 were set to .0
## In smc, smcs < 0 were set to .0
alpha_D <- psych::alpha(data_items[, items_D], warnings = FALSE)$total$raw_alpha

rekap_rel <- data.frame(
  Aspek = c("Aspek A (Persepsi Karhutla)", 
            "Aspek B (Pemahaman Karhutla)", 
            "Aspek C (Persepsi Perkuliahan Online)", 
            "Aspek D (Dampak Perkuliahan)"),
  Cronbach_Alpha = round(c(alpha_A, alpha_B, alpha_C, alpha_D), 4),
  Kriteria = ifelse(c(alpha_A, alpha_B, alpha_C, alpha_D) >= 0.70, "RELIABEL", "TIDAK RELIABEL")
)

print(rekap_rel, row.names = FALSE)
##                                  Aspek Cronbach_Alpha Kriteria
##            Aspek A (Persepsi Karhutla)         0.9801 RELIABEL
##           Aspek B (Pemahaman Karhutla)         0.9337 RELIABEL
##  Aspek C (Persepsi Perkuliahan Online)         0.8955 RELIABEL
##           Aspek D (Dampak Perkuliahan)         0.7675 RELIABEL