% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cgmguru-functions-docs.R
\name{sensor_wear}
\alias{sensor_wear}
\title{Calculate Sensor Wear}
\usage{
sensor_wear(df, end_date = NULL, ndays = 14,
 reading_minutes = NULL)
}
\arguments{
\item{df}{A dataframe containing CGM data with columns:
\itemize{
  \item \code{id}: Subject identifier
  \item \code{time}: POSIXct measurement timestamp
  \item \code{gl}: Glucose value in mg/dL
}}

\item{end_date}{End timestamp for the calculation window. If \code{NULL},
each subject's last valid timestamp is used. \code{Date} values are
converted with \code{as.POSIXct()}, matching iglu's manual active-percent
behavior.}

\item{ndays}{Number of days in the retrospective window. Defaults to 14.}

\item{reading_minutes}{Reading interval in minutes. If \code{NULL}, it is
inferred per id from the median positive difference between valid readings.}
}
\value{
A tibble with columns \code{id}, \code{sensor_wear}, \code{ndays},
  \code{start_date}, and \code{end_date}.
}
\description{
Calculates the percent of expected CGM readings observed during a fixed
retrospective window. This C++ implementation follows the manual-range logic
used by \code{iglu::active_percent(range_type = "manual")}: valid readings
in \code{[end_date - ndays, end_date]} are divided by the expected number of
readings over \code{ndays} days.

If \code{end_date = NULL}, each subject's last valid timestamp defines that
subject's retrospective window. If \code{end_date} is supplied, the same
endpoint is used for all subjects, which is useful for a common study cutoff
or report date. Duplicate timestamps within a subject are de-duplicated after
sorting, and rows with missing \code{time} or \code{gl} do not count as
observed readings.
}
\examples{
library(iglu)
data(example_data_5_subject)
sensor_wear(example_data_5_subject, ndays = 14, reading_minutes = 5)
}
\seealso{
\link{detect_all_events}, \code{\link[iglu:active_percent]{iglu::active_percent}}
}
