# 2025-05-28
#데이터 로드 및 모델 생성
data(mtcars)
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
#데이터 구조 확인
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
model <-lm(mpg~hp, data = mtcars)#독립변수 1개, 단순선형(직선)회귀
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
# Residuals: 잔차(오차)
# Coefficients:회귀계수(Estimate)
# (Intercept):절편(상수): 30.9886
# 독립변수 hp 회귀계수는 : -0.06823
# pr(>|t|) 유의확률입니다.
# 0.000000179<유의수전 0.05
# 귀무가설: 회귀계수는 0이다
# 대립가설: 회귀계수는 0이 아니다.
# 따라서 귀무가설 기각, 통계적으로 유의한 변수라고 합니다.
format(1.79e-07,scientific=FALSE)
## [1] "0.000000179"
format(1.788e-07,scientific=FALSE)
## [1] "0.0000001788"
#Estimate /Std. Error : 표준오차=t valu(통계량)
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
# 30 DF (분모 자유도) = n(관측치수)-p(독립변수수,1)-1
# n=32개
# Residual standard error : 3.863
# 평균적으로 예측값과 실제값의 차이가 약 3.863 정도라는 뜻
# 30 degrees of freedom = 자유도
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
# Adjusted R-squard: 0.5892 : 수정된 결정계수
# 1. faithful 데이터 로드 및 모델 생성
data(faithful)
library(dplyr)
#데이터 구조 확인
glimpse(faithful)
## Rows: 272
## Columns: 2
## $ eruptions <dbl> 3.600, 1.800, 3.333, 2.283, 4.533, 2.883, 4.700, 3.600, 1.95…
## $ waiting <dbl> 79, 54, 74, 62, 85, 55, 88, 85, 51, 85, 54, 84, 78, 47, 83, …
head(faithful)
## eruptions waiting
## 1 3.600 79
## 2 1.800 54
## 3 3.333 74
## 4 2.283 62
## 5 4.533 85
## 6 2.883 55
tail(faithful)
## eruptions waiting
## 267 4.750 75
## 268 4.117 81
## 269 2.150 46
## 270 4.417 90
## 271 1.817 46
## 272 4.467 74
summary(faithful)
## eruptions waiting
## Min. :1.600 Min. :43.0
## 1st Qu.:2.163 1st Qu.:58.0
## Median :4.000 Median :76.0
## Mean :3.488 Mean :70.9
## 3rd Qu.:4.454 3rd Qu.:82.0
## Max. :5.100 Max. :96.0
model <-lm(eruptions~waiting, data = faithful)#독립변수 1개, 단순선형(직선)회귀
summary(model)
##
## 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
# 독립변수 hp 회귀계수는 : 0.075628
# pr(>|t|) 유의확률입니다.
# 2e-16<유의수전 0.05
# 귀무가설: 회귀계수는 0이다
# 대립가설: 회귀계수는 0이 아니다.
# 1) waiting변수는 통계적으로 유의미함. 회귀계수가 유의미(2e-16)이하 이기때문
# 2) 결정계수는 분출시간의 변화량중 81%가 대기시간과 영향이 있다는 의미
# 3) 해당 모형 전체는 통계적으로 유의미함
format(2e-16,scientific=FALSE)
## [1] "0.0000000000000002"
# 2.pressure 데이터 로드 및 모델 생성
data(pressure)
library(dplyr)
#데이터 구조 확인
glimpse(pressure)
## Rows: 19
## Columns: 2
## $ temperature <dbl> 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240,…
## $ pressure <dbl> 0.0002, 0.0012, 0.0060, 0.0300, 0.0900, 0.2700, 0.7500, 1.…
head(pressure)
## temperature pressure
## 1 0 0.0002
## 2 20 0.0012
## 3 40 0.0060
## 4 60 0.0300
## 5 80 0.0900
## 6 100 0.2700
tail(pressure)
## temperature pressure
## 14 260 96
## 15 280 157
## 16 300 247
## 17 320 376
## 18 340 558
## 19 360 806
summary(pressure)
## temperature pressure
## Min. : 0 Min. : 0.0002
## 1st Qu.: 90 1st Qu.: 0.1800
## Median :180 Median : 8.8000
## Mean :180 Mean :124.3367
## 3rd Qu.:270 3rd Qu.:126.5000
## Max. :360 Max. :806.0000
model <-lm( temperature~ pressure, data = pressure)#독립변수 1개, 단순선형(직선)회귀
summary(model)
##
## Call:
## lm(formula = temperature ~ pressure, data = pressure)
##
## Residuals:
## Min 1Q Median 3Q Max
## -132.791 -62.813 6.507 67.033 90.759
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 132.79072 19.94314 6.658 4.03e-06 ***
## pressure 0.37969 0.07929 4.788 0.000171 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 75.56 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):절편(상수): 132.79072
# 독립변수 hp 회귀계수는 : 0.37969
# pr(>|t|) 유의확률입니다.
# 4.03e-06 유의수전 0.05
# 귀무가설: 회귀계수는 0이다
# 대립가설: 회귀계수는 0이 아니다.
# 1) 회귀계수가 유의미(4.03e-06)이하 이기때문
# 2) 결정계수0.87은 압력의 변화량중 87%가 온도와 영향이 있다는 의미
# 3) 유의확률이 매우 작다는것으로 설명력이 충분하다 볼 수 있음
# 3.women 데이터 로드 및 모델 생성
data(women)
library(dplyr)
#데이터 구조 확인
glimpse(women)
## Rows: 15
## Columns: 2
## $ height <dbl> 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72
## $ weight <dbl> 115, 117, 120, 123, 126, 129, 132, 135, 139, 142, 146, 150, 154…
head(women)
## height weight
## 1 58 115
## 2 59 117
## 3 60 120
## 4 61 123
## 5 62 126
## 6 63 129
tail(women)
## height weight
## 10 67 142
## 11 68 146
## 12 69 150
## 13 70 154
## 14 71 159
## 15 72 164
summary(women)
## height weight
## Min. :58.0 Min. :115.0
## 1st Qu.:61.5 1st Qu.:124.5
## Median :65.0 Median :135.0
## Mean :65.0 Mean :136.7
## 3rd Qu.:68.5 3rd Qu.:148.0
## Max. :72.0 Max. :164.0
model <-lm( height~ weight, data = women)#독립변수 1개, 단순선형(직선)회귀
summary(model)
##
## Call:
## lm(formula = height ~ weight, data = women)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.83233 -0.26249 0.08314 0.34353 0.49790
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 25.723456 1.043746 24.64 2.68e-12 ***
## weight 0.287249 0.007588 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.44 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
# Residuals: 잔차(오차)
# Coefficients:회귀계수(Estimate)
# (Intercept):절편(상수): 25.723456
# 독립변수 hp 회귀계수는 : 0.287249
# pr(>|t|) 유의확률입니다.
# 2.68e-12 유의수전 0.05
# 귀무가설: 회귀계수는 0이다
# 대립가설: 회귀계수는 0이 아니다.
# 1) 회귀계수가 2.68e-12이고 키가 몸무게에 유의미한 영향을 준다 할 수 있음
# 2) 결정계수0.99은 키변수를 통해 몸무게를 매우 정확하게 예측할 수 있다는 의미
# 3) 회귀모형 전체가 높은 신뢰도를 가지고 있다는 의미