contrmat | R Documentation |
Function to construct a matrix that indicates which two groups have been contrasted against each other in each row of a dataset.
contrmat(data, grp1, grp2, last, shorten=FALSE, minlen=2, check=TRUE, append=TRUE)
data |
a data frame in wide format. |
grp1 |
either the name (given as a character string) or the position (given as a single number) of the first group variable in the data frame. |
grp2 |
either the name (given as a character string) or the position (given as a single number) of the second group variable in the data frame. |
last |
optional character string to specify which group will be placed in the last column of the matrix (must be one of the groups in the group variables). If not given, the most frequently occurring second group is placed last. |
shorten |
logical to specify whether the variable names corresponding to the group names should be shortened (the default is |
minlen |
integer to specify the minimum length of the shortened variable names (the default is 2). |
check |
logical to specify whether the variables names should be checked to ensure that they are syntactically valid variable names and if not, they are adjusted (by |
append |
logical to specify whether the contrast matrix should be appended to the data frame specified via the |
The function can be used to construct a matrix that indicates which two groups have been contrasted against each other in each row of a data frame (with 1
for the first group, -1
for the second group, and 0
otherwise).
The grp1
and grp2
arguments are used to specify the group variables in the dataset (either as character strings or as numbers indicating the column positions of these variables in the dataset). Optional argument last
is used to specify which group will be placed in the last column of the matrix.
If shorten=TRUE
, the variable names corresponding to the group names are shortened (to at least minlen
; the actual length might be longer to ensure uniqueness of the variable names).
The examples below illustrate the use of this function.
A matrix with as many variables as there are groups.
Wolfgang Viechtbauer (wvb@metafor-project.org, https://www.metafor-project.org).
Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. Journal of Statistical Software, 36(3), 1–48. https://doi.org/10.18637/jss.v036.i03
to.wide
for a function to create ‘wide’ format datasets.
dat.senn2013
, dat.hasselblad1998
, dat.lopez2019
for illustrative examples.
### restructure to wide format
dat <- dat.senn2013
dat <- dat[c(1,4,3,2,5,6)]
dat <- to.wide(dat, study="study", grp="treatment", ref="placebo", grpvars=4:6)
dat
### add contrast matrix
dat <- contrmat(dat, grp1="treatment.1", grp2="treatment.2")
dat
### data in long format
dat <- dat.hasselblad1998
dat
### restructure to wide format
dat <- to.wide(dat, study="study", grp="trt", ref="no_contact", grpvars=6:7)
dat
### add contrast matrix
dat <- contrmat(dat, grp1="trt.1", grp2="trt.2", shorten=TRUE, minlen=3)
dat
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.