============================================================================================================
Not Yet Published.

1. Getting Started

 

The data set (long format) consists of four columns. Factor A (with levels \(a_i\)) and Factor B (with levels \(b_j\)) for \(i,j\in\{1,2\}\).

simData_Holds <- function(seed=277, N=50, nullB=F) {
  #' Input -
  #' seed:  random seed (default is fixed)
  #' N:     the number of subjects (default is 50)
  #' nullB: whether the effect B is not significant.
  #'        
  #' Output - the data frame for a 2x2 repeated measures design (compound symmetry holds)
  set.seed(seed)
  if(nullB) {
    ## 2x2 repeated measures data simulation (no effect B)
    mat <- MASS::mvrnorm(N,rep(100,4),diag(c(2,2,2,2)))
    
  } else { #nullB==F
    ## 2x2 repeated measures data simulation
    mat <- MASS::mvrnorm(N,c(50,52,52,54),diag(c(20,20,20,20)))
  }
  
  mat <- sweep(mat, 1, rnorm(N,sd=10), "+") #rho=10^2 / (10^2 + 20)=0.83
  data.frame("subj"=paste0("s",1:N),
             "score"=c(mat),
             "A"=rep(rep(c("a1","a2"),2),each=N),
             "B"=rep(c("b1","b2"),each=N*2),
             stringsAsFactors=T)
}

df <- simData_Holds(seed=1, nullB=T)
dat <- matrix(df$score, ncol=4,
              dimnames=list(paste0("s",1:length(unique(df$subj))),
                            c("a1b1","a2b1","a1b2","a2b2"))) #wide data format

datalist.fixed <- list("nS"=nrow(dat), "I"=diag(4), "Y"=dat,
                       "hA"=.5, "hB"=.5, "hAB"=.5,
                       "lwr"=mean(dat)-6*sd(dat),
                       "upr"=mean(dat)+6*sd(dat))

cov(dat) %>% #sample covariance matrix
  kable(digits=3) %>% kable_styling(full_width=F)
a1b1 a2b1 a1b2 a2b2
a1b1 119.317 120.747 119.125 118.294
a2b1 120.747 125.045 122.249 121.861
a1b2 119.125 122.249 122.938 120.576
a2b2 118.294 121.861 120.576 121.600

   

2. Scale to the Determinant

 

