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)

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

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 make.names) so that they are (the default is TRUE).

append

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

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 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.

Value

A matrix with as many variables as there are groups.

References

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

See also

to.wide for a function to create ‘wide’ format datasets.

dat.senn2013, dat.hasselblad1998, dat.lopez2019 for illustrative examples.

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
#>                    study comment   treatment.1 ni.1  mi.1 sdi.1   treatment.2 ni.2  mi.2 sdi.2 id
#> 1            Alex (1998)  change     metformin  291  0.13 1.428  sulfonylurea  300  0.50 1.450  1
#> 2           Baksi (2004)  change rosiglitazone  218 -1.20 1.112       placebo  233  0.10 1.036  2
#> 3           Costa (1997)  change      acarbose   36 -1.10 0.360       placebo   29 -0.30 0.700  3
#> 4        Davidson (2007)  change rosiglitazone  117 -1.20 1.097       placebo  116  0.14 1.093  4
#> 5       De Fronzo (1995)  change     metformin  213 -1.70 1.459       placebo  209  0.20 1.446  5
#> 6          Derosa (2004)     raw  pioglitazone   45  6.80 0.800 rosiglitazone   42  6.70 0.900  6
#> 7          Garber (2008)  change  vildagliptin  132 -0.63 1.034       placebo  144  0.07 1.080  7
#> 8  Gonzalez-Ortiz (2004)  change     metformin   34 -1.30 1.861       placebo   37 -0.90 1.803  8
#> 9        Hanefeld (2004)     raw     metformin  320  7.45 1.073  pioglitazone  319  7.61 1.072  9
#> 10      Hermansen (2007)  change   sitagliptin  106 -0.30 0.940       placebo  106  0.27 0.940 10
#> 11       Johnston (1994)  change      miglitol   68 -0.41 1.072       placebo   63  0.33 1.032 11
#> 12      Johnston (1998a)  change      miglitol   91 -0.43 0.954       placebo   43  0.98 1.311 12
#> 13      Johnston (1998b)  change      miglitol   49 -0.12 1.400       placebo   34  0.56 1.166 13
#> 14        Kerenyi (2004)  change rosiglitazone  160 -0.91 0.990       placebo  154 -0.14 0.920 14
#> 15            Kim (2007)  change     metformin   56 -1.10 1.139 rosiglitazone   57 -1.10 1.341 15
#> 16         Kipnes (2001)  change  pioglitazone  182 -1.20 1.369       placebo  181  0.10 1.024 16
#> 17          Lewin (2007)  change     metformin  431 -0.74 1.106       placebo  144  0.08 1.004 17
#> 18         Moulin (2006)  change    benfluorex  161 -0.82 1.028       placebo  156  0.19 1.374 18
#> 19          Oyama (2008)  change      acarbose   41 -0.70 0.800  sulfonylurea   43 -0.30 0.600 19
#> 20     Rosenstock (2008)  change rosiglitazone   59 -1.17 1.229       placebo   57 -0.08 1.208 20
#> 21         Stucci (1996)     raw    benfluorex   28  8.03 1.290       placebo   30  8.26 1.350 21
#> 22 Vongthavaravat (2002)  change rosiglitazone  164 -1.10 1.559  sulfonylurea  170  0.10 0.992 22
#> 23         Willms (1999)  change      acarbose   31 -2.30 1.782       placebo   29 -1.30 1.831 23
#> 24         Willms (1999)  change     metformin   29 -2.50 0.862       placebo   29 -1.30 1.831 24
#> 25  Wolffenbuttel (1999)  change rosiglitazone  183 -0.90 1.100       placebo  192  0.20 1.110 25
#> 26           Yang (2003)  change     metformin   96 -0.95 1.500 rosiglitazone  102 -1.09 1.650 26
#> 27            Zhu (2003)  change rosiglitazone  210 -1.90 1.470       placebo  105 -0.40 1.300 27
#>     comp   design
#> 1  me-su    me-su
#> 2  ro-pl    ro-pl
#> 3  ac-pl    ac-pl
#> 4  ro-pl    ro-pl
#> 5  me-pl    me-pl
#> 6  pi-ro    pi-ro
#> 7  vi-pl    vi-pl
#> 8  me-pl    me-pl
#> 9  me-pi    me-pi
#> 10 si-pl    si-pl
#> 11 mi-pl    mi-pl
#> 12 mi-pl    mi-pl
#> 13 mi-pl    mi-pl
#> 14 ro-pl    ro-pl
#> 15 me-ro    me-ro
#> 16 pi-pl    pi-pl
#> 17 me-pl    me-pl
#> 18 be-pl    be-pl
#> 19 ac-su    ac-su
#> 20 ro-pl    ro-pl
#> 21 be-pl    be-pl
#> 22 ro-su    ro-su
#> 23 ac-pl ac-me-pl
#> 24 me-pl ac-me-pl
#> 25 ro-pl    ro-pl
#> 26 me-ro    me-ro
#> 27 ro-pl    ro-pl

