train()插入符函数出错

时间:2017-12-01 01:06:27

标签: r neural-network r-caret

我正在尝试使用插入符号为径向基函数网络进行基本模型选择,但是当我使用插入符号中的train()函数时,会出现以下错误:

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

我不知道我在这里做错了什么,我希望你可以帮助我。 这是代码:

    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")

1 个答案:

答案 0 :(得分:1)

如果您要使用matrix作为train的输入,则必须将其命名。

?caret::train

  

对于默认方法,x是一个对象,其中样本在行中,而要素在列中。这可以是简单的矩阵,数据帧或其他类型(例如稀疏矩阵),但必须具有列名

您正在RSNNS

之后加载包caret
  

库(RSNNS)
  加载所需的包:Rcpp

     

附加包裹:'RSNNS'

     

以下对象从'package:caret'屏蔽:

     

confusionMatrix,火车