# 1. MEMUAT PACKAGE DAN DATA
library(ggplot2)
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Loading required package: carData
library(readr)

# Data: battery capacity vs overnight charging freq per week
data <- read_csv("smartphone_battery_features.csv")
## Rows: 5000 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Device_ID, background_app_usage_level, signal_strength_avg
## dbl (12): device_age_months, battery_capacity_mah, avg_screen_on_hours_per_d...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data <- data.frame(
  battery_capacity = data$battery_capacity_mah,
  overnight_charging = data$overnight_charging_freq_per_week
)

# 2. ANALISIS DESKRIPTIF DAN VISUALISASI
print("Statistik Deskriptif:")
## [1] "Statistik Deskriptif:"
summary(data)
##  battery_capacity overnight_charging
##  Min.   :3000     Min.   :0.000     
##  1st Qu.:4000     1st Qu.:1.000     
##  Median :4500     Median :3.000     
##  Mean   :4135     Mean   :3.465     
##  3rd Qu.:5000     3rd Qu.:5.000     
##  Max.   :5000     Max.   :7.000
# Scatter plot
ggplot(data, aes(x = battery_capacity, y = overnight_charging)) +
  geom_point(color = "blue", size = 3) +
  labs(title = "Hubungan Battery Capacity dan Overnight Charging",
       x = "Battery Capacity (mAh)", y = "Overnight Charging per Week") +
  theme_minimal()

# Korelasi
cor_test <- cor.test(data$battery_capacity,
                     data$overnight_charging)

print(paste("Korelasi Pearson:", round(cor_test$estimate, 4)))
## [1] "Korelasi Pearson: 0.0024"
print(paste("p-value korelasi:", round(cor_test$p.value, 4)))
## [1] "p-value korelasi: 0.8648"
# 3. MEMBANGUN MODEL REGRESI
model <- lm(overnight_charging ~ battery_capacity, data = data)
print("Ringkasan Model Regresi:")
## [1] "Ringkasan Model Regresi:"
summary(model)
## 
## Call:
## lm(formula = overnight_charging ~ battery_capacity, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4717 -2.4567 -0.4567  1.5433  3.5433 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.434e+00  1.840e-01   18.67   <2e-16 ***
## battery_capacity 7.455e-06  4.379e-05    0.17    0.865    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.307 on 4998 degrees of freedom
## Multiple R-squared:  5.8e-06,    Adjusted R-squared:  -0.0001943 
## F-statistic: 0.02899 on 1 and 4998 DF,  p-value: 0.8648
# 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.9246
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.014
cat("   p-value =",  round(bp_test$p.value, 4), "\n")
##    p-value = 0.9058
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 Tidak ada 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 = 2.0032
cat("   p-value =", round(dw_test$p.value, 4), "\n")
##    p-value = 0.5443
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: Overnight Charging =", round(intercept, 2), "+", round(slope, 2), "* Battery Capacity\n")
## Persamaan Regresi: Overnight Charging = 3.43 + 0 * Battery Capacity
cat("\nInterpretasi:\n")
## 
## Interpretasi:
cat("1. Intercept (β0 =", round(intercept, 2), "):\n")
## 1. Intercept (β0 = 3.43 ):
cat("   Frekuensi overnight charging ketika kapasitas baterai = 0 adalah", round(intercept, 2), "kali per minggu\n")
##    Frekuensi overnight charging ketika kapasitas baterai = 0 adalah 3.43 kali per minggu
cat("2. Slope (β1 =",  round(slope, 5), "):\n")
## 2. Slope (β1 = 1e-05 ):
cat("   Setiap penambahan 1 mAh kapasitas baterai, frekuensi overnight charging berubah", round(slope, 2), "kali\n")
##    Setiap penambahan 1 mAh kapasitas baterai, frekuensi overnight charging berubah 0 kali
# 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: [3.074, 3.795]
cat("   Slope:     [",  round(conf_int[2,1], 3), ", ",  round(conf_int[2,2], 3), "]\n", sep="")
##    Slope:     [0, 0]
# Uji hipotesis slope
cat("\nUji Hipotesis untuk Slope (β1):\n")
## 
## Uji Hipotesis untuk Slope (β1):
cat("   H0: β1 = 0 (tidak ada hubungan linear)\n")
##    H0: β1 = 0 (tidak ada hubungan linear)
cat("   H1: β1 ≠ 0 (ada hubungan linear)\n")
##    H1: β1 ≠ 0 (ada hubungan linear)
summary_model <- summary(model)
slope_pvalue <- summary_model$coefficients[2,4]
cat("   p-value =", round(slope_pvalue, 6), "\n")
##    p-value = 0.86481
if(slope_pvalue < 0.05) {
  cat("   Keputusan: Tolak H0, ada hubungan linear signifikan\n")
} else {
  cat("   Keputusan: Gagal tolak H0, tidak ada hubungan linear signifikan\n")
}
##    Keputusan: Gagal tolak H0, tidak ada hubungan linear 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² = 0
cat("   Artinya:", round(r_squared*100, 2),  "% variasi overnight charging dijelaskan oleh battery capacity\n")
##    Artinya: 0 % variasi overnight charging dijelaskan oleh battery capacity
# 8. VISUALISASI MODEL
ggplot(data, aes(x = battery_capacity,  y = overnight_charging)) +
  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,5), "X"),
       x = "Battery Capacity (mAh)",
       y = "Overnight Charging") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# 9. PREDIKSI
new_data <- data.frame( battery_capacity = c(3000, 5000))
prediction <- predict(model, newdata = new_data, interval = "confidence")
cat("\n=== PREDIKSI ===\n")
## 
## === PREDIKSI ===
cat("Untuk 3000 mAh, prediksi =", round(prediction[1,"fit"], 2), "\n")
## Untuk 3000 mAh, prediksi = 3.46
cat("Untuk 5000 mAh, prediksi =", round(prediction[2,"fit"], 2), "\n")
## Untuk 5000 mAh, prediksi = 3.47
# 10. DIAGNOSTIC PLOTS
par(mfrow = c(2,2))
plot(model, which = 1:4)

par(mfrow = c(1,1))

# 11. RINGKASAN LENGKAP
cat("\n=== RINGKASAN ANALISIS ===\n")
## 
## === RINGKASAN ANALISIS ===
cat("1. Model:\n")
## 1. Model:
cat("   Overnight = β0 + β1*Battery Capacity + ε\n")
##    Overnight = β0 + β1*Battery Capacity + ε
cat("2. Estimasi:\n")
## 2. Estimasi:
cat("   Y =",  round(intercept,3), "+",  round(slope,5), "X\n")
##    Y = 3.434 + 1e-05 X
cat("3. R² =",  round(r_squared,4),  "(", round(r_squared*100,1), "%)\n")
## 3. R² = 0 ( 0 %)
cat("4. Asumsi:\n")
## 4. Asumsi:
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.9058
cat("   - Autokorelasi: p =", round(dw_test$p.value,4), "\n")
##    - Autokorelasi: p = 0.5443
print("Analisis regresi linear sederhana selesai!")
## [1] "Analisis regresi linear sederhana selesai!"