Week9-HWAnswers

CoreyJackson

6/10/2018

Step 1: Load the data

# find the NAs in column "Ozone" and replace them by the mean value of this column
air$Ozone[is.na(air$Ozone)] <- mean(air$Ozone, na.rm=TRUE)
# find the NAs in column "Solar.R" and replace those NAs by the mean value of this column
air$Solar.R[is.na(air$Solar.R)] <- mean(air$Solar.R, na.rm=TRUE)

Step 2: Create train and test data sets

# create a list of random index for air data and store the index in a variable called "ranIndex"
randIndex <- sample(1:dim(air)[1])
# In order to split data, create a 2/3 cutpoint and round the number
cutpoint2_3 <- floor(2*dim(air)[1]/3)
# check the 2/3 cutpoint
cutpoint2_3

# 4) Compute models and plot the results for 'svm'(in the e1071) and 'lm'
#install.packages("e1071")
library(e1071)

## Warning: package 'e1071' was built under R version 3.5.2

# svm function in "e1071
svm_e <- svm(Ozone~., # set "Ozone" as target variable,and use all other variables to predict
data=trainData_Corey # specify the data to use in the analysis
)
# check the model
svm_e