geeCRT package

Hengshi Yu, Fan Li, Paul Rathouz, Elizabeth L. Turner, John Preisser

2021-04-14

Overview

geeCRT is an R package for implementing the bias-corrected generalized estimating equations in analyzing cluster randomized trials.

Population-averaged models have been increasingly used in the design and analysis of cluster randomized trials (CRTs). To facilitate the applications of population-averaged models in CRTs, we implement the generalized estimating equations (GEE) and matrix-adjusted estimating equations (MAEE) approaches to jointly estimate the marginal mean models correlation models both for general CRTs and stepped wedge CRTs.

Despite the general GEE/MAEE approach, we also implement a fast cluster-period GEE method specifically for stepped wedge CRTs with large and variable cluster-period sizes. The individual-level GEE/MAEE approach becomes computationally infeasible in this setting due to inversion of high-dimensional covariance matrices and the enumeration of a high-dimensional design matrix for the correlation estimation equations. The package gives a simple and efficient estimating equations approach based on the cluster-period means to estimate the intervention effects as well as correlation parameters.

In addition, the package also provides functions for generating correlated binary data with specific mean vector and correlation matrix based on the multivariate probit method (Emrich and Piedmonte 1991) or the conditional linear family method (Qaqish 2003). These two functions facilitate generating correlated binary data in future simulation studies.

geemaee() example: matrix-adjusted GEE for estimating the mean and correlation parameters in CRTs

The geemaee() function implements the matrix-adjusted GEE or regular GEE developed for analyzing cluster randomized trials (CRTs). It provides valid estimation and inference for the treatment effect and intraclass correlation parameters within the population-averaged modeling framework. The program allows for flexible marginal mean model specifications. The program also offers bias-corrected intraclass correlation coefficient (ICC) estimates as well as bias-corrected sandwich variances for both the treatment effect parameter and the ICC parameters. The technical details of the matrix-adjusted GEE approach are provided in (Preisser, Lu, and Qaqish 2008) and (Li, Turner, and Preisser 2018).

For the individual-level data, we use the geemaee() function to estimate the marginal mean and correlation parameters in CRTs. We use two simulated stepped wedge CRT datasets with true nested exchangeable correlation structure to illustrate the geemaee() function examples.

Simulated dataset with 12 clusters and 4 periods.

period1 period2 period3 period4 treatment id period y_bin y_con
1 0 0 0 0 1 1 0 -0.5728721
1 0 0 0 0 1 1 0 1.0462372
1 0 0 0 0 1 1 1 0.6033255
1 0 0 0 0 1 1 0 -0.8680703
1 0 0 0 0 1 1 0 -1.8845704
0 1 0 0 1 1 2 0 -0.5572751

We first create an auxiliary function createzCrossSec() to help create the design matrix for the estimating equations of the correlation parameters. We then collect design matrix X for the mean parameters with five period indicators and the treatment indicator.

createzCrossSec = function (m) {

    Z = NULL
    n = dim(m)[1]
    
    for (i in 1:n) {
        
        alpha_0 = 1; alpha_1 = 2; n_i = c(m[i, ]); n_length = length(n_i)
        POS = matrix(alpha_1, sum(n_i), sum(n_i))
        loc1 = 0; loc2 = 0
        
        for (s in 1:n_length) {
            
            n_t = n_i[s]; loc1 = loc2 + 1; loc2 = loc1 + n_t - 1
            
            for (k in loc1:loc2) {

                for (j in loc1:loc2) {

                    if (k != j) { POS[k, j] = alpha_0 } else { POS[k, j] = 0 }}}}

        zrow = diag(2); z_c = NULL
        
        for (j in 1:(sum(n_i) - 1)) { 

            for (k in (j + 1):sum(n_i)) {z_c = rbind(z_c, zrow[POS[j,k],])}}
        
        Z = rbind(Z, z_c) }

    return(Z)}

We implement the geemaee() function on both the continuous outcome and binary outcome, and consider both matrix-adjusted estimating equations (MAEE) with alpadj = TRUE and uncorrected generalized estimating equations (GEE) with alpadj = FALSE. For the shrink argument, we use the "ALPHA" method to tune step sizes and focus on using estimated variances in the correlation estimating equations rather than using unit variances by specifying makevone = FALSE.

sampleSWCRT = sampleSWCRTSmall

### Individual-level id, period, outcome, and design matrix
id = sampleSWCRT$id; period = sampleSWCRT$period;
X = as.matrix(sampleSWCRT[, c('period1', 'period2', 'period3', 'period4', 'treatment')])
m = as.matrix(table(id, period)); n = dim(m)[1]; t = dim(m)[2]

### design matrix for correlation parameters
Z = createzCrossSec(m) 

### (1) Matrix-adjusted estimating equations and GEE 
### on continous outcome with nested exchangeable correlation structure
 
### MAEE
est_maee_ind_con = geemaee(y = sampleSWCRT$y_con, 
                           X = X, id  = id, Z = Z, 
                           family = "continuous", 
                           maxiter = 500, epsilon = 0.001, 
                           printrange = TRUE, alpadj = TRUE, 
                           shrink = "ALPHA", makevone = FALSE)

