% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pedanalysis.R
\name{pedne}
\alias{pedne}
\title{Calculate Effective Population Size}
\usage{
pedne(
  ped,
  method = c("coancestry", "inbreeding", "demographic"),
  by = NULL,
  reference = NULL,
  nsamples = 1000,
  ncores = 1,
  seed = NULL
)
}
\arguments{
\item{ped}{A \code{tidyped} object.}

\item{method}{Character. The method to compute Ne. One of \code{"coancestry"} (default), \code{"inbreeding"}, or \code{"demographic"}.}

\item{by}{Character. The name of the column used to group cohorts (e.g., "Year", "BirthYear").
If NULL, calculates overall Ne for all individuals.}

\item{reference}{Character vector. Optional subset of individual IDs defining the reference cohort.
If NULL, uses all individuals in the pedigree.}

\item{nsamples}{Integer. Number of individuals to randomly sample per cohort when using the \code{"coancestry"} method. Very large cohorts will be sampled down to this size to save memory and time (default: 1000).}

\item{ncores}{Integer. Number of cores for parallel processing. Currently only effective for \code{method = "coancestry"} (default: 1).}

\item{seed}{Integer or NULL. Random seed passed to \code{set.seed()} before sampling
in the coancestry method, ensuring reproducible \eqn{N_e} estimates.
Default is \code{NULL} (no fixed seed).}
}
\value{
A \code{data.table} with columns:
\itemize{
  \item \code{Cohort}: Cohort or grouping variable value.
  \item \code{N}: Number of individuals in the cohort.
  \item \code{Ne}: Calculated effective population size.
  \item \code{...}: Additional columns depending on the selected method (e.g., \code{NSampled}, \code{DeltaC}, \code{MeanF}, \code{DeltaF}, \code{Nm}, \code{Nf}).
}
}
\description{
Calculates the effective population size (Ne) based on the rate of
coancestry, the rate of inbreeding, or demographic parent numbers.
}
\details{
The effective population size can be calculated using one of three methods:

\itemize{
  \item \strong{"coancestry"} (Default): Based on the rate of coancestry (\eqn{f_{ij}}) between 
  pairs of individuals. In this context, \eqn{f_{ij}} is the probability that two alleles 
  randomly sampled from individuals $i$ and $j$ are identical by descent, which is equivalent 
  to half the additive genetic relationship (\eqn{f_{ij} = a_{ij} / 2}). 
  This method is generally more robust as it accounts for full genetic drift and 
  bottlenecks (Cervantes et al., 2011).
  \deqn{\Delta c_{ij} = 1 - (1 - c_{ij})^{1/(\frac{ECG_i + ECG_j}{2})}}
  \deqn{N_e = \frac{1}{2 \overline{\Delta c}}}
  To handle large populations, this method samples \code{nsamples} individuals per cohort 
  and computes the mean rate of coancestry among them. Sampled relationship
  coefficients are obtained from batched matrix-free \eqn{AX} products, so
  the full additive relationship matrix is not constructed.
  
  \item \strong{"inbreeding"}: Based on the individual rate of inbreeding ($F_i$) (Gutiérrez et al., 2008, 2009).
  \deqn{\Delta F_i = 1 - (1 - F_i)^{1/(ECG_i - 1)}}
  \deqn{N_e = \frac{1}{2 \overline{\Delta F}}}
  
  \item \strong{"demographic"}: Based on the demographic census of breeding males and females (Wright, 1931).
  \deqn{N_e = \frac{4 N_m N_f}{N_m + N_f}}
  Where \eqn{N_m} and \eqn{N_f} are the number of unique male and female parents contributing to the cohort.
}
}
\examples{
\donttest{
# Coancestry-based Ne (default) using a simple pedigree grouped by year
tp_simple <- tidyped(simple_ped)
tp_simple$BirthYear <- 2000 + tp_simple$Gen
ne_coan <- suppressMessages(pedne(tp_simple, by = "BirthYear", seed = 42L))
ne_coan

# Inbreeding-based Ne using an inbred pedigree
tp_inbred <- tidyped(inbred_ped)
ne_inb <- suppressMessages(pedne(tp_inbred, method = "inbreeding", by = "Gen"))
ne_inb

# Demographic Ne from the number of contributing sires and dams
ne_demo <- suppressMessages(pedne(tp_simple, method = "demographic", by = "BirthYear"))
ne_demo
}

}
\references{
Cervantes, I., Goyache, F., Molina, A., Valera, M., & Gutiérrez, J. P. (2011). Estimation of effective population size from the rate of coancestry in pedigreed populations. \emph{Journal of Animal Breeding and Genetics}, 128(1), 56-63.

Gutiérrez, J. P., Cervantes, I., Molina, A., Valera, M., & Goyache, F. (2008). Individual increase in inbreeding allows estimating effective sizes from pedigrees. \emph{Genetics Selection Evolution}, 40(4), 359-370.

Gutiérrez, J. P., Cervantes, I., & Goyache, F. (2009). Improving the estimation of realized effective population sizes in farm animals. \emph{Journal of Animal Breeding and Genetics}, 126(4), 327-332.

Wright, S. (1931). Evolution in Mendelian populations. \emph{Genetics}, 16(2), 97-159.
}
