I am having trouble with the caret package rpart model. When I run the code in Part 1 I get the following error:
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'))
model fit failed for Fold1: cp=0.02 Error in (function (formula, data, weights, subset, na.action = na.rpart, : Argument params not matched
However, when I run the code in Part 2, I do not get an error.
for (test_cp in seq(.001,.02,.001)){
test<-rpart(samp.f, dat=credit, cp=test_cp, maxdepth=5)
}
Any suggestions?
I am running the following setup:
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
The rpart
function does not have a params options, but a parms option. So in train you need to use parms instead of params.
rpart(formula, data, weights, subset, na.action = na.rpart, method, model = FALSE, x = FALSE, y = TRUE, parms, control, cost, ...)
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'))