| Title: | Integrating Morphological Modeling and Machine Learning for Decision Support |
|---|---|
| Description: | Integrating morphological modeling with machine learning to support structured decision-making (e.g., in management and consulting). The package enumerates a morphospace of feasible configurations and uses random forests to estimate class probabilities over that space, bridging deductive model exploration with empirical validation. It includes utilities for factorizing inputs, model training, morphospace construction, and an interactive 'shiny' app for scenario exploration. |
| Authors: | Oskar Kosch [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-2697-1393>) |
| Maintainer: | Oskar Kosch <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-06-07 08:17:01 UTC |
| Source: | https://github.com/theogrost/mlmorph |
Create a morphospace of predictor combinations with class probabilities
create_morphospace(the_data, model, shiny = FALSE)create_morphospace(the_data, model, shiny = FALSE)
the_data |
A data.frame used to derive unique values of predictors. |
model |
A classification model fitted via a formula interface that
supports |
shiny |
Logical; if |
A list with components:
morphospace: data frame with all predictor combinations,
class label column (named as the dependent), calculated (probability),
and purely_simulated flag.
dependent: character scalar with the outcome name.
independent: character vector of predictor names.
all_vars: character vector c(independent, dependent).
purely_simulated: logical vector aligned with morphospace.
n <- 60 y <- factor(sample(letters[1:3], n, TRUE)) x1 <- factorize_numeric_vector(runif(n, 10, 20), breaks_no = 3) x2 <- factorize_numeric_vector(runif(n, 1, 2), breaks_no = 3) df <- data.frame(y, x1, x2) fit <- create_rf_model(df, dependent = "y", ntree = 50)$model ms <- create_morphospace(df, fit) names(ms)n <- 60 y <- factor(sample(letters[1:3], n, TRUE)) x1 <- factorize_numeric_vector(runif(n, 10, 20), breaks_no = 3) x2 <- factorize_numeric_vector(runif(n, 1, 2), breaks_no = 3) df <- data.frame(y, x1, x2) fit <- create_rf_model(df, dependent = "y", ntree = 50)$model ms <- create_morphospace(df, fit) names(ms)
Create a random forest classification model
create_rf_model( data, dependent = colnames(data)[ncol(data)], independent = setdiff(colnames(data), dependent), train_validate_split = 0.8, shiny = FALSE, ... )create_rf_model( data, dependent = colnames(data)[ncol(data)], independent = setdiff(colnames(data), dependent), train_validate_split = 0.8, shiny = FALSE, ... )
data |
A data.frame containing predictors and the outcome. |
dependent |
Character scalar; the name of the outcome (must be a factor for classification).
Defaults to the last column of |
independent |
Character vector; names of predictor variables.
Defaults to all columns except |
train_validate_split |
Numeric in (0, 1); proportion of rows used for training. Default is |
shiny |
Logical; if |
... |
Additional arguments passed to randomForest (e.g., |
A named list with components:
model: a randomForest return object.
variables_importance: matrix from importance.
model_performance_on_test: a confusionMatrix return object on the validation set.
n <- 60 y <- factor(sample(letters[1:3], n, TRUE)) x1 <- factorize_numeric_vector(runif(n, 10, 20), breaks_no = 3) x2 <- factorize_numeric_vector(runif(n, 1, 2), breaks_no = 5) df <- data.frame(y, x1, x2) fit <- create_rf_model(df, dependent = "y", ntree = 50) names(fit)n <- 60 y <- factor(sample(letters[1:3], n, TRUE)) x1 <- factorize_numeric_vector(runif(n, 10, 20), breaks_no = 3) x2 <- factorize_numeric_vector(runif(n, 1, 2), breaks_no = 5) df <- data.frame(y, x1, x2) fit <- create_rf_model(df, dependent = "y", ntree = 50) names(fit)
Turn binary vector into a factor
factorize_binary_vector(data_vector, custom_labels = NULL)factorize_binary_vector(data_vector, custom_labels = NULL)
data_vector |
Logical vector. |
custom_labels |
Optional length-2 character vector: first for |
A factor with two levels in TRUE, FALSE order.
factorize_binary_vector(c(TRUE, FALSE, TRUE))factorize_binary_vector(c(TRUE, FALSE, TRUE))
Turn character vector into a factor
factorize_character_vector(data_vector, custom_labels = NULL)factorize_character_vector(data_vector, custom_labels = NULL)
data_vector |
Character vector. |
custom_labels |
Optional named character vector where names are original values and values are labels. |
A factor with labeled levels.
factorize_character_vector(c("A First", "B Second", "C Third"))factorize_character_vector(c("A First", "B Second", "C Third"))
Identity factorization for numbered strings
factorize_identity(data_vector)factorize_identity(data_vector)
data_vector |
Character vector where values are already labeled (e.g., |
A factor with levels == labels.
factorize_identity(c("1. First", "2. Second", "3. Third"))factorize_identity(c("1. First", "2. Second", "3. Third"))
Heuristic factorization for all columns of a data frame
factorize_nicely_dataframe(data_frame)factorize_nicely_dataframe(data_frame)
data_frame |
A data frame. |
A data frame with all columns converted to factors.
df <- data.frame(x = runif(20), y = rep(c(TRUE, FALSE, TRUE, TRUE), 5)) factorize_nicely_dataframe(df)df <- data.frame(x = runif(20), y = rep(c(TRUE, FALSE, TRUE, TRUE), 5)) factorize_nicely_dataframe(df)
Heuristic factorization for a single vector
factorize_nicely_vector(data_vector)factorize_nicely_vector(data_vector)
data_vector |
A vector (numeric, logical, or character). |
A factor (ordered for numeric inputs with many distinct values).
factorize_nicely_vector(c("a", "b", "a"))factorize_nicely_vector(c("a", "b", "a"))
Turn numeric vector into an ordered factor
factorize_numeric_vector( data_vector, method = c("equal_bins", "equal_distance", "custom_breaks"), breaks_no = 5, custom_breaks = NULL, custom_labels = NULL )factorize_numeric_vector( data_vector, method = c("equal_bins", "equal_distance", "custom_breaks"), breaks_no = 5, custom_breaks = NULL, custom_labels = NULL )
data_vector |
Numeric vector. |
method |
Factorization rule: one of |
breaks_no |
Integer |
custom_breaks |
Optional numeric vector of cut points (strictly increasing) used when |
custom_labels |
Optional character vector of labels. If supplied, its length should equal |
An ordered factor with interval labels.
factorize_numeric_vector(runif(10))factorize_numeric_vector(runif(10))
Load tabular data (xlsx, csv, or json)
load_data(data_path)load_data(data_path)
data_path |
Character scalar; path to a |
A base data.frame with the imported data.
tmp_csv <- tempfile(fileext = ".csv") utils::write.csv(data.frame(a = 1:2, b = c("x", "y")), tmp_csv, row.names = FALSE) load_data(tmp_csv) tmp_json <- tempfile(fileext = ".json") jsonlite::write_json(list(a = 1:2, b = c("x","y")), tmp_json, auto_unbox = TRUE) load_data(tmp_json) tmp_xlsx <- tempfile(fileext = ".xlsx") openxlsx::write.xlsx(data.frame(a = 1:2, b = c("x","y")), tmp_xlsx) load_data(tmp_xlsx)tmp_csv <- tempfile(fileext = ".csv") utils::write.csv(data.frame(a = 1:2, b = c("x", "y")), tmp_csv, row.names = FALSE) load_data(tmp_csv) tmp_json <- tempfile(fileext = ".json") jsonlite::write_json(list(a = 1:2, b = c("x","y")), tmp_json, auto_unbox = TRUE) load_data(tmp_json) tmp_xlsx <- tempfile(fileext = ".xlsx") openxlsx::write.xlsx(data.frame(a = 1:2, b = c("x","y")), tmp_xlsx) load_data(tmp_xlsx)
Launch the MLmorph shiny app
MLmorph( host = "127.0.0.1", port = NULL, launch.browser = TRUE, maxUploadSize = 200 * 1024^2 )MLmorph( host = "127.0.0.1", port = NULL, launch.browser = TRUE, maxUploadSize = 200 * 1024^2 )
host |
Host interface to bind (default |
port |
Integer port or |
launch.browser |
Logical; open in a browser. Default |
maxUploadSize |
Maximum request size in bytes; sets
|
The value returned by runApp.
if(interactive()){ MLmorph() }if(interactive()){ MLmorph() }