fit_ncm: Fit Neutral Community Model (NCM) with optional grouping and...

View source: R/fit_ncm.R

fit_ncmR Documentation

Fit Neutral Community Model (NCM) with optional grouping and total pooling

Description

Fit Neutral Community Model (NCM) with optional grouping and total pooling

Usage

fit_ncm(
  otu,
  grp = NULL,
  group_col = "group",
  groups = NULL,
  simplify = TRUE,
  return_model = FALSE,
  ...
)

Arguments

otu

OTU table, rows = samples, columns = species (data.frame or matrix)

grp

Optional data.frame with rownames = sample IDs, and one column specifying group membership. If NULL, all samples in otu are used as one group.

group_col

Column name in grp that contains group labels (default "group").

groups

Character vector of group names to analyze. If NULL and grp is provided, the function fits models for each group AND for all samples combined (total). If non-NULL, only those groups are analyzed (no total model).

simplify

If TRUE and only one model is fitted, return the model list directly (not nested).

return_model

Whether to return the nlsLM model object (default FALSE to save space).

...

Additional arguments passed to nlsLM (e.g., lower, upper, control).

Value

A list containing model results. If multiple models, a named list with keys: - "all" (if total model fitted) and group names. Each value is a list with: m, N, Nm, ci, rsqr, predictions, (model optional).

Examples

# Load example data
data(example_data)
otu <- example_data$otu
grp <- example_data$grp

# 1. No grouping: fit the total model using all samples
res_total <- fit_ncm(otu)
print(paste("m =", round(res_total$m, 4)))
print(paste("R^2 =", round(res_total$rsqr, 4)))
head(res_total$predictions)

# 2. With grouping: fit models for all groups + total
res_all <- fit_ncm(otu, grp)

# 3. Only specific groups (no total model), returns a single model object
res_sub <- fit_ncm(otu, grp, groups = "A")
res_sub$status_summary


ncmR documentation built on April 19, 2026, 5:07 p.m.