#### This function performs A Box-Cox Transformation on a raster dataset #### boxcox_raster <- function(bcras){ # require the necessary packages require(caret) require(raster) require(vcdExtra) # first get the frequency table (attribute table) of the raster att_tab <- raster::freq(x = bcras, digits = 0, useNA = 'no') # then expand this out to case form for box-cox bc_tab <- expand.dft(x = att_tab, var.names = NULL, freq = "count") # then convert the bc_tab dataframe to a vector of values for box-cox bc_data <- as.vector(bc_tab) # then run BoxCoxTrans on the raster values to identify a lambda value to transform the data bc_res <- BoxCoxTrans(y = bc_data, fudge = 0.1) # and export the lambda value bc_lambda <- bc_res$lambda # then transform the original raster using this lambda value result <- bcras^bc_lambda }