sjevelazco / flexsdm

Useful tools for constructing species distribution models
https://sjevelazco.github.io/flexsdm/
52 stars 5 forks source link

variable importance #380

Closed dorjismo closed 1 month ago

dorjismo commented 5 months ago

How do I get variable importance? I have used flexsdm to model my species. I now want to find out variable importance. I do not know in this package. You help will be greatly appreciated as it is my thesis. Thanks

geamaro commented 5 months ago

Hello,

Suppose you had a Maxent (Maxnet) model fited with flexsdm, named "maxent.fit.model". You can use {fitMaxnet} package do estimate variable importance, like this:

flexsdm variable importance (fitMaxnet)

maxent.varimp <- fitMaxnet::varImportance( theModel = with(maxent.fit.model, model), occSWD = as.data.frame(presence.points.model), bkgSWD = as.data.frame(background.points.model), responseType = "cloglog", numReplicates = 50 )

maxent.varimp <- data.frame(maxent.varimp) maxent.varimp$Var <- row.names(maxent.varimp) maxent.varimp <- arrange(maxent.varimp, Var)

file.name <- paste0("Plot - FINAL MODEL Var Permutation Importance.tiff") file.name <- iconv(file.name, to = "ASCII//TRANSLIT") file.name <- file.path(some.folder, file.name)

tiff(filename = file.name, units = "cm", width = 20, height = 12, res = 400)

maxent.varimp %>% arrange(maxent.varimp) %>% mutate(Var = factor(Var, levels = Var)) %>% ggplot(aes(x = Var, y = maxent.varimp)) + geom_col(fill = "#076FA2", width = 0.6) + geom_text(aes(label = paste0(maxent.varimp, "%")), hjust = -0.1, colour = "black", fontface = "bold") + coord_flip() + xlab("") + ylab("Permutation importance (%)") + scale_y_continuous(limits = c(0,85), n.breaks = 10)

dev.off()

I hope it helps...

Best wishes

G.

On Mon, Jun 3, 2024 at 9:49 AM dorjismo @.***> wrote:

How do I get variable importance? I have used flexsdm to model my species. I now want to find out variable importance. I do not know in this package. You help will be greatly appreciated as it is my thesis. Thanks

— Reply to this email directly, view it on GitHub https://github.com/sjevelazco/flexsdm/issues/380, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKVCFCJCW47LTZJZFLWLF7TZFRX6RAVCNFSM6AAAAABIWRZ7G6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGMZTCMRRGM3DIOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

--

George Amaro

dorjismo commented 5 months ago

Hi,

Sorry I could not follow. Looks like it does not work with models fitted with flexsdm. Below is the code I used for maxent and random forest

max_t <- tune_max( data = pres_max, response = "occ", predictors = c("bio2", "bio3", "bio8", "bio9", "bio12", "bio15"), partition = ".part", background = bg_max_part, grid = gridtest, thr = "max_sens_spec", metric = "TSS", clamp = TRUE, pred_type = "cloglog", n_cores = 10 # activate two cores to speed up this process )

rf_t <- tune_raf( data = par_part, response = "occ", predictors = c("bio2", "bio3", "bio8", "bio9", "bio12", "bio15"), partition = ".part", grid = tune_grid, thr = c("max_sens_spec", "equal_sens_spec", "max_sorensen"), metric = "TSS", n_cores = 10 ).

Would be grateful if you have further help.

Thanks a lot.

Regards Sangay

On Tue, 4 Jun 2024 at 00:33, geamaro @.***> wrote:

Hello,

Suppose you had a Maxent (Maxnet) model fited with flexsdm, named "maxent.fit.model". You can use {fitMaxnet} package do estimate variable importance, like this:

flexsdm variable importance (fitMaxnet)

maxent.varimp <- fitMaxnet::varImportance( theModel = with(maxent.fit.model, model), occSWD = as.data.frame(presence.points.model), bkgSWD = as.data.frame(background.points.model), responseType = "cloglog", numReplicates = 50 )

maxent.varimp <- data.frame(maxent.varimp) maxent.varimp$Var <- row.names(maxent.varimp) maxent.varimp <- arrange(maxent.varimp, Var)

file.name <- paste0("Plot - FINAL MODEL Var Permutation Importance.tiff") file.name <- iconv(file.name, to = "ASCII//TRANSLIT") file.name <- file.path(some.folder, file.name)

tiff(filename = file.name, units = "cm", width = 20, height = 12, res = 400)

maxent.varimp %>% arrange(maxent.varimp) %>% mutate(Var = factor(Var, levels = Var)) %>% ggplot(aes(x = Var, y = maxent.varimp)) + geom_col(fill = "#076FA2", width = 0.6) + geom_text(aes(label = paste0(maxent.varimp, "%")), hjust = -0.1, colour = "black", fontface = "bold") + coord_flip() + xlab("") + ylab("Permutation importance (%)") + scale_y_continuous(limits = c(0,85), n.breaks = 10)

dev.off()

I hope it helps...

Best wishes

G.

On Mon, Jun 3, 2024 at 9:49 AM dorjismo @.***> wrote:

How do I get variable importance? I have used flexsdm to model my species. I now want to find out variable importance. I do not know in this package. You help will be greatly appreciated as it is my thesis. Thanks

— Reply to this email directly, view it on GitHub https://github.com/sjevelazco/flexsdm/issues/380, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AKVCFCJCW47LTZJZFLWLF7TZFRX6RAVCNFSM6AAAAABIWRZ7G6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGMZTCMRRGM3DIOA>

. You are receiving this because you are subscribed to this thread.Message ID: @.***>

--

George Amaro

— Reply to this email directly, view it on GitHub https://github.com/sjevelazco/flexsdm/issues/380#issuecomment-2145362809, or unsubscribe https://github.com/notifications/unsubscribe-auth/BI5CYFZZB2PZWY4BBMDFTODZFR5FFAVCNFSM6AAAAABIWRZ7G6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBVGM3DEOBQHE . You are receiving this because you authored the thread.Message ID: @.***>

peterpothmann commented 4 months ago

A function to calculate variable importance is on the to-do list: Issue #55