### add contrast matrix
dat <- contrmat(dat, grp1="treatment.1", grp2="treatment.2")
dat
#>                    study comment   treatment.1 ni.1  mi.1 sdi.1   treatment.2 ni.2  mi.2 sdi.2 id
#> 1            Alex (1998)  change     metformin  291  0.13 1.428  sulfonylurea  300  0.50 1.450  1
#> 2           Baksi (2004)  change rosiglitazone  218 -1.20 1.112       placebo  233  0.10 1.036  2
#> 3           Costa (1997)  change      acarbose   36 -1.10 0.360       placebo   29 -0.30 0.700  3
#> 4        Davidson (2007)  change rosiglitazone  117 -1.20 1.097       placebo  116  0.14 1.093  4
#> 5       De Fronzo (1995)  change     metformin  213 -1.70 1.459       placebo  209  0.20 1.446  5
#> 6          Derosa (2004)     raw  pioglitazone   45  6.80 0.800 rosiglitazone   42  6.70 0.900  6
#> 7          Garber (2008)  change  vildagliptin  132 -0.63 1.034       placebo  144  0.07 1.080  7
#> 8  Gonzalez-Ortiz (2004)  change     metformin   34 -1.30 1.861       placebo   37 -0.90 1.803  8
#> 9        Hanefeld (2004)     raw     metformin  320  7.45 1.073  pioglitazone  319  7.61 1.072  9
#> 10      Hermansen (2007)  change   sitagliptin  106 -0.30 0.940       placebo  106  0.27 0.940 10
#> 11       Johnston (1994)  change      miglitol   68 -0.41 1.072       placebo   63  0.33 1.032 11
#> 12      Johnston (1998a)  change      miglitol   91 -0.43 0.954       placebo   43  0.98 1.311 12
#> 13      Johnston (1998b)  change      miglitol   49 -0.12 1.400       placebo   34  0.56 1.166 13
#> 14        Kerenyi (2004)  change rosiglitazone  160 -0.91 0.990       placebo  154 -0.14 0.920 14
#> 15            Kim (2007)  change     metformin   56 -1.10 1.139 rosiglitazone   57 -1.10 1.341 15
#> 16         Kipnes (2001)  change  pioglitazone  182 -1.20 1.369       placebo  181  0.10 1.024 16
#> 17          Lewin (2007)  change     metformin  431 -0.74 1.106       placebo  144  0.08 1.004 17
#> 18         Moulin (2006)  change    benfluorex  161 -0.82 1.028       placebo  156  0.19 1.374 18
#> 19          Oyama (2008)  change      acarbose   41 -0.70 0.800  sulfonylurea   43 -0.30 0.600 19
#> 20     Rosenstock (2008)  change rosiglitazone   59 -1.17 1.229       placebo   57 -0.08 1.208 20
#> 21         Stucci (1996)     raw    benfluorex   28  8.03 1.290       placebo   30  8.26 1.350 21
#> 22 Vongthavaravat (2002)  change rosiglitazone  164 -1.10 1.559  sulfonylurea  170  0.10 0.992 22
#> 23         Willms (1999)  change      acarbose   31 -2.30 1.782       placebo   29 -1.30 1.831 23
#> 24         Willms (1999)  change     metformin   29 -2.50 0.862       placebo   29 -1.30 1.831 24
#> 25  Wolffenbuttel (1999)  change rosiglitazone  183 -0.90 1.100       placebo  192  0.20 1.110 25
#> 26           Yang (2003)  change     metformin   96 -0.95 1.500 rosiglitazone  102 -1.09 1.650 26
#> 27            Zhu (2003)  change rosiglitazone  210 -1.90 1.470       placebo  105 -0.40 1.300 27
#>     comp   design acarbose benfluorex metformin miglitol pioglitazone rosiglitazone sitagliptin
#> 1  me-su    me-su        0          0         1        0            0             0           0
#> 2  ro-pl    ro-pl        0          0         0        0            0             1           0
#> 3  ac-pl    ac-pl        1          0         0        0            0             0           0
#> 4  ro-pl    ro-pl        0          0         0        0            0             1           0
#> 5  me-pl    me-pl        0          0         1        0            0             0           0
#> 6  pi-ro    pi-ro        0          0         0        0            1            -1           0
#> 7  vi-pl    vi-pl        0          0         0        0            0             0           0
#> 8  me-pl    me-pl        0          0         1        0            0             0           0
#> 9  me-pi    me-pi        0          0         1        0           -1             0           0
#> 10 si-pl    si-pl        0          0         0        0            0             0           1
#> 11 mi-pl    mi-pl        0          0         0        1            0             0           0
#> 12 mi-pl    mi-pl        0          0         0        1            0             0           0
#> 13 mi-pl    mi-pl        0          0         0        1            0             0           0
#> 14 ro-pl    ro-pl        0          0         0        0            0             1           0
#> 15 me-ro    me-ro        0          0         1        0            0            -1           0
#> 16 pi-pl    pi-pl        0          0         0        0            1             0           0
#> 17 me-pl    me-pl        0          0         1        0            0             0           0
#> 18 be-pl    be-pl        0          1         0        0            0             0           0
#> 19 ac-su    ac-su        1          0         0        0            0             0           0
#> 20 ro-pl    ro-pl        0          0         0        0            0             1           0
#> 21 be-pl    be-pl        0          1         0        0            0             0           0
#> 22 ro-su    ro-su        0          0         0        0            0             1           0
#> 23 ac-pl ac-me-pl        1          0         0        0            0             0           0
#> 24 me-pl ac-me-pl        0          0         1        0            0             0           0
#> 25 ro-pl    ro-pl        0          0         0        0            0             1           0
#> 26 me-ro    me-ro        0          0         1        0            0            -1           0
#> 27 ro-pl    ro-pl        0          0         0        0            0             1           0
#>    sulfonylurea vildagliptin placebo
#> 1            -1            0       0
#> 2             0            0      -1
#> 3             0            0      -1
#> 4             0            0      -1
#> 5             0            0      -1
#> 6             0            0       0
#> 7             0            1      -1
#> 8             0            0      -1
#> 9             0            0       0
#> 10            0            0      -1
#> 11            0            0      -1
#> 12            0            0      -1
#> 13            0            0      -1
#> 14            0            0      -1
#> 15            0            0       0
#> 16            0            0      -1
#> 17            0            0      -1
#> 18            0            0      -1
#> 19           -1            0       0
#> 20            0            0      -1
#> 21            0            0      -1
#> 22           -1            0       0
#> 23            0            0      -1
#> 24            0            0      -1
#> 25            0            0      -1
#> 26            0            0       0
#> 27            0            0      -1

