data(mtcars)
# 다중 선형회귀모형 적합
model <- lm(mpg ~ wt + hp + drat, data = mtcars)
# 회귀 결과 요약
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt + hp + drat, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.3598 -1.8374 -0.5099  0.9681  5.7078 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 29.394934   6.156303   4.775 5.13e-05 ***
## wt          -3.227954   0.796398  -4.053 0.000364 ***
## hp          -0.032230   0.008925  -3.611 0.001178 ** 
## drat         1.615049   1.226983   1.316 0.198755    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.561 on 28 degrees of freedom
## Multiple R-squared:  0.8369, Adjusted R-squared:  0.8194 
## F-statistic: 47.88 on 3 and 28 DF,  p-value: 3.768e-11
# wt, hp의 유의확률은 < 0.01로 통계적으로
# 유의미하다.

# 결정계수는 0.84로 mpg의 변화에 대해 84%를
# 설명함을 의미한다.

# F-검정의 유의확률은 0.001 이하로 설명변수는
# 종속변수에 유의미한 영향력을 가진다.

# 데이터 확인
data(iris)
# 다중 선형회귀모형 적합
model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = iris)
# 회귀 결과 요약
summary(model)
## 
## Call:
## lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
##     data = iris)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.82816 -0.21989  0.01875  0.19709  0.84570 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.85600    0.25078   7.401 9.85e-12 ***
## Sepal.Width   0.65084    0.06665   9.765  < 2e-16 ***
## Petal.Length  0.70913    0.05672  12.502  < 2e-16 ***
## Petal.Width  -0.55648    0.12755  -4.363 2.41e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3145 on 146 degrees of freedom
## Multiple R-squared:  0.8586, Adjusted R-squared:  0.8557 
## F-statistic: 295.5 on 3 and 146 DF,  p-value: < 2.2e-16
# Petal.Length, Petal.Width 변수의 유의확률은
# 0.001이하로 유의미하다.

# 결정계수는 0.8586으로 Sepal.Length의 변화에 
# 대해 약 85%를 설명함을 의미한다.

# 설명변수가 종속변수에 영향을 준다는 것을 의미

# 결측치 제거
data(airquality)
airquality_clean <- na.omit(airquality)
# 다중 선형회귀모형 적합
model <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality_clean)
# 회귀 결과 요약
summary(model)
## 
## Call:
## lm(formula = Ozone ~ Solar.R + Wind + Temp, data = airquality_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -40.485 -14.219  -3.551  10.097  95.619 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -64.34208   23.05472  -2.791  0.00623 ** 
## Solar.R       0.05982    0.02319   2.580  0.01124 *  
## Wind         -3.33359    0.65441  -5.094 1.52e-06 ***
## Temp          1.65209    0.25353   6.516 2.42e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 21.18 on 107 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.5948 
## F-statistic: 54.83 on 3 and 107 DF,  p-value: < 2.2e-16
# Wind가 클수록 Ozone은 낮아진다.

# 결정계수는 0.6059으로 Ozone의 변화에
# 대해 세 독립변수가 60%를 설명함을 의미한다.

# 유의확률은 0.001이하로 최소 하나의 설명변수가
# 종속변수에 유의하단 것을 의미한다.