%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % File: covariance_matrices.m % % Last modified: 12/08/2019 % % Author(s): Michael J Grayling (michael.grayling@newcastle.ac.uk) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% Initialise required symbolic variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% e = sym('e'); % sigma_e b = sym('b'); % sigma_b n = sym('n'); %%%%% D = 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set up covariance matrix for 'base' case of 9 patients (3 eth, 4 seq) X = zeros(27, 11); % First column is 1's for intercept term X(:, 1) = 1; % Fill in next 2 columns for period effects per_block = [0, 0; 1, 0; 0, 1]; for i = 1:9 X((1 + 3*(i - 1)):(3*i), 2:3) = per_block; end % Fill in next 2 for treatments, using Latin Square allocation tr_block = [0, 0; 1, 0; 0, 1; 1, 0; 0, 1; 0, 0; 0, 1; 0, 0; 1, 0]; X(:, 4:5) = [tr_block; tr_block; tr_block]; % Fill in next 2 columns for ethnicities X(10:18, 6) = 1; X(19:27, 7) = 1; % Fill in final 4 columns for interactions for i = 10:18 if X(i,4) == 1 X(i,8) = 1; end if X(i,5) == 1 X(i,9) = 1; end end for i = 19:27 if X(i,4) == 1 X(i,10) = 1; end if X(i,5) == 1 X(i,11) = 1; end end % Initialise covariance matrix of the responses Sigma_3 = b^2 + e^2*eye(3); Sigma = b*zeros(27, 27); for i = 1:9 Sigma((1 + 3*(i - 1)):(3*i), (1 + 3*(i - 1)):(3*i)) = Sigma_3; end % Compute covariance matrix of fixed effects cov = inv(transpose(X)*inv(Sigma)*X); % Then, in general if there are n patients, the covariance matrix becomes % 9/n times this general_cov = (9/n)*cov; % This gives 12sigma_e^2/n as the variance of the interaction terms %%%%% D = 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Set up covariance matrix for 'base' case of 12 patients (3 eth, 4 seq) X = zeros(48, 15); % First column is 1's for intercept term X(:, 1) = 1; % Fill in next 3 columns for period effects per_block = [0, 0, 0; 1, 0, 0; 0, 1, 0; 0, 0, 1]; for i = 1:12 X((1 + 4*(i - 1)):(4*i), 2:4) = per_block; end % Fill in next 2 for treatments, using Latin Square allocation tr_block = [0, 0, 0; 1, 0, 0; 0, 1, 0; 0, 0, 1; 1, 0, 0; 0, 1, 0; 0, 0, 1; 0, 0, 0; 0, 1, 0; 0, 0, 1; 0, 0, 0; 1, 0, 0; 0, 0, 1; 0, 0, 0; 1, 0, 0; 0, 1, 0]; X(:, 5:7) = [tr_block; tr_block; tr_block]; % Fill in next 2 columns for ethnicities X(17:32, 8) = 1; X(33:48, 9) = 1; % Fill in final 6 columns for interactions for i = 17:32 if X(i,5) == 1 X(i,10) = 1; end if X(i,6) == 1 X(i,11) = 1; end if X(i,7) == 1 X(i,12) = 1; end end for i = 33:48 if X(i,5) == 1 X(i,13) = 1; end if X(i,6) == 1 X(i,14) = 1; end if X(i,7) == 1 X(i,15) = 1; end end % Initialise covariance matrix of the responses Sigma_4 = b^2 + e^2*eye(4); Sigma = b*zeros(48, 48); for i = 1:12 Sigma((1 + 4*(i - 1)):(4*i), (1 + 4*(i - 1)):(4*i)) = Sigma_4; end % Compute covariance matrix of fixed effects cov = inv(transpose(X)*inv(Sigma)*X); % Then, in general if there are n patients, the covariance matrix becomes % 12/n times this general_cov = (12/n)*cov; % This gives 12sigma_e^2/n as the variance of the interaction terms