% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sncm.fit.R
\name{sncm.fit}
\alias{sncm.fit}
\title{Fit Sloan Neutral Community Model (SNCM)}
\usage{
sncm.fit(spp, pool = NULL, stats = TRUE, taxon = NULL)
}
\arguments{
\item{spp}{A matrix or data frame where rows represent communities (samples)
and columns represent species/taxa. Values should be abundances or counts.}

\item{pool}{Optional. A matrix or data frame representing the source pool for
calculating relative abundances. If NULL, the source pool is calculated
from the spp matrix. Default is \code{NULL}.}

\item{stats}{Logical. If \code{TRUE}, returns fit statistics including AIC, BIC,
R-squared, and RMSE for model comparison. If \code{FALSE}, returns predicted vs.
observed frequencies with confidence intervals. Default is \code{TRUE}.}

\item{taxon}{Optional. A data frame containing taxonomic information to
merge with results when \code{stats = FALSE}. Should have row names matching species
names. Default is \code{NULL}.}
}
\value{
If \code{stats = TRUE}, returns a data frame with model fit statistics including:
\itemize{
\item \code{m}: Migration rate parameter from NLS fit
\item \code{m.ci}: Confidence interval for m parameter
\item \code{m.mle}: Migration rate from maximum likelihood estimation
\item \code{maxLL}, \code{binoLL}, \code{poisLL}: Log-likelihood values for SNCM, binomial, and Poisson models
\item \code{Rsqr}, \code{Rsqr.bino}, \code{Rsqr.pois}: R-squared values for each model
\item \code{RMSE}, \code{RMSE.bino}, \code{RMSE.pois}: Root mean squared error for each model
\item \code{AIC}, \code{BIC}: Information criteria for model selection
\item \code{N}: Average number of individuals per community
\item \code{Samples}: Number of samples/communities
\item \code{Richness}: Number of taxa analyzed
\item \code{Detect}: Detection limit (1/N)
}
If \code{stats = FALSE}, returns a data frame with observed and predicted
frequencies along with confidence intervals for visualization.
}
\description{
The function implements the Sloan Neutral Community Model which
predicts the occurrence frequency of taxa based on their relative abundance
in the source pool and a migration parameter (\code{m}).  It compares the
neutral model against binomial and Poisson null models using various
statistical measures.
}
\details{
The model assumes neutral processes govern community assembly. Three models are compared: SNCM (beta distribution), binomial null model, and Poisson null model.
}
\examples{
# Generate community data
set.seed(42)
n_samples <- 100
n_species <- 80

# Log-normal abundances
mean_abund <- rlnorm(n_species, meanlog = 2, sdlog = 1.5)
# Simulate community matrix with multinomial sampling
spp_data <- t(sapply(seq_len(n_samples), function(i) {
  rmultinom(
    1,
    size = sample(500:2000, 1),
    prob = mean_abund / sum(mean_abund)
  )[, 1]
}))
colnames(spp_data) <- paste0("Species_", seq_len(n_species))

fit_stats <- sncm.fit(spp_data, stats = TRUE)
predictions <- sncm.fit(spp_data, stats = FALSE)


}
\references{
Sloan WT, Lunn M, Woodcock S, Head IM, Nee S, Curtis TP. (2006) Quantifying
the roles of immigration and chance in shaping prokaryote community
structure. Environ Microbiol. 8(4):732-40.
\url{doi:10.1111/j.1462-2920.2005.00956.x}
}
\seealso{
\code{\link[=plot_neutral_model]{plot_neutral_model()}}, \code{\link[=fit_neutral_model]{fit_neutral_model()}}
}
