Skip to content
Prev 200664 / 398506 Next

SVM Param Tuning with using SNOW package

I cannot really be sure what you are trying to do,  but doing a bit of  
"surgery" on your code lets it run on a multicore Mac:

library(e1071)
library(snow)
library(pls)

data(gasoline)

X=gasoline$NIR
Y=gasoline$octane

NR=10
cost1=seq(0.5,30, length=NR)

sv.lin<- function(c) {

for (i in 1:NR) {

ind=sample(1:60,50)
gTest<-  data.frame(Y=I(Y[-ind]),X=I(X[-ind,]))
gTrain<- data.frame(Y=I(Y[ind]),X=I(X[ind,]))

svm.lin   	  <- svm(gTrain$X,gTrain$Y, kernel="linear",cost=c[i],  
cross=5)
results.lin   <- predict(svm.lin, gTest$X)

e.test.lin     <- sqrt(sum((results.lin-gTest$Y)^2)/length(gTest$Y))

return(e.test.lin)
}
}

cl<- makeCluster(2, type="SOCK" )

clusterEvalQ(cl, library(e1071))
cost1=seq(0.5,30, length=NR)

clusterExport(cl,c("NR","Y","X",  "cost1"))
# Pretty sure you need a copy of cost1 on each node.


RMSEP<-clusterApply(cl, cost1, sv.lin)
# I thought the second argument was the matrix or vector over which to  
iterate.

stopCluster(cl)

# Since I don't know what the model meant, I cannot determine whehter  
this result is interpretable>
 > RMSEP
[[1]]
[1] 0.1921887

[[2]]
[1] 0.1924917

[[3]]
[1] 0.1885066

[[4]]
[1] 0.1871466

[[5]]
[1] 0.3550932

[[6]]
[1] 0.1226460

[[7]]
[1] 0.2426345

[[8]]
[1] 0.2126299

[[9]]
[1] 0.2276286

[[10]]
[1] 0.2064534