Title: | A Collection of Contrast Methods |
---|---|
Description: | One degree of freedom contrasts for 'lm', 'glm', 'gls', and 'geese' objects. |
Authors: | Alan O'Callaghan [cre], Max Kuhn [aut], Steve Weston [aut], Jed Wing [aut], James Forester [aut], Thorn Thaler [aut] |
Maintainer: | Alan O'Callaghan <[email protected]> |
License: | GPL-2 |
Version: | 0.24.2 |
Built: | 2025-02-16 03:01:33 UTC |
Source: | https://github.com/alanocallaghan/contrast |
This function computes one or more contrasts of the estimated regression coefficients in a fit from one of the functions in Design, along with standard errors, confidence limits, t or Z statistics, P-values.
## S3 method for class 'lm' contrast(fit, ...) ## S3 method for class 'gls' contrast(fit, ...) ## S3 method for class 'lme' contrast(fit, ...) ## S3 method for class 'geese' contrast(fit, ...) contrast_calc( fit, a, b, cnames = NULL, type = c("individual", "average"), weights = "equal", conf.int = 0.95, fcType = "simple", fcFunc = I, covType = NULL, ..., env = parent.frame(2) )
## S3 method for class 'lm' contrast(fit, ...) ## S3 method for class 'gls' contrast(fit, ...) ## S3 method for class 'lme' contrast(fit, ...) ## S3 method for class 'geese' contrast(fit, ...) contrast_calc( fit, a, b, cnames = NULL, type = c("individual", "average"), weights = "equal", conf.int = 0.95, fcType = "simple", fcFunc = I, covType = NULL, ..., env = parent.frame(2) )
fit |
A fit of class |
... |
For |
a , b
|
Lists containing conditions for all predictors in the model that
will be contrasted to form the hypothesis |
cnames |
A vector of character strings naming the contrasts when
|
type |
A character string. Set |
weights |
A numeric vector, used when |
conf.int |
The confidence level for confidence intervals for the contrasts. |
fcType |
A character string: "simple", "log" or "signed". |
fcFunc |
A function to transform the numerator and denominator of fold changes. |
covType |
A string matching the method for estimating the covariance
matrix. The default value produces the typical estimate. See
|
env |
An environment in which evaluate fit. |
These functions mirror rms::contrast.rms()
but have fewer options.
There are some between-package inconsistencies regarding degrees of freedom in some models. See the package vignette for more details.
Fold changes are calculated for each hypothesis. When fcType ="simple"
, the
ratio of the a
group predictions over the b
group predictions are used. When fcType = "signed"
, the ratio is used
if it is greater than 1; otherwise the negative inverse (e.g.,
-1/ratio
) is returned.
a list of class contrast.Design
containing the elements
Contrast
, SE
, Z
, var
, df.residual
Lower
, Upper
, Pvalue
, X
,
cnames
, and foldChange
, which denote the contrast estimates, standard
errors, Z or t-statistics, variance matrix, residual degrees of freedom
(this is NULL
if the model was not ols
), lower and upper confidence
limits, 2-sided P-value, design matrix, and contrast names (or NULL
).
rms::contrast.rms()
, sandwich::vcovHC()
library(nlme) Orthodont2 <- Orthodont Orthodont2$newAge <- Orthodont$age - 11 fm1Orth.lme2 <- lme(distance ~ Sex * newAge, data = Orthodont2, random = ~ newAge | Subject ) summary(fm1Orth.lme2) contrast(fm1Orth.lme2, a = list(Sex = levels(Orthodont2$Sex), newAge = 8 - 11), b = list(Sex = levels(Orthodont2$Sex), newAge = 10 - 11) ) # --------------------------------------------------------------------------- anova_model <- lm(expression ~ diet * group, data = two_factor_crossed) anova(anova_model) library(ggplot2) theme_set(theme_bw() + theme(legend.position = "top")) ggplot(two_factor_crossed) + aes(x = diet, y = expression, col = group, shape = group) + geom_point() + geom_smooth(aes(group = group), method = lm, se = FALSE) int_model <- lm(expression ~ diet * group, data = two_factor_crossed) main_effects <- lm(expression ~ diet + group, data = two_factor_crossed) # Interaction effect is probably real: anova(main_effects, int_model) # Test treatment in low fat diet: veh_group <- list(diet = "low fat", group = "vehicle") trt_group <- list(diet = "low fat", group = "treatment") contrast(int_model, veh_group, trt_group) # --------------------------------------------------------------------------- car_mod <- lm(mpg ~ am + wt, data = mtcars) print(summary(car_mod), digits = 5) mean_wt <- mean(mtcars$wt) manual_trans <- list(am = 0, wt = mean_wt) auto_trans <- list(am = 1, wt = mean_wt) print(contrast(car_mod, manual_trans, auto_trans), digits = 5)
library(nlme) Orthodont2 <- Orthodont Orthodont2$newAge <- Orthodont$age - 11 fm1Orth.lme2 <- lme(distance ~ Sex * newAge, data = Orthodont2, random = ~ newAge | Subject ) summary(fm1Orth.lme2) contrast(fm1Orth.lme2, a = list(Sex = levels(Orthodont2$Sex), newAge = 8 - 11), b = list(Sex = levels(Orthodont2$Sex), newAge = 10 - 11) ) # --------------------------------------------------------------------------- anova_model <- lm(expression ~ diet * group, data = two_factor_crossed) anova(anova_model) library(ggplot2) theme_set(theme_bw() + theme(legend.position = "top")) ggplot(two_factor_crossed) + aes(x = diet, y = expression, col = group, shape = group) + geom_point() + geom_smooth(aes(group = group), method = lm, se = FALSE) int_model <- lm(expression ~ diet * group, data = two_factor_crossed) main_effects <- lm(expression ~ diet + group, data = two_factor_crossed) # Interaction effect is probably real: anova(main_effects, int_model) # Test treatment in low fat diet: veh_group <- list(diet = "low fat", group = "vehicle") trt_group <- list(diet = "low fat", group = "treatment") contrast(int_model, veh_group, trt_group) # --------------------------------------------------------------------------- car_mod <- lm(mpg ~ am + wt, data = mtcars) print(summary(car_mod), digits = 5) mean_wt <- mean(mtcars$wt) manual_trans <- list(am = 0, wt = mean_wt) auto_trans <- list(am = 1, wt = mean_wt) print(contrast(car_mod, manual_trans, auto_trans), digits = 5)
Print a Contrast Object
## S3 method for class 'contrast' print(x, X = FALSE, fun = function(u) u, ...)
## S3 method for class 'contrast' print(x, X = FALSE, fun = function(u) u, ...)
x |
Result of |
X |
A logical: set |
fun |
A function to transform the contrast, SE, and lower and upper
confidence limits before printing. For example, specify |
... |
Not used. |
Complete Two-Factor Experiment
A gene expression experiment was run to assess the effect of a compound
under two different diets: high fat and low fat. The main comparisons of
interest are the difference between the treated and untreated groups within
a diet. The interaction effect was a secondary hypothesis. For illustration,
we only include the expression value of one of the genes. The study design
was a full two-way factorial with n = 24
samples.
two_factor_crossed |
A data frame |
two_factor_crossed
two_factor_crossed
Incomplete Two-Factor Experiment with Repeated Measurments
In a gene expression experiment, stem cells were differentiated using a set of factors (such as media types, cell spreads etc.). These factors were collapsed into a single cell environment configurations variable. The cell lines were assays over three days. Two of the configurations were only run on the first day and the other two were assays at baseline.
To get the materials, three donors provided materials. These donors provided (almost) equal replication across the two experimental factors (day and configuration).
One of the goals of this experiment was to assess pre-specified
differences in the configuration at each time point. For example, the
differences between configurations A
and B
at day one is of interest.
Also, the differences between configurations C
and D
at each time points
were important.
Since there are missing cells in the design, it is not a complete two-way factorial. One way to analyze this experiment is to further collapse the time and configuration data into a single variable and then specify each comparison using this factor.
two_factor_incompl |
A data frame |
two_factor_incompl
two_factor_incompl