### GEE
est_uee_ind_con = geemaee(y = sampleSWCRT$y_con, 
                          X = X, id = id, Z = Z, 
                          family = "continuous", 
                          maxiter = 500, epsilon = 0.001, 
                          printrange = TRUE, alpadj = FALSE, 
                          shrink = "ALPHA", makevone = FALSE)

### (2) Matrix-adjusted estimating equations and GEE 
### on binary outcome with nested exchangeable correlation structure

### MAEE
est_maee_ind_bin = geemaee(y = sampleSWCRT$y_bin, 
                           X = X, id = id, Z = Z, 
                           family = "binomial", 
                           maxiter = 500, epsilon = 0.001, 
                           printrange = TRUE, alpadj = TRUE, 
                           shrink = "ALPHA", makevone = FALSE)
print(est_maee_ind_bin)
## GEE for correlated Gaussian data 
##  Number of Iterations: 7 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7731496 0.2861811  0.2397046  0.2507701  0.2623524  0.2528516
## [2,]    1 -0.5067925 0.3163229  0.3125495  0.3364462  0.3626966  0.3340832
## [3,]    2 -0.6159656 0.4107850  0.5793391  0.6398733  0.7076154  0.6341025
## [4,]    3 -0.5932682 0.5956314  0.5498687  0.6043812  0.6650312  0.6087172
## [5,]    4 -0.9959081 0.4904630  0.5373051  0.5918909  0.6525862  0.5889413
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.09615638 0.03212815 0.03375024 0.03546144 0.03373575
## [2,]     1 0.03369215 0.03077853 0.03223000 0.03375486 0.03224127
### GEE
est_uee_ind_bin = geemaee(y = sampleSWCRT$y_bin, 
                          X = X, id = id, Z = Z, 
                          family = "binomial", 
                          maxiter = 500, epsilon = 0.001, 
                          printrange = TRUE, alpadj = FALSE, 
                          shrink = "ALPHA", makevone = FALSE)

Then we have the following output:

 # MAEE for continuous outcome
 print(est_maee_ind_con)
## GEE for correlated Gaussian data 
##  Number of Iterations: 6 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0  0.1563400 0.1233724 0.11343910  0.1185200  0.1238356  0.1185676
## [2,]    1  0.2598417 0.1396584 0.15564306  0.1665805  0.1783248  0.1655108
## [3,]    2  0.2750434 0.1744216 0.09769357  0.1056224  0.1143643  0.1037000
## [4,]    3  0.3265847 0.2269849 0.20527795  0.2202021  0.2363784  0.2191884
## [5,]    4 -0.1846654 0.1916254 0.14636555  0.1592412  0.1733169  0.1587060
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.07335831  0.0385460 0.04035981 0.04226387 0.04042324
## [2,]     1 0.03210581  0.0160329 0.01683282 0.01767569 0.01684501
 # GEE for continuous outcome
 print(est_uee_ind_con)
## GEE for correlated Gaussian data 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0  0.1558990 0.1174052 0.11335326  0.1184565  0.1237977  0.1185069
## [2,]    1  0.2600341 0.1331291 0.15660456  0.1676591  0.1795345  0.1666850
## [3,]    2  0.2744039 0.1658703 0.09732366  0.1052335  0.1139553  0.1035272
## [4,]    3  0.3294253 0.2163095 0.20723038  0.2223924  0.2388282  0.2219057
## [5,]    4 -0.1857484 0.1828737 0.14726062  0.1602631  0.1744810  0.1599123
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.05304519 0.03055184 0.03200002 0.03352079 0.03204680
## [2,]     1 0.03019984 0.01459571 0.01532844 0.01610073 0.01533637
 # MAEE for binary outcome
 print(est_maee_ind_bin)
## GEE for correlated Gaussian data 
##  Number of Iterations: 7 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7731496 0.2861811  0.2397046  0.2507701  0.2623524  0.2528516
## [2,]    1 -0.5067925 0.3163229  0.3125495  0.3364462  0.3626966  0.3340832
## [3,]    2 -0.6159656 0.4107850  0.5793391  0.6398733  0.7076154  0.6341025
## [4,]    3 -0.5932682 0.5956314  0.5498687  0.6043812  0.6650312  0.6087172
## [5,]    4 -0.9959081 0.4904630  0.5373051  0.5918909  0.6525862  0.5889413
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.09615638 0.03212815 0.03375024 0.03546144 0.03373575
## [2,]     1 0.03369215 0.03077853 0.03223000 0.03375486 0.03224127
 # GEE for binary outcome
 print(est_uee_ind_bin)
## GEE for correlated Gaussian data 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7716253 0.2693959  0.2404336  0.2516128  0.2633193  0.2537330
## [2,]    1 -0.5040824 0.2983658  0.3111895  0.3349180  0.3609889  0.3328046
## [3,]    2 -0.6329007 0.3872258  0.5796856  0.6406367  0.7089294  0.6350176
## [4,]    3 -0.5905097 0.5625779  0.5551809  0.6102428  0.6715582  0.6149354
## [5,]    4 -0.9942493 0.4649278  0.5404357  0.5954360  0.6566581  0.5926395
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.06909291 0.02771682 0.02909958 0.03055802 0.02908517
## [2,]     1 0.03017185 0.02749985 0.02879600 0.03015777 0.02880640