model.full.fixed1 <- "
 data {
   int<lower=1> nS;        // number of subjects
   vector[4] Y[nS];        // responses
   real<lower=0> hA;       // scale parameter of the variance of the standardized projected treatment effect A
   real<lower=0> hB;       // scale parameter of the variance of the standardized projected treatment effect B
   real<lower=0> hAB;      // scale parameter of the variance of the standardized projected interaction effect AB
   matrix[4,4] I;          // scale matrix for the inverse-Wishart prior
   real lwr;               // lower bound for the grand mean
   real upr;               // upper bound for the grand mean
 }

 parameters {
   real<lower=lwr, upper=upr> mu;     // grand mean
   cov_matrix[4] Sigma;               // error covariance matrix
   real<lower=0> gA;                  // variance of the standardized projected treatment effect A
   real<lower=0> gB;                  // variance of the standardized projected treatment effect B
   real<lower=0> gAB;                 // variance of the standardized projected interaction effect AB
   real tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
   real tABfix;                       // standardized projected interaction effect AB
 }

 transformed parameters {
   real multiplier;       // (square root) geometric mean of the determinant of the error covariance matrix
   matrix[2,2] muS;       // condition means
   
   multiplier = pow(determinant(Sigma), 0.125);
   for (i in 1:2) {
     for (j in 1:2) {
       muS[i,j] = mu + multiplier * (pow(-1,i+1) * tAfix / sqrt(2) 
                                   + pow(-1,j+1) * tBfix / sqrt(2)
                                   + pow(-1,i+j) * tABfix / 2);
     }
   }
 }

 model {
   target += uniform_lpdf(mu | lwr, upr);
   target += scaled_inv_chi_square_lpdf(gA | 1, hA);
   target += scaled_inv_chi_square_lpdf(gB | 1, hB);
   target += scaled_inv_chi_square_lpdf(gAB | 1, hAB);
   target += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   target += normal_lpdf(tABfix | 0, sqrt(gAB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

stanmodel.full.fixed1 <- stan_model(model_code=model.full.fixed1)

stanfit.full.fixed1 <- sampling(stanmodel.full.fixed1, data=datalist.fixed,
                                iter=10000, warmup=1000, chains=2, seed=277, refresh=0)

rstan::summary(stanfit.full.fixed1)[1] %>% kable(digits=4) %>% kable_classic(full_width=F)
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 99.7573 0.0098 1.5349 96.7420 98.7305 99.7642 100.7824 102.8008 24742.763 0.9999
Sigma[1,1] 117.4602 0.3313 24.1378 79.4155 100.2423 114.2335 130.8969 173.7910 5307.955 0.9999
Sigma[1,2] 118.8360 0.3389 24.5652 80.1772 101.3929 115.5413 132.7098 175.9992 5254.324 0.9999
Sigma[1,3] 117.2592 0.3350 24.2963 78.9342 99.8866 114.0236 130.9745 173.7444 5259.837 0.9999
Sigma[1,4] 116.4312 0.3342 24.1621 78.5868 99.2643 113.2134 129.9449 172.6888 5226.169 0.9999
Sigma[2,1] 118.8360 0.3389 24.5652 80.1772 101.3929 115.5413 132.7098 175.9992 5254.324 0.9999
Sigma[2,2] 123.0770 0.3480 25.2934 83.3825 105.0822 119.8596 137.4297 182.1853 5281.441 0.9999
Sigma[2,3] 120.3233 0.3436 24.9034 81.0474 102.5683 117.0530 134.3753 178.4049 5251.995 0.9999
Sigma[2,4] 119.9330 0.3429 24.8121 81.0273 102.2411 116.5535 133.9399 177.6778 5235.929 0.9999
Sigma[3,1] 117.2592 0.3350 24.2963 78.9342 99.8866 114.0236 130.9745 173.7444 5259.837 0.9999
Sigma[3,2] 120.3233 0.3436 24.9034 81.0474 102.5683 117.0530 134.3753 178.4049 5251.995 0.9999
Sigma[3,3] 121.0463 0.3410 24.8816 81.8834 103.3107 117.8357 134.9566 179.1700 5325.283 0.9999
Sigma[3,4] 118.6883 0.3391 24.5753 80.0236 101.1327 115.4523 132.5789 176.1859 5252.537 0.9999
Sigma[4,1] 116.4312 0.3342 24.1621 78.5868 99.2643 113.2134 129.9449 172.6888 5226.169 0.9999
Sigma[4,2] 119.9330 0.3429 24.8121 81.0273 102.2411 116.5535 133.9399 177.6778 5235.929 0.9999
Sigma[4,3] 118.6883 0.3391 24.5753 80.0236 101.1327 115.4523 132.5789 176.1859 5252.537 0.9999
Sigma[4,4] 119.7100 0.3392 24.6415 81.1521 102.1886 116.4113 133.5780 177.1496 5277.938 0.9999
gA 2.2373 0.6801 49.9085 0.0337 0.0915 0.1836 0.4386 4.9431 5384.724 1.0005
gB 2.0122 0.7346 80.7622 0.0343 0.0923 0.1832 0.4502 5.4395 12085.721 1.0001
gAB 1.5208 0.5099 47.2976 0.0348 0.0927 0.1867 0.4539 4.8204 8603.410 1.0000
tAfix 0.0476 0.0003 0.0510 -0.0530 0.0134 0.0476 0.0818 0.1487 22261.542 1.0000
tBfix -0.0560 0.0004 0.0561 -0.1650 -0.0935 -0.0568 -0.0179 0.0543 21697.077 0.9999
tABfix 0.0577 0.0004 0.0624 -0.0659 0.0158 0.0583 0.0996 0.1790 21997.531 1.0000
multiplier 2.5590 0.0012 0.1270 2.3258 2.4718 2.5532 2.6403 2.8248 10728.387 0.9999
muS[1,1] 99.8160 0.0096 1.5278 96.8063 98.7909 99.8187 100.8422 102.8283 25536.582 1.0000
muS[1,2] 99.8705 0.0099 1.5490 96.8279 98.8271 99.8797 100.9125 102.9118 24511.913 0.9999
muS[2,1] 99.4968 0.0100 1.5583 96.4460 98.4629 99.5019 100.5361 102.5651 24074.376 0.9999
muS[2,2] 99.8459 0.0098 1.5373 96.8311 98.8202 99.8497 100.8816 102.8712 24824.819 0.9999
lp__ -519.6798 0.0378 3.1208 -526.7187 -521.5969 -519.3372 -517.4148 -514.6338 6818.786 1.0005

   

3. Scale to the Trace

 

model.full.fixed2 <- "
 data {
   int<lower=1> nS;        // number of subjects
   vector[4] Y[nS];        // responses
   real<lower=0> hA;       // scale parameter of the variance of the standardized projected treatment effect A
   real<lower=0> hB;       // scale parameter of the variance of the standardized projected treatment effect B
   real<lower=0> hAB;      // scale parameter of the variance of the standardized projected interaction effect AB
   matrix[4,4] I;          // scale matrix for the inverse-Wishart prior
   real lwr;               // lower bound for the grand mean
   real upr;               // upper bound for the grand mean
 }

 parameters {
   real<lower=lwr, upper=upr> mu;     // grand mean
   cov_matrix[4] Sigma;               // error covariance matrix
   real<lower=0> gA;                  // variance of the standardized projected treatment effect A
   real<lower=0> gB;                  // variance of the standardized projected treatment effect B
   real<lower=0> gAB;                 // variance of the standardized projected interaction effect AB
   real tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
   real tABfix;                       // standardized projected interaction effect AB
 }

 transformed parameters {
   real multiplier;       // using the trace of the error covariance matrix
   matrix[2,2] muS;       // condition means
   
   multiplier = sqrt(trace(Sigma))/2;
   for (i in 1:2) {
     for (j in 1:2) {
       muS[i,j] = mu + multiplier * (pow(-1,i+1) * tAfix / sqrt(2) 
                                   + pow(-1,j+1) * tBfix / sqrt(2)
                                   + pow(-1,i+j) * tABfix / 2);
     }
   }
 }

 model {
   target += uniform_lpdf(mu | lwr, upr);
   target += scaled_inv_chi_square_lpdf(gA | 1, hA);
   target += scaled_inv_chi_square_lpdf(gB | 1, hB);
   target += scaled_inv_chi_square_lpdf(gAB | 1, hAB);
   target += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   target += normal_lpdf(tABfix | 0, sqrt(gAB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

stanmodel.full.fixed2 <- stan_model(model_code=model.full.fixed2)

stanfit.full.fixed2 <- sampling(stanmodel.full.fixed2, data=datalist.fixed,
                                iter=10000, warmup=1000, chains=2, seed=277, refresh=0)

rstan::summary(stanfit.full.fixed2)[1] %>% kable(digits=4) %>% kable_classic(full_width=F)
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 99.7497 0.0110 1.5132 96.7458 98.7520 99.7579 100.7565 102.7484 19004.461 0.9999
Sigma[1,1] 112.5110 0.3222 22.3830 76.7505 96.4542 109.9006 125.3106 163.4410 4827.003 1.0001
Sigma[1,2] 113.7576 0.3292 22.7776 77.2118 97.3963 111.1177 126.8081 165.5439 4786.230 1.0001
Sigma[1,3] 112.2086 0.3260 22.5571 76.2301 96.0264 109.5078 125.0870 164.0741 4786.279 1.0002
Sigma[1,4] 111.4266 0.3238 22.4273 75.6004 95.3847 108.7909 124.2319 162.4887 4797.479 1.0002
Sigma[2,1] 113.7576 0.3292 22.7776 77.2118 97.3963 111.1177 126.8081 165.5439 4786.230 1.0001
Sigma[2,2] 117.9125 0.3384 23.4830 80.2717 101.0808 115.2100 131.4224 171.9502 4816.126 1.0001
Sigma[2,3] 115.1738 0.3337 23.1325 78.2785 98.5306 112.3629 128.4259 168.0419 4804.961 1.0002
Sigma[2,4] 114.8407 0.3323 23.0542 78.0859 98.2810 112.1203 128.1466 167.7923 4813.941 1.0002
Sigma[3,1] 112.2086 0.3260 22.5571 76.2301 96.0264 109.5078 125.0870 164.0741 4786.279 1.0002
Sigma[3,2] 115.1738 0.3337 23.1325 78.2785 98.5306 112.3629 128.4259 168.0419 4804.961 1.0002
Sigma[3,3] 115.9556 0.3304 23.1423 79.0584 99.3823 113.1477 129.2263 169.6462 4906.834 1.0003
Sigma[3,4] 113.6303 0.3293 22.8547 77.2653 97.3289 110.9060 126.7462 166.7048 4817.968 1.0002
Sigma[4,1] 111.4266 0.3238 22.4273 75.6004 95.3847 108.7909 124.2319 162.4887 4797.479 1.0002
Sigma[4,2] 114.8407 0.3323 23.0542 78.0859 98.2810 112.1203 128.1466 167.7923 4813.941 1.0002
Sigma[4,3] 113.6303 0.3293 22.8547 77.2653 97.3289 110.9060 126.7462 166.7048 4817.968 1.0002
Sigma[4,4] 114.7231 0.3276 22.9193 78.2144 98.3132 111.9410 127.7715 167.7882 4895.785 1.0002
gA 0.9726 0.1101 10.9044 0.0341 0.0903 0.1829 0.4366 4.6023 9812.643 1.0000
gB 1.2917 0.1957 16.9901 0.0340 0.0905 0.1802 0.4391 4.9596 7538.285 1.0001
gAB 1.0297 0.1036 9.9372 0.0326 0.0907 0.1835 0.4473 5.0528 9197.087 1.0006
tAfix 0.0115 0.0001 0.0129 -0.0136 0.0029 0.0114 0.0201 0.0371 20076.276 1.0001
tBfix -0.0138 0.0001 0.0138 -0.0410 -0.0229 -0.0137 -0.0047 0.0132 19316.011 0.9999
tABfix 0.0143 0.0001 0.0156 -0.0161 0.0039 0.0140 0.0246 0.0456 17686.797 0.9999
multiplier 10.6865 0.0148 1.0362 8.8874 9.9515 10.6096 11.3280 12.9495 4881.576 1.0002
muS[1,1] 99.8084 0.0107 1.5008 96.8417 98.8041 99.8124 100.7981 102.7551 19642.584 0.9999
muS[1,2] 99.8641 0.0112 1.5281 96.8615 98.8422 99.8684 100.8787 102.8767 18718.319 0.9999
muS[2,1] 99.4842 0.0112 1.5380 96.4159 98.4691 99.4924 100.4997 102.5125 18725.428 0.9999
muS[2,2] 99.8423 0.0110 1.5208 96.8418 98.8330 99.8484 100.8448 102.8559 19069.350 0.9999
lp__ -519.6523 0.0361 3.0816 -526.5558 -521.5420 -519.2966 -517.4402 -514.6380 7279.681 1.0001

   

4. Scale to the Frobenius Norm

 

model.full.fixed3 <- "
 data {
   int<lower=1> nS;        // number of subjects
   vector[4] Y[nS];        // responses
   real<lower=0> hA;       // scale parameter of the variance of the standardized projected treatment effect A
   real<lower=0> hB;       // scale parameter of the variance of the standardized projected treatment effect B
   real<lower=0> hAB;      // scale parameter of the variance of the standardized projected interaction effect AB
   matrix[4,4] I;          // scale matrix for the inverse-Wishart prior
   real lwr;               // lower bound for the grand mean
   real upr;               // upper bound for the grand mean
 }

 parameters {
   real<lower=lwr, upper=upr> mu;     // grand mean
   cov_matrix[4] Sigma;               // error covariance matrix
   real<lower=0> gA;                  // variance of the standardized projected treatment effect A
   real<lower=0> gB;                  // variance of the standardized projected treatment effect B
   real<lower=0> gAB;                 // variance of the standardized projected interaction effect AB
   real tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
   real tABfix;                       // standardized projected interaction effect AB
 }

 transformed parameters {
   real multiplier;       // using the Frobenius norm of the error covariance matrix
   matrix[2,2] muS;       // condition means
   
   multiplier = pow(sum(square(to_vector(Sigma))), 0.25)/sqrt(2);
   for (i in 1:2) {
     for (j in 1:2) {
       muS[i,j] = mu + multiplier * (pow(-1,i+1) * tAfix / sqrt(2) 
                                   + pow(-1,j+1) * tBfix / sqrt(2)
                                   + pow(-1,i+j) * tABfix / 2);
     }
   }
 }

 model {
   target += uniform_lpdf(mu | lwr, upr);
   target += scaled_inv_chi_square_lpdf(gA | 1, hA);
   target += scaled_inv_chi_square_lpdf(gB | 1, hB);
   target += scaled_inv_chi_square_lpdf(gAB | 1, hAB);
   target += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   target += normal_lpdf(tABfix | 0, sqrt(gAB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

stanmodel.full.fixed3 <- stan_model(model_code=model.full.fixed3)

stanfit.full.fixed3 <- sampling(stanmodel.full.fixed3, data=datalist.fixed,
                                iter=10000, warmup=1000, chains=2, seed=277, refresh=0)

rstan::summary(stanfit.full.fixed3)[1] %>% kable(digits=4) %>% kable_classic(full_width=F)
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 99.7292 0.0099 1.5118 96.7372 98.7282 99.7407 100.7373 102.7095 23271.321 0.9999
Sigma[1,1] 113.2544 0.3477 23.1177 76.4457 96.9045 110.3681 126.1205 167.0388 4420.422 1.0006
Sigma[1,2] 114.5202 0.3556 23.5100 77.2043 97.7424 111.6116 127.7114 169.4161 4369.857 1.0006
Sigma[1,3] 112.9562 0.3502 23.2212 75.8780 96.4125 110.1725 125.9105 166.9113 4397.520 1.0006
Sigma[1,4] 112.1402 0.3485 23.0698 75.3913 95.7518 109.2474 125.0628 166.3431 4382.741 1.0006
Sigma[2,1] 114.5202 0.3556 23.5100 77.2043 97.7424 111.6116 127.7114 169.4161 4369.857 1.0006
Sigma[2,2] 118.6851 0.3653 24.2076 80.4049 101.4527 115.7026 132.2980 174.7494 4391.341 1.0005
Sigma[2,3] 115.9336 0.3582 23.7874 78.1056 99.0593 113.1224 129.2882 171.0880 4409.739 1.0006
Sigma[2,4] 115.5600 0.3567 23.6902 77.9545 98.7130 112.6443 128.7707 171.2230 4410.461 1.0006
Sigma[3,1] 112.9562 0.3502 23.2212 75.8780 96.4125 110.1725 125.9105 166.9113 4397.520 1.0006
Sigma[3,2] 115.9336 0.3582 23.7874 78.1056 99.0593 113.1224 129.2882 171.0880 4409.739 1.0006
Sigma[3,3] 116.6942 0.3550 23.7232 78.9387 99.7790 113.9760 129.9789 171.6126 4466.373 1.0005
Sigma[3,4] 114.3368 0.3526 23.4294 76.9899 97.7019 111.6760 127.4746 169.2446 4415.316 1.0006
Sigma[4,1] 112.1402 0.3485 23.0698 75.3913 95.7518 109.2474 125.0628 166.3431 4382.741 1.0006
Sigma[4,2] 115.5600 0.3567 23.6902 77.9545 98.7130 112.6443 128.7707 171.2230 4410.461 1.0006
Sigma[4,3] 114.3368 0.3526 23.4294 76.9899 97.7019 111.6760 127.4746 169.2446 4415.316 1.0006
Sigma[4,4] 115.3995 0.3517 23.4751 78.0471 98.7204 112.5614 128.5235 169.8665 4455.965 1.0006
gA 1.0620 0.1245 14.6900 0.0340 0.0898 0.1775 0.4269 5.0174 13915.329 1.0001
gB 1.2700 0.1858 16.4044 0.0337 0.0910 0.1850 0.4451 5.3205 7795.513 0.9999
gAB 1.7607 0.8901 76.1931 0.0336 0.0892 0.1815 0.4417 4.7939 7328.041 1.0002
tAfix 0.0082 0.0001 0.0092 -0.0099 0.0022 0.0081 0.0142 0.0266 22998.671 0.9999
tBfix -0.0098 0.0001 0.0097 -0.0294 -0.0161 -0.0098 -0.0035 0.0092 20278.671 1.0001
tABfix 0.0102 0.0001 0.0110 -0.0113 0.0027 0.0100 0.0174 0.0321 22202.737 0.9999
multiplier 15.0720 0.0225 1.5087 12.4519 14.0044 14.9639 15.9852 18.4010 4503.303 1.0006
muS[1,1] 99.7878 0.0097 1.5007 96.7878 98.8022 99.7920 100.7793 102.7502 23736.503 0.9999
muS[1,2] 99.8437 0.0101 1.5261 96.8263 98.8394 99.8515 100.8544 102.8282 22846.849 0.9999
muS[2,1] 99.4633 0.0101 1.5370 96.4336 98.4388 99.4681 100.4826 102.4751 23156.891 0.9999
muS[2,2] 99.8221 0.0099 1.5177 96.7971 98.8160 99.8334 100.8287 102.7931 23396.504 0.9999
lp__ -519.6183 0.0377 3.1076 -526.6027 -521.4763 -519.2696 -517.3638 -514.6343 6780.098 1.0001