Reputation: 3213
cancer <- read.csv('breast-cancer-wisconsin.data', header = FALSE, na.strings="?")
cancer <- cancer[complete.cases(cancer),]
names(cancer)[11] <- "class"
cancer[, 11] <- factor(cancer[, 11], labels = c("benign", "malignant"))
library(gbm)
Firstly, I remove 'NA' values using complete.cases and make the eleventh column, the "class", as factor. I want to use "class" as the response variable and other columns, except the first one, as predictor variables.
On my first attempt, I typed in:
boost.cancer <- gbm(class ~ .-V1, data = cancer, distribution = "bernoulli")
Error in gbm.fit(x, y, offset = offset, distribution = distribution, w = w, :
Bernoulli requires the response to be in {0,1}
Then, I use the contrasts of the class instead of class.
boost.cancer <- gbm(contrasts(class) ~ .-V1, distribution = "bernoulli", data = cancer)
Error in model.frame.default(formula = contrasts(class) ~ . - V1, data = cancer, :
variable lengths differ (found for 'V1')
How do I correct these errors? I'm sure there is something wrong with my method.
Upvotes: 3
Views: 7317
Reputation: 21
You can also use:
boost.cancer <- gbm((unclass(class)-1) ~ .-V1, data = cancer, distribution = "bernoulli") summary(boost.cancer)
Do the similar thing while "predict" function and determining Confusion Matrix with Accuracy.
Upvotes: 0
Reputation: 8488
As the error says, your response is not in [0,1]. You can do this instead of creating the factor:
> cancer$class <- (cancer$class -2)/2
> boost.cancer <- gbm(class ~ .-V1, data = cancer, distribution = "bernoulli")
> boost.cancer
gbm(formula = class ~ . - V1, distribution = "bernoulli", data = cancer)
A gradient boosted model with bernoulli loss function.
100 iterations were performed.
There were 9 predictors of which 4 had non-zero influence.
Upvotes: 3