# 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 : Membandingkan performa mesin mobil vs Harga mobil
data <- read.csv("C:/Users/ASUS VIVO/Downloads//global_cars_enhanced.csv")
data <- data.frame(
  Price_USD  = data$Price_USD,
  Horsepower = data$Horsepower
)

# 2. ANALISIS DESKRIPTIF DAN VISUALISASI
print("Statistik Deskriptif:")
## [1] "Statistik Deskriptif:"
summary(data)
##    Price_USD        Horsepower   
##  Min.   :  5221   Min.   : 70.0  
##  1st Qu.: 29419   1st Qu.:188.2  
##  Median : 59180   Median :329.5  
##  Mean   : 60849   Mean   :328.3  
##  3rd Qu.: 89692   3rd Qu.:454.8  
##  Max.   :119587   Max.   :599.0
# Scatter plot
ggplot(data, aes(x = Price_USD, y = Horsepower)) +
  geom_point(color = "blue", size = 3) +
  labs(title = "Hubungan Horsepower dan Price_USD ",
       x = "Horsepower", y = "Price_USD") +
  theme_minimal()

# Korelasi
cor_test <- cor.test(data$Horsepower, 
                     data$Price_USD)
print(paste("Korelasi Pearson:", round(cor_test$estimate, 4)))
## [1] "Korelasi Pearson: 0.1049"
print(paste("p-value korelasi:", round(cor_test$p.value, 4)))
## [1] "p-value korelasi: 0.0695"
# 3. MEMBANGUN MODEL REGRESI
model <- lm(Price_USD ~ Horsepower, data = data)
print("Ringkasan Model Regresi:")
## [1] "Ringkasan Model Regresi:"
summary(model)
## 
## Call:
## lm(formula = Price_USD ~ Horsepower, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -58408 -27754  -1372  30086  60752 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 53101.69    4691.68  11.318   <2e-16 ***
## Horsepower     23.59      12.95   1.822   0.0695 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 34310 on 298 degrees of freedom
## Multiple R-squared:  0.01101,    Adjusted R-squared:  0.007694 
## F-statistic: 3.318 on 1 and 298 DF,  p-value: 0.06952
# 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.9488
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.117
cat("   p-value =", round(bp_test$p.value, 4), "\n")
##    p-value = 0.7323
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 = 1.7969
cat("   p-value =", round(dw_test$p.value, 4), "\n")
##    p-value = 0.0388
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: Nilai =", round(intercept, 2), "+", round(slope, 2), "* Horsepower\n")
## Persamaan Regresi: Nilai = 53101.69 + 23.59 * Horsepower
cat("\nInterpretasi:\n")
## 
## Interpretasi:
cat("1. Intercept (β0 =", round(intercept, 2), "):\n")
## 1. Intercept (β0 = 53101.69 ):
cat("   Harga mobil saat horsepower = 0 adalah", round(intercept, 2), "USD\n")
##    Harga mobil saat horsepower = 0 adalah 53101.69 USD
cat("2. Slope (β1 =", round(slope, 2), "):\n")
## 2. Slope (β1 = 23.59 ):
cat("   Setiap kenaikan 1 unit horsepower, harga mobil meningkat", round(slope, 2), "USD\n")
##    Setiap kenaikan 1 unit horsepower, harga mobil meningkat 23.59 USD
# 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: [43868.67, 62334.7]
cat("   Slope:     [", round(conf_int[2,1], 3), ", ", round(conf_int[2,2], 3), "]\n", sep = "")
##    Slope:     [-1.896, 49.084]
# 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.069517
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.011
cat("   Artinya:", round(r_squared * 100, 2), "% variasi harga mobil (Price_USD) dapat dijelaskan oleh horsepower\n")
##    Artinya: 1.1 % variasi harga mobil (Price_USD) dapat dijelaskan oleh horsepower
# 8. VISUALISASI MODEL
ggplot(data, aes(x = Horsepower, y = Price_USD)) +
  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 = "Horsepower", y = "Price_USD") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# 9. PREDIKSI
new_data <- data.frame(Horsepower = c(100, 150))
prediction <- predict(model, newdata = new_data, interval = "confidence")
cat("\n=== PREDIKSI ===\n")
## 
## === PREDIKSI ===
cat("Untuk Horsepower = 100, prediksi harga mobil =", round(prediction[1, "fit"], 2), "USD\n")
## Untuk Horsepower = 100, prediksi harga mobil = 55461.12 USD
cat("Untuk Horsepower = 150, prediksi harga mobil =", round(prediction[2, "fit"], 2), "USD\n")
## Untuk Horsepower = 150, prediksi harga mobil = 56640.84 USD
# 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: Price_USD = β0 + β1*Horsepower + ε\n")
## 1. Model: Price_USD = β0 + β1*Horsepower + ε
cat("2. Estimasi: Y =", round(intercept, 3), "+", round(slope, 3), "* X\n")
## 2. Estimasi: Y = 53101.68 + 23.594 * X
cat("3. R² =", round(r_squared, 4), "(", round(r_squared*100, 1), "%)\n")
## 3. R² = 0.011 ( 1.1 %)
cat("4. Uji F (model): p-value =", 
    round(summary_model$fstatistic[1], 4), "\n")
## 4. Uji F (model): p-value = 3.3183
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.7323
cat("   - Autokorelasi: p =", round(dw_test$p.value, 4), "\n")
##    - Autokorelasi: p = 0.0388
# 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!"

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.