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 #> 9 0 1 0 1 0.959994
names(fit$best)[fit$best == 1]
#> [1] "Sepal.Width" "Petal.Width"
# with to_formula to_formula(names(fit$best)[fit$best == 1], "Species")
#> Species ~ Sepal.Width + Petal.Width #> <environment: 0x5611e9c7a8a8>