rpart插入符号"参数参数不匹配"错误

时间:2018-05-22 16:54:36

标签: r r-caret rpart

我在使用插入包rpart模型时遇到问题。当我在第1部分中运行代码时,出现以下错误:

第1部分

library(rpart)
library(caret)

creditNames <- c("Checking", "Duration", "CreditHistory", "Purpose",
                 "CreditAmount", "Savings", "Employment", 
                 "InstallmentRate", "GenderMarital", "OtherDebtors",
                 "YearsAtResidence", "RealEstate", "Age", 
                 "OtherInstallment", "Housing", "ExistingCredit",
                 "Job", "NumLiable", "Phone", "Foreign", "Credit")

url="http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data"
credit_data <- read.table(url, sep=" ", header = FALSE, 
                  col.names = creditNames, 
                  stringsAsFactors = FALSE)

creditHistory <- c(A30="All Paid", A31="All Paid This Bank",
                   A32="Up To Date", A33="Late Payment",
                   A34="Critical Account")
credit_data$CreditHistory <- as.factor(creditHistory[credit_data$CreditHistory])

credit_data$Credit <- ifelse(credit_data$Credit == 1, "Good", "Bad")
credit_data$Credit <- factor(credit_data$Credit, levels = c("Good", "Bad"))

fitControl <- trainControl(method = 'cv', number = 6)

Grid <- expand.grid(
                    cp=.02)

vars <- names(credit_data)[c(5,13,3,7)]
samp.f <- as.formula(paste(names(credit_data)[21], paste(vars, collapse = " + "),sep="~"))

myvars <- c("Credit", "CreditAmount", "Age") #"CreditHistory", ,  "Employment"


samp.m <- train(samp.f,
                data=credit_data,
                method='rpart',
                trControl = fitControl,
                tuneLength=20,
                metric = "Accuracy",
                tuneGrid = Grid,
                na.action = na.omit,
                params=list(split='information'))
  

Fold1的模型拟合失败:cp = 0.02(函数(公式,数据,   权重,子集,na.action = na.rpart,:参数params not not   匹配

但是,当我在第2部分中运行代码时,我没有收到错误。

第2部分

for (test_cp in seq(.001,.02,.001)){
   test<-rpart(samp.f, dat=credit, cp=test_cp, maxdepth=5)
}

有什么建议吗?

我正在运行以下设置:

R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
other attached packages:
[1] caret_6.0-79    ggplot2_2.2.1   lattice_0.20-35 rpart_4.1-13 

1 个答案:

答案 0 :(得分:2)

rpart函数没有params选项,而是parms选项。所以在火车上你需要使用parms而不是params。

  

rpart(公式,数据,权重,子集,na.action = na.rpart,方法,         model = FALSE,x = FALSE,y = TRUE, parms ,控制,成本,......)

samp.m <- train(samp.f,
                data=credit_data,
                method='rpart',
                trControl = fitControl,
                tuneLength=20,
                metric = "Accuracy",
                tuneGrid = Grid,
                na.action = na.omit,
  #-->#         parms=list(split='information'))