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(outliers)
library(car)
## Loading required package: carData
# Input data
df <- read.csv("C:/Users/User/Documents/SEMESTER 6/Analisis Regresi/Insurance.csv")
data <- df[c("age", "charges")]
# Cek Missing Value
cat("\n=== Missing Value ===\n")
##
## === Missing Value ===
total_missing <- sum(is.na(data))
total_missing
## [1] 0
# Outlier
detect_outliers <- function(data) {
cat("\n=== Deteksi Outlier ===\n\n")
outlier_report <- list()
for(var in names(data)[sapply(data, is.numeric)]) {
cat("Analisis outlier untuk variabel:", var, "\n")
# Statistik deskriptif
stats <- summary(data[[var]])
iqr_val <- IQR(data[[var]], na.rm = TRUE)
q1 <- quantile(data[[var]], 0.25, na.rm = TRUE)
q3 <- quantile(data[[var]], 0.75, na.rm = TRUE)
lower_bound <- q1 - 1.5 * iqr_val
upper_bound <- q3 + 1.5 * iqr_val
# Deteksi outlier dengan metode IQR
outliers_iqr <- data[[var]][data[[var]] < lower_bound | data[[var]] > upper_bound]
# Deteksi outlier dengan metode Z-score
z_scores <- scale(data[[var]])
outliers_z <- data[[var]][abs(z_scores) > 3]
# Deteksi outlier dengan metode Grubbs (uji statistik)
if(length(na.omit(data[[var]])) > 6) {
tryCatch({
grubbs_test <- grubbs.test(na.omit(data[[var]]))
grubbs_outlier <- ifelse(grubbs_test$p.value < 0.05, "Terdeteksi", "Tidak terdeteksi")
}, error = function(e) {
grubbs_outlier <- "Tidak dapat dihitung"
})
} else {
grubbs_outlier <- "Data tidak cukup"
}
# Ringkasan
outlier_report[[var]] <- list(
n_outliers_iqr = length(outliers_iqr),
n_outliers_z = length(outliers_z),
grubbs_result = grubbs_outlier,
lower_bound = lower_bound,
upper_bound = upper_bound,
outlier_values = unique(round(outliers_iqr, 2))
)
cat(" - Outlier (IQR method):", length(outliers_iqr), "\n")
cat(" - Outlier (Z-score > 3):", length(outliers_z), "\n")
cat(" - Uji Grubbs:", grubbs_outlier, "\n")
cat(" - Batas bawah:", round(lower_bound, 2), "\n")
cat(" - Batas atas:", round(upper_bound, 2), "\n")
if(length(outliers_iqr) > 0) {
cat(" - Nilai outlier:", paste(head(unique(round(outliers_iqr, 2)), 5), collapse = ", "), "\n")
}
cat("\n")
}
return(outlier_report)
}
# Deteksi outlier
outlier_analysis <- detect_outliers(data)
##
## === Deteksi Outlier ===
##
## Analisis outlier untuk variabel: age
## - Outlier (IQR method): 0
## - Outlier (Z-score > 3): 0
## - Uji Grubbs: Tidak terdeteksi
## - Batas bawah: -9
## - Batas atas: 87
##
## Analisis outlier untuk variabel: charges
## - Outlier (IQR method): 139
## - Outlier (Z-score > 3): 7
## - Uji Grubbs: Terdeteksi
## - Batas bawah: -13109.15
## - Batas atas: 34489.35
## - Nilai outlier: 39611.76, 36837.47, 37701.88, 38711, 35585.58
# Visual
visualize_outliers <- function(data) {
numeric_vars <- names(data)[sapply(data, is.numeric)]
# Boxplot untuk setiap variabel numerik
par(mfrow = c(2, 3))
for(var in numeric_vars[1:min(6, length(numeric_vars))]) {
boxplot(data[[var]], main = var, col = "lightblue",
ylab = "Biaya", outline = TRUE)
grid()
}
par(mfrow = c(1, 1))
# Histogram dengan overlay outlier
for(var in numeric_vars[1:min(3, length(numeric_vars))]) {
# Hitung batas outlier
q1 <- quantile(data[[var]], 0.25, na.rm = TRUE)
q3 <- quantile(data[[var]], 0.75, na.rm = TRUE)
iqr_val <- IQR(data[[var]], na.rm = TRUE)
lower_bound <- q1 - 1.5 * iqr_val
upper_bound <- q3 + 1.5 * iqr_val
# Identifikasi outlier
is_outlier <- data[[var]] < lower_bound | data[[var]] > upper_bound
# Plot histogram
hist_data <- ggplot(data.frame(value = data[[var]]), aes(x = value)) +
geom_histogram(aes(y = ..density..), bins = 30, fill = "lightblue", alpha = 0.7) +
geom_density(color = "darkblue", linewidth = 1) +
geom_vline(xintercept = c(lower_bound, upper_bound),
color = "red", linetype = "dashed", linewidth = 1) +
labs(title = paste("Distribusi dan Outlier:", var),
x = var, y = "Density") +
theme_minimal() +
annotate("text", x = lower_bound, y = 0,
label = "Bawah", vjust = 2, color = "red") +
annotate("text", x = upper_bound, y = 0,
label = "Atas", vjust = 2, color = "red")
print(hist_data)
}
# Scatter plot matrix untuk melihat outlier multivariat
if(length(numeric_vars) >= 3) {
pairs(data[, numeric_vars[1:min(4, length(numeric_vars))]],
main = "Scatter Plot Matrix untuk Deteksi Outlier",
pch = 19, col = alpha("blue", 0.6))
}
}
# Jalankan visualisasi
visualize_outliers(data)

## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.


# ANALISIS DESKRIPTIF DAN VISUALISASI
print("Statistik Deskriptif:")
## [1] "Statistik Deskriptif:"
## age charges
## Min. :18.00 Min. : 1122
## 1st Qu.:27.00 1st Qu.: 4740
## Median :39.00 Median : 9382
## Mean :39.21 Mean :13270
## 3rd Qu.:51.00 3rd Qu.:16640
## Max. :64.00 Max. :63770
# Scatter plot
ggplot(data, aes(x = age, y = charges)) +
geom_point(color = "blue", size = 3) +
labs(title = "Hubungan Usia dan Biaya",
x = "Usia", y = "Biaya") +
theme_minimal()

# Korelasi
cor_test <- cor.test(data$age, data$charges)
print(paste("Korelasi Pearson:", round(cor_test$estimate, 4)))
## [1] "Korelasi Pearson: 0.299"
print(paste("p-value korelasi:", round(cor_test$p.value, 4)))
## [1] "p-value korelasi: 0"
# MEMBANGUN MODEL REGRESI
model <- lm(charges ~ age, data = data)
print("Ringkasan Model Regresi:")
## [1] "Ringkasan Model Regresi:"
##
## Call:
## lm(formula = charges ~ age, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8059 -6671 -5939 5440 47829
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3165.9 937.1 3.378 0.000751 ***
## age 257.7 22.5 11.453 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11560 on 1336 degrees of freedom
## Multiple R-squared: 0.08941, Adjusted R-squared: 0.08872
## F-statistic: 131.2 on 1 and 1336 DF, p-value: < 2.2e-16
# 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.6577
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")

# 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 = 4e-04
cat(" p-value =", round(bp_test$p.value, 4), "\n")
## p-value = 0.9838
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)

# 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.0333
cat(" p-value =", round(dw_test$p.value, 4), "\n")
## p-value = 0.7285
if(dw_test$p.value > 0.05) {
cat(" Keputusan: Tidak ada autokorelasi\n")
} else {
cat(" Keputusan: Ada autokorelasi\n")
}
## Keputusan: Tidak ada autokorelasi
# INTERPRETASI KOEFISIEN
cat("\n=== INTERPRETASI KOEFISIEN ===\n")
##
## === INTERPRETASI KOEFISIEN ===
intercept <- coef(model)[1]
slope <- coef(model)[2]
cat("Persamaan Regresi: Biaya =", round(intercept, 2), "+", round(slope, 2), "* Usia\n")
## Persamaan Regresi: Biaya = 3165.89 + 257.72 * Usia
##
## Interpretasi:
cat("1. Intercept (β0 =", round(intercept, 2), "):\n")
## 1. Intercept (β0 = 3165.89 ):
cat(" Biaya ketika usia = 0 adalah", round(intercept, 2))
## Biaya ketika usia = 0 adalah 3165.89
cat("2. Slope (β1 =", round(slope, 2), "):\n")
## 2. Slope (β1 = 257.72 ):
cat(" Setiap penambahan 1 usia, biaya meningkat", round(slope, 2))
## Setiap penambahan 1 usia, biaya meningkat 257.72
# 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: [1327.44, 5004.33]
cat(" Slope: [", round(conf_int[2,1], 3), ", ", round(conf_int[2,2], 3), "]\n", sep = "")
## Slope: [213.579, 301.866]
# Uji hipotesis untuk 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
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: Tolak H0, ada hubungan linear signifikan
# 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.0894
cat(" Artinya:", round(r_squared * 100, 2), "% variasi biaya dapat dijelaskan oleh usia\n")
## Artinya: 8.94 % variasi biaya dapat dijelaskan oleh usia
# VISUALISASI MODEL
ggplot(data, aes(x = age, y = charges)) +
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 = "Usia", y = "Biaya") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# PREDIKSI
new_data <- data.frame(age = c(19, 33))
prediction <- predict(model, newdata = new_data, interval = "confidence")
cat("\n=== PREDIKSI ===\n")
##
## === PREDIKSI ===
cat("Untuk Usia 19, prediksi biaya =", round(prediction[1, "fit"], 2), "\n")
## Untuk Usia 19, prediksi biaya = 8062.61
cat("Untuk Usia 33, prediksi biaya =", round(prediction[2, "fit"], 2), "\n")
## Untuk Usia 33, prediksi biaya = 11670.73
# 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: Biaya = β0 + β1*Usia + ε\n")
## 1. Model: Biaya = β0 + β1*Usia + ε
cat("2. Estimasi: Y =", round(intercept, 3), "+", round(slope, 3), "* X\n")
## 2. Estimasi: Y = 3165.885 + 257.723 * X
cat("3. R² =", round(r_squared, 4), "(", round(r_squared*100, 1), "%)\n")
## 3. R² = 0.0894 ( 8.9 %)
cat("4. Uji F (model): p-value =",
round(summary_model$fstatistic[1], 4), "\n")
## 4. Uji F (model): p-value = 131.174
## 5. 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.9838
cat(" - Autokorelasi: p =", round(dw_test$p.value, 4), "\n")
## - Autokorelasi: p = 0.7285
# 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
)