### data in long format
dat <- dat.hasselblad1998
dat
#>    id study            authors year            trt  xi   ni
#> 1   1     1        Reid et al. 1974     no_contact  75  731
#> 2   2     1        Reid et al. 1974 ind_counseling 363  714
#> 3   3     2    Cottraux et al. 1983     no_contact   9  140
#> 4   4     2    Cottraux et al. 1983 ind_counseling  23  140
#> 5   5     2    Cottraux et al. 1983 grp_counseling  10  138
#> 6   6     3       Slama et al. 1990     no_contact   2  106
#> 7   7     3       Slama et al. 1990 ind_counseling   9  205
#> 8   8     4    Jamrozik et al. 1984     no_contact  58  549
#> 9   9     4    Jamrozik et al. 1984 ind_counseling 237 1561
#> 10 10     5      Rabkin et al. 1984     no_contact   0   33
#> 11 11     5      Rabkin et al. 1984 ind_counseling   9   48
#> 12 12     6   Decker and Evans 1989      self_help  20   49
#> 13 13     6   Decker and Evans 1989 ind_counseling  16   43
#> 14 14     7    Richmond et al. 1986     no_contact   3  100
#> 15 15     7    Richmond et al. 1986 ind_counseling  31   98
#> 16 16     8              Leung 1991     no_contact   1   31
#> 17 17     8              Leung 1991 ind_counseling  26   95
#> 18 18     9  Mothersill et al. 1988      self_help  11   78
#> 19 19     9  Mothersill et al. 1988 ind_counseling  12   85
#> 20 20     9  Mothersill et al. 1988 grp_counseling  29  170
#> 21 21    10    Langford et al. 1983     no_contact   6   39
#> 22 22    10    Langford et al. 1983 ind_counseling  17   77
#> 23 23    11       Gritz et al. 1992     no_contact  79  702
#> 24 24    11       Gritz et al. 1992      self_help  77  694
#> 25 25    12    Campbell et al. 1986     no_contact  18  671
#> 26 26    12    Campbell et al. 1986      self_help  21  535
#> 27 27    13     Sanders et al. 1989     no_contact  64  642
#> 28 28    13     Sanders et al. 1989 ind_counseling 107  761
#> 29 29    14    Hilleman et al. 1993 ind_counseling  12   76
#> 30 30    14    Hilleman et al. 1993 grp_counseling  20   74
#> 31 31    15     Gillams et al. 1984 ind_counseling   9   55
#> 32 32    15     Gillams et al. 1984 grp_counseling   3   26
#> 33 33    16 Mogielnicki et al. 1986      self_help   7   66
#> 34 34    16 Mogielnicki et al. 1986 grp_counseling  32  127
#> 35 35    17        Page et al. 1986     no_contact   5   62
#> 36 36    17        Page et al. 1986 ind_counseling   8   90
#> 37 37    18    Vetter and Ford 1990     no_contact  20  234
#> 38 38    18    Vetter and Ford 1990 ind_counseling  34  237
#> 39 39    19  Williams and Hall 1988     no_contact   0   20
#> 40 40    19  Williams and Hall 1988 grp_counseling   9   20
#> 41 41    20    Pallonen et al. 1994     no_contact   8  116
#> 42 42    20    Pallonen et al. 1994      self_help  19  149
#> 43 43    21     Russell et al. 1983     no_contact  95 1107
#> 44 44    21     Russell et al. 1983 ind_counseling 143 1031
#> 45 45    22 Stewart and Rosser 1982     no_contact  15  187
#> 46 46    22 Stewart and Rosser 1982 ind_counseling  36  504
#> 47 47    23     Russell et al. 1979     no_contact  78  584
#> 48 48    23     Russell et al. 1979 ind_counseling  73  675
#> 49 49    24    Kendrick et al. 1995     no_contact  69 1177
#> 50 50    24    Kendrick et al. 1995 ind_counseling  54  888

