# 1. MEMUAT PACKAGE DAN DATA
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.5.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Warning: package 'car' was built under R version 4.5.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.2
# BACA FILE FEATURES
features <- read.csv("C:/Users/Msi user/Downloads/ai_productivity_features.csv")
colnames(features)
##  [1] "Employee_ID"                  "job_role"                    
##  [3] "experience_years"             "ai_tool_usage_hours_per_week"
##  [5] "tasks_automated_percent"      "manual_work_hours_per_week"  
##  [7] "learning_time_hours_per_week" "deadline_pressure_level"     
##  [9] "meeting_hours_per_week"       "collaboration_hours_per_week"
## [11] "error_rate_percent"           "task_complexity_score"       
## [13] "focus_hours_per_day"          "work_life_balance_score"     
## [15] "burnout_risk_score"
# Ambil 2 variabel dan rename
data <- features[, c("focus_hours_per_day", "task_complexity_score")]
colnames(data) <- c("jam_ai", "skill_ai")
data <- na.omit(data)

# 2. ANALISIS DESKRIPTIF DAN VISUALISASI
cat("Statistik Deskriptif:\n")
## Statistik Deskriptif:
summary(data)
##      jam_ai         skill_ai     
##  Min.   :1.000   Min.   : 1.000  
##  1st Qu.:3.900   1st Qu.: 3.000  
##  Median :4.800   Median : 6.000  
##  Mean   :4.694   Mean   : 5.508  
##  3rd Qu.:5.600   3rd Qu.: 8.000  
##  Max.   :8.000   Max.   :10.000
# Scatter plot
ggplot(data, aes(x = jam_ai, y = skill_ai)) +
  geom_point(color = "blue", size = 3) +
  labs(title = "Hubungan Jam Penggunaan AI dan Skill AI",
       x = "Jam Penggunaan AI",
       y = "Skill AI") +
  theme_minimal()

# Korelasi
cor_test <- cor.test(data$jam_ai, data$skill_ai)
cat("\nKorelasi Pearson:", round(cor_test$estimate, 4), "\n")
## 
## Korelasi Pearson: -0.0103
cat("p-value:", round(cor_test$p.value, 4), "\n")
## p-value: 0.4912
# 3. MODEL REGRESI
model <- lm(skill_ai ~ jam_ai, data = data)
summary_model <- summary(model)
summary_model
## 
## Call:
## lm(formula = skill_ai ~ jam_ai, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5887 -2.5014  0.4333  2.4904  4.5651 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.61068    0.15575  36.024   <2e-16 ***
## jam_ai      -0.02197    0.03191  -0.688    0.491    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.862 on 4498 degrees of freedom
## Multiple R-squared:  0.0001054,  Adjusted R-squared:  -0.0001169 
## F-statistic: 0.4739 on 1 and 4498 DF,  p-value: 0.4912
# 4. UJI ASUMSI REGRESI LINEAR
cat("\n=== UJI ASUMSI REGRESI LINEAR ===\n")
## 
## === UJI ASUMSI REGRESI LINEAR ===
# 4.1 Normalitas Residual
shapiro_test <- shapiro.test(residuals(model))
cat("1. UJI NORMALITAS (Shapiro-Wilk):\n")
## 1. UJI NORMALITAS (Shapiro-Wilk):
cat("   Statistik W =", round(shapiro_test$statistic, 4), "\n")
##    Statistik W = 0.9394
cat("   p-value =", round(shapiro_test$p.value, 4), "\n")
##    p-value = 0
if(shapiro_test$p.value > 0.05) {
  cat("   Keputusan: Residual berdistribusi normal\n")
} else {
  cat("   Keputusan: Residual tidak normal\n")
}
##    Keputusan: Residual tidak normal
# Q-Q Plot
qqnorm(residuals(model), main = "Q-Q Plot Residual")
qqline(residuals(model), col = "red")

# 4.2 Homoskedastisitas
bp_test <- bptest(model)
cat("\n2. UJI HOMOSKEDASTISITAS (Breusch-Pagan):\n")
## 
## 2. UJI HOMOSKEDASTISITAS (Breusch-Pagan):
cat("   Statistik LM =", round(bp_test$statistic, 4), "\n")
##    Statistik LM = 0.0128
cat("   p-value =", round(bp_test$p.value, 4), "\n")
##    p-value = 0.91
if(bp_test$p.value > 0.05) {
  cat("   Keputusan: Varian residual homogen\n")
} else {
  cat("   Keputusan: Ada heteroskedastisitas\n")
}
##    Keputusan: Varian residual homogen
# Plot Residual vs Fitted
plot(fitted(model), residuals(model),
     main = "Residual vs Fitted Values",
     xlab = "Fitted Values", 
     ylab = "Residuals",
     pch = 19, col = "blue")
abline(h = 0, col = "red", lty = 2)

# 4.3 Autokorelasi
dw_test <- dwtest(model)
cat("\n3. UJI AUTOKORELASI (Durbin-Watson):\n")
## 
## 3. UJI AUTOKORELASI (Durbin-Watson):
cat("   Statistik DW =", round(dw_test$statistic, 4), "\n")
##    Statistik DW = 1.9896
cat("   p-value =", round(dw_test$p.value, 4), "\n")
##    p-value = 0.3636
if(dw_test$p.value > 0.05) {
  cat("   Keputusan: Tidak ada autokorelasi\n")
} else {
  cat("   Keputusan: Ada autokorelasi\n")
}
##    Keputusan: Tidak ada autokorelasi
# 5. INTERPRETASI KOEFISIEN
cat("\n=== INTERPRETASI KOEFISIEN ===\n")
## 
## === INTERPRETASI KOEFISIEN ===
intercept <- coef(model)[1]
slope <- coef(model)[2]

