View source: R/longitudinal_grmtree.R
| prepare_longitudinal_data | R Documentation |
Constructs a wide-format data frame suitable for
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.
prepare_longitudinal_data(
data,
items_t1,
items_t2,
covariates = NULL,
id = NULL
)
data |
A data frame containing item response columns for both time points and any covariates. |
items_t1 |
Character vector of column names for Time 1 item responses, in the order they should appear in the response matrix. |
items_t2 |
Character vector of column names for Time 2 item responses,
in the same order as |
covariates |
Optional character vector of column names for covariates
to include in the output data frame (e.g., |
id |
Optional character string specifying the subject ID column name. If provided, the ID column is included in the output. |
A data frame with:
All columns specified in covariates
resp_wideA matrix-valued column with
2 * length(items_t1) columns. The first half contains T1
responses (named Item1_T1, ..., ItemM_T1) and the
second half contains T2 responses (named Item1_T2, ...,
ItemM_T2).
Rows with any missing values in the response matrix are removed, with a message indicating how many rows were dropped.
longitudinal_grmtree for fitting the tree
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.