### restructure to wide format
dat <- to.wide(dat, study="study", grp="trt", ref="no_contact", grpvars=6:7)
dat
#>    id study            authors year          trt.1 xi.1 ni.1          trt.2 xi.2 ni.2  comp   design
#> 1   1     1        Reid et al. 1974 ind_counseling  363  714     no_contact   75  731 in-no    in-no
#> 2   2     2    Cottraux et al. 1983 grp_counseling   10  138     no_contact    9  140 gr-no gr-in-no
#> 3   3     2    Cottraux et al. 1983 ind_counseling   23  140     no_contact    9  140 in-no gr-in-no
#> 4   4     3       Slama et al. 1990 ind_counseling    9  205     no_contact    2  106 in-no    in-no
#> 5   5     4    Jamrozik et al. 1984 ind_counseling  237 1561     no_contact   58  549 in-no    in-no
#> 6   6     5      Rabkin et al. 1984 ind_counseling    9   48     no_contact    0   33 in-no    in-no
#> 7   7     6   Decker and Evans 1989 ind_counseling   16   43      self_help   20   49 in-se    in-se
#> 8   8     7    Richmond et al. 1986 ind_counseling   31   98     no_contact    3  100 in-no    in-no
#> 9   9     8              Leung 1991 ind_counseling   26   95     no_contact    1   31 in-no    in-no
#> 10 10     9  Mothersill et al. 1988 grp_counseling   29  170      self_help   11   78 gr-se gr-in-se
#> 11 11     9  Mothersill et al. 1988 ind_counseling   12   85      self_help   11   78 in-se gr-in-se
#> 12 12    10    Langford et al. 1983 ind_counseling   17   77     no_contact    6   39 in-no    in-no
#> 13 13    11       Gritz et al. 1992      self_help   77  694     no_contact   79  702 se-no    se-no
#> 14 14    12    Campbell et al. 1986      self_help   21  535     no_contact   18  671 se-no    se-no
#> 15 15    13     Sanders et al. 1989 ind_counseling  107  761     no_contact   64  642 in-no    in-no
#> 16 16    14    Hilleman et al. 1993 grp_counseling   20   74 ind_counseling   12   76 gr-in    gr-in
#> 17 17    15     Gillams et al. 1984 grp_counseling    3   26 ind_counseling    9   55 gr-in    gr-in
#> 18 18    16 Mogielnicki et al. 1986 grp_counseling   32  127      self_help    7   66 gr-se    gr-se
#> 19 19    17        Page et al. 1986 ind_counseling    8   90     no_contact    5   62 in-no    in-no
#> 20 20    18    Vetter and Ford 1990 ind_counseling   34  237     no_contact   20  234 in-no    in-no
#> 21 21    19  Williams and Hall 1988 grp_counseling    9   20     no_contact    0   20 gr-no    gr-no
#> 22 22    20    Pallonen et al. 1994      self_help   19  149     no_contact    8  116 se-no    se-no
#> 23 23    21     Russell et al. 1983 ind_counseling  143 1031     no_contact   95 1107 in-no    in-no
#> 24 24    22 Stewart and Rosser 1982 ind_counseling   36  504     no_contact   15  187 in-no    in-no
#> 25 25    23     Russell et al. 1979 ind_counseling   73  675     no_contact   78  584 in-no    in-no
#> 26 26    24    Kendrick et al. 1995 ind_counseling   54  888     no_contact   69 1177 in-no    in-no

