library(csv)
## Warning: package 'csv' was built under R version 4.5.2
library(ggplot2)
library(car)
## Loading required package: carData
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
data <- read.csv("~/Belajar kuliah lukas/Analisis Regresi/Data/admission.csv")
summary(data)
##    gre_score      toefl_score     univ_ranking   motiv_letter_strength
##  Min.   :290.0   Min.   : 92.0   Min.   :1.000   Min.   :1.000        
##  1st Qu.:308.0   1st Qu.:103.0   1st Qu.:2.000   1st Qu.:2.500        
##  Median :317.0   Median :107.0   Median :3.000   Median :3.500        
##  Mean   :316.5   Mean   :107.2   Mean   :3.114   Mean   :3.374        
##  3rd Qu.:325.0   3rd Qu.:112.0   3rd Qu.:4.000   3rd Qu.:4.000        
##  Max.   :340.0   Max.   :120.0   Max.   :5.000   Max.   :5.000        
##  recommendation_strength      gpa         research_exp  admission_score
##  Min.   :1.000           Min.   :6.800   Min.   :0.00   Min.   :34.00  
##  1st Qu.:3.000           1st Qu.:8.127   1st Qu.:0.00   1st Qu.:63.00  
##  Median :3.500           Median :8.560   Median :1.00   Median :72.00  
##  Mean   :3.484           Mean   :8.576   Mean   :0.56   Mean   :72.14  
##  3rd Qu.:4.000           3rd Qu.:9.040   3rd Qu.:1.00   3rd Qu.:82.00  
##  Max.   :5.000           Max.   :9.920   Max.   :1.00   Max.   :97.00
head(data)
##   gre_score toefl_score univ_ranking motiv_letter_strength
## 1       337         118            4                   4.5
## 2       324         107            4                   4.0
## 3       316         104            3                   3.0
## 4       322         110            3                   3.5
## 5       314         103            2                   2.0
## 6       330         115            5                   4.5
##   recommendation_strength  gpa research_exp admission_score
## 1                     4.5 9.65            1              92
## 2                     4.5 8.87            1              76
## 3                     3.5 8.00            1              72
## 4                     2.5 8.67            1              80
## 5                     3.0 8.21            0              65
## 6                     3.0 9.34            1              90
tail(data)
##     gre_score toefl_score univ_ranking motiv_letter_strength
## 495       301          99            3                   2.5
## 496       332         108            5                   4.5
## 497       337         117            5                   5.0
## 498       330         120            5                   4.5
## 499       312         103            4                   4.0
## 500       327         113            4                   4.5
##     recommendation_strength  gpa research_exp admission_score
## 495                     2.0 8.45            1              68
## 496                     4.0 9.02            1              87
## 497                     5.0 9.87            1              96
## 498                     5.0 9.56            1              93
## 499                     5.0 8.43            0              73
## 500                     4.5 9.04            0              84
ggplot(data, aes(x = gpa, y = admission_score)) +
  geom_point(color = "blue", size = 3) +
  labs(title = "Hubungan GPA dan Admission Score",
       x = "GPA", y = "Admission Score") +
  theme_minimal()

cor_test <- cor.test(data$gpa, data$admission_score)
print(paste("Korelasi Pearson:", round(cor_test$estimate, 4)))
## [1] "Korelasi Pearson: 0.8828"
print(paste("p-value korelasi:", round(cor_test$p.value, 4)))
## [1] "p-value korelasi: 0"
# 3. MEMBANGUN MODEL REGRESI
model <- lm(admission_score ~ gpa, data = data)
print("Ringkasan Model Regresi:")
## [1] "Ringkasan Model Regresi:"
summary(model)
## 
## Call:
## lm(formula = admission_score ~ gpa, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -27.6247  -2.8559   0.6617   3.8753  17.7697 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -105.0275     4.2355  -24.80   <2e-16 ***
## gpa           20.6572     0.4926   41.93   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.656 on 498 degrees of freedom
## Multiple R-squared:  0.7793, Adjusted R-squared:  0.7788 
## F-statistic:  1758 on 1 and 498 DF,  p-value: < 2.2e-16
cat("\n=== UJI ASUMSI REGRESI LINEAR ===\n")
## 
## === UJI ASUMSI REGRESI LINEAR ===
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.9507
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
qqnorm(residuals(model), main = "Q-Q Plot Residual")
qqline(residuals(model), col = "red")

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 = 20.6523
cat("   p-value =", round(bp_test$p.value, 4), "\n")
##    p-value = 0
if(bp_test$p.value > 0.05) {
  cat("   Keputusan: Varian residual homogen\n")
} else {
  cat("   Keputusan: Ada heteroskedastisitas\n")
}
##    Keputusan: Ada heteroskedastisitas
# 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 = 0.9726
cat("   p-value =", round(dw_test$p.value, 4), "\n")
##    p-value = 0
if(dw_test$p.value > 0.05) {
  cat("   Keputusan: Tidak ada autokorelasi\n")
} else {
  cat("   Keputusan: Ada autokorelasi\n")
}
##    Keputusan: Ada autokorelasi
# 5. INTERPRETASI KOEFISIEN
cat("\n=== INTERPRETASI KOEFISIEN ===\n")
## 
## === INTERPRETASI KOEFISIEN ===
intercept <- coef(model)[1]
slope <- coef(model)[2]

cat("Persamaan Regresi:\n")
## Persamaan Regresi:
cat("Admission Score =", round(intercept, 2), "+", round(slope, 2), "* GPA\n")
## Admission Score = -105.03 + 20.66 * GPA
# 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: [-113.349, -96.706]
cat("   Slope:     [", round(conf_int[2,1], 3), ", ", round(conf_int[2,2], 3), "]\n", sep = "")
##    Slope:     [19.689, 21.625]
# 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
# 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.7793
cat("   Artinya:", round(r_squared * 100, 2), "% variasi admission score dapat dijelaskan oleh gpa\n")
##    Artinya: 77.93 % variasi admission score dapat dijelaskan oleh gpa
# 8. VISUALISASI MODEL
ggplot(data, aes(x = gpa, y = admission_score)) +
  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 = "gpa", y = "admission score") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# 9. PREDIKSI
new_data <- data.frame(gpa = 8.5)
prediction <- predict(model, newdata = new_data, interval = "confidence")

cat("\n=== Prediksi Admission Score ===\n")
## 
## === Prediksi Admission Score ===
cat("Untuk gpa 8.5, prediksi admission score =", round(prediction[1, "fit"], 2), "\n")
## Untuk gpa 8.5, prediksi admission score = 70.56
print(cbind(new_data, prediction))
##   gpa      fit      lwr      upr
## 1 8.5 70.55896 69.96949 71.14843
# 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: admission score = β0 + β1*gpa + ε\n")
## 1. Model: admission score = β0 + β1*gpa + ε
cat("2. Estimasi: Y =", round(intercept, 3), "+", round(slope, 3), "* X\n")
## 2. Estimasi: Y = -105.027 + 20.657 * X
cat("3. R² =", round(r_squared, 4), "(", round(r_squared*100, 1), "%)\n")
## 3. R² = 0.7793 ( 77.9 %)
cat("4. Uji F (model): p-value =", 
    round(summary_model$fstatistic[1], 4), "\n")
## 4. Uji F (model): p-value = 1758.324
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
cat("   - Autokorelasi: p =", round(dw_test$p.value, 4), "\n")
##    - Autokorelasi: p = 0
# 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
)

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

```