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

 

Omitting AB

model.omitAB.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
   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 tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
 }

 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));
     }
   }
 }

 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 += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

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

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

rstan::summary(stanfit.omitAB.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.9591 0.0106 1.5148 97.0332 98.9426 99.9514 100.9693 102.9795 20513.010 1.0000
Sigma[1,1] 118.1367 0.3367 24.3968 79.9970 100.8706 115.0178 131.9670 173.6816 5249.314 1.0000
Sigma[1,2] 119.5482 0.3440 24.8154 80.7914 101.8692 116.3986 133.6826 176.2572 5204.332 1.0000
Sigma[1,3] 117.9468 0.3405 24.5116 79.8964 100.5565 114.7555 131.8670 174.0313 5181.623 1.0000
Sigma[1,4] 117.1055 0.3380 24.3518 79.2421 99.6743 114.0471 131.0395 173.1725 5191.228 1.0000
Sigma[2,1] 119.5482 0.3440 24.8154 80.7914 101.8692 116.3986 133.6826 176.2572 5204.332 1.0000
Sigma[2,2] 123.8378 0.3527 25.5345 84.1158 105.6028 120.5936 138.3782 181.4534 5241.177 1.0000
Sigma[2,3] 121.0509 0.3486 25.1047 82.0080 103.1376 117.8412 135.4867 178.3581 5186.775 1.0000
Sigma[2,4] 120.6516 0.3463 24.9933 81.7629 102.6581 117.4988 134.9386 177.6668 5209.896 1.0000
Sigma[3,1] 117.9468 0.3405 24.5116 79.8964 100.5565 114.7555 131.8670 174.0313 5181.623 1.0000
Sigma[3,2] 121.0509 0.3486 25.1047 82.0080 103.1376 117.8412 135.4867 178.3581 5186.775 1.0000
Sigma[3,3] 121.7557 0.3460 25.0432 82.7377 104.0137 118.5213 136.0366 179.2507 5239.267 1.0000
Sigma[3,4] 119.3846 0.3429 24.7278 81.0709 101.7134 116.2630 133.4746 175.6486 5200.481 1.0000
Sigma[4,1] 117.1055 0.3380 24.3518 79.2421 99.6743 114.0471 131.0395 173.1725 5191.228 1.0000
Sigma[4,2] 120.6516 0.3463 24.9933 81.7629 102.6581 117.4988 134.9386 177.6668 5209.896 1.0000
Sigma[4,3] 119.3846 0.3429 24.7278 81.0709 101.7134 116.2630 133.4746 175.6486 5200.481 1.0000
Sigma[4,4] 120.4017 0.3415 24.7606 81.9955 102.5655 117.3900 134.6590 176.6339 5257.054 1.0000
gA 1.5451 0.3545 35.3563 0.0352 0.0926 0.1846 0.4438 5.1784 9949.880 0.9999
gB 1.0474 0.1083 10.6034 0.0348 0.0921 0.1840 0.4420 4.9899 9577.335 1.0004
tAfix 0.0512 0.0003 0.0505 -0.0479 0.0174 0.0512 0.0845 0.1505 23133.803 1.0001
tBfix -0.0637 0.0004 0.0554 -0.1744 -0.1005 -0.0637 -0.0266 0.0444 22716.953 0.9999
multiplier 2.5648 0.0012 0.1254 2.3342 2.4777 2.5597 2.6465 2.8248 10721.619 1.0000
muS[1,1] 99.9365 0.0106 1.5184 96.9995 98.9107 99.9231 100.9552 102.9546 20481.651 1.0000
muS[1,2] 100.1671 0.0105 1.5091 97.2436 99.1560 100.1569 101.1719 103.1689 20775.342 1.0000
muS[2,1] 99.7512 0.0107 1.5313 96.7675 98.7283 99.7545 100.7795 102.8041 20290.771 1.0001
muS[2,2] 99.9818 0.0106 1.5247 97.0011 98.9563 99.9770 101.0003 103.0152 20506.165 1.0001
lp__ -517.5278 0.0357 2.8788 -524.0535 -519.2484 -517.1974 -515.4366 -512.8954 6494.126 1.0001

   

3. Scale to the Trace

 

Omitting AB

model.omitAB.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
   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 tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
 }

 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));
     }
   }
 }

 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 += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

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

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

