Auto

변수 설명

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" ...
  1. 392개 자동차의 정보
  2. 392개 관측치의 9개의 변수들로 이루어짐.
  3. 변수: 2개의 범주형 변수와 7개의 수치형 변수가 있음.
    mpg: 연비
    cylinders: 엔진 실린더 개수
    displacement: 배기량
    horsepower: 마력(hp)
    weight: 자동차 무게
    acceleration: 초당 가속력
    year: 출시년도
    origin: 제조국(1: 미국, 2: 유럽, 3: 일본)
    name: 자동차 이름
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))

  1. 분포
  • 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(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')

  1. 수치형변수상관계수(중복없이 작성)
  1. 강한 상관관계(절댓값이 0.5이상인 경우)
  • mpg:
  • cylinders:
  • displacement:
  • horsepower:
  1. 약한 상관관계(절댓값이 0.5미만인 경우)
  • mod
  • dfe
boxplot(mpg ~ origin, data = Auto, main = "mpg & origin")

  1. 연속형변수 mpg와 범주형 변수 origin의 관계
    t-test의 p-value의 값은 2.2e-16으로 아주 작아서 두 그룹 간의 차이가 없다는 귀무가설을 거절할 수 있다. 즉, 두 그룹간의 차이가 있다.

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_a<-sample(dim(Auto1)[1], dim(Auto1)[1]*0.7)
test_a<- (-train_a)

auto_train<-Auto1[train_a,]
auto_test<-Auto1[test_a,]

(b) Stepwise selection

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")

(c) Linear

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

(d) Polynomial Regression

(e) Step Function

(f) Natural Spline

(g) Smoothing Spline

(h)Local Regression

(i) Generalized Additive Models(GAM)