# 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
# Data contoh: Hubungan tinggi badan dan berat badan
data <- read.csv("C:/Users/Yohana Gabriela/OneDrive/Documents/500_Person_Gender_Height_Weight_Index.csv")
data_reg <- data[c("Height","Weight")]
# 2. ANALISIS DESKRIPTIF DAN VISUALISASI
print("Statistik Deskriptif:")
## [1] "Statistik Deskriptif:"
summary(data)
## Gender Height Weight Index
## Length:500 Min. :140.0 Min. : 50 Min. :0.000
## Class :character 1st Qu.:156.0 1st Qu.: 80 1st Qu.:3.000
## Mode :character Median :170.5 Median :106 Median :4.000
## Mean :169.9 Mean :106 Mean :3.748
## 3rd Qu.:184.0 3rd Qu.:136 3rd Qu.:5.000
## Max. :199.0 Max. :160 Max. :5.000
# Scatter plot
ggplot(data_reg, aes(x = Height, y = Weight)) +
geom_point(color = "blue", size = 4) +
labs(title = "Hubungan Tinggi Badan dan Berat Badan",
x = "Tinggi Badan (cm)", y = "Berat Badan (kg)") +
theme_minimal()

# Korelasi
cor_test <- cor.test(data_reg$Height, data_reg$Weight)
print(paste("Korelasi Pearson:", round(cor_test$estimate, )))
## [1] "Korelasi Pearson: 0"
print(paste("p-value korelasi:", round(cor_test$p.value, 4)))
## [1] "p-value korelasi: 0.9921"
# 3. MEMBANGUN MODEL REGRESI
model <- lm(Weight ~ Height, data = data_reg)
print("Ringkasan Model Regresi:")
## [1] "Ringkasan Model Regresi:"
summary(model)
##
## Call:
## lm(formula = Weight ~ Height, data = data_reg)
##
## Residuals:
## Min 1Q Median 3Q Max
## -56.025 -26.016 -0.017 29.989 54.022
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.059e+02 1.513e+01 6.996 8.51e-12 ***
## Height 8.819e-04 8.862e-02 0.010 0.992
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32.42 on 498 degrees of freedom
## Multiple R-squared: 1.989e-07, Adjusted R-squared: -0.002008
## F-statistic: 9.904e-05 on 1 and 498 DF, p-value: 0.9921
# 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.953
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.1547
cat(" p-value =", round(bp_test$p.value, 4), "\n")
## p-value = 0.6941
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 = 4)

# 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.0172
cat(" p-value =", round(dw_test$p.value, 4), "\n")
## p-value = 0.576
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)[4]
cat("Persamaan Regresi: Weight =", round(intercept, 4), "+", round(slope, 4), "* Height\n")
## Persamaan Regresi: Weight = 105.8501 + NA * Height
cat("\nInterpretasi:\n")
##
## Interpretasi:
cat("1. Intercept (β0 =", round(intercept, 4), "):\n")
## 1. Intercept (β0 = 105.8501 ):
cat(" Berat badan ketika tinggi = 0 cm adalah", round(intercept, 4), "kg (tidak realistis secara praktis)\n")
## Berat badan ketika tinggi = 0 cm adalah 105.8501 kg (tidak realistis secara praktis)
cat("2. Slope (β1 =", round(slope, 4), "):\n")
## 2. Slope (β1 = NA ):
cat(" Setiap kenaikan 1 cm tinggi badan, berat badan meningkat sekitar", round(slope, 2), "kg\n")
## Setiap kenaikan 1 cm tinggi badan, berat badan meningkat sekitar NA kg
# 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: [76.125, 135.575]
cat(" Slope: [", round(conf_int[2,1], 3), ", ", round(conf_int[2,2], 3), "]\n", sep = "")
## Slope: [-0.173, 0.175]
# 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.992064
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("Koefisien Determinasi (R²) =", round(r_squared, 4), "\n")
## Koefisien Determinasi (R²) = 0
cat("Artinya:", round(r_squared*100,2), "% variasi berat badan dapat dijelaskan oleh tinggi badan\n")
## Artinya: 0 % variasi berat badan dapat dijelaskan oleh tinggi badan
# 8. VISUALISASI MODEL DENGAN GARIS REGRESI
ggplot(data_reg, aes(x = Height, y = Weight)) +
geom_point(color = "blue", size = 2) +
geom_smooth(method = "lm", se = TRUE, color = "red", fill = "pink") +
labs(title = "Garis Regresi Linear",
subtitle = paste("Weight =", round(intercept, 2), "+", round(slope, 2), "* Height"),
x = "Tinggi Badan (cm)", y = "Berat Badan (kg)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# 9. PREDIKSI
new_data <- data.frame(Height = c(170, 180))
prediction <- predict(model, newdata = new_data, interval = "confidence")
cat("\n=== PREDIKSI ===\n")
##
## === PREDIKSI ===
cat("Prediksi berat badan untuk tinggi 170 cm =", round(prediction[1, "fit"], 2), "kg\n")
## Prediksi berat badan untuk tinggi 170 cm = 106 kg
cat("Prediksi berat badan untuk tinggi 180 cm =", round(prediction[2, "fit"], 2), "kg\n")
## Prediksi berat badan untuk tinggi 180 cm = 106.01 kg
# 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: Weight = β0 + β1*Height + ε\n")
## 1. Model: Weight = β0 + β1*Height + ε
cat("2. Estimasi: Weight =", round(intercept, 3), "+", round(slope, 3), "* Height\n")
## 2. Estimasi: Weight = 105.85 + NA * Height
cat("3. R² =", round(r_squared, 4), "(", round(r_squared*100, 1), "%)\n")
## 3. R² = 0 ( 0 %)
cat("4. Uji F (model): p-value =", summary_model$fstatistic[1], "\n")
## 4. Uji F (model): p-value = 9.903581e-05
cat("5. Asumsi:\n")
## 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.6941
cat(" - Autokorelasi: p =", round(dw_test$p.value, 4), "\n")
## - Autokorelasi: p = 0.576
# Simpan hasil analisis
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
)
print("Analisis regresi linear sederhana selesai!")
## [1] "Analisis regresi linear sederhana selesai!"