[R-sig-ME] time delayed response as a covariate in lme4

Ben Bolker bbolker at gmail.com
Tue Jan 20 15:07:01 CET 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 15-01-20 08:24 AM, marKo wrote:
> I have a dataset which have a continuous outcome variable and a
> time (chron) covariate for 99 subjects (id). To get an idea:
> 
>> str(dataframe)
> 'data.frame':   36352 obs. of  9 variables: $ response  : int  100
> 79 63 50 71 73 62 72 76 77 ... $ id   : Factor w/ 99 levels
> "g1_1","g1_12",..: 2 2 2 2 2 2 2 2 2 2 ... $ time   :Classes
> 'chron', 'dates', 'times'  atomic [1:36352] 15875 15875 15875 15875
> 15875 ... .. ..- attr(*, "format")= Named chr [1:2] "m/d/y"
> "h:m:s" .. .. ..- attr(*, "names")= chr [1:2] "dates" "times" ..
> ..- attr(*, "origin")= Named num [1:3] 1 1 1970 .. .. ..- attr(*,
> "names")= chr [1:3] "month" "day" "year"
> 
> I would like to use the time delayed response as a
> predictor/covariate. Lets say a would like to use response at
> time-1 as a covariate. How can this be done?
> 
> Something like (conceptually):
> 
> model<-lmer(response~poly(time, n) + response(time -1) +
> (poly(time, n)|id, data=dataframe)
> 
> I suppose that I could use some correlation structure for this (in
> nlme, because I think that lme4 do not support this a this stage),
> although I'd rather do this in lme4.

  The standard way to do this in R is to shift the variable by
creating a shifted response variable that is NA in the first row
(because we don't know the response before the observations started)
and runs from 1 to (nobs-1), e.g.

  dataframe <- transform(dataframe,
    shiftresp=c(NA,response[1:(nrow(dataframe)-1)])

You can also use c(NA,head(response,-1)) for this although it may be
less transparent.

  I might suggest transforming your chron response to an explicit
numeric variable -- it might be more transparent (e.g. do you want
your covariate scale to be in response per second?)


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQEcBAEBAgAGBQJUvmEFAAoJEOCV5YRblxUH3BsIAM2S1IFo7hraXTP+gHk1vAG6
vla9p8pjYCYaBlnVVc3zdLXJZP1+BhlXh6A/LTuU1rnc0e4O9yQwW5JM+SqVdDua
Ur3XhMnR+n/PUvJyaoSQ92fFv9rpgP/JtDf36Om2VLodLNVBnWxUrSnnTnd/QeE4
hUv79mGE/RqbueP0H8YwcNAmNmZR/iMd+uC2nYli9Viv42Fk6lE/oNs13wW5AiOT
BytB45z0rX5qesjam+HY9UX+0dejSV5ldRN8fQ9SJPSMddDZzny5KIbmdgho6lxc
0N4suZXIfztU56IbAYJDDwCZmE7Vuv3rkMSci1jY6UbaYCihW86V9BYzK3QTZ+I=
=h/4l
-----END PGP SIGNATURE-----



More information about the R-sig-mixed-models mailing list