
Identify Time-Varying Variables within Subjects
Source:R/mStat_identify_time_varying_vars.R
mStat_identify_time_varying_vars.RdThis function checks each specified adjustable variable to determine if it is time-varying within the same subject. If a variable is found to be time-varying for any subject, the function stops and reports which subjects caused this issue. If no variable is time-varying, a message confirms that all variables are consistent within subjects.
Arguments
- meta.dat
A data frame containing the metadata where each row corresponds to an observation.
- adj.vars
A character vector of names of the adjustable variables in
meta.datthat need to be checked for time-variance within the same subject.- subject.var
The name of the variable in
meta.datthat identifies the subject. This variable is used to group observations by subject.
Value
The function does not return a value. If it finds that an adjustable variable is time-varying within subjects, it stops and throws an error with a message specifying the variable and the problematic subjects. If all variables are non-time-varying within subjects, it outputs a message confirming this.
Examples
# Example 1: All variables are non-time-varying within subjects
meta.dat <- data.frame(
subject = c(1, 1, 2, 2, 3, 3, 4, 4),
age = c(30, 30, 25, 25, 40, 40, 35, 35),
bmi = c(22, 22, 24, 24, 28, 28, 26, 26)
)
adj.vars <- c("age", "bmi")
subject.var <- "subject"
mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var)
#> $time_varying_vars
#> character(0)
#>
#> $non_time_varying_vars
#> [1] "age" "bmi"
#>
# Example 2: One variable varies within subjects
meta.dat2 <- data.frame(
subject = c(1, 1, 2, 2, 3, 3, 4, 4),
age = c(30, 31, 25, 25, 40, 40, 35, 35),
bmi = c(22, 22, 24, 24, 28, 28, 26, 26)
)
mStat_identify_time_varying_vars(meta.dat2, adj.vars, subject.var)
#> $time_varying_vars
#> [1] "age"
#>
#> $non_time_varying_vars
#> [1] "bmi"
#>
# Example 3: Empty metadata returns both variables as non-time-varying
meta.dat3 <- data.frame(subject = integer(0), age = integer(0), bmi = integer(0))
mStat_identify_time_varying_vars(meta.dat3, adj.vars, subject.var)
#> $time_varying_vars
#> character(0)
#>
#> $non_time_varying_vars
#> [1] "age" "bmi"
#>