% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/longitudinal_grmtree.R
\name{prepare_longitudinal_data}
\alias{prepare_longitudinal_data}
\title{Prepare Wide-Format Longitudinal PROM Data}
\usage{
prepare_longitudinal_data(
  data,
  items_t1,
  items_t2,
  covariates = NULL,
  id = NULL
)
}
\arguments{
\item{data}{A data frame containing item response columns for both time
points and any covariates.}

\item{items_t1}{Character vector of column names for Time 1 item responses,
in the order they should appear in the response matrix.}

\item{items_t2}{Character vector of column names for Time 2 item responses,
in the same order as \code{items_t1}. Must have the same length.}

\item{covariates}{Optional character vector of column names for covariates
to include in the output data frame (e.g., \code{c("age", "sex")}).}

\item{id}{Optional character string specifying the subject ID column name.
If provided, the ID column is included in the output.}
}
\value{
A data frame with:
\describe{
\item{Covariates}{All columns specified in \code{covariates}}
\item{\code{resp_wide}}{A matrix-valued column with
\code{2 * length(items_t1)} columns. The first half contains T1
responses (named \code{Item1_T1}, ..., \code{ItemM_T1}) and the
second half contains T2 responses (named \code{Item1_T2}, ...,
\code{ItemM_T2}).}
}
Rows with any missing values in the response matrix are removed, with
a message indicating how many rows were dropped.
}
\description{
Constructs a wide-format data frame suitable for
\code{\link{longitudinal_grmtree}} from separate Time 1 and Time 2 item
response columns. The function creates a matrix-valued column containing
the concatenated T1 and T2 responses, along with any specified covariates.
}
\examples{
library(grmtree)

# Load the synthetic longitudinal data
data("grmtree_long_data", package = "grmtree")

  # Prepare the wide-format response matrix from separate T1 and T2 columns
  items_t1 <- c("MOS_Listen", "MOS_Info", "MOS_Advice_Crisis", "MOS_Confide",
                "MOS_Advice_Want", "MOS_Fears", "MOS_Personal", "MOS_Understand")
  ld <- prepare_longitudinal_data(
    data = grmtree_long_data,
    items_t1 = items_t1,
    items_t2 = paste0(items_t1, "_year1"),
    covariates = c("sex", "age", "residency", "job",
                   "education", "comorbidity_count", "ever_smoker")
  )

  # Check structure
  str(ld$resp_wide)  # 1500 x 16 matrix

}
\seealso{
\code{\link{longitudinal_grmtree}} for fitting the tree
}
