如何使R中naiveBayes()的公式参数泛型?

如何使R中naiveBayes()的公式参数泛型?,r,classification,naivebayes,R,Classification,Naivebayes,使用库“e1071”的函数naiveBayes()时,需要插入以下类型的公式: myFormula <- myClass~ feature1 + feature2 + feature3 myFormula您可以使用动态引用所有其他列 myFormula <- cyl ~ . naiveBayes(myFormula, data = mtcars) Call: naiveBayes.default(x = X, y = Y, laplace = laplace) A-

使用库
“e1071”
的函数
naiveBayes()
时,需要插入以下类型的公式:

myFormula <- myClass~ feature1 + feature2 + feature3

myFormula您可以使用
动态引用所有其他列

myFormula <- cyl ~ .
naiveBayes(myFormula, data = mtcars)



    Call:
naiveBayes.default(x = X, y = Y, laplace = laplace)

A-priori probabilities:
Y
      4       6       8 
0.34375 0.21875 0.43750 

Conditional probabilities:
   mpg
Y       [,1]     [,2]
  4 26.66364 4.509828
  6 19.74286 1.453567
  8 15.10000 2.560048

   disp
Y       [,1]     [,2]
  4 105.1364 26.87159
  6 183.3143 41.56246
  8 353.1000 67.77132

运行此命令:
naiveBayes(myFormula,data=df)
我在model.frame.default中获得
错误(formula=myFormula,data=df,na.action=function(object,:变量长度不同(为'Class')
。可能使用点还包括最后一列(带有“Class属性”?)我也不明白,所以我知道这可能看起来很混乱,但是
Class
是第一个属性,而
survive
这个“Class属性”(我也在使用泰坦尼克号数据集)在mtcars数据集和第二列中试过。你能用
dput
来提供你的数据吗?我明白,我想我得到了你想要的:)关于我的问题的要点是获得
~。
。目前,我只能在第一堂课中使用几个if-else语句,因此我将您的答案标记为正确答案:再次感谢!
dynamicNB <- function(data, class) {
  myFormula <- substitute(class ~ .)
  naiveBayes(eval(myFormula), data = data)
}

dynamicNB(class = mpg, data = mtcars)