2

I want to use principle component regression to find essential components and then extract those components to apply further SVR analysis, but I got some problems when doing this.

First try, I follow the suggestions from: How to extract components after performing principal component regression for further analysis in R caret package And here is my code:

library(caret)
library(tidyverse)
library(pls)
library(e1071)
library(kernlab)

# Load the data
data("Boston", package = "MASS")
# Split the data into training and test set
set.seed(123)
training.samples <- Boston$medv %>% createDataPartition(p = 0.8, list = FALSE)
train.data  <- Boston[training.samples, ]
test.data <- Boston[-training.samples, ]
set.seed(1)
sigDist <- sigest(medv~., data = train.data, frac = 1)

svrGrid <- expand.grid(.sigma = sigDist, .C = 2^(-2:7))
set.seed(2)
svrPCR <- train(medv~., data = train.data,
                method = "svmLinear",
                tuneGrid = svrGrid,                  
                preProcess = c("center","scale","pcr"), # if center and scale needed
                trControl=trainControl("LOOCV"))

Here is my error:

Error: The tuning parameter grid should have columns C

Second try, I simply extract [[scores]]from pcr results, then use them in svr:

pcr_model <- pcr(medv~., data = train.data, scale =TRUE, validation = "LOO")
engivaluepcr=as.data.frame(pcr_model[['scores']][,1:13])
engivaluepcr=cbind(train.data$medv,engivaluepcr)
setnames(engivaluepcr,"train.data$medv","medv")
pcrsvr <- svm(medv ~ ., engivaluepcr, validation = "LOO",kernel='linear')
pred_test2 <-predict(pcrsvr,test.data)

And here is my new error:

Error in eval(predvars, data, env) : object 'Comp 1' not found

Maybe those two ways above are both not good methods. Please give me some suggestions about how to use the result from pcr in further svr analysis, or maybe just help me to solve those errors above.

Thanks, -C.T.

Edit 1: I just figure out, when using method = "svmLinear" we don't need .sigma in svrGrid. After eliminating sigma, I can get my result successfully. Hope this can be helpful to others. -C.T.

Chestnut T
  • 39
  • 6

0 Answers0