-
Notifications
You must be signed in to change notification settings - Fork 7
/
swe_splitCovariate.m
34 lines (31 loc) · 1.48 KB
/
swe_splitCovariate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [crossCov, longCov, avgCov] = swe_splitCovariate(cov, subject)
% Splits a covariate into pure cross-sectional and longitudinal components.
% =========================================================================
% The split is done first by extracting the within-subect average to build
% the cross-sectional covariate, which is also centered. Second, the
% longitudinal covariate is simply the difference between the original
% covariate and the non-centered cross-sectional covariate.
% =========================================================================
% FORMAT: [crossCov, longCov, avgCov] = swe_splitCovariate(cov, subject)
% -------------------------------------------------------------------------
% Inputs:
% - cov: the covariate to split
% - subject: vector contening the subject numbering
% -------------------------------------------------------------------------
% Outputs:
% - crossCov: the centered cross-sectional covariate
% - longCov: the longitudinal covariate
% - avgCrossCov: the average value (scalar) of the time-varying covariate
% =========================================================================
% written by Bryan Guillaume
% Version Info: $Format:%ci$ $Format:%h$
uSubject = unique(subject);
nSubj = length(uSubject);
crossCov = 0 * cov;
for i = 1:nSubj
crossCov(subject == uSubject(i)) = mean(cov(subject == uSubject(i)));
end
longCov = cov - crossCov;
avgCov = mean(crossCov);
crossCov = crossCov - avgCov;
end