cat("Persamaan Regresi: Skill_AI =", 
    round(intercept, 2), "+", 
    round(slope, 2), "* Jam_AI\n")
## Persamaan Regresi: Skill_AI = 5.61 + -0.02 * Jam_AI
cat("\nInterpretasi:\n")
## 
## Interpretasi:
cat("1. Intercept (β0 =", round(intercept, 2), "):\n")
## 1. Intercept (β0 = 5.61 ):
cat("   Skill AI ketika jam penggunaan = 0 adalah", 
    round(intercept, 2), "\n")
##    Skill AI ketika jam penggunaan = 0 adalah 5.61
cat("2. Slope (β1 =", round(slope, 2), "):\n")
## 2. Slope (β1 = -0.02 ):
cat("   Setiap penambahan 1 jam penggunaan AI, skill meningkat", 
    round(slope, 2), "\n")
##    Setiap penambahan 1 jam penggunaan AI, skill meningkat -0.02
# 6. ESTIMASI PARAMETER DAN INFERENSI
cat("\n=== ESTIMASI PARAMETER ===\n")
## 
## === ESTIMASI PARAMETER ===
conf_int <- confint(model, level = 0.95)

cat("Interval Kepercayaan 95%:\n")
## Interval Kepercayaan 95%:
cat("   Intercept: [", round(conf_int[1,1], 3), ", ", 
    round(conf_int[1,2], 3), "]\n", sep="")
##    Intercept: [5.305, 5.916]
cat("   Slope:     [", round(conf_int[2,1], 3), ", ", 
    round(conf_int[2,2], 3), "]\n", sep="")
##    Slope:     [-0.085, 0.041]
# Uji hipotesis slope
slope_pvalue <- summary_model$coefficients[2,4]
cat("\np-value slope =", round(slope_pvalue, 6), "\n")
## 
## p-value slope = 0.491218
if(slope_pvalue < 0.05) {
  cat("Keputusan: Tolak H0 (signifikan)\n")
} else {
  cat("Keputusan: Gagal tolak H0 (tidak signifikan)\n")
}
## Keputusan: Gagal tolak H0 (tidak signifikan)
# 7. KOEFISIEN DETERMINASI
r_squared <- summary_model$r.squared
cat("\nKoefisien Determinasi (R²):\n")
## 
## Koefisien Determinasi (R²):
cat("   R² =", round(r_squared, 4), "\n")
##    R² = 1e-04
cat("   Artinya:", round(r_squared * 100, 2),
    "% variasi skill AI dijelaskan oleh jam penggunaan AI\n")
##    Artinya: 0.01 % variasi skill AI dijelaskan oleh jam penggunaan AI
# 8. VISUALISASI MODEL
ggplot(data, aes(x = jam_ai, y = skill_ai)) +
  geom_point(color = "blue", size = 3) +
  geom_smooth(method = "lm", se = TRUE,
              color = "red", fill = "pink") +
  labs(title = "Garis Regresi Linear",
       subtitle = paste("Y =", round(intercept, 2),
                        "+", round(slope, 2), "X"),
       x = "Jam Penggunaan AI",
       y = "Skill AI") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# 9. PREDIKSI
new_data <- data.frame(jam_ai = c(5, 10))
prediction <- predict(model, newdata = new_data,
                      interval = "confidence")

cat("\n=== PREDIKSI ===\n")
## 
## === PREDIKSI ===
cat("Untuk 5 jam AI, prediksi skill =", 
    round(prediction[1,"fit"], 2), "\n")
## Untuk 5 jam AI, prediksi skill = 5.5
cat("Untuk 10 jam AI, prediksi skill =", 
    round(prediction[2,"fit"], 2), "\n")
## Untuk 10 jam AI, prediksi skill = 5.39
# 10. DIAGNOSTIC PLOTS
par(mfrow = c(2,2))
plot(model)

par(mfrow = c(1,1))

# 11. RINGKASAN LENGKAP
cat("\n=== RINGKASAN ANALISIS ===\n")
## 
## === RINGKASAN ANALISIS ===
cat("Model: Skill_AI = β0 + β1*Jam_AI + ε\n")
## Model: Skill_AI = β0 + β1*Jam_AI + ε
cat("Estimasi: Y =", round(intercept, 3), 
    "+", round(slope, 3), "* X\n")
## Estimasi: Y = 5.611 + -0.022 * X
cat("R² =", round(r_squared, 4), 
    "(", round(r_squared*100,1), "%)\n")
## R² = 1e-04 ( 0 %)
cat("Normalitas p =", round(shapiro_test$p.value,4), "\n")
## Normalitas p = 0
cat("Homoskedastisitas p =", round(bp_test$p.value,4), "\n")
## Homoskedastisitas p = 0.91
cat("Autokorelasi p =", round(dw_test$p.value,4), "\n")
## Autokorelasi p = 0.3636
# Simpan hasil
hasil <- list(
  model = model,
  coefficients = coef(model),
  r_squared = r_squared,
  assumptions = list(
    normality = shapiro_test$p.value,
    homoscedasticity = bp_test$p.value,
    autocorrelation = dw_test$p.value
  ),
  confidence_intervals = conf_int
)

save(hasil, file = "hasil_regresi_features.RData")

print("Analisis regresi linear sederhana selesai!")
## [1] "Analisis regresi linear sederhana selesai!"