head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : int 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : int 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : int 3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : int 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : int 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
## - attr(*, "na.action")= 'omit' Named int [1:5] 33 127 331 337 355
## ..- attr(*, "names")= chr [1:5] "33" "127" "331" "337" ...
Auto$origin<-as.factor(Auto$origin)
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : int 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : int 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : int 3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : int 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
## - attr(*, "na.action")= 'omit' Named int [1:5] 33 127 331 337 355
## ..- attr(*, "names")= chr [1:5] "33" "127" "331" "337" ...
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 1:245 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 2: 68 ford pinto : 5
## Median :15.50 Median :76.00 3: 79 toyota corolla : 5
## Mean :15.54 Mean :75.98 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 amc hornet : 4
## Max. :24.80 Max. :82.00 chevrolet chevette: 4
## (Other) :365
Auto1<-Auto[,-9]
par(mfrow=c(1,3))
barplot(table(Auto$origin), main="Origin")
hist(Auto$mpg, main="mpg",freq=F)
lines(density(Auto$mpg))
hist(Auto$cylinders, main="cylinders",freq=F)
lines(density(Auto$cylinders))
hist(Auto$displacement, main="displacement",freq=F)
lines(density(Auto$displacement))
hist(Auto$horsepower, main="horsepower",freq=F)
lines(density(Auto$horsepower))
hist(Auto$acceleration, main="acceleration",freq=F)
lines(density(Auto$acceleration))
hist(Auto$year, main="year",freq=F)
lines(density(Auto$year))
pairs(Auto[,1:7])
corr_a<-cor(Auto[,1:7])
round(corr_a,2)
## mpg cylinders displacement horsepower weight acceleration year
## mpg 1.00 -0.78 -0.81 -0.78 -0.83 0.42 0.58
## cylinders -0.78 1.00 0.95 0.84 0.90 -0.50 -0.35
## displacement -0.81 0.95 1.00 0.90 0.93 -0.54 -0.37
## horsepower -0.78 0.84 0.90 1.00 0.86 -0.69 -0.42
## weight -0.83 0.90 0.93 0.86 1.00 -0.42 -0.31
## acceleration 0.42 -0.50 -0.54 -0.69 -0.42 1.00 0.29
## year 0.58 -0.35 -0.37 -0.42 -0.31 0.29 1.00
corrplot(corr_a,method="number", type='lower')
boxplot(mpg ~ origin, data = Auto, main = "mpg & origin")
In this exercise, we will predict the number of applications received using the the other variables in the College data set.
set.seed(1)
train_a<-sample(dim(Auto1)[1], dim(Auto1)[1]*0.7)
test_a<- (-train_a)
auto_train<-Auto1[train_a,]
auto_test<-Auto1[test_a,]
selec_for <- regsubsets(mpg ~ ., data = auto_train, nvmax = ncol(Auto1)-1, method = "forward")
selec_sum <- summary(selec_for)
selec_sum
## Subset selection object
## Call: regsubsets.formula(mpg ~ ., data = auto_train, nvmax = ncol(Auto1) -
## 1, method = "forward")
## 8 Variables (and intercept)
## Forced in Forced out
## cylinders FALSE FALSE
## displacement FALSE FALSE
## horsepower FALSE FALSE
## weight FALSE FALSE
## acceleration FALSE FALSE
## year FALSE FALSE
## origin2 FALSE FALSE
## origin3 FALSE FALSE
## 1 subsets of each size up to 7
## Selection Algorithm: forward
## cylinders displacement horsepower weight acceleration year origin2
## 1 ( 1 ) " " " " " " "*" " " " " " "
## 2 ( 1 ) " " " " " " "*" " " "*" " "
## 3 ( 1 ) " " " " " " "*" " " "*" "*"
## 4 ( 1 ) " " " " " " "*" " " "*" "*"
## 5 ( 1 ) " " " " " " "*" "*" "*" "*"
## 6 ( 1 ) " " "*" " " "*" "*" "*" "*"
## 7 ( 1 ) " " "*" "*" "*" "*" "*" "*"
## origin3
## 1 ( 1 ) " "
## 2 ( 1 ) " "
## 3 ( 1 ) " "
## 4 ( 1 ) "*"
## 5 ( 1 ) "*"
## 6 ( 1 ) "*"
## 7 ( 1 ) "*"
cat("Cp:",which.min(selec_sum$cp),"\n BIC:",which.min(selec_sum$bic),"\n Adj R^2:",which.min(selec_sum$adjr2))
## Cp: 4
## BIC: 4
## Adj R^2: 1
selec_plot <- function(stat, y.label, adj2 = FALSE) {
plot(stat, xlab = "Number of Variables", ylab = y.label, xaxt = "n", type = "l")
axis(side = 1, at = 1:length(stat))
if (adj2==FALSE) {
#가장 작은 값에서 한 단위의 표준오차 내의 값들의 집합
stat_1se <- min(stat) + (sd(stat) / sqrt(length(stat)))
sub <- which(stat< stat_1se)
}
else{
#가장 큰 값에서 한 단위의 표준오차 내의 값들의 집합
stat_1se <- max(stat) - (sd(stat) / sqrt(length(stat)))
sub <- which(stat > stat_1se)
}
#한 단위의 표준오차 내의 값을 가지는 것들 중 변수의 개수가 가장 작은 것 표시
abline(h = stat_1se, col = "red", lty = 2)
abline(v = sub[1], col = "green", lty = 2)
}
par(mfrow=c(1, 3))
selec_plot(selec_sum$cp, "Cp")
selec_plot(selec_sum$bic, "BIC")
selec_plot(selec_sum$adjr2, "Adjusted R2", adj2 = TRUE) # higher values are better
coef(selec_for,4)#추정계수
## (Intercept) weight year origin2 origin3
## -22.236794432 -0.005931732 0.822639585 3.047130577 1.888357504
plot(selec_for,scale="Cp")
plot(selec_for,scale="bic")
plot(selec_for,scale="adjr2")
coef(selec_for,4)
## (Intercept) weight year origin2 origin3
## -22.236794432 -0.005931732 0.822639585 3.047130577 1.888357504
m_a<-lm(mpg ~weight+year+origin,data=Auto1,subset=train_a)
summary(m_a)
##
## Call:
## lm(formula = mpg ~ weight + year + origin, data = Auto1, subset = train_a)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.1053 -2.1676 -0.1565 1.9119 10.6645
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -22.236794 4.802836 -4.630 5.70e-06 ***
## weight -0.005932 0.000311 -19.070 < 2e-16 ***
## year 0.822640 0.057967 14.192 < 2e-16 ***
## origin2 3.047131 0.646654 4.712 3.94e-06 ***
## origin3 1.888357 0.616201 3.065 0.0024 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.364 on 269 degrees of freedom
## Multiple R-squared: 0.8238, Adjusted R-squared: 0.8212
## F-statistic: 314.4 on 4 and 269 DF, p-value: < 2.2e-16
m_a.pred<-predict(m_a,auto_test)
m_a.error<-mean((m_a.pred-auto_test$mpg)^2)
m_a.error
## [1] 11.5436