0

I'm trying to use caret to do a basic model selection for a radial basis function network, but when i use the train() function from caret, the following error appears :

Error in UseMethod("train") : 
no applicable method for 'train' applied to an object of class "c('matrix', 
'double', 'numeric')"

I have no idea of what I'm doing wrong here, i hope you can help me with thtat one. Here's the code :

    Data1<-as.matrix(runif(1000))
    Data2<-as.matrix(runif(1000))
    Data3<-as.matrix(runif(1000))
    Data4<-as.matrix(runif(1000))
    Data5<-as.matrix(runif(1000))
    Data6<-as.matrix(runif(1000))

    data<-cbind(Data1,Data2,Data3,Data4,Data5,Data6)
    colnames(data)<-c("Feature1","Feature2","Feature3","Feture4","Feature5","Feature6")

    targetfunction<-function(xi){
      error<-rnorm(1,0,0.1)
      return (sin(2*xi[1])*xi[2]+0.5*(xi[3]-0.5)^2+xi[4]+error)
    }
    target<-as.matrix(rep(0,times=1000))
    for (i in 1:1000){
      target[i]<-as.matrix(targetfunction(data[i,]))
    }

    library(mRMRe)

    #Binding data and target
    DM = cbind(data, target)
    DM = mRMR.data(as.data.frame(DM))


    s1 = mRMR.classic(data = DM, feature_count = 1, target_indices = c(7))
    s2 = mRMR.classic(data = DM, feature_count = 2, target_indices = c(7))
    s3 = mRMR.classic(data = DM, feature_count = 3, target_indices = c(7))
    s4 = mRMR.classic(data = DM, feature_count = 4, target_indices = c(7))
    s5 = mRMR.classic(data = DM, feature_count = 5, target_indices = c(7))
    s6 = mRMR.classic(data = DM, feature_count = 6, target_indices = c(7))
    #Optimal solutions for feature selection (Mutual information)
    solutions(s1)
    solutions(s2)
    solutions(s3)
    o = solutions(s4)
    solutions(s5)
    solutions(s6)

    #for reproducibility 
    o = c(4,2,1,5)
    #########################################################################################
    #Model selection 
    #########################################################################################
    library(caret)
    library(RSNNS)

    #Splitting data
    prepValues = data[,o]

    trainSet = prepValues[1:750,]
    testset = prepValues[751:1000,]
    colnames(trainSet) = c("x1","x2","x3","x4")
    colnames(target) = "targ"
    test = cbind(target[1:750], trainSet)

    #Training model 
    rbf = train(trainSet, target[1:750], method = "rbf")
Lucien Ledune
  • 120
  • 11

1 Answers1

1

If you are going to use a matrix as input to train, it must be named.

?caret::train

For the default method, x is an object where samples are in rows and features are in columns. This could be a simple matrix, data frame or other type (e.g. sparse matrix) but must have column names

You are loading package RSNNS after caret

library(RSNNS)
Loading required package: Rcpp

Attaching package: ‘RSNNS’

The following objects are masked from ‘package:caret’:

confusionMatrix, train

Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • Yeah i saw that in the train() help and tried adding names to trainSet and target with colnames() but that didn't solve my problem sadly – Lucien Ledune Dec 01 '17 at 01:38
  • @LucienLedune, the error reports your input is not appropriate for the method. Happy to help you troubleshoot if you make the code in your question reproducible, see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for advice on how to do that. – Kevin Arseneau Dec 01 '17 at 01:45
  • I think it should be reproducible now. (The problem is coming from the last line, under the model selection part) – Lucien Ledune Dec 01 '17 at 02:02
  • I have found an hint ! When I use caret::train() instead of train(), it doesn't give me the error...but now instead Rstudio crashes ... – Lucien Ledune Dec 01 '17 at 02:20
  • I get the same result, I updated my answer to note the package conflict. – Kevin Arseneau Dec 01 '17 at 02:22