Skip to contents

This 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.

Usage

mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var)

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.dat that need to be checked for time-variance within the same subject.

subject.var

The name of the variable in meta.dat that 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
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)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
#> $time_varying_vars
#> character(0)
#> 
#> $non_time_varying_vars
#> [1] "age" "bmi"
#> 

# Example 2: Missing variables in meta.dat
try({
  meta.dat <- data.frame(
    subject = c(1, 1, 2, 2, 3, 3, 4, 4),
    age = c(30, 30, 25, 25, 40, 40, 35, 35)
  )
  adj.vars <- c("age", "bmi")  # "bmi" is missing
  subject.var <- "subject"
  mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var)
})
#> Error in mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var) : 
#>   The following required variables are missing in meta.dat: bmi.

# Example 3: One variable is time-varying
try({
  meta.dat <- 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)
  )
  adj.vars <- c("age", "bmi")
  subject.var <- "subject"
  mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var)
})
#> $time_varying_vars
#> [1] "age"
#> 
#> $non_time_varying_vars
#> [1] "bmi"
#> 

# Example 4: Empty data set
try({
  meta.dat <- data.frame(subject = integer(0), age = integer(0), bmi = integer(0))
  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 5: Mixed case with time-varying and non-time-varying variables
try({
  meta.dat <- data.frame(
    subject = c(1, 1, 2, 2, 3, 3, 4, 4),
    age = c(30, 31, 25, 26, 40, 41, 35, 36),
    bmi = c(22, 23, 24, 25, 28, 29, 26, 27)
  )
  adj.vars <- c("age", "bmi")
  subject.var <- "subject"
  mStat_identify_time_varying_vars(meta.dat, adj.vars, subject.var)
})
#> $time_varying_vars
#> [1] "age" "bmi"
#> 
#> $non_time_varying_vars
#> character(0)
#>