check.timeinvar: Check if Variable is Time-Invariant for Each Subject

View source: R/check.timeinvar.r

check.timeinvarR Documentation

Check if Variable is Time-Invariant for Each Subject

Description

Function that checks if a variable is time-invariant for each subject.

Usage

check.timeinvar(x, id, data, out=1, na.rm=TRUE)

Arguments

x

argument to specify the variable to check.

id

argument to specify a subject id variable.

data

optional data frame that contains the variables specified above.

out

either a string or an integer (1 = "logical", 2 = "id", 3 = "data") indicating what information should be returned in case there are subjects where the variable is not time-invariant.

na.rm

logical indicating whether missing values should be removed before checking (default is TRUE).

Details

The function checks if the values of a variable are constant (i.e., time-invariant) for each subject.

When na.rm=TRUE (the default), missing values are ignored. When setting na.rm=FALSE, then missing values are treated as distinct values from any non-missing values. See ‘Examples’.

Value

When out = 1 or out = "logical", the function simply returns a logical (i.e., TRUE or FALSE), depending on whether the variable is time-invariant within each subject.

When out = 2 or out = "id", the function returns a vector with the ids of those subjects where the variable is not time-invariant.

When out = 3 or out = "data", the function returns the data for those subjects where the variable is not time-invariant.

Author(s)

Wolfgang Viechtbauer wvb@wvbauer.com

See Also

get.timeinvar

Examples

# illustrative dataset
dat <- data.frame(subj=rep(1:4, each=5),
                  obs = 1:5,
                  age = rep(c(20,31,27,22), each=5),
                  stress = c(2,3,1,4,2, 3,3,3,3,3, 1,1,2,6,4, 1,2,1,3,1))
dat

# check the age variable
check.timeinvar(age, subj, data=dat)

# check the stress variable
check.timeinvar(stress, subj, data=dat)

# for which subjects is stress non-constant
check.timeinvar(stress, subj, data=dat, out=2)

# show the data for those subjects
check.timeinvar(stress, subj, data=dat, out=3)

# by default missings are ignored
dat$age[2] <- NA
check.timeinvar(age, subj, data=dat)

# treat NAs as distinct values
check.timeinvar(age, subj, data=dat, na.rm=FALSE)
check.timeinvar(age, subj, data=dat, na.rm=FALSE, out=2)
check.timeinvar(age, subj, data=dat, na.rm=FALSE, out=3)

wviechtb/esmpack documentation built on April 12, 2025, 9:48 p.m.