Data dan Model

# Load data bawaan R
data(mtcars)

# Model 1: regresi sederhana mpg ~ hp
model1 <- lm(mpg ~ hp, data = mtcars)

# Model 2: regresi berganda mpg ~ hp + wt
model2 <- lm(mpg ~ hp + wt, data = mtcars)

Ringkasan Model

summary(model1)
## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07
summary(model2)
## 
## Call:
## lm(formula = mpg ~ hp + wt, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.941 -1.600 -0.182  1.050  5.854 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.22727    1.59879  23.285  < 2e-16 ***
## hp          -0.03177    0.00903  -3.519  0.00145 ** 
## wt          -3.87783    0.63273  -6.129 1.12e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared:  0.8268, Adjusted R-squared:  0.8148 
## F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12

Pemeriksaan Asumsi

1. Normalitas Error

# Uji Shapiro-Wilk
shapiro.test(residuals(model1))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model1)
## W = 0.92337, p-value = 0.02568
shapiro.test(residuals(model2))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model2)
## W = 0.92792, p-value = 0.03427
# Plot QQ-plot
par(mfrow=c(1,2))
qqnorm(residuals(model1), main="Model 1: QQ Plot")
qqline(residuals(model1), col="red")

qqnorm(residuals(model2), main="Model 2: QQ Plot")
qqline(residuals(model2), col="red")

par(mfrow=c(1,1))

2. Homoskedastisitas

library(lmtest)
## Warning: package 'lmtest' was built under R version 4.5.1
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.1
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# Breusch-Pagan test
bptest(model1)
## 
##  studentized Breusch-Pagan test
## 
## data:  model1
## BP = 0.049298, df = 1, p-value = 0.8243
bptest(model2)
## 
##  studentized Breusch-Pagan test
## 
## data:  model2
## BP = 0.88072, df = 2, p-value = 0.6438
# Plot residual vs fitted
plot.new()
par(mfrow=c(1,2))
plot(fitted(model1), residuals(model1),
     main="Model 1: Residual vs Fitted",
     xlab="Fitted Values", ylab="Residuals")
abline(h=0, col="red")

plot.new()

plot(fitted(model2), residuals(model2),
     main="Model 2: Residual vs Fitted",
     xlab="Fitted Values", ylab="Residuals")
abline(h=0, col="red")

par(mfrow=c(1,1))

3. Multikolinearitas

library(car)
## Warning: package 'car' was built under R version 4.5.1
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.1
vif(model2)
##       hp       wt 
## 1.766625 1.766625

Kesimpulan