Simulated dataset with 12 clusters and 5 periods

period1 period2 period3 period4 period5 treatment id period y_bin y_con
1 0 0 0 0 0 1 1 1 0.6789756
1 0 0 0 0 0 1 1 1 0.3776961
1 0 0 0 0 0 1 1 1 -0.5519475
1 0 0 0 0 0 1 1 1 -1.3102658
1 0 0 0 0 0 1 1 1 0.2654200
1 0 0 0 0 0 1 1 1 -0.3327958
########################################################################
### Example 2): simulated SW-CRT with larger cluster-period sizes (20~30)
########################################################################
## This will elapse longer. 
sampleSWCRT = sampleSWCRTLarge

### Individual-level id, period, outcome, and design matrix
id = sampleSWCRT$id; period =  sampleSWCRT$period;
X = as.matrix(sampleSWCRT[, c('period1', 'period2', 'period3', 'period4', 'period5', 'treatment')])
m = as.matrix(table(id, period)); n = dim(m)[1]; t = dim(m)[2]
### design matrix for correlation parameters
Z = createzCrossSec(m) 

### (1) Matrix-adjusted estimating equations and GEE 
### on continous outcome with nested exchangeable correlation structure
 
### MAEE
est_maee_ind_con = geemaee(y = sampleSWCRT$y_con, 
                           X = X, id  = id, Z = Z, 
                           family = "continuous", 
                           maxiter = 500, epsilon = 0.001, 
                           printrange = TRUE, alpadj = TRUE, 
                           shrink = "ALPHA", makevone = FALSE)
print(est_maee_ind_con)
## GEE for correlated Gaussian data 
##  Number of Iterations: 6 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0  0.19231933 0.1048518 0.13134291 0.13722549 0.14337189 0.13681137
## [2,]    1  0.13855318 0.1089971 0.08559214 0.09070281 0.09631631 0.08746208
## [3,]    2  0.14135330 0.1246032 0.09471802 0.10092099 0.10769442 0.10114962
## [4,]    3  0.20421027 0.1452583 0.10146597 0.11068698 0.12096977 0.11160100
## [5,]    4  0.24716971 0.1684305 0.17177784 0.18728285 0.20433337 0.18746260
## [6,]    5 -0.08406266 0.1322349 0.13858326 0.15310151 0.16917367 0.15161045
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.09465688 0.02442148 0.02594067 0.02764751 0.02572805
## [2,]     1 0.03469533 0.01939519 0.02047886 0.02163615 0.02033902
### GEE
est_uee_ind_con = geemaee(y = sampleSWCRT$y_con, 
                          X = X, id = id, Z = Z, 
                          family = "continuous", 
                          maxiter = 500, epsilon = 0.001, 
                          printrange = TRUE, alpadj = FALSE, 
                          shrink = "ALPHA", makevone = FALSE)
print(est_uee_ind_con)
## GEE for correlated Gaussian data 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0  0.19190139 0.0996732 0.13145806  0.1373518  0.1435101  0.1369554
## [2,]    1  0.13814149 0.1035213 0.08569554  0.0908225  0.0964508  0.0875894
## [3,]    2  0.14064776 0.1185910 0.09464091  0.1008293  0.1075822  0.1010819
## [4,]    3  0.20346170 0.1383136 0.10102922  0.1101788  0.1203834  0.1111059
## [5,]    4  0.24633487 0.1602727 0.17211627  0.1876575  0.2047488  0.1878631
## [6,]    5 -0.08318282 0.1259570 0.13859217  0.1531209  0.1692063  0.1516836
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.08146407 0.02277749 0.02412259 0.02563971 0.02397541
## [2,]     1 0.03076420 0.01819528 0.01919418 0.02026185 0.01907859
### (2) Matrix-adjusted estimating equations and GEE 
### on binary outcome with nested exchangeable correlation structure

### MAEE
est_maee_ind_bin = geemaee(y = sampleSWCRT$y_bin, 
                           X = X, id = id, Z = Z, 
                           family = "binomial", 
                           maxiter = 500, epsilon = 0.001, 
                           printrange = TRUE, alpadj = TRUE, 
                           shrink = "ALPHA", makevone = FALSE)
print(est_maee_ind_bin)
## GEE for correlated Gaussian data 
##  Number of Iterations: 7 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03457213 0.2241538  0.2392075  0.2498653  0.2610001  0.2473707
## [2,]    1 -0.34208716 0.2377506  0.2099319  0.2248003  0.2408185  0.2234759
## [3,]    2 -0.67675480 0.2823888  0.2743190  0.2961203  0.3199988  0.2912959
## [4,]    3  0.02997130 0.3119529  0.2000433  0.2183246  0.2399443  0.2115277
## [5,]    4 -0.42930737 0.3863814  0.1803455  0.1973792  0.2176510  0.1991261
## [6,]    5 -0.81816822 0.2919980  0.2800864  0.3069737  0.3365978  0.3032212
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.11496936 0.04117568 0.04294094 0.04478275 0.04281833
## [2,]     1 0.06242691 0.03481106 0.03630878 0.03787122 0.03624330
### GEE
est_uee_ind_bin = geemaee(y = sampleSWCRT$y_bin, 
                          X = X, id = id, Z = Z, 
                          family = "binomial", 
                          maxiter = 500, epsilon = 0.001, 
                          printrange = TRUE, alpadj = FALSE, 
                          shrink = "ALPHA", makevone = FALSE)
