1. Neste problema, irá desenvolver um modelo para prever se um determinado carro tem um consumo de combustível “elevado” ou “baixo” com base no conjunto de dados Auto do package ISLR.
    1. Crie uma variável binária, mpg01, que contenha um 1 se mpg contiver um valor acima da sua mediana, e um 0 se mpg contiver um valor abaixo da sua mediana. Pode calcular a mediana usando a funçao median(). Note que pode ser útil usar a funçao data.frame() para criar um único conjunto de dados contendo tanto mpg01 como as outras variáveis de Auto
library(ISLR)
names(Auto)
## [1] "mpg"          "cylinders"    "displacement" "horsepower"   "weight"      
## [6] "acceleration" "year"         "origin"       "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   Min.   :1.000   amc matador       :  5  
##  1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000   ford pinto        :  5  
##  Median :15.50   Median :76.00   Median :1.000   toyota corolla    :  5  
##  Mean   :15.54   Mean   :75.98   Mean   :1.577   amc gremlin       :  4  
##  3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000   amc hornet        :  4  
##  Max.   :24.80   Max.   :82.00   Max.   :3.000   chevrolet chevette:  4  
##                                                  (Other)           :365
size.mpg<-length(Auto$mpg)
n<-1
mpg01<-c()
m<-median(Auto$mpg)
print(Auto$mpg[1:20])
##  [1] 18 15 18 16 17 15 14 14 14 15 15 14 15 14 24 22 18 21 27 26
while(n<=size.mpg){
  if(Auto$mpg[n]>=m){
   mpg01<-c(mpg01,1) 
  }else{
    mpg01<-c(mpg01,0)
  }
  n<-n+1
}
print(mpg01[1:20])
##  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1
full.data<-data.frame(Auto,mpg01)
str(full.data)
## 'data.frame':    392 obs. of  10 variables:
##  $ mpg         : num  18 15 18 16 17 15 14 14 14 15 ...
##  $ cylinders   : num  8 8 8 8 8 8 8 8 8 8 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : num  130 165 150 150 140 198 220 215 225 190 ...
##  $ weight      : num  3504 3693 3436 3433 3449 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : num  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : num  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 ...
##  $ mpg01       : num  0 0 0 0 0 0 0 0 0 0 ...
  1. Explore os dados gra camente para investigar a associacao entre mpg01 e as outras caractersticas. Quais das outras caractersticas parecem mais propensas a ser uteis na previsao de mpg01? Os gra cos de dispersao e os gra cos de caixa-de-bigodes podem ser ferramentas uteis para responder a esta pergunta. Descreva as suas descobertas. Thanks to the graphs we can see that there is a relation obviously between the mpg and mpg01, but it’s not significant to predict mpg01.Apart from this, we can see that between horsepower and mpg01, there is a relation since when the horsepower is lower the mpg tends to be above of the median(mpg01=1),but if it rises the contrary happens. With the weight there is a similar behavior. These are the best characteristics to predict mg01.

  2. Divida os dados num conjunto de treino e num conjunto de teste.