I need some help with managing several SVM models in R. I have around 100 data files and I would like to read each file and subsequently train a model for that file using e1071 package. The names of all files are contained in one file so its easier to track each file. I have used the following code but couldn’t reach the solution.
x<-read.table("data.dat", header=F)
x=as.vector(t(x))
vectory <- vector(mode="list", length=length(x))
vectorz <- vector(mode="list", length=length(x))
for (i in 1:length(x))
{
x[i] <- substr(x[i], 3, 100)
#assign(gsub("-", "_", x[i]), read.table(x[i], header=T, #sep=","))
val <- gsub("-", "_", x[i])
vectory[[val]] <- read.table(x[i], header=T, sep=",")
data(vectory[[val]])
valmodel <- x[i]
paste(valmodel, "_model", sep="")
vectorz[[valmodel]] <- ksvm(label ~ ., data=vectory[[val]])
}
I am confused with what exactly do I need to do for a data function call and data parameter while calling ksvm function.
Regards
As Scott mentioned, your example isn’t reproducible (and the quetsion doesn’t make it clear what your problem is). I rewrote your code so that it is less confusing. It’s untested, so you may need to tweak it.
General principles:
Name your variables meaningfully. Call file names
file_names, notx.If you are looping over lists (or vectors), use
lapply(orllplyfromplyr), rather than using aforloop since this involves extra boilerplate code and creating dummy variables. Notice how you don’t needvectoryorvectorzanymore.Separate the code for reading in data from the code for running the model. They are different tasks, and muddling the code together will muddle your thinking. (Also, when one model fails, you don’t want to have to read your data in again.)