plotModel: Plot Model Coefficients and Importance

View source: R/global.visu.R

plotModelR Documentation

Plot Model Coefficients and Importance

Description

This function visualizes the coefficients of a model, with the option to plot feature importance. It supports various model types, including SOTA models and Random Forests, and can display the results in a variety of layouts, including plots of feature coefficients, feature importance, or decision trees.

Usage

plotModel(
  mod,
  X,
  y,
  sort.features = FALSE,
  sort.ind = NULL,
  feature.name = FALSE,
  col.sign = c("deepskyblue1", "firebrick1"),
  main = "",
  slim = FALSE,
  importance = FALSE,
  res_clf = NULL
)

Arguments

mod

A model object (e.g., a fitted machine learning model such as SVM, logistic regression, or random forest).

X

A data matrix or data frame where each row is an observation and each column is a feature.

y

A vector of class labels (e.g., 1 and -1 for binary classification, or continuous for regression) corresponding to the rows in 'X'.

sort.features

Logical; if 'TRUE', the features will be sorted by their discriminance with respect to 'y'. Default is 'FALSE'.

sort.ind

A vector of indices for sorting the features. If 'NULL', the function will determine the order based on discriminance. Default is 'NULL'.

feature.name

Logical; if 'TRUE', the feature names will be displayed on the plot.

col.sign

A vector of two colors for positive and negative coefficients (default is 'c("deepskyblue1", "firebrick1")').

main

A string for the title of the plot.

slim

Logical; if 'TRUE', the plot will be simplified (e.g., removing axis labels and ticks). Default is 'FALSE'.

importance

Logical; if 'TRUE', the feature importance will be plotted. Default is 'FALSE'.

res_clf

Optional; a result object containing cross-validation or feature importance data. This is used when 'importance' is 'TRUE' for models that have a cross-validation feature.

Details

This function generates a plot of model coefficients or feature importance, depending on the model type and the 'importance' parameter. The function supports SOTA models (like SVM or logistic regression), where coefficients are visualized, and Random Forest models, where decision trees can be plotted. The plot can also include feature importance, if available.

If the model is a 'Random Forest', the function will plot a decision tree. For other models, it will plot the feature coefficients with color-coded bars for positive and negative coefficients.

Value

If the model is a 'Random Forest', the function returns a decision tree plot. For other models, it returns a 'ggplot' object showing the feature coefficients or importance.

Author(s)

Edi Prifti (IRD)

Examples

# Example usage for a classification model
model <- train(logistic_regression_model)  # Assume this is a pre-trained model
X <- data.frame(feature1 = rnorm(100), feature2 = rnorm(100))
y <- sample(c(1, -1), 100, replace = TRUE)

# Plot the model's coefficients
plotModel(model, X, y, main = "Model Coefficients Plot")

# Plot the model's importance (if available)
plotModel(model, X, y, importance = TRUE)


predomics/predomicspkg documentation built on Dec. 11, 2024, 11:06 a.m.