[R] Lagged Covariate

Mahesh Samtani msamtani at gmail.com
Thu Mar 12 22:12:36 CET 2015


I am doing some mixed effects modeling where we would like to test the
hypothesis that the dependent variable (Dependent.var) as a function of
time depends on the value of the covariate 6 months to a year earlier. The
difficulty is that not all subjects have measurements every 6 months (as
shown below for the 1st three subjects). We would like to derive the new
datasets for the 6 month lag, 1 year lag and so on using efficient code.
For the 6 month lag the Time zero row could get thrown out and the
covariate column would move one cell down (and the covariate value at year
3 would be taken away as well). Is there some easy way to make these
datasets


   Subj.ID TIME Dependent.Var   Covariate
1        1  0.0   -0.02720222  0.05055147
2        1  0.5   -0.95878074  0.07402854
3        1  1.0    0.05178116  0.15676033
4        1  1.5   -0.90101795  0.05398925
5        1  2.0    0.05873045  0.35908569
6        1  2.5   -0.20770872  0.37844074
7        1  3.0    0.60331684 -1.05727034
8        2  0.0   -0.35234081 -0.43684877
9        2  0.5   -3.41076246  0.34309430
10       2  1.0    3.12190327  0.83680466
11       2  1.5   -0.04691748  0.59614786
12       2  2.0   -0.59465266 -0.09676054
13       2  2.5   -2.30089870 -0.68813666
14       2  3.0    0.07287830 -0.72876164
15       3  0.0   -0.94644384 -1.62981406
16       3  0.5   -1.06351763  0.19774138
17       3  1.0   -0.46948937 -0.59600225
18       3  1.5    1.80077781  0.46992426
19       3  2.0   -0.27466530  1.22268195
20       3  2.5   -0.87696707 -2.29894151
21       3  3.0    0.45811417  1.76158843

	[[alternative HTML version deleted]]



More information about the R-help mailing list