### add contrast matrix
dat <- contrmat(dat, grp1="trt.1", grp2="trt.2", shorten=TRUE, minlen=3)
dat
#>    id study            authors year          trt.1 xi.1 ni.1          trt.2 xi.2 ni.2  comp   design
#> 1   1     1        Reid et al. 1974 ind_counseling  363  714     no_contact   75  731 in-no    in-no
#> 2   2     2    Cottraux et al. 1983 grp_counseling   10  138     no_contact    9  140 gr-no gr-in-no
#> 3   3     2    Cottraux et al. 1983 ind_counseling   23  140     no_contact    9  140 in-no gr-in-no
#> 4   4     3       Slama et al. 1990 ind_counseling    9  205     no_contact    2  106 in-no    in-no
#> 5   5     4    Jamrozik et al. 1984 ind_counseling  237 1561     no_contact   58  549 in-no    in-no
#> 6   6     5      Rabkin et al. 1984 ind_counseling    9   48     no_contact    0   33 in-no    in-no
#> 7   7     6   Decker and Evans 1989 ind_counseling   16   43      self_help   20   49 in-se    in-se
#> 8   8     7    Richmond et al. 1986 ind_counseling   31   98     no_contact    3  100 in-no    in-no
#> 9   9     8              Leung 1991 ind_counseling   26   95     no_contact    1   31 in-no    in-no
#> 10 10     9  Mothersill et al. 1988 grp_counseling   29  170      self_help   11   78 gr-se gr-in-se
#> 11 11     9  Mothersill et al. 1988 ind_counseling   12   85      self_help   11   78 in-se gr-in-se
#> 12 12    10    Langford et al. 1983 ind_counseling   17   77     no_contact    6   39 in-no    in-no
#> 13 13    11       Gritz et al. 1992      self_help   77  694     no_contact   79  702 se-no    se-no
#> 14 14    12    Campbell et al. 1986      self_help   21  535     no_contact   18  671 se-no    se-no
#> 15 15    13     Sanders et al. 1989 ind_counseling  107  761     no_contact   64  642 in-no    in-no
#> 16 16    14    Hilleman et al. 1993 grp_counseling   20   74 ind_counseling   12   76 gr-in    gr-in
#> 17 17    15     Gillams et al. 1984 grp_counseling    3   26 ind_counseling    9   55 gr-in    gr-in
#> 18 18    16 Mogielnicki et al. 1986 grp_counseling   32  127      self_help    7   66 gr-se    gr-se
#> 19 19    17        Page et al. 1986 ind_counseling    8   90     no_contact    5   62 in-no    in-no
#> 20 20    18    Vetter and Ford 1990 ind_counseling   34  237     no_contact   20  234 in-no    in-no
#> 21 21    19  Williams and Hall 1988 grp_counseling    9   20     no_contact    0   20 gr-no    gr-no
#> 22 22    20    Pallonen et al. 1994      self_help   19  149     no_contact    8  116 se-no    se-no
#> 23 23    21     Russell et al. 1983 ind_counseling  143 1031     no_contact   95 1107 in-no    in-no
#> 24 24    22 Stewart and Rosser 1982 ind_counseling   36  504     no_contact   15  187 in-no    in-no
#> 25 25    23     Russell et al. 1979 ind_counseling   73  675     no_contact   78  584 in-no    in-no
#> 26 26    24    Kendrick et al. 1995 ind_counseling   54  888     no_contact   69 1177 in-no    in-no
#>    grp ind sel no_
#> 1    0   1   0  -1
#> 2    1   0   0  -1
#> 3    0   1   0  -1
#> 4    0   1   0  -1
#> 5    0   1   0  -1
#> 6    0   1   0  -1
#> 7    0   1  -1   0
#> 8    0   1   0  -1
#> 9    0   1   0  -1
#> 10   1   0  -1   0
#> 11   0   1  -1   0
#> 12   0   1   0  -1
#> 13   0   0   1  -1
#> 14   0   0   1  -1
#> 15   0   1   0  -1
#> 16   1  -1   0   0
#> 17   1  -1   0   0
#> 18   1   0  -1   0
#> 19   0   1   0  -1
#> 20   0   1   0  -1
#> 21   1   0   0  -1
#> 22   0   0   1  -1
#> 23   0   1   0  -1
#> 24   0   1   0  -1
#> 25   0   1   0  -1
#> 26   0   1   0  -1