Learn R Programming

Publish (version 2017.10.24)

parseInteractionTerms: Parse interaction terms

Description

Parse interaction terms for regression tables

Usage

parseInteractionTerms(terms, xlevels, units, format.factor, format.contrast,
  format.scale, format.scale.unit, sep = ": ", ...)

Arguments

terms

Terms of a formula

xlevels

Factor levels corresponding to the variables in terms

units

named list with unit labels. names should match variable names in formula.

format.factor

For categorical variables. A string which specifies the format for factor labels. The string should contain the keywords "var" and "level" which will be replaced by the name of the variable and the current level, respectively. Default is "var(level)".

format.contrast

For categorical variables. A string which specifies the format for constrast statements. The string should contain the keywords "var", "level" and "ref" which will be replaced by the name of the variable, the current level and the reference level, respectively.

format.scale

For continuous variables. For categorical variables. A string which specifies the format for factor labels. The string should contain the keywords "var" and "level" which will be replaced by the name of the variable and the current level, respectively. Default is "var(level)".

format.scale.unit

For continuous variables which have a unit. A string which specifies the format for factor labels. The string should contain the keywords "var" and "unit" which will be replaced by the name of the variable and the unit, respectively. Default is "var(unit)".

sep

a character string to separate the terms. Default is ": ".

...

Not yet used

Value

List of contrasts which can be passed to lava::estimate.

Details

Prepare a list of contrasts which combines regression coefficients to describe statistical interactions.

See Also

lava::estimate

Examples

Run this code
# NOT RUN {
tt <- terms(formula(SBP~age+sex*BMI))
xlev <- list(sex=c("male","female"),BMI=c("normal","overweight","obese"))
parseInteractionTerms(terms=tt,xlevels=xlev)
parseInteractionTerms(terms=tt,xlevels=xlev,format.factor="var level")
parseInteractionTerms(terms=tt,xlevels=xlev,format.contrast="var(level:ref)")

tt2 <- terms(formula(SBP~age*factor(sex)+BMI))
xlev2 <- list("factor(sex)"=c("male","female"))
parseInteractionTerms(terms=tt2,xlevels=xlev2)
parseInteractionTerms(terms=tt2,xlevels=xlev2,units=list(age="yrs"))


data(Diabetes)
fit <- glm(bp.2s~age*factor(gender)+BMI,data=Diabetes)
parseInteractionTerms(terms=terms(fit$formula),xlevels=fit$xlevels,
                      format.scale="var -- level:ref",units=list("age"='years'))
parseInteractionTerms(terms=terms(fit$formula),xlevels=fit$xlevels,
                      format.scale.unit="var -- level:ref",units=list("age"='years'))
it <- parseInteractionTerms(terms=terms(fit$formula),xlevels=fit$xlevels)
ivars <- unlist(lapply(it,function(x)attr(x,"variables")))
lava::estimate(fit,function(p)lapply(unlist(it),eval,envir=sys.parent(-1)))


# }

Run the code above in your browser using DataLab