The goal of parsnip is to provide a tidy, unified interface to models that can be used to try a range of models without getting bogged down in the syntactical minutiae of the underlying packages.
One challenge with different modeling functions available in R that do the same thing is that they can have different interfaces and arguments. For example, to fit a random forest regression model, we might have:
# From randomForest rf_1 <- randomForest( y ~ ., data = ., mtry = 10, ntree = 2000, importance = TRUE ) # From ranger rf_2 <- ranger( y ~ ., data = dat, mtry = 10, num.trees = 2000, importance = "impurity" ) # From sparklyr rf_3 <- ml_random_forest( dat, intercept = FALSE, response = "y", features = names(dat)[names(dat) != "y"], col.sample.rate = 10, num.trees = 2000 )
Note that the model syntax can be very different and that the argument names (and formats) are also different. This is a pain if you switch between implementations.
In this example:
The goals of parsnip are to:
ranger::rangeror other specific packages.
trees) so that users only need to remember a single name. This will help across model types too so that
treeswill be the same argument across random forest as well as boosting or bagging.
Using the example above, the
parsnip approach would be:
library(parsnip) rand_forest(mtry = 10, trees = 2000) %>% set_engine("ranger", importance = "impurity") %>% set_mode("regression") #> Random Forest Model Specification (regression) #> #> Main Arguments: #> mtry = 10 #> trees = 2000 #> #> Engine-Specific Arguments: #> importance = impurity #> #> Computational engine: ranger
The engine can be easily changed. To use Spark, the change is straightforward:
rand_forest(mtry = 10, trees = 2000) %>% set_engine("spark") %>% set_mode("regression") #> Random Forest Model Specification (regression) #> #> Main Arguments: #> mtry = 10 #> trees = 2000 #> #> Computational engine: spark
Either one of these model specifications can be fit in the same way:
rand_forest(mtry = 10, trees = 2000) %>% set_engine("ranger", importance = "impurity") %>% set_mode("regression") %>% fit(mpg ~ ., data = mtcars) #> parsnip model object #> #> Fit time: 71ms #> Ranger result #> #> Call: #> ranger::ranger(formula = mpg ~ ., data = data, mtry = ~10, num.trees = ~2000, importance = ~"impurity", num.threads = 1, verbose = FALSE, seed = sample.int(10^5, 1)) #> #> Type: Regression #> Number of trees: 2000 #> Sample size: 32 #> Number of independent variables: 10 #> Mtry: 10 #> Target node size: 5 #> Variable importance mode: impurity #> Splitrule: variance #> OOB prediction error (MSE): 5.699772 #> R squared (OOB): 0.8430857
A list of all
parsnip models across different CRAN packages can be found at
Data sets previously found in
parsnip are now find in the
This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
For questions and discussions about tidymodels packages, modeling, and machine learning, please post on RStudio Community.
If you think you have encountered a bug, please submit an issue.
Either way, learn how to create and share a reprex (a minimal, reproducible example), to clearly communicate about your code.