# 데이터 가져오기
url <- "https://raw.githubusercontent.com/rich-hyun/kufa_data_1/main/14year_kufa_data.csv"
kufa_data <- read.csv(url, header = TRUE, sep = ",", stringsAsFactors = FALSE)

options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("car")
## 'C:/Users/passi/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'car'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## 
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
##  C:\Users\passi\AppData\Local\Temp\Rtmp0wgH53\downloaded_packages
install.packages("lmtest")
## 'C:/Users/passi/AppData/Local/R/win-library/4.3'의 위치에 패키지(들)을 설치합니다.
## (왜냐하면 'lib'가 지정되지 않았기 때문입니다)
## 패키지 'lmtest'를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다
## Warning: 패키지 'lmtest'의 이전설치를 삭제할 수 없습니다
## Warning in file.copy(savedcopy, lib, recursive = TRUE):
## C:\Users\passi\AppData\Local\R\win-library\4.3\00LOCK\lmtest\libs\x64\lmtest.dll를
## C:\Users\passi\AppData\Local\R\win-library\4.3\lmtest\libs\x64\lmtest.dll로
## 복사하는데 문제가 발생했습니다: Permission denied
## Warning: 'lmtest'를 복구하였습니다
## 
## 다운로드된 바이너리 패키지들은 다음의 위치에 있습니다
##  C:\Users\passi\AppData\Local\Temp\Rtmp0wgH53\downloaded_packages
library(car)
## Warning: 패키지 'car'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: carData
## Warning: 패키지 'carData'는 R 버전 4.3.2에서 작성되었습니다
library(lmtest)
## Warning: 패키지 'lmtest'는 R 버전 4.3.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: zoo
## Warning: 패키지 'zoo'는 R 버전 4.3.2에서 작성되었습니다
## 
## 다음의 패키지를 부착합니다: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# 다중 선형 회귀 분석 수행
model <- lm(h14exp ~ h14inc + h14fc001 + h14ba570, data = kufa_data)

# 회귀분석
summary(model)
## 
## Call:
## lm(formula = h14exp ~ h14inc + h14fc001 + h14ba570, data = kufa_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -14016  -1286   -394    907  49863 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.714e+03  1.368e+02  12.534  < 2e-16 ***
## h14inc      4.702e-01  1.652e-02  28.459  < 2e-16 ***
## h14fc001    3.486e-02  5.101e-03   6.835  1.2e-11 ***
## h14ba570    3.660e+00  1.076e+00   3.401  0.00069 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2836 on 1458 degrees of freedom
##   (결측으로 인하여 7336개의 관측치가 삭제되었습니다.)
## Multiple R-squared:  0.4215, Adjusted R-squared:  0.4204 
## F-statistic: 354.2 on 3 and 1458 DF,  p-value: < 2.2e-16
# 잔차분석
par(mfrow=c(2,2))
plot(model)

# 1-다중공선성 확인
vif(model)
##   h14inc h14fc001 h14ba570 
## 1.059983 1.124499 1.094389
# 2-정규성 확인(QQ플릇))
qqnorm(model$residuals)
qqline(model$residuals, col = "red")

# 3-등분산성 확인 (브루쉬-파건)
bptest(model)
## 
##  studentized Breusch-Pagan test
## 
## data:  model
## BP = 23.301, df = 3, p-value = 3.495e-05
# 4- 독립성 확인(더빈-왓슨)
dwtest(model)
## 
##  Durbin-Watson test
## 
## data:  model
## DW = 1.9773, p-value = 0.3289
## alternative hypothesis: true autocorrelation is greater than 0
# 5- 오차의 선형성
plot(predict(model), residuals(model),
     main = "Residuals vs Fitted Values Plot")

# 플롯에 수평선을 추가하여 무작위 분포를 참조합니다.
abline(h = 0, col = "red")