Utility function to create a formula object. Note that it may be very useful when you use pipes.

to_formula(attrs, class)

Arguments

attrs

Character vector with names of independent variables.

class

Single string with a dependent variable's name.

Examples

# evaluator from FSelector package evaluator <- function(subset, data, dependent = names(iris)[5]) { library(rpart) k <- 5 splits <- runif(nrow(data)) results <- sapply(1:k, function(i) { test.idx <- (splits >= (i - 1) / k) & (splits < i / k) train.idx <- !test.idx test <- data[test.idx, , drop = FALSE] train <- data[train.idx, , drop = FALSE] tree <- rpart(to_formula(subset, dependent), train) error.rate <- sum(test[[dependent]] != predict(tree, test, type = "c")) / nrow(test) return(1 - error.rate) }) return(mean(results)) } set.seed(123) fit <- feature_search(attributes = names(iris)[-5], fun = evaluator, data = iris, mode = "exhaustive", parallel = FALSE) fit$best
#> Sepal.Length Sepal.Width Petal.Length Petal.Width values #> 7 1 0 0 1 0.9575926
names(fit$best)[fit$best == 1]
#> [1] "Sepal.Length" "Petal.Width"
# with to_formula to_formula(names(fit$best)[fit$best == 1], "Species")
#> Species ~ Sepal.Length + Petal.Width #> <environment: 0x55f4c005dc48>