print(est_uee_ind_bin)
## GEE for correlated Gaussian data 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03532021 0.2130112  0.2391542  0.2498171  0.2609580  0.2472393
## [2,]    1 -0.34154512 0.2256323  0.2100181  0.2248984  0.2409307  0.2236170
## [3,]    2 -0.67644339 0.2683572  0.2745740  0.2964326  0.3203797  0.2916198
## [4,]    3  0.03090417 0.2963087  0.2002704  0.2186117  0.2403231  0.2117340
## [5,]    4 -0.43011798 0.3665889  0.1798608  0.1969091  0.2172283  0.1986538
## [6,]    5 -0.81865546 0.2768705  0.2801134  0.3070590  0.3367553  0.3032859
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.09974917 0.03709677 0.03868651 0.04034505 0.03857485
## [2,]     1 0.05686766 0.03128090 0.03262829 0.03403398 0.03256722

cpgeeSWD() example: cluster-period GEE for estimating the marginal mean and correlation parameters in cross-sectional SW-CRTs

The cpgeeSWD() function implements the cluster-period GEE developed for cross-sectional stepped wedge cluster randomized trials (SW-CRTs). It provides valid estimation and inference for the treatment effect and intraclass correlation parameters within the GEE framework, and is computationally efficient for analyzing SW-CRTs with large cluster sizes. The program currently only allows for a marginal mean model with discrete period effects and the intervention indicator without additional covariates. The program offers bias-corrected ICC estimates as well as bias-corrected sandwich variances for both the treatment effect parameter and the ICC parameters. The technical details of the cluster-period GEE approach are provided in (Li et al. 2021).

Simulated dataset with 12 clusters and 4 periods.

We summarize the individual-level simulated SW-CRT data to cluster-period data and use the cpgeeSWD() function to estimate the marginal mean and correlation parameters on cluster-period means of binary outcome. We first transform the variables to get the cluster-period mean outcome y_cp, mean parameters’ design matrix X_cp as well as other arguments.

########################################################################
### Example 1): simulated SW-CRT with smaller cluster-period sizes (5~10)
########################################################################

sampleSWCRT = sampleSWCRTSmall

### cluster-period id, period, outcome, and design matrix
### id, period, outcome
id = sampleSWCRT$id; period = sampleSWCRT$period; y =  sampleSWCRT$y_bin
X = as.matrix(sampleSWCRT[, c('period1', 'period2', 'period3', 'period4', 'treatment')])
 
m = as.matrix(table(id, period)); n = dim(m)[1]; t = dim(m)[2]
clp_mu = tapply(y,list(id,period), FUN=mean)
y_cp = c(t(clp_mu))
 
### design matrix for correlation parameters
trt = tapply(X[, t + 1], list(id, period), FUN=mean)
trt = c(t(trt))

time = tapply(period,list(id, period), FUN = mean); time = c(t(time))
X_cp = matrix(0, n * t, t)

s = 1
for (i in 1:n) { for (j in 1:t) { X_cp[s, time[s]] = 1; s = s + 1 }}
X_cp = cbind(X_cp, trt); id_cp = rep(1:n, each = t); m_cp =  c(t(m))

We implement the cpgeeSWD() function on all the three choices of the correlation structure including "exchangeable", "nest_exch" and "exp_decay". We consider both matrix-adjusted estimating equations (MAEE) with alpadj = TRUE and uncorrected generalized estimating equations (GEE) with alpadj = FALSE.

### cluster-period matrix-adjusted estimating equations (MAEE) 
### with exchangeable, nested exchangeable and exponential decay correlation structures 
# exponential
est_maee_exc = cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "exchangeable", 
                        alpadj = TRUE)
print(est_maee_exc)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7678066 0.2703014  0.2394340  0.2506194  0.2623375  0.2575649
## [2,]    1 -0.4899672 0.2929374  0.3175202  0.3415573  0.3679292  0.3465760
## [3,]    2 -0.6692544 0.3719364  0.5582006  0.6161580  0.6810928  0.6184389
## [4,]    3 -0.5967261 0.5257133  0.5630605  0.6178896  0.6788478  0.6378908
## [5,]    4 -0.9712041 0.4316492  0.5346929  0.5883089  0.6479608  0.5905832
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.07171243 0.03869076 0.04059179  0.0425914 0.04058122
# nested exchangeable
est_maee_nex = cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "nest_exch", 
                        alpadj = TRUE)