rstan::summary(stanfit.omitAB.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.9301 0.0099 1.5068 96.9467 98.9309 99.9376 100.9353 102.8373 23158.304 1.0001
Sigma[1,1] 114.9093 0.3308 23.1301 78.1838 98.4267 112.1320 127.7734 168.7592 4889.582 1.0003
Sigma[1,2] 116.2070 0.3383 23.5211 78.9008 99.4898 113.1820 129.3295 171.1543 4833.876 1.0002
Sigma[1,3] 114.6189 0.3355 23.2634 77.9481 98.2465 111.6705 127.5340 168.7966 4807.208 1.0002
Sigma[1,4] 113.8016 0.3336 23.1399 77.1702 97.4491 110.9440 126.5981 167.9818 4810.331 1.0002
Sigma[2,1] 116.2070 0.3383 23.5211 78.9008 99.4898 113.1820 129.3295 171.1543 4833.876 1.0002
Sigma[2,2] 120.4026 0.3474 24.2135 81.9303 103.2306 117.2993 134.1056 177.0744 4858.420 1.0002
Sigma[2,3] 117.6209 0.3437 23.8244 79.9736 100.7866 114.6171 130.8296 172.9236 4804.387 1.0002
Sigma[2,4] 117.2525 0.3421 23.7549 79.8775 100.4568 114.2468 130.6192 172.5901 4821.355 1.0002
Sigma[3,1] 114.6189 0.3355 23.2634 77.9481 98.2465 111.6705 127.5340 168.7966 4807.208 1.0002
Sigma[3,2] 117.6209 0.3437 23.8244 79.9736 100.7866 114.6171 130.8296 172.9236 4804.387 1.0002
Sigma[3,3] 118.3568 0.3420 23.8117 80.7140 101.5347 115.4310 131.6115 173.1926 4848.939 1.0002
Sigma[3,4] 115.9948 0.3393 23.5254 78.9225 99.4726 113.0437 129.0053 170.4675 4808.696 1.0002
Sigma[4,1] 113.8016 0.3336 23.1399 77.1702 97.4491 110.9440 126.5981 167.9818 4810.331 1.0002
Sigma[4,2] 117.2525 0.3421 23.7549 79.8775 100.4568 114.2468 130.6192 172.5901 4821.355 1.0002
Sigma[4,3] 115.9948 0.3393 23.5254 78.9225 99.4726 113.0437 129.0053 170.4675 4808.696 1.0002
Sigma[4,4] 117.0563 0.3383 23.5958 79.8647 100.5275 114.0635 130.1721 171.9314 4865.363 1.0002
gA 19.5017 15.1998 1345.0530 0.0335 0.0913 0.1820 0.4450 5.5095 7830.791 1.0003
gB 1.1347 0.1339 15.4817 0.0345 0.0899 0.1816 0.4292 5.0860 13368.071 1.0000
tAfix 0.0126 0.0001 0.0126 -0.0119 0.0042 0.0124 0.0209 0.0380 19768.030 1.0000
tBfix -0.0156 0.0001 0.0136 -0.0427 -0.0247 -0.0155 -0.0065 0.0107 20027.531 0.9999
multiplier 10.7966 0.0150 1.0553 8.9792 10.0564 10.7088 11.4315 13.1242 4930.412 1.0003
muS[1,1] 99.9073 0.0098 1.5059 96.9044 98.9122 99.9140 100.9083 102.8171 23507.182 1.0001
muS[1,2] 100.1436 0.0098 1.4995 97.1692 99.1522 100.1552 101.1504 103.0692 23473.032 1.0001
muS[2,1] 99.7166 0.0101 1.5252 96.7003 98.6972 99.7217 100.7348 102.6424 22889.535 1.0001
muS[2,2] 99.9528 0.0101 1.5220 96.9468 98.9441 99.9634 100.9678 102.8922 22761.181 1.0001
lp__ -517.5162 0.0366 2.9153 -524.2111 -519.2111 -517.1727 -515.4076 -512.8667 6355.134 1.0003

   

4. Scale to the Frobenius Norm

 

Omitting AB

model.omitAB.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
   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 tAfix;                        // standardized projected treatment effect A
   real tBfix;                        // standardized projected treatment effect B
 }

 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));
     }
   }
 }

 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 += inv_wishart_lpdf(Sigma | 5, I);
   target += normal_lpdf(tAfix | 0, sqrt(gA));
   target += normal_lpdf(tBfix | 0, sqrt(gB));
   for (k in 1:nS) {
     target += multi_normal_lpdf(Y[k] | to_vector(muS), Sigma);
   }
 }
