rm(list = ls())
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data(mtcars)
# 데이터 구조 확인
glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
tail(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
# lm : 회귀분석 사용함수
model <- lm(mpg ~ hp, data = mtcars)
summary(model)
##
## 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
# Residuls : 잔차(오차)
# Coeffcients : 회귀계수(Estimate)
# (Intercept) : 절편(상수) : 30.9886
# 독립변수 hp 회귀계수 : -0.06823
# Pr(>|t|) : 유의 확률
# 0.000000179 < 유의수준 0.05
# 귀무가설 : 회귀계수는 0이다.
# 대립 가설 : 회귀게수는 0이 아니다.
# 따라서 귀무가설 기각, 통계적으로 유의한
# 변수라 한다.
# 0.000000179로 표시
format(1.79e-07, scientific = FALSE)
## [1] "0.000000179"
# Multiple R-squared(결정계수) : 0.6024
# 결정계수 0~1 사이의 값을 갖는다.
# 결정계수 1이라면 선형회귀 직선 위에 모든
# 관측자가 있음을 의미함.
# 다음 모델이 가장 큰 결정계수를 가진 회귀모델
# model3 <- lm(weight ~ height, data = women)
# 일반적으로 결정계수 값이 0.75이상이면
# 좋은 설명력을 가진 모형이다.
# Estimate /std. Error : 표준오차 = t-value
# (통통계량)
30.09886 / 1.63392
## [1] 18.42126
# t 통계량의 절대값이 클수록 통계적으로
# 유의(significance)하다.
# Signif. codes : 유의수준
# '*'가 있으면 유의수준 0.05에서 유의한 변수
# 회귀모형의 유의성 검정 판단에 필요한
# F-statistic: 45.46, 0.0000001788
# 귀무가설 : 모든 회귀계수는 0
# 대립가설 : 적어도 하나의 회귀계수는 0이 아님
# p-value: 0.0000001788 < 0.05(유의수준)
# 귀무가설 기각, 따라서 통계적으로 유의한
# 모형이라 한다.
# 결과적으로 회귀계수가 하나라도 0이 아니면
# 통계적으로 유의한 모형이다.
# 표본크기 - 독립변수의 수 - 1 = 자유도
# n - 1 - 1 = 30
# 1 and 30 DF : 1 DF (분자 자유도) =
# 회귀모형에 사용된 설명변수 개수
# 30 DF (분모 자유도) = n(관측지수)
# - p(독립변수 수, 1) - 1
# n = 32개
# Residual standard error : 3.863
# 평균적으로 예측값과 실제값의 차이가 약
# 3.863 정도라는 뜻
# 30 degrees of freedom = 자유도
# Adjusted R-squared: 0.5892 : 수정된
# 결정계수
data("faithful")
model1 <- lm(eruptions ~ waiting, data = faithful)
summary(model1)
##
## Call:
## lm(formula = eruptions ~ waiting, data = faithful)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.29917 -0.37689 0.03508 0.34909 1.19329
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.874016 0.160143 -11.70 <2e-16 ***
## waiting 0.075628 0.002219 34.09 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4965 on 270 degrees of freedom
## Multiple R-squared: 0.8115, Adjusted R-squared: 0.8108
## F-statistic: 1162 on 1 and 270 DF, p-value: < 2.2e-16
# Residuals : 잔차(오차)
# Coefficients : 회귀계수(Estimate)
# (Intercept) : 절편(상수) : -1.874016
# 독립변수 waiting 회귀계수는: 0.075628
# Pr(>|t|) 유의확률이다.
# 0.0000000000000002 < 유의수준 0.05
#
# n - 1 - 1 = 270, n = 272
# Residual standard error: 0.4965
# degrees of freedom = 270
#Adjusted R-squared: 0.8108
format(2e-16, scientific=FALSE)
## [1] "0.0000000000000002"
format(1.79e-07, scientific=FALSE)
## [1] "0.000000179"
data("pressure")
model2 <- lm(pressure ~ temperature, data = pressure)
summary(model2)
##
## Call:
## lm(formula = pressure ~ temperature, data = pressure)
##
## Residuals:
## Min 1Q Median 3Q Max
## -158.08 -117.06 -32.84 72.30 409.43
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -147.8989 66.5529 -2.222 0.040124 *
## temperature 1.5124 0.3158 4.788 0.000171 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 150.8 on 17 degrees of freedom
## Multiple R-squared: 0.5742, Adjusted R-squared: 0.5492
## F-statistic: 22.93 on 1 and 17 DF, p-value: 0.000171
# Residuals : 잔차(오차)
# Coefficients : 회귀계수(Estimate)
# (Intercept) : 절편(상수) : -147.8989
# 독립변수 temperature 회귀계수는: 1.5124
# Pr(>|t|) 유의확률이다.
# 0.000171 < 유의수준 0.05
# 귀무가설 : 회귀계수는 0이다.
# 대립가설 : 회귀계수는 0이 아니다.
# 따라서 귀무가설 기각, 통계적으로 유의한
# 변수라고 한다.
# Multiple R-squared: 0.5742, Adjusted
# R-squared: 0.5492
# 결정계수는 중간이므로 모델이 데이터에 대한
# 설명이 애매하다.
#
# n - 1 - 1 = 17, n = 19
# Residual standard error: 150.8
# degrees of freedom = 17
#Adjusted R-squared: 0.5492
data("women")
model3 <- lm(weight ~ height, data = women)
summary(model3)
##
## Call:
## lm(formula = weight ~ height, data = women)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7333 -1.1333 -0.3833 0.7417 3.1167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -87.51667 5.93694 -14.74 1.71e-09 ***
## height 3.45000 0.09114 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.525 on 13 degrees of freedom
## Multiple R-squared: 0.991, Adjusted R-squared: 0.9903
## F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14
# 0.0000000000000109 < 유의수준 0.05
# 귀무가설 : 회귀계수는 0이다.
# 대립가설 : 회귀계수는 0이 아니다.
# 따라서 귀무가설 기각, 통계적으로 유의한
# 변수라고 한다.
# Multiple R-squared: 0.991, Adjusted
# R-squared: 0.9903
# 결정계수는 1과 가까우므로 모델이 데이터를 잘
# 설명한다고 할 수 있다.
# n - 1 - 1 = 13, n = 15
# Residual standard error: 1.525
# degrees of freedom = 17
#Adjusted R-squared: 0.9903