print(est_maee_nex)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 6 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7721492 0.2931061  0.2388699  0.2498629  0.2613680  0.2533655
## [2,]    1 -0.5036505 0.3216055  0.3154306  0.3394624  0.3658386  0.3392457
## [3,]    2 -0.6217770 0.4152335  0.5684728  0.6271700  0.6927849  0.6233328
## [4,]    3 -0.5994803 0.5967744  0.5467135  0.6003143  0.6598640  0.6091679
## [5,]    4 -0.9861662 0.4905999  0.5305528  0.5839117  0.6431778  0.5821589
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.10820010 0.04714541 0.04951304 0.05200312 0.04952131
## [2,]     1 0.05385588 0.03727153 0.03907559 0.04097119 0.03907206
# exponential decay 
est_maee_ed = cpgeeSWD(y  = y_cp, X = X_cp, id = id_cp, 
                       m = m_cp, corstr = "exp_decay", 
                       alpadj = TRUE)
print(est_maee_ed)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 7 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7710180 0.2950154  0.2375293  0.2483319  0.2596306  0.2513006
## [2,]    1 -0.4988642 0.3224197  0.3145635  0.3388380  0.3654862  0.3397012
## [3,]    2 -0.6284311 0.4156078  0.5720337  0.6309674  0.6968561  0.6305171
## [4,]    3 -0.6140801 0.5986182  0.5461283  0.5997932  0.6594254  0.6124552
## [5,]    4 -0.9866725 0.4904297  0.5309814  0.5843712  0.6436174  0.5841529
## 
## Results for correlation parameters 
##      Alpha  Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.1115235  0.0471122 0.04949153 0.05199521 0.04946812
## [2,]     1 0.6158576  0.1759633 0.18398863 0.19240603 0.18381541
### cluster-period GEE 
### with exchangeable, nested exchangeable and exponential decay correlation structures

# exchangeable
est_uee_exc <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "exchangeable",
                        alpadj = FALSE)
print(est_uee_exc)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7679011 0.2611220  0.2398530  0.2510783  0.2628390  0.2564986
## [2,]    1 -0.4911553 0.2852177  0.3154908  0.3393963  0.3656321  0.3424027
## [3,]    2 -0.6697603 0.3647602  0.5627720  0.6215708  0.6874968  0.6213395
## [4,]    3 -0.5940272 0.5205574  0.5631388  0.6182225  0.6795178  0.6340687
## [5,]    4 -0.9751028 0.4299957  0.5367262  0.5907718  0.6509467  0.5916886
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.05725857 0.03375491 0.03539291 0.03711574 0.03538186
# nested exchangeable
est_uee_nex <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "nest_exch", 
                        alpadj = FALSE)
print(est_uee_nex)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7701899 0.2728928  0.2396011  0.2507209  0.2623647  0.2544085
## [2,]    1 -0.4993131 0.2999219  0.3142455  0.3381255  0.3643392  0.3384140
## [3,]    2 -0.6424236 0.3867725  0.5686219  0.6278135  0.6940831  0.6244480
## [4,]    3 -0.5956930 0.5566382  0.5540211  0.6083847  0.6688445  0.6182629
## [5,]    4 -0.9836857 0.4595437  0.5345098  0.5883958  0.6483261  0.5870942
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.07500427 0.03992967 0.04189842 0.04396787 0.04190479
## [2,]     1 0.04814669 0.03293754 0.03450776 0.03615783 0.03449733
# exponential decay 
est_uee_ed <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                       m = m_cp, corstr = 'exp_decay', 
                       alpadj = FALSE)
print(est_uee_ed)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta   Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.7691313 0.2760920  0.2385661  0.2495298  0.2610036  0.2526378
## [2,]    1 -0.4960302 0.3027753  0.3135021  0.3375362  0.3639236  0.3382822
## [3,]    2 -0.6436889 0.3904936  0.5720324  0.6315468  0.6981797  0.6302718
## [4,]    3 -0.6064791 0.5635697  0.5533253  0.6077753  0.6683419  0.6196995
## [5,]    4 -0.9866876 0.4640753  0.5348939  0.5888701  0.6488594  0.5884695
## 
## Results for correlation parameters 
##      Alpha  Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.0800789 0.04026846 0.04227077 0.04437711 0.04228027
## [2,]     1 0.7046177 0.17583729 0.18376072 0.19206554 0.18353049

Simulated dataset with 12 clusters and 5 periods

########################################################################
### Example 2): simulated SW-CRT with larger cluster-period sizes (20~30)
########################################################################

sampleSWCRT = sampleSWCRTLarge

### cluster-period id, period, outcome, and design matrix
### id, period, outcome
id = sampleSWCRT$id; period =  sampleSWCRT$period; y =  sampleSWCRT$y_bin
X = as.matrix(sampleSWCRT[, c('period1', 'period2', 'period3', 'period4', 'period5', 'treatment')])
 
m = as.matrix(table(id, period)); n = dim(m)[1]; t = dim(m)[2]
clp_mu<-tapply(y,list(id,period), FUN=mean)
y_cp <- c(t(clp_mu))
 
### design matrix for correlation parameters
trt <- tapply(X[, t + 1], list(id, period), FUN=mean)
trt <- c(t(trt))

time <- tapply(period,list(id, period), FUN = mean); time <- c(t(time))
X_cp <- matrix(0, n * t, t)

