byPhasebyGroup: Function to create dummy codes for phase and group specific...

View source: R/reparameterize.r

byPhasebyGroupR Documentation

Function to create dummy codes for phase and group specific effects.

Description

In ICT data with 2+ groups and 2+ phases, dummy coding can be used to estimate group and phase specific intercepts and slopes.

Usage

byPhasebyGroup(data, time, phase, group, retime = TRUE)

Arguments

data

A data.frame with a time and phase variables (see PersonAlytic), and a grouping variable (which would normally be passed to PersonAlytic via the ivs parameter). Alternatively, data=NULL can be used in conjunction with time, phase, and group to produce generic variable names and a design matrix.

time

Character. The name of the time variable, or if data=NULL, time must be a numeric vector with all of time points in a hypothetical study.

phase

Character. The name of the phase variable, or if data=NULL, phase must be either a numeric integer specifying the number of phases or a character vector with the phase names.

group

Character. The name of the group variable., or if data=NULL, group must be either a numeric integer specifying the number of groups or a character vector with the group names.

retime

Logical. Default is TRUE. Should the time (slope) variables be rescaled within each phase and group to go from 0 to the number of time points minus 1.

Examples


# produce a design matrix for a hypothetical study with 20 time points,
# 2 phases, and 3 groups
studyDesign <- byPhasebyGroup(NULL, -9:10, 2, 3)
head(studyDesign$data)
studyDesign$dummyNames

# an alternative specification
studyDesign <- byPhasebyGroup(NULL, -9:10, c("baseline", "intervention"),
                 c("treatment", "control"))
head(studyDesign$data)
studyDesign$dummyNames

# create group and phase specific dummy indicators after creating an
# artificial "Group" variable in the Ovary data.
OvaryICT2 <- OvaryICT
OvaryICT2$Group <- as.numeric(OvaryICT2$Mare<7)
OvaryICT2 <- byPhasebyGroup(OvaryICT2, "Time", "Phase", "Group")

# view the new variables, group and phase specific intercepts and slopes
OvaryICT2$dummyNames

# view the right hand side formula of the fixed effects produced by
# `byPhasebyGroup`, noting the `-1` elimination of an overall intercept
OvaryICT2$fixed

# fit a model using the new variables with
# - no intercept
# - no overall time variable
userFormula <- list()
userFormula$fixed <- formula(paste("follicles ~ ", OvaryICT2$fixed)) # add dependent variable
userFormula$random <- ~ Time | Mare

t0 <- PersonAlytic(output      = 'byPhasebyGroup'     ,
                   data        = OvaryICT2$data       ,
                   ids         = "Mare"               ,
                   dvs         = "follicles"          ,
                   time        = "Time"               ,
                   ivs         = OvaryICT2$dummyNames ,
                   package     = "nlme"               ,
                   userFormula = userFormula          ,
                   correlation = "corARMA(p=1)"       ,
                   autoSelect  = list()               ,
                   method      = "ML"                 )
summary(t0)

# note that the following moves the `-1` to the end of the formula, test
# that this still works
userFormula$fixed <- update(formula(paste(" ~ ", OvaryICT2$fixed)), follicles ~ .)
t1 <- PersonAlytic(output      = 'byPhasebyGroup'     ,
                   data        = OvaryICT2$data       ,
                   ids         = "Mare"               ,
                   dvs         = "follicles"          ,
                   time        = "Time"               ,
                   ivs         = OvaryICT2$dummyNames ,
                   package     = "nlme"               ,
                   userFormula = userFormula          ,
                   correlation = "corARMA(p=1)"       ,
                   autoSelect  = list()               ,
                   method      = "ML"                 )
summary(t1)

# if nothing returns, the test passes
testthat::expect_equal(summary(t0)$tTable, summary(t1)$tTable)

## Not run: 

# multiple DVs with no target_ivs and group models
# this fails to implement the userFormula, see https://github.com/ICTatRTI/PersonAlytics/issues/25
t2 <- PersonAlytic(output          = 'MultiDVnoIDnoIV'          ,
                   data            = OvaryICT2$data             ,
                   ids             = "Mare"                     ,
                   dvs             = names(OvaryICT)[c(3,9:11)] ,
                   ivs             = OvaryICT2$dummyNames       ,
                   time            = "Time"                     ,
                   package         = "nlme"                     ,
                   individual_mods = FALSE                      ,
                   target_ivs      = NULL                       ,
                   autoSelect      = list()                     ,
                   userFormula     = userFormula                )



## End(Not run)


ICTatRTI/PersonAlytics documentation built on Dec. 13, 2024, 11:06 p.m.