head(College)
## Private Apps Accept Enroll Top10perc Top25perc
## Abilene Christian University Yes 1660 1232 721 23 52
## Adelphi University Yes 2186 1924 512 16 29
## Adrian College Yes 1428 1097 336 22 50
## Agnes Scott College Yes 417 349 137 60 89
## Alaska Pacific University Yes 193 146 55 16 44
## Albertson College Yes 587 479 158 38 62
## F.Undergrad P.Undergrad Outstate Room.Board Books
## Abilene Christian University 2885 537 7440 3300 450
## Adelphi University 2683 1227 12280 6450 750
## Adrian College 1036 99 11250 3750 400
## Agnes Scott College 510 63 12960 5450 450
## Alaska Pacific University 249 869 7560 4120 800
## Albertson College 678 41 13500 3335 500
## Personal PhD Terminal S.F.Ratio perc.alumni Expend
## Abilene Christian University 2200 70 78 18.1 12 7041
## Adelphi University 1500 29 30 12.2 16 10527
## Adrian College 1165 53 66 12.9 30 8735
## Agnes Scott College 875 92 97 7.7 37 19016
## Alaska Pacific University 1500 76 72 11.9 2 10922
## Albertson College 675 67 73 9.4 11 9727
## Grad.Rate
## Abilene Christian University 60
## Adelphi University 56
## Adrian College 54
## Agnes Scott College 59
## Alaska Pacific University 15
## Albertson College 55
str(College)
## 'data.frame': 777 obs. of 18 variables:
## $ Private : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 2 2 2 2 2 ...
## $ Apps : num 1660 2186 1428 417 193 ...
## $ Accept : num 1232 1924 1097 349 146 ...
## $ Enroll : num 721 512 336 137 55 158 103 489 227 172 ...
## $ Top10perc : num 23 16 22 60 16 38 17 37 30 21 ...
## $ Top25perc : num 52 29 50 89 44 62 45 68 63 44 ...
## $ F.Undergrad: num 2885 2683 1036 510 249 ...
## $ P.Undergrad: num 537 1227 99 63 869 ...
## $ Outstate : num 7440 12280 11250 12960 7560 ...
## $ Room.Board : num 3300 6450 3750 5450 4120 ...
## $ Books : num 450 750 400 450 800 500 500 450 300 660 ...
## $ Personal : num 2200 1500 1165 875 1500 ...
## $ PhD : num 70 29 53 92 76 67 90 89 79 40 ...
## $ Terminal : num 78 30 66 97 72 73 93 100 84 41 ...
## $ S.F.Ratio : num 18.1 12.2 12.9 7.7 11.9 9.4 11.5 13.7 11.3 11.5 ...
## $ perc.alumni: num 12 16 30 37 2 11 26 37 23 15 ...
## $ Expend : num 7041 10527 8735 19016 10922 ...
## $ Grad.Rate : num 60 56 54 59 15 55 63 73 80 52 ...
summary(College)
## Private Apps Accept Enroll Top10perc
## No :212 Min. : 81 Min. : 72 Min. : 35 Min. : 1.00
## Yes:565 1st Qu.: 776 1st Qu.: 604 1st Qu.: 242 1st Qu.:15.00
## Median : 1558 Median : 1110 Median : 434 Median :23.00
## Mean : 3002 Mean : 2019 Mean : 780 Mean :27.56
## 3rd Qu.: 3624 3rd Qu.: 2424 3rd Qu.: 902 3rd Qu.:35.00
## Max. :48094 Max. :26330 Max. :6392 Max. :96.00
## Top25perc F.Undergrad P.Undergrad Outstate
## Min. : 9.0 Min. : 139 Min. : 1.0 Min. : 2340
## 1st Qu.: 41.0 1st Qu.: 992 1st Qu.: 95.0 1st Qu.: 7320
## Median : 54.0 Median : 1707 Median : 353.0 Median : 9990
## Mean : 55.8 Mean : 3700 Mean : 855.3 Mean :10441
## 3rd Qu.: 69.0 3rd Qu.: 4005 3rd Qu.: 967.0 3rd Qu.:12925
## Max. :100.0 Max. :31643 Max. :21836.0 Max. :21700
## Room.Board Books Personal PhD
## Min. :1780 Min. : 96.0 Min. : 250 Min. : 8.00
## 1st Qu.:3597 1st Qu.: 470.0 1st Qu.: 850 1st Qu.: 62.00
## Median :4200 Median : 500.0 Median :1200 Median : 75.00
## Mean :4358 Mean : 549.4 Mean :1341 Mean : 72.66
## 3rd Qu.:5050 3rd Qu.: 600.0 3rd Qu.:1700 3rd Qu.: 85.00
## Max. :8124 Max. :2340.0 Max. :6800 Max. :103.00
## Terminal S.F.Ratio perc.alumni Expend
## Min. : 24.0 Min. : 2.50 Min. : 0.00 Min. : 3186
## 1st Qu.: 71.0 1st Qu.:11.50 1st Qu.:13.00 1st Qu.: 6751
## Median : 82.0 Median :13.60 Median :21.00 Median : 8377
## Mean : 79.7 Mean :14.09 Mean :22.74 Mean : 9660
## 3rd Qu.: 92.0 3rd Qu.:16.50 3rd Qu.:31.00 3rd Qu.:10830
## Max. :100.0 Max. :39.80 Max. :64.00 Max. :56233
## Grad.Rate
## Min. : 10.00
## 1st Qu.: 53.00
## Median : 65.00
## Mean : 65.46
## 3rd Qu.: 78.00
## Max. :118.00
par(mfrow=c(2,4))
hist(College$Apps, main="Apps")
hist(College$Accept, main="Accept")
hist(College$Enroll, main="Enroll")
hist(College$Top10perc, main="Top10perc")
hist(College$Top25perc, main="Top25perc")
hist(College$F.Undergrad, main="F.Undergrad")
hist(College$P.Undergrad, main="P.Undergrad")
hist(College$Outstate, main="Outstate")
par(mfrow=c(2,4))
hist(College$Room.Board, main="Room.Board")
hist(College$Books, main="Books")
hist(College$Personal, main="Personal")
hist(College$PhD,main="PhD")
hist(College$Terminal, main="Terminal")
hist(College$perc.alumni, main="perc.alumni")
hist(College$Expend, main="Expend")
hist(College$Grad.Rate, main="Grad.Rate")
4. 분포 - Private에서 공립학교가 212개 사립학교가 565개로 사립학교가
공립학교의 2.5배 이상 많음. - 히스토그램을 보면 Top25perc와 Grad.Rate이
변수들 중 가장 정규분포의 형태와 비슷해보임. - Apps, Accept, Enroll,
Top10perc, F.Undergrad, P.undergrad, Room.Board, Books, Peraonal,
perc.alumni, Expend은 분포가 왼쪽으로 치우쳐져 있음. 오른쪽 꼬리가 긴
경우가 많은 것으로 보아 이상치가 많은 것으로 보임. - PhD, Terminal은
분포가 오른쪽으로 치우쳐져 있음.
pairs(College[,-1])
cor(College[,-1])
## Apps Accept Enroll Top10perc Top25perc
## Apps 1.00000000 0.94345057 0.84682205 0.3388337 0.35163990
## Accept 0.94345057 1.00000000 0.91163666 0.1924469 0.24747574
## Enroll 0.84682205 0.91163666 1.00000000 0.1812935 0.22674511
## Top10perc 0.33883368 0.19244693 0.18129353 1.0000000 0.89199497
## Top25perc 0.35163990 0.24747574 0.22674511 0.8919950 1.00000000
## F.Undergrad 0.81449058 0.87422328 0.96463965 0.1412887 0.19944466
## P.Undergrad 0.39826427 0.44127073 0.51306860 -0.1053563 -0.05357664
## Outstate 0.05015903 -0.02575455 -0.15547734 0.5623305 0.48939383
## Room.Board 0.16493896 0.09089863 -0.04023168 0.3714804 0.33148989
## Books 0.13255860 0.11352535 0.11271089 0.1188584 0.11552713
## Personal 0.17873085 0.20098867 0.28092946 -0.0933164 -0.08081027
## PhD 0.39069733 0.35575788 0.33146914 0.5318280 0.54586221
## Terminal 0.36949147 0.33758337 0.30827407 0.4911350 0.52474884
## S.F.Ratio 0.09563303 0.17622901 0.23727131 -0.3848745 -0.29462884
## perc.alumni -0.09022589 -0.15998987 -0.18079413 0.4554853 0.41786429
## Expend 0.25959198 0.12471701 0.06416923 0.6609134 0.52744743
## Grad.Rate 0.14675460 0.06731255 -0.02234104 0.4949892 0.47728116
## F.Undergrad P.Undergrad Outstate Room.Board Books
## Apps 0.81449058 0.39826427 0.05015903 0.16493896 0.132558598
## Accept 0.87422328 0.44127073 -0.02575455 0.09089863 0.113525352
## Enroll 0.96463965 0.51306860 -0.15547734 -0.04023168 0.112710891
## Top10perc 0.14128873 -0.10535628 0.56233054 0.37148038 0.118858431
## Top25perc 0.19944466 -0.05357664 0.48939383 0.33148989 0.115527130
## F.Undergrad 1.00000000 0.57051219 -0.21574200 -0.06889039 0.115549761
## P.Undergrad 0.57051219 1.00000000 -0.25351232 -0.06132551 0.081199521
## Outstate -0.21574200 -0.25351232 1.00000000 0.65425640 0.038854868
## Room.Board -0.06889039 -0.06132551 0.65425640 1.00000000 0.127962970
## Books 0.11554976 0.08119952 0.03885487 0.12796297 1.000000000
## Personal 0.31719954 0.31988162 -0.29908690 -0.19942818 0.179294764
## PhD 0.31833697 0.14911422 0.38298241 0.32920228 0.026905731
## Terminal 0.30001894 0.14190357 0.40798320 0.37453955 0.099954700
## S.F.Ratio 0.27970335 0.23253051 -0.55482128 -0.36262774 -0.031929274
## perc.alumni -0.22946222 -0.28079236 0.56626242 0.27236345 -0.040207736
## Expend 0.01865162 -0.08356842 0.67277862 0.50173942 0.112409075
## Grad.Rate -0.07877313 -0.25700099 0.57128993 0.42494154 0.001060894
## Personal PhD Terminal S.F.Ratio perc.alumni
## Apps 0.17873085 0.39069733 0.36949147 0.09563303 -0.09022589
## Accept 0.20098867 0.35575788 0.33758337 0.17622901 -0.15998987
## Enroll 0.28092946 0.33146914 0.30827407 0.23727131 -0.18079413
## Top10perc -0.09331640 0.53182802 0.49113502 -0.38487451 0.45548526
## Top25perc -0.08081027 0.54586221 0.52474884 -0.29462884 0.41786429
## F.Undergrad 0.31719954 0.31833697 0.30001894 0.27970335 -0.22946222
## P.Undergrad 0.31988162 0.14911422 0.14190357 0.23253051 -0.28079236
## Outstate -0.29908690 0.38298241 0.40798320 -0.55482128 0.56626242
## Room.Board -0.19942818 0.32920228 0.37453955 -0.36262774 0.27236345
## Books 0.17929476 0.02690573 0.09995470 -0.03192927 -0.04020774
## Personal 1.00000000 -0.01093579 -0.03061311 0.13634483 -0.28596808
## PhD -0.01093579 1.00000000 0.84958703 -0.13053011 0.24900866
## Terminal -0.03061311 0.84958703 1.00000000 -0.16010395 0.26713029
## S.F.Ratio 0.13634483 -0.13053011 -0.16010395 1.00000000 -0.40292917
## perc.alumni -0.28596808 0.24900866 0.26713029 -0.40292917 1.00000000
## Expend -0.09789189 0.43276168 0.43879922 -0.58383204 0.41771172
## Grad.Rate -0.26934396 0.30503785 0.28952723 -0.30671041 0.49089756
## Expend Grad.Rate
## Apps 0.25959198 0.146754600
## Accept 0.12471701 0.067312550
## Enroll 0.06416923 -0.022341039
## Top10perc 0.66091341 0.494989235
## Top25perc 0.52744743 0.477281164
## F.Undergrad 0.01865162 -0.078773129
## P.Undergrad -0.08356842 -0.257000991
## Outstate 0.67277862 0.571289928
## Room.Board 0.50173942 0.424941541
## Books 0.11240908 0.001060894
## Personal -0.09789189 -0.269343964
## PhD 0.43276168 0.305037850
## Terminal 0.43879922 0.289527232
## S.F.Ratio -0.58383204 -0.306710405
## perc.alumni 0.41771172 0.490897562
## Expend 1.00000000 0.390342696
## Grad.Rate 0.39034270 1.000000000
boxplot(Apps ~ Private, data = College, main = "Apps & Private")
t.test(College$Apps~College$Private)
##
## Welch Two Sample t-test
##
## data: College$Apps by College$Private
## t = 9.7985, df = 244.49, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group No and group Yes is not equal to 0
## 95 percent confidence interval:
## 2997.758 4506.223
## sample estimates:
## mean in group No mean in group Yes
## 5729.920 1977.929
In this exercise, we will predict the number of applications received
using the the other variables in the College data set.
(a) Split the data set into a training set and a test set.
set.seed(1)
train_c<-sample(dim(College)[1], dim(College)[1]*0.7)
test_c<- (-train_c)
coll_train<-College[train_c,]
coll_test<-College[test_c,]
m_c = lm(Apps~. , data = College, subset=train_c)
summary(m_c)
##
## Call:
## lm(formula = Apps ~ ., data = College, subset = train_c)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5816.1 -451.6 -1.0 327.2 7445.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.377e+02 5.076e+02 -1.059 0.28995
## PrivateYes -5.045e+02 1.648e+02 -3.061 0.00232 **
## Accept 1.722e+00 4.763e-02 36.159 < 2e-16 ***
## Enroll -1.055e+00 2.437e-01 -4.329 1.80e-05 ***
## Top10perc 5.358e+01 6.440e+00 8.320 7.64e-16 ***
## Top25perc -1.614e+01 5.092e+00 -3.170 0.00161 **
## F.Undergrad 2.970e-02 4.399e-02 0.675 0.49976
## P.Undergrad 7.162e-02 3.649e-02 1.963 0.05019 .
## Outstate -8.841e-02 2.176e-02 -4.064 5.57e-05 ***
## Room.Board 1.630e-01 5.577e-02 2.923 0.00362 **
## Books 2.727e-01 2.723e-01 1.001 0.31715
## Personal -7.316e-03 7.283e-02 -0.100 0.92002
## PhD -9.676e+00 5.360e+00 -1.805 0.07161 .
## Terminal -3.781e-01 6.015e+00 -0.063 0.94990
## S.F.Ratio 1.627e+01 1.608e+01 1.012 0.31214
## perc.alumni 2.358e+00 4.853e+00 0.486 0.62722
## Expend 5.986e-02 1.337e-02 4.476 9.34e-06 ***
## Grad.Rate 7.158e+00 3.520e+00 2.034 0.04248 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1022 on 525 degrees of freedom
## Multiple R-squared: 0.9332, Adjusted R-squared: 0.9311
## F-statistic: 431.6 on 17 and 525 DF, p-value: < 2.2e-16
m_c.pred<-predict(m_c,coll_test)
m_c.error<-mean((m_c.pred-coll_test$Apps)^2)
m_c.error
## [1] 1261630
1)잔차가 선형성인지, 선 주위로 무작위로 분포했는지: 잔차들이 한쪽
끝에 몰려있는 모습이 나타난다.
2)잔차가 정규분포를 따르는지(직선에 근접했는지): 이상값들이 많아
정규분포를 따르지 않는 것으로 보인다.
3)잔차가 동일분산을 따르는지(값들이 패턴없이 일정하게 분포되어있는지):
잔차들이 뭉쳐있는 모습이 많이 보인다.
4)이상값이 얼마나 영향을 주는지(점선으로 된 곡선 영역 밖에 있는지) :
이상치들이 많고 큰 영향을 주는 이상값들이 몇개 보인다.
par(mfrow=c(2,2))
plot(m_c)
x_c<-model.matrix(Apps~., data=College)[, -1]
y_c<-College$Apps
set.seed(2)
ridge_c<-glmnet(x_c[train_c, ],y_c[train_c],alpha=0)
cv.ridge_c<-cv.glmnet(x_c[train_c, ],y_c[train_c],alpha=0)
plot(cv.ridge_c)
# 그래프 x축 맨 위의 숫자는 lambda에 따라 0이 아닌 계수(coefficient)의 개수(lambda가 증가할수록 계수가 0이 됨.)
# y 축은 mean squared error, x축은 lambda에 log를 취한 값.
rlam_c<-cv.ridge_c$lambda.min
rlam_c
## [1] 367.5286
ridge_c.pred<-predict(ridge_c,s=rlam_c, newx=x_c[test_c, ])
ridge_c.error<-mean((ridge_c.pred-y_c[test_c])^2)
ridge_c.error
## [1] 1121034
lasso_c<-glmnet(x_c[train_c, ],y_c[train_c],alpha=1)
cv.lasso_c<-cv.glmnet(x_c[train_c, ],y_c[train_c],alpha=1)
plot(cv.lasso_c)
# 그래프 x축 맨 위의 숫자는 각 lambda에 따라 0이 아닌 계수(coefficient)의 개수
# y 축은 mean squared error, x축은 lambda에 log를 취한 값.
lasslam_c<-cv.lasso_c$lambda.min
lasslam_c
## [1] 10.46735
lasso_c.pred<-predict(lasso_c,s=lasslam_c, newx=x_c[test_c, ])
lasso_c.error<-mean((lasso_c.pred-y_c[test_c])^2)
m_c.error; ridge_c.error;lasso_c.error #error 비교
## [1] 1261630
## [1] 1121034
## [1] 1228323
errors<-c(m_c.error, ridge_c.error,lasso_c.error)
min_c <- 1+(errors==min(errors))
barplot(errors,names.arg=c("Linear","Ridge","Lasso"),col=c("black","red")[min_c])
가장 작은 예측 MSE를 가진 모델은 ridge모델이다.
library(ISLR2)
head(Boston)
## crim zn indus chas nox rm age dis rad tax ptratio lstat medv
## 1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 4.98 24.0
## 2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 9.14 21.6
## 3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 4.03 34.7
## 4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 2.94 33.4
## 5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 5.33 36.2
## 6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 5.21 28.7
str(Boston)
## 'data.frame': 506 obs. of 13 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
summary(Boston)
## crim zn indus chas
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000
## 1st Qu.: 0.08205 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000
## nox rm age dis
## Min. :0.3850 Min. :3.561 Min. : 2.90 Min. : 1.130
## 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02 1st Qu.: 2.100
## Median :0.5380 Median :6.208 Median : 77.50 Median : 3.207
## Mean :0.5547 Mean :6.285 Mean : 68.57 Mean : 3.795
## 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08 3rd Qu.: 5.188
## Max. :0.8710 Max. :8.780 Max. :100.00 Max. :12.127
## rad tax ptratio lstat
## Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 1.73
## 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.: 6.95
## Median : 5.000 Median :330.0 Median :19.05 Median :11.36
## Mean : 9.549 Mean :408.2 Mean :18.46 Mean :12.65
## 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:16.95
## Max. :24.000 Max. :711.0 Max. :22.00 Max. :37.97
## medv
## Min. : 5.00
## 1st Qu.:17.02
## Median :21.20
## Mean :22.53
## 3rd Qu.:25.00
## Max. :50.00
length(which(Boston$chas==1))
## [1] 35
length(which(Boston$chas==0))
## [1] 471
par(mfrow=c(2,4))
hist(Boston$crim, main="crim")
hist(Boston$zn, main="zn")
hist(Boston$indus, main="indus")
hist(Boston$nox, main="nox")
hist(Boston$rm, main="rm")
hist(Boston$age, main="age")
hist(Boston$dis, main="dis")
hist(Boston$rad, main="rad")
4. 분포
- chas에서 강계에 위치한 경우는 35개, 그렇지 않은 경우는 471개임.
- 히스토그램을 보면 rm이 변수들 중 가장 정규분포의 형태와 비슷해보임.
- crim, zn, dis, nox,dis, lstat은 분포가 왼쪽으로 치우쳐져 있음. 오른쪽 꼬리가 긴 경우가 많은 것으로 보아 이상치가 많은 것으로 보임.
- age, ptratio은 분포가 오른쪽으로 치우쳐져 있음.
```r
par(mfrow=c(2,4))
hist(Boston$tax, main="tax")
hist(Boston$ptratio, main="ptratio")
hist(Boston$lstat, main="lstat")
hist(Boston$medv, main="medv")
pairs(Boston[,-4])
cor(Boston[,-4])
## crim zn indus nox rm age
## crim 1.0000000 -0.2004692 0.4065834 0.4209717 -0.2192467 0.3527343
## zn -0.2004692 1.0000000 -0.5338282 -0.5166037 0.3119906 -0.5695373
## indus 0.4065834 -0.5338282 1.0000000 0.7636514 -0.3916759 0.6447785
## nox 0.4209717 -0.5166037 0.7636514 1.0000000 -0.3021882 0.7314701
## rm -0.2192467 0.3119906 -0.3916759 -0.3021882 1.0000000 -0.2402649
## age 0.3527343 -0.5695373 0.6447785 0.7314701 -0.2402649 1.0000000
## dis -0.3796701 0.6644082 -0.7080270 -0.7692301 0.2052462 -0.7478805
## rad 0.6255051 -0.3119478 0.5951293 0.6114406 -0.2098467 0.4560225
## tax 0.5827643 -0.3145633 0.7207602 0.6680232 -0.2920478 0.5064556
## ptratio 0.2899456 -0.3916785 0.3832476 0.1889327 -0.3555015 0.2615150
## lstat 0.4556215 -0.4129946 0.6037997 0.5908789 -0.6138083 0.6023385
## medv -0.3883046 0.3604453 -0.4837252 -0.4273208 0.6953599 -0.3769546
## dis rad tax ptratio lstat medv
## crim -0.3796701 0.6255051 0.5827643 0.2899456 0.4556215 -0.3883046
## zn 0.6644082 -0.3119478 -0.3145633 -0.3916785 -0.4129946 0.3604453
## indus -0.7080270 0.5951293 0.7207602 0.3832476 0.6037997 -0.4837252
## nox -0.7692301 0.6114406 0.6680232 0.1889327 0.5908789 -0.4273208
## rm 0.2052462 -0.2098467 -0.2920478 -0.3555015 -0.6138083 0.6953599
## age -0.7478805 0.4560225 0.5064556 0.2615150 0.6023385 -0.3769546
## dis 1.0000000 -0.4945879 -0.5344316 -0.2324705 -0.4969958 0.2499287
## rad -0.4945879 1.0000000 0.9102282 0.4647412 0.4886763 -0.3816262
## tax -0.5344316 0.9102282 1.0000000 0.4608530 0.5439934 -0.4685359
## ptratio -0.2324705 0.4647412 0.4608530 1.0000000 0.3740443 -0.5077867
## lstat -0.4969958 0.4886763 0.5439934 0.3740443 1.0000000 -0.7376627
## medv 0.2499287 -0.3816262 -0.4685359 -0.5077867 -0.7376627 1.0000000
boxplot(crim ~ chas, data = Boston, main = "chas & crim")
t.test(Boston$crim~Boston$chas)
##
## Welch Two Sample t-test
##
## data: Boston$crim by Boston$chas
## t = 3.2224, df = 120.42, p-value = 0.001636
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
## 0.7298305 3.0557226
## sample estimates:
## mean in group 0 mean in group 1
## 3.744447 1.851670
In this exercise, we will predict the per capita crime rate by town using the the other variables in the Boston data set. (a) Split the data set into a training set and a test set.
set.seed(3)
train_b<-sample(dim(Boston)[1], dim(Boston)[1]*0.7)
test_b<- (-train_b)
bos_train<-Boston[train_b,]
bos_test<-Boston[test_b,]
m_b = lm(crim ~ ., data = Boston)
summary(m_b)
##
## Call:
## lm(formula = crim ~ ., data = Boston)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.534 -2.248 -0.348 1.087 73.923
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.7783938 7.0818258 1.946 0.052271 .
## zn 0.0457100 0.0187903 2.433 0.015344 *
## indus -0.0583501 0.0836351 -0.698 0.485709
## chas -0.8253776 1.1833963 -0.697 0.485841
## nox -9.9575865 5.2898242 -1.882 0.060370 .
## rm 0.6289107 0.6070924 1.036 0.300738
## age -0.0008483 0.0179482 -0.047 0.962323
## dis -1.0122467 0.2824676 -3.584 0.000373 ***
## rad 0.6124653 0.0875358 6.997 8.59e-12 ***
## tax -0.0037756 0.0051723 -0.730 0.465757
## ptratio -0.3040728 0.1863598 -1.632 0.103393
## lstat 0.1388006 0.0757213 1.833 0.067398 .
## medv -0.2200564 0.0598240 -3.678 0.000261 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.46 on 493 degrees of freedom
## Multiple R-squared: 0.4493, Adjusted R-squared: 0.4359
## F-statistic: 33.52 on 12 and 493 DF, p-value: < 2.2e-16
m_b.pred<-predict(m_b,bos_test)
m_b.error<-mean((m_b.pred-bos_test$crim)^2)
1)잔차가 선형성인지, 선 주위로 무작위로 분포했는지: 대체적으로
선형성을 가지지만, 잔차들이 한쪽에 몰려있는 모습이 나타난다.
2)잔차가 정규분포를 따르는지(직선에 근접했는지): 정규분포를 따르지 않는
것으로 보인다.
3)잔차가 동일분산을 따르는지(값들이 패턴없이 일정하게 분포되어있는지):
잔차들이 뭉쳐있는 모습이 많이 보인다.
4)이상값이 얼마나 영향을 주는지(점선으로 된 곡선 영역 밖에 있는지) :
이상치들은 있지만, 큰 영향을 주지는 않는다.
par(mfrow=c(2,2))
plot(m_b)
x_b<-model.matrix(crim~., data=Boston)[, -4]
y_b<-Boston$crim
set.seed(4)
ridge_b<-glmnet(x_b[train_b, ],y_b[train_b],alpha=0)
cv.ridge_b<-cv.glmnet(x_b[train_b, ],y_b[train_b],alpha=0)
plot(cv.ridge_b)
rlam_b<-cv.ridge_b$lambda.min
rlam_b
## [1] 0.5086605
ridge_b.pred<-predict(ridge_b,s=rlam_b, newx=x_b[test_b, ])
ridge_b.error<-mean((ridge_b.pred-y_b[test_b])^2)
lasso_b<-glmnet(x_b[train_b, ],y_b[train_b],alpha=1)
cv.lasso_b<-cv.glmnet(x_b[train_b, ],y_b[train_b],alpha=1)
plot(cv.lasso_b)
lasslam_b<-cv.lasso_b$lambda.min
lasslam_b
## [1] 0.009985212
lasso_b.pred<-predict(lasso_b,s=lasslam_b, newx=x_b[test_b, ])
lasso_b.error<-mean((lasso_b.pred-y_b[test_b])^2)
m_b.error; ridge_b.error;lasso_b.error #error 비교
## [1] 71.99841
## [1] 76.97298
## [1] 76.01978
errors_b<-c(m_b.error, ridge_b.error,lasso_b.error)
min_b <- 1+(errors_b==min(errors_b))
barplot(errors_b,names.arg=c("Linear","Ridge","Lasso"),col=c("black","red")[min_b])
- cross validation 결과 MSE가 가장 작은 lambda값은 0.009985212이다. -
위의 lambda를 사용한 lasso 모델로 예측했을 때의 MSE는 76.01978이다.