s = 1
for(i in 1:n){for(j in 1:t){X_cp[s, time[s]] <- 1; s = s + 1}}
X_cp <- cbind(X_cp, trt); id_cp <- rep(1:n, each= t); m_cp <-  c(t(m))

### cluster-period matrix-adjusted estimating equations (MAEE) 
### with exchangeable, nested exchangeable and exponential decay correlation structures 
# exponential
est_maee_exc <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                         m = m_cp, corstr = "exchangeable", 
                         alpadj = TRUE)
print(est_maee_exc)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03937150 0.1955148  0.2373442  0.2480663  0.2592915  0.2392651
## [2,]    1 -0.33407908 0.2022096  0.2200174  0.2352782  0.2517452  0.2396738
## [3,]    2 -0.67338220 0.2343484  0.2944134  0.3178251  0.3435471  0.3183472
## [4,]    3  0.04608714 0.2414272  0.2332480  0.2536047  0.2774156  0.2480103
## [5,]    4 -0.43352358 0.2901120  0.1710562  0.1877125  0.2079820  0.1965489
## [6,]    5 -0.82493433 0.1977208  0.2758767  0.3031771  0.3333531  0.3000391
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.07782417 0.03688003 0.03829676 0.03976975 0.03827983
# nested exchangeable
est_maee_nex <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                         m = m_cp, corstr = "nest_exch", 
                         alpadj = TRUE)
print(est_maee_nex)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03432555 0.2278441  0.2393377  0.2499941  0.2611267  0.2479004
## [2,]    1 -0.34293068 0.2424034  0.2082967  0.2231294  0.2391131  0.2212194
## [3,]    2 -0.67730319 0.2887517  0.2713904  0.2930314  0.3167289  0.2876482
## [4,]    3  0.02825126 0.3212402  0.1938752  0.2119627  0.2334399  0.2046802
## [5,]    4 -0.42990720 0.3990553  0.1849263  0.2025603  0.2234539  0.2035330
## [6,]    5 -0.81687942 0.3038572  0.2835181  0.3107636  0.3407871  0.3069380
## 
## Results for correlation parameters 
##      Alpha  Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.1201529 0.04650374 0.04831496 0.05019968 0.04830353
## [2,]     1 0.0584004 0.03424640 0.03557674 0.03696081 0.03558240
# exponential decay 
est_maee_ed <- cpgeeSWD(y  = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "exp_decay", 
                        alpadj = TRUE)
print(est_maee_ed)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 7 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03340439 0.2236591  0.2415963  0.2524580  0.2638106  0.2513261
## [2,]    1 -0.32963269 0.2361087  0.2096366  0.2232709  0.2378969  0.2223343
## [3,]    2 -0.66086972 0.2793325  0.2679038  0.2864419  0.3065960  0.2820858
## [4,]    3  0.05215247 0.3068537  0.2126799  0.2276517  0.2447418  0.2231794
## [5,]    4 -0.38679195 0.3815072  0.1392210  0.1482120  0.1597143  0.1537140
## [6,]    5 -0.85944259 0.2843752  0.2312996  0.2529042  0.2766009  0.2467741
## 
## Results for correlation parameters 
##      Alpha  Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.1143454 0.04222592 0.04384679 0.04553347 0.04380728
## [2,]     1 0.7000112 0.14390913 0.15014145 0.15665442 0.15034373
### cluster-period GEE 
### with exchangeable, nested exchangeable and exponential decay correlation structures

# exchangeable
est_uee_exc <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "exchangeable",
                        alpadj = FALSE)
print(est_uee_exc)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 3 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03987191 0.1891780  0.2375607  0.2482853  0.2595112  0.2404060
## [2,]    1 -0.33454114 0.1959210  0.2189623  0.2341927  0.2506256  0.2376880
## [3,]    2 -0.67375572 0.2281604  0.2923708  0.3156737  0.3412787  0.3152896
## [4,]    3  0.04529970 0.2371119  0.2301168  0.2503246  0.2740128  0.2444258
## [5,]    4 -0.43427752 0.2857409  0.1708179  0.1874729  0.2077462  0.1952451
## [6,]    5 -0.82467526 0.1978609  0.2760276  0.3033256  0.3334993  0.3001943
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.07013385 0.03368912 0.03498342 0.03632918 0.03496968
# nested exchangeable
est_uee_nex <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                        m = m_cp, corstr = "nest_exch", 
                        alpadj = FALSE)
print(est_uee_nex)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 3 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03499108 0.2169350  0.2392903  0.2499507  0.2610881  0.2477876
## [2,]    1 -0.34245090 0.2305101  0.2084051  0.2232474  0.2392423  0.2213878
## [3,]    2 -0.67698816 0.2749173  0.2716631  0.2933535  0.3171098  0.2879913
## [4,]    3  0.02904193 0.3057033  0.1942266  0.2123667  0.2339216  0.2050350
## [5,]    4 -0.43058289 0.3793522  0.1842784  0.2018893  0.2227843  0.2028661
## [6,]    5 -0.81733511 0.2886977  0.2833499  0.3106202  0.3406770  0.3067671
## 
## Results for correlation parameters 
##      Alpha   Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.10499225 0.04215716 0.04379742 0.04550421 0.04378621
## [2,]     1 0.05350042 0.03080632 0.03200335 0.03324877 0.03200919
# exponential decay 
est_uee_ed <- cpgeeSWD(y = y_cp, X = X_cp, id = id_cp, 
                       m = m_cp, corstr = 'exp_decay', 
                       alpadj = FALSE)
