Skip to contents

An R package providing a versatile toolkit for mathematical modeling, developed as a companion to the book Mathematical Modeling: Algorithms and Programming Implementation (China Machine Press). It focuses on implementing rigorous algorithms in a user-friendly manner.

Current Version (0.0.14) introduces a data-cleaning toolkit — missing-value diagnostics with Little’s MCAR test (na_mcar_test()), missingness visuals (plot_na_heatmap(), plot_na_bar()), simple and model-based imputation (impute(), impute_model() with kNN / linear regression / decision trees), multiple imputation by FCS chained equations (impute_multiple()) with Rubin’s-rules pooling (rubin_pool(), pool_fit(); Barnard-Rubin small-sample df), and outlier handling (outlier()) — plus the Excel-style pivot_table(). Together with the previous release’s tidy statistical inference toolkit (stat_*() tests, bootstrap and permutation frameworks, export_table()), the multivariate statistics toolkit (mv_*()), the factor-analysis weighting weight_fa(), and the renamed weight_* / mf_* families. All this builds on the regression prediction toolkit, time-series workflow, epidemic modeling, differential equation models, grey prediction, Markov chain models, interpolation and curve fitting, and a rich suite of evaluation methods (AHP, Entropy, CRITIC, PCA, TOPSIS, Fuzzy, RSR, DEA).

Key Features

Installation

Once released on CRAN, install mathmodels with:

install.packages("mathmodels")

You can install the latest development version directly from GitHub:

remotes::install_github("zhjx19/mathmodels")

Getting Started

library(mathmodels)

# --- AHP (Analytic Hierarchy Process) ---
A = matrix(c(1,   1/2, 4, 3,   3,
             2,   1,   7, 5,   5,
             1/4, 1/7, 1, 1/2, 1/3,
             1/3, 1/5, 2, 1,   1,
             1/3, 1/5, 3, 1,   1), byrow = TRUE, nrow = 5)
AHP(A)

# --- Epidemic compartment modeling ---
result = ode_sir(
  init   = c(S = 990, I = 10, R = 0),
  params = c(beta = 0.15, gamma = 0.1),  # R0 = 1.5
  times  = seq(0, 100, by = 0.5))

# Visualize
plot_compartments(result, compartments = c("S", "I", "R"))

# Compute epidemic metrics
metrics = epi_metrics(result, beta = 0.15, gamma = 0.1, N = 1000)
metrics$R0         # basic reproduction number

# --- Time series ---
x = as_ts_df(log(AirPassengers))
fit = ts_sarima(x)
fc  = ts_forecast(fit, h = 12)
plot_ts_forecast(x, fc)           # forecast with CIs

# --- Statistical inference (tidy in, tidy out) ---
res = stat_t_test(sleep, .cols = extra, group = group)
res                               # one tidy row: statistic, p, CI, Cohen's d
export_table(res, output = "latex", caption = "Two-sample t-test")

# --- Multivariate statistics ---
r = mv_pca(mtcars, cyl:carb)
plot_mv_scree(r, type = "cum")    # cumulative variance scree

Learning More

For detailed documentation, tutorials, and in-depth examples on using the mathmodels package, please refer to the comprehensive online manual:

📘 mathmodels Package Manual - Simplifying Mathematical Modeling (Online Book, In Chinese)

This online book is the definitive guide to the package’s functionalities. Currently implemented modules include: