metafor (version 2.4-0)

contrmat: Construct Contrast Matrix for Two-Group Comparisons

Description

The function constructs a matrix that indicates which two groups have been contrasted against each other in each row of the dataset.

Usage

contrmat(data, grp1, grp2, last, shorten=FALSE, minlen=2, append=TRUE)

Arguments

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 indicating whether the variable names corresponding to the group names should be shortened (default is FALSE).

minlen

integer indicating the minimum length of the shortened variable names (2 by default).

append

logical indicating whether the contrast matrix should be appended to the data frame specified via the data argument (default is TRUE). If append=FALSE, only the contrast matrix is returned.

Value

A matrix with as many variables as there are groups.

Details

The function can be used to construct a matrix that indicates which two groups have been contrasted against each other in each row of the 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.

References

Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. Journal of Statistical Software, 36(3), 1--48. https://www.jstatsoft.org/v036/i03.

See Also

to.wide, dat.senn2013, dat.hasselblad1998

Examples

Run this code
# NOT RUN {
### 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)
dat
# }

Run the code above in your browser using DataLab