print(est_uee_ed)
## GEE and MAEE for Cluster-Period Summaries 
##  Number of Iterations: 4 
## Results for marginal mean parameters 
##      Beta    Estimate MB-stderr BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]    0 -0.03459151 0.2123822  0.2414782  0.2523416  0.2636966  0.2510466
## [2,]    1 -0.33001054 0.2238965  0.2100290  0.2237648  0.2385050  0.2228663
## [3,]    2 -0.66158744 0.2652147  0.2697183  0.2886630  0.3092792  0.2844552
## [4,]    3  0.05115744 0.2911203  0.2127361  0.2279738  0.2454327  0.2231941
## [5,]    4 -0.38956628 0.3612911  0.1403145  0.1497434  0.1617831  0.1547516
## [6,]    5 -0.85722995 0.2692278  0.2336074  0.2555548  0.2796454  0.2493893
## 
## Results for correlation parameters 
##      Alpha  Estimate BC0-stderr BC1-stderr BC2-stderr BC3-stderr
## [1,]     0 0.0989716 0.03850721 0.03997936 0.04151109 0.03993102
## [2,]     1 0.7279137 0.13726650 0.14328210 0.14956972 0.14351625

simbinPROBIT() example: generating correlated binary data using the multivariate probit method

The simbinPROBIT() function generates correlated binary data using the multivariate Probit method (Emrich and Piedmonte 1991). It simulates a vector of binary outcomes according the specified marginal mean vector and correlation structure. Constraints and compatibility between the marginal mean and correlation matrix are checked.

We use the simbinPROBIT() function to generate correlated binary data with different correlation structures. We consider simulating a cross-sectional SW-CRT dataset with 2 clusters, 3 periods with the same cluster-period size of 5. We use two mean vectors for the two clusters and specify the mu argument.

For the exchangeable correlation structure, we specify both the within-period and inter-period correlation parameters to be 0.015. We use 0.03 and 0.015 for the within-period and inter-period correlations, respectively. The exponential decay correlation structure has an decay parameter 0.8 with the within-period correlation parameter 0.03.

#### Simulate 2 clusters, 3 periods and cluster-period size of 5

t = 3; n = 2; m = 5
# means of cluster 1
u_c1 = c(0.4, 0.3, 0.2)
u1 <- rep(u_c1, c(rep(m, t)))
# means of cluster 2
u_c2 = c(0.35, 0.25, 0.2)
u2 <- rep(u_c2, c(rep(m, t)))

# List of mean vectors
mu = list(); mu[[1]] = u1; mu[[2]] = u2;

# List of correlation matrices

## correlation parameters
alpha0 = 0.03; alpha1 = 0.015; rho = 0.8

## (1) exchangeable
Sigma = list()
Sigma[[1]] = diag(m * t) * ( 1 - alpha1) + matrix(alpha1, m * t,  m * t )

Sigma[[2]] = diag(m * t) * ( 1 - alpha1) + matrix(alpha1, m * t,  m * t )

y_exc = simbinPROBIT(mu = mu, Sigma = Sigma, n = n)

## (2) nested exchangeable
Sigma = list()
cor_matrix = matrix(alpha1, m * t,  m * t)
loc1 = 0; loc2 = 0
for(t in 1:t){loc1 = loc2 + 1; loc2 = loc1 + m - 1
  for(i in loc1:loc2){for(j in loc1:loc2){
         if(i != j){cor_matrix[i, j] = alpha0}else{cor_matrix[i, j] = 1}}}}
 
Sigma[[1]] = cor_matrix; Sigma[[2]] = cor_matrix

y_nex = simbinPROBIT(mu = mu, Sigma = Sigma, n = n)

## (3) exponential decay
 
Sigma = list()
 
### function to find the period of the ith index
 region_ij<-function(points, i){diff = i - points
     for(h in 1:(length(diff) - 1)){if(diff[h] > 0 & diff[h + 1] <= 0){find <- h}}
  return(find)}

 cor_matrix = matrix(0,  m * t,  m * t)
 useage_m = cumsum(m * t); useage_m = c(0, useage_m)

 for(i in 1:(m * t)){i_reg = region_ij(useage_m, i)
      for(j in 1:(m * t)){j_reg = region_ij(useage_m, j)
          if(i_reg == j_reg & i != j){
              cor_matrix[i, j] = alpha0}else if(i == j){cor_matrix[i, j] = 1
 }else if(i_reg != j_reg){cor_matrix[i,j] = alpha0 * (rho^(abs(i_reg - j_reg)))}}}

Sigma[[1]] = cor_matrix; Sigma[[2]] = cor_matrix

y_ed = simbinPROBIT(mu = mu, Sigma = Sigma, n = n)

simbinCLF() example: generating correlated binary data using the conditional linear family method

The simbinCLF() function generates correlated binary data using the conditional linear family method (Qaqish 2003). It simulates a vector of binary outcomes according the specified marginal mean vector and correlation structure. Natural constraints and compatibility between the marginal mean and correlation matrix are checked.

We use the simbinCLF() function to generate correlated binary data with different correlation structures. We consider simulating a cross-sectional SW-CRT dataset with 2 clusters, 3 periods with the same cluster-period size of 5. We use two mean vectors for the two clusters and specify the mu argument.

For the exchangeable correlation structure, we specify both the within-period and inter-period correlation parameters to be 0.015. We use 0.03 and 0.015 for the within-period and inter-period correlations, respectively. The exponential decay correlation structure has an decay parameter 0.8 with the within-period correlation parameter 0.03.

##### Simulate 2 clusters, 3 periods and cluster-period size of 5

t = 3; n = 2; m = 5
 
# means of cluster 1
u_c1 = c(0.4, 0.3, 0.2)
u1 <- rep(u_c1, c(rep(m, t)))
# means of cluster 2
u_c2 = c(0.35, 0.25, 0.2)
u2 <- rep(u_c2, c(rep(m, t)))

# List of mean vectors
mu = list()
mu[[1]] = u1; mu[[2]] = u2;

# List of correlation matrices
 
## correlation parameters
alpha0 = 0.03; alpha1 = 0.015; rho = 0.8

## (1) exchangeable
Sigma = list()
Sigma[[1]] = diag(m * t) * ( 1 - alpha1) + matrix(alpha1, m * t,  m * t )
Sigma[[2]] = diag(m * t) * ( 1 - alpha1) + matrix(alpha1, m * t,  m * t )
y_exc = simbinCLF(mu = mu, Sigma = Sigma, n = n)

## (2) nested exchangeable
Sigma = list()
cor_matrix = matrix(alpha1, m * t,  m * t)
loc1 = 0; loc2 = 0
for(t in 1:t){loc1 = loc2 + 1; loc2 = loc1 + m - 1
    for(i in loc1:loc2){for(j in loc1:loc2){
         if(i != j){cor_matrix[i, j] = alpha0}else{cor_matrix[i, j] = 1}}}}
 
Sigma[[1]] = cor_matrix; Sigma[[2]] = cor_matrix
y_nex = simbinCLF(mu = mu, Sigma = Sigma, n = n)

## (3) exponential decay
 
Sigma = list()

### function to find the period of the ith index
region_ij<-function(points, i){diff = i - points
     for(h in 1:(length(diff) - 1)){if(diff[h] > 0 & diff[h + 1] <= 0){find <- h}}
  return(find)}

cor_matrix = matrix(0,  m * t,  m * t)
useage_m = cumsum(m * t); useage_m = c(0, useage_m)

for(i in 1:(m * t)){i_reg = region_ij(useage_m, i)
      for(j in 1:(m * t)){j_reg = region_ij(useage_m, j)
          if(i_reg == j_reg & i != j){
              cor_matrix[i, j] = alpha0}else if(i == j){cor_matrix[i, j] = 1
 }else if(i_reg != j_reg){cor_matrix[i,j] = alpha0 * (rho^(abs(i_reg - j_reg)))}}}

Sigma[[1]] = cor_matrix; Sigma[[2]] = cor_matrix

y_ed = simbinCLF(mu = mu, Sigma = Sigma, n = n)

Session Information

## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] geeCRT_0.1.0
## 
## loaded via a namespace (and not attached):
##  [1] mvtnorm_1.1-1     digest_0.6.27     MASS_7.3-53       magrittr_2.0.1    evaluate_0.14    
##  [6] rootSolve_1.8.2.1 highr_0.8         rlang_0.4.10      stringi_1.5.3     rmarkdown_2.7    
## [11] tools_4.0.3       stringr_1.4.0     xfun_0.22         yaml_2.2.1        compiler_4.0.3   
## [16] htmltools_0.5.1.1 knitr_1.31

References

Emrich, Lawrence J, and Marion R Piedmonte. 1991. “A Method for Generating High-Dimensional Multivariate Binary Variates.” The American Statistician 45 (4): 302–4.

Li, Fan, Elizabeth L Turner, and John S Preisser. 2018. “Sample Size Determination for Gee Analyses of Stepped Wedge Cluster Randomized Trials.” Biometrics 74 (4): 1450–8.

Li, Fan, Hengshi Yu, Paul J Rathouz, Elizabeth L Turner, and John S Preisser. 2021. “Marginal modeling of cluster-period means and intraclass correlations in stepped wedge designs with binary outcomes.” Biostatistics, February. https://doi.org/10.1093/biostatistics/kxaa056.

Preisser, John S, Bing Lu, and Bahjat F Qaqish. 2008. “Finite Sample Adjustments in Estimating Equations and Covariance Estimators for Intracluster Correlations.” Statistics in Medicine 27 (27): 5764–85.

Qaqish, Bahjat F. 2003. “A Family of Multivariate Binary Distributions for Simulating Correlated Binary Variables with Specified Marginal Means and Correlations.” Biometrika 90 (2): 455–63.