% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/analyze.R
\name{analyze_attrition}
\alias{analyze_attrition}
\title{Analyze Employee Attrition}
\usage{
analyze_attrition(
  survey,
  attrition_file,
  emp_id_col = NULL,
  term_date_col,
  scale_points,
  time_periods = c(90, 180, 365),
  attribute_cols = NULL,
  min_group_size = 5,
  plot = FALSE
)
}
\arguments{
\item{survey}{A glint_survey object or data frame containing survey data}

\item{attrition_file}{Path to CSV file containing employee attrition data}

\item{emp_id_col}{Character string specifying the column name for employee ID}

\item{term_date_col}{Character string specifying the column name for termination date}

\item{scale_points}{Integer specifying the number of scale points (2-11)}

\item{time_periods}{Integer vector specifying time periods in days to analyze
(default: c(90, 180, 365))}

\item{attribute_cols}{Optional character vector of attribute column names to
segment results by (e.g., \code{c("Department", "Gender")}). Attributes must
already be joined to the survey via \code{join_attributes()}. When \code{NULL}
(default), results are returned for the overall population.}

\item{min_group_size}{Minimum number of employees required in an attribute group
for it to be included in results (default: 5). Ignored when
\code{attribute_cols = NULL}.}

\item{plot}{Logical. If \code{TRUE}, prints a grouped bar chart of favorable
vs. unfavorable attrition rates faceted by time period and returns the data
invisibly. When \code{attribute_cols} is supplied, the first attribute
column is used as an additional facet dimension. Requires \pkg{ggplot2}.
Default: \code{FALSE}.}
}
\value{
A tibble with one row per (attribute group)-question-time period
combination containing:
\describe{
\item{attribute columns}{One column per entry in \code{attribute_cols}
(only present when \code{attribute_cols} is supplied)}
\item{group_size}{Number of employees in the attribute group (only present
when \code{attribute_cols} is supplied)}
\item{question}{The question text}
\item{days}{The time period in days}
\item{favorable_n}{Number of employees who responded favorably}
\item{favorable_attrition}{Proportion who left within time period (favorable)}
\item{unfavorable_n}{Number of employees who responded unfavorably}
\item{unfavorable_attrition}{Proportion who left within time period (unfavorable)}
\item{attrition_ratio}{Ratio of unfavorable to favorable attrition rates}
}
When \code{plot = TRUE}, the same tibble is returned invisibly after
printing the plot.
}
\description{
Analyzes the relationship between survey responses and employee attrition,
calculating how much more (or less) likely employees are to leave within
specified time periods if they respond unfavorably vs favorably to survey questions.
}
\examples{
survey_path <- system.file("extdata", "survey_export.csv", package = "vivaglint")
attrition_file <- system.file("extdata", "attrition.csv", package = "vivaglint")
attr_path <- system.file("extdata", "employee_attributes.csv", package = "vivaglint")
survey <- read_glint_survey(survey_path, emp_id_col = "EMP ID")

# Overall attrition analysis
attrition <- analyze_attrition(
  survey,
  attrition_file = attrition_file,
  emp_id_col = "EMP ID",
  term_date_col = "Termination Date",
  scale_points = 5
)

# Attrition segmented by Department and Gender
survey_enriched <- join_attributes(survey, attr_path, emp_id_col = "EMP ID")
attrition_by_dept <- analyze_attrition(
  survey_enriched,
  attrition_file = attrition_file,
  emp_id_col = "EMP ID",
  term_date_col = "Termination Date",
  scale_points = 5,
  attribute_cols = c("Department", "Gender"),
  min_group_size = 2
)

# With attrition chart
if (requireNamespace("ggplot2", quietly = TRUE)) {
  analyze_attrition(survey, attrition_file = attrition_file,
                    emp_id_col = "EMP ID", term_date_col = "Termination Date",
                    scale_points = 5, plot = TRUE)
}
}
