8Assignment

Irving Cedillo

5

Warning: package 'plotly' was built under R version 4.5.2
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
Warning: package 'caret' was built under R version 4.5.2
Loading required package: lattice
Warning: package 'ISLR2' was built under R version 4.5.3

Attaching package: 'ISLR2'
The following objects are masked from 'package:ISLR':

    Auto, Credit
── Attaching packages ────────────────────────────────────── tidymodels 1.4.1 ──
✔ broom        1.0.12     ✔ rsample      1.3.1 
✔ dials        1.4.2      ✔ tailor       0.1.0 
✔ dplyr        1.1.4      ✔ tidyr        1.3.1 
✔ infer        1.0.9      ✔ tune         2.0.1 
✔ modeldata    1.5.1      ✔ workflows    1.3.0 
✔ parsnip      1.3.3      ✔ workflowsets 1.1.1 
✔ purrr        1.1.0      ✔ yardstick    1.3.2 
✔ recipes      1.3.1      
Warning: package 'broom' was built under R version 4.5.3
── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
✖ rsample::calibration()   masks caret::calibration()
✖ purrr::discard()         masks scales::discard()
✖ dplyr::filter()          masks plotly::filter(), stats::filter()
✖ dplyr::lag()             masks stats::lag()
✖ purrr::lift()            masks caret::lift()
✖ yardstick::precision()   masks caret::precision()
✖ yardstick::recall()      masks caret::recall()
✖ yardstick::sensitivity() masks caret::sensitivity()
✖ yardstick::specificity() masks caret::specificity()
✖ recipes::step()          masks stats::step()
#a
x1<- runif(500)-.5
x2<- runif(500)-.5

y<- 1*(x1^2 -x2^2 >0)

fake_data<-data.frame(x1,x2,y)|>
  mutate( y = factor(y, levels = c(0, 1), labels = c("Class_0", "Class_1"))  )

#b
ggplot(data = fake_data,
       mapping = aes(x=x1, y=x2, color = factor(y))) +
  geom_point()

#c,d
data_split = initial_split(fake_data, prop = 0.80, strata = y)

training = training(data_split)
testing = testing(data_split)

ctrl<- trainControl(
  method  = 'cv',
  number = 5,
  classProbs = TRUE,
  summaryFunction = twoClassSummary
)

set.seed(12345)

log_reg<-train(
  y~x1+x2,
  data=training, 
  method='glm',
  family='binomial',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl
)


training$predicted_class <- predict(log_reg, training)

ggplot(data = training,
       mapping = aes(x=x1, y=x2, color = factor(predicted_class))) +
  geom_point()

log_reg_2<-train(
  y~tan(x2)+sin(x1*x1)+cos(x2*x2),
  data=training, 
  method='glm',
  family='binomial',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl
)


training$predicted_class_2 <- predict(log_reg_2, training)

ggplot(data = training,
       mapping = aes(x=x1, y=x2, color = factor(predicted_class_2))) +
  geom_point()

#g
svm<-train(
  y~x1+x2,
  data=training, 
  method='svmLinear',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl
)


training$predicted_class_svm <- predict(svm, training)

ggplot(data = training,
       mapping = aes(x=x1, y=x2, color = factor(predicted_class_svm))) +
  geom_point()

#h
svm<-train(
  y~x1+x2,
  data=training, 
  method='svmRadial',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl
)


training$predicted_class_nlsvm <- predict(svm, training)

ggplot(data = training,
       mapping = aes(x=x1, y=x2, color = factor(predicted_class_nlsvm))) +
  geom_point()

The SVM radial was able to identify the linear separation between the generated classes the best from the output plot. Interestingly enoguht the linear SVM was not able to correctly classify or differentiate between the two classes.

7

attach(Auto)
The following object is masked from package:ggplot2:

    mpg
data_auto <- Auto |>
  mutate(
    Class_MPG = as.factor(ifelse(mpg > median(mpg), "Above_Med", "Below_Med"))
  )|>
  select(-mpg)

svm_grid_linear <- expand.grid(
   C =c(0.1,1,10,100)
)


svm_grid <- expand.grid(
  sigma= c(0.01,0.05,0.1),
  C =c(0.1,1,10,100)
)

ctrl<- trainControl(
  method  = 'cv',
  number = 5,
  classProbs = TRUE,
  summaryFunction = twoClassSummary
)


svm_model_linear <- suppressWarnings(train(
  Class_MPG ~ .,
  data = data_auto,
  method='svmLinear',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl,
  tuneGrid = svm_grid_linear
))

print(svm_model_linear)
Support Vector Machines with Linear Kernel 

392 samples
  8 predictor
  2 classes: 'Above_Med', 'Below_Med' 

Pre-processing: Box-Cox transformation (7), centered (310), scaled (310) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 313, 314, 314, 313, 314 
Resampling results across tuning parameters:

  C      ROC        Sens       Spec     
    0.1  0.9499671  0.9079487  0.8670513
    1.0  0.9585076  0.9335897  0.8669231
   10.0  0.9606213  0.8979487  0.8771795
  100.0  0.9682512  0.8980769  0.9078205