"

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

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

rstan::summary(stanfit.omitAB.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.9303 0.0108 1.5201 96.9528 98.9157 99.9249 100.9453 102.9338 19869.358 1.0002
Sigma[1,1] 114.8907 0.3474 23.0571 78.6516 98.2259 112.1645 128.4732 168.0868 4403.757 1.0011
Sigma[1,2] 116.1821 0.3551 23.4137 79.2214 99.2325 113.2985 129.7868 170.5220 4348.443 1.0011
Sigma[1,3] 114.5877 0.3534 23.1699 78.0547 97.8573 111.8273 128.0491 167.8966 4297.952 1.0012
Sigma[1,4] 113.8016 0.3502 23.0089 77.6414 97.1599 110.9093 127.3156 167.1387 4317.005 1.0012
Sigma[2,1] 116.1821 0.3551 23.4137 79.2214 99.2325 113.2985 129.7868 170.5220 4348.443 1.0011
Sigma[2,2] 120.3663 0.3641 24.0747 82.3255 102.8778 117.3992 134.3137 175.5216 4371.904 1.0011
Sigma[2,3] 117.5843 0.3618 23.7072 80.1681 100.5512 114.6583 131.3951 171.9643 4293.578 1.0012
Sigma[2,4] 117.2442 0.3587 23.5961 80.0028 100.1674 114.2845 131.0429 171.3183 4327.466 1.0012
Sigma[3,1] 114.5877 0.3534 23.1699 78.0547 97.8573 111.8273 128.0491 167.8966 4297.952 1.0012
Sigma[3,2] 117.5843 0.3618 23.7072 80.1681 100.5512 114.6583 131.3951 171.9643 4293.578 1.0012
Sigma[3,3] 118.3197 0.3620 23.6993 80.9202 101.2784 115.3526 132.0073 172.8010 4286.128 1.0013
Sigma[3,4] 115.9977 0.3574 23.3941 79.1551 99.1164 113.1303 129.8124 169.4703 4283.570 1.0013
Sigma[4,1] 113.8016 0.3502 23.0089 77.6414 97.1599 110.9093 127.3156 167.1387 4317.005 1.0012
Sigma[4,2] 117.2442 0.3587 23.5961 80.0028 100.1674 114.2845 131.0429 171.3183 4327.466 1.0012
Sigma[4,3] 115.9977 0.3574 23.3941 79.1551 99.1164 113.1303 129.8124 169.4703 4283.570 1.0013
Sigma[4,4] 117.0817 0.3551 23.4435 80.1594 100.0839 114.2123 130.9687 170.4574 4358.931 1.0012
gA 0.9242 0.0931 8.8693 0.0338 0.0894 0.1786 0.4378 4.6912 9077.213 1.0006
gB 1.1238 0.3654 36.4595 0.0342 0.0904 0.1822 0.4322 4.1432 9953.451 1.0002
tAfix 0.0089 0.0001 0.0089 -0.0084 0.0029 0.0087 0.0147 0.0269 18574.830 1.0000
tBfix -0.0112 0.0001 0.0097 -0.0305 -0.0176 -0.0112 -0.0048 0.0075 16033.253 1.0000
multiplier 15.1828 0.0227 1.4974 12.6226 14.1112 15.0635 16.1217 18.4445 4349.283 1.0012
muS[1,1] 99.9052 0.0108 1.5201 96.9456 98.8933 99.8934 100.9212 102.9086 19943.973 1.0002
muS[1,2] 100.1439 0.0107 1.5137 97.1869 99.1324 100.1336 101.1520 103.1213 20178.928 1.0002
muS[2,1] 99.7167 0.0110 1.5374 96.7207 98.6909 99.7099 100.7479 102.7395 19598.153 1.0002
muS[2,2] 99.9554 0.0109 1.5343 96.9659 98.9280 99.9479 100.9827 102.9645 19785.271 1.0002
lp__ -517.4796 0.0368 2.8700 -524.0551 -519.1583 -517.1331 -515.4513 -512.8906 6098.713 1.0001