slds-lmu / ame

average marginal effects for machine learning
Other
1 stars 1 forks source link

AME with factor variables #12

Closed scholbeck closed 5 years ago

scholbeck commented 5 years ago

I'm getting an error trying to compute the AME for a factor variable season with 4 levels (1, 2, 3, 4). I used the day dataset from the bikesharing data at https://archive.ics.uci.edu/ml/datasets/bike+sharing+dataset

Code: df = day df$season = as.factor(df$season) tsk = makeRegrTask(data = df, target = "cnt") mod = train("regr.svm", tsk) computeAME(mod, df, "season")

Error: Error in contrasts <-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

abethman commented 5 years ago

I am having a similar problem (different error message though) computing AMEs for a factor variable, but in my case I am using random forest instead of SVM,

Minimal Example:

tsk <- makeRegrTask(data = iris, target = "Sepal.Length")
mod <- train(makeLearner("regr.randomForest"), tsk)
computeAME(mod, data = iris, features = c("Sepal.Width"), at = list(Sepal.Width = c(2,3,4)))
computeAME(mod, data = iris, features = c("Species"))

Error:

Error in predict.randomForest(.model$learner.model, newdata = .newdata,  : 
  Type of predictors in new data do not match that of the training data.
giuseppec commented 5 years ago

@scholbeck can you try making a PR that fixes this? Would be great!

scholbeck commented 5 years ago

@scholbeck can you try making a PR that fixes this? Would be great!

Yes, I'm looking into it. Concerning the issue @abethman mentioned, I'm suspecting a problem with setting the levels of the newly created factor variable (Species in this case).

scholbeck commented 5 years ago

I fixed the issue and opened a PR.

giuseppec commented 5 years ago

Merged. Will close this issue here, please check and reopen if something unexpected happens.