ROC was used to select the optimal model using the largest value.
The final value used for the model was C = 100.
svm_model_Radial<- suppressWarnings(train(
  Class_MPG ~ .,
  data = data_auto,
  method='svmRadial',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl,
  tuneGrid = svm_grid
))

print(svm_model_Radial)
Support Vector Machines with Radial Basis Function Kernel 

392 samples
  8 predictor
  2 classes: 'Above_Med', 'Below_Med' 

Pre-processing: Box-Cox transformation (7), centered (310), scaled (310) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 313, 314, 314, 314, 313 
Resampling results across tuning parameters:

  sigma  C      ROC        Sens       Spec     
  0.01     0.1  0.9172650  0.7819231  0.8632051
  0.01     1.0  0.9368080  0.9130769  0.8670513
  0.01    10.0  0.9383925  0.9182051  0.8619231
  0.01   100.0  0.9343031  0.9435897  0.8210256
  0.05     0.1  0.9190007  0.5229487  0.7587179
  0.05     1.0  0.9158514  0.4443590  0.9384615
  0.05    10.0  0.9118376  0.4443590  0.9333333
  0.05   100.0  0.9077449  0.4750000  0.9180769
  0.10     0.1  0.9127120  0.4562821  0.7332051
  0.10     1.0  0.9128008  0.4597436  0.8153846
  0.10    10.0  0.9046220  0.3310256  0.9538462
  0.10   100.0  0.9024195  0.4542308  0.8102564

ROC was used to select the optimal model using the largest value.
The final values used for the model were sigma = 0.01 and C = 10.
plot(svm_model_Radial)

8

attach(OJ)
set.seed(12345)
train_index <- createDataPartition(
  OJ$Purchase, 
  p = 800 / nrow(OJ), 
  list = FALSE
) 

training = OJ[train_index, ]
testing = OJ[-train_index, ]

svm_grid <- expand.grid(
  sigma= c(0.01,0.05,0.1),
  C =c(0.01,.1,1,2,4,6,8,10)
)

svm_model<-suppressWarnings(train(
  Purchase ~.,
  data = training, 
  method='svmRadial',
  preProcess = c("BoxCox", "center", "scale"),
  metric='ROC',
  trControl = ctrl,
  tuneGrid = svm_grid
)
)

print(svm_model)
Support Vector Machines with Radial Basis Function Kernel 

801 samples
 17 predictor
  2 classes: 'CH', 'MM' 

Pre-processing: Box-Cox transformation (7), centered (17), scaled (17) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 641, 640, 642, 640, 641 
Resampling results across tuning parameters:

  sigma  C      ROC        Sens       Spec     
  0.01    0.01  0.8717847  0.7566590  0.8366615
  0.01    0.10  0.8725668  0.7608037  0.8398361
  0.01    1.00  0.8916834  0.8854829  0.7501792
  0.01    2.00  0.8924311  0.8916053  0.7404506
  0.01    4.00  0.8919574  0.8875237  0.7403994
  0.01    6.00  0.8915870  0.8915843  0.7371224
  0.01    8.00  0.8912611  0.8895434  0.7306196
  0.01   10.00  0.8918123  0.8875026  0.7242704
  0.05    0.01  0.8714394  0.7751315  0.8269329
  0.05    0.10  0.8787731  0.8690722  0.7341014
  0.05    1.00  0.8860286  0.8957080  0.7179724
  0.05    2.00  0.8829385  0.8977488  0.6921659
  0.05    4.00  0.8809011  0.8895434  0.6952893
  0.05    6.00  0.8801076  0.8875026  0.6888377
  0.05    8.00  0.8792789  0.8874816  0.6887865
  0.05   10.00  0.8780326  0.8854408  0.6952381
  0.10    0.01  0.8698243  0.7935199  0.7949309
  0.10    0.10  0.8711035  0.8527667  0.7276498
  0.10    1.00  0.8790906  0.9018304  0.6825397
  0.10    2.00  0.8775910  0.8936251  0.6857143
  0.10    4.00  0.8721282  0.8874816  0.6823861
  0.10    6.00  0.8668637  0.8834000  0.6663594
  0.10    8.00  0.8605169  0.8772775  0.6599590
  0.10   10.00  0.8544256  0.8792973  0.6534562

ROC was used to select the optimal model using the largest value.
The final values used for the model were sigma = 0.01 and C = 2.
train_predictions <-predict(svm_model, newdata=training)
train_error<- mean(train_predictions != training$Purchase)
cat("\nTraining Error:", round(train_error, 1), "\n")

Training Error: 0.2 
test_predictions <-predict(svm_model, newdata=testing)
test_error<- mean(test_predictions != testing$Purchase)
cat("\nTest Error:", round(train_error, 1), "\n")

Test Error: 0.2