R/simulated_annealing.R
simulated_annealing.Rd
Algorithm randomly chooses a set of features, trains on them, scores the model. Then the algorithm slightly modifies the chosen features randomly and tests to see if the model improves. If there is improvement, the newer model is kept, if not the algorithm tests to see if the worse model is still kept based on a acceptance probability that decreases as iterations continue and if the model performs worse.
simulated_annealing(scorer, X, y, c = 1, iterations = 100, bools = FALSE)
scorer | A custom user-supplied function that accepts X and y (as defined below) as input and returns the error of the datasets. |
---|---|
X | data frame of training features |
y | data frame of training targets |
c | control rate of feature perturbation |
iterations | number of iterations |
bools | If true function returns array of boolean values instead of column indicies |
array of selected features
custom_scorer_fn <- function(data) { model <- lm(Y ~ ., data) return(mean(model$residuals^2)) } df <- tgp::friedman.1.data() data <- dplyr::select(df, -Ytrue) X <- data[1:(length(data)-1)] y <- data[length(data)] features <- featureselection::simulated_annealing(custom_scorer_fn, X, y)