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

marKo mtoncic at ffri.hr
Tue Jan 20 17:04:19 CET 2015


On 01/20/2015 03:07 PM, Ben Bolker wrote:
> -----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-----
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

Thanks for your reply Ben.

I thought that it might be something like that. I have another problem 
in that area, because I actually have two response variable (something 
like y and z coordinates) and, since they are on the same scale, I am 
trying to fit a models to the outcome conditional to the response 
coordinates.
To make it mode clear:

response    response_id    time    id
r1x    x    t1    1
r1z    z    t1    1
r2x    x    t2    1
r2z    z    t2    1
….

so the model actually looks something like that:

model<-lmer(response~response_id*poly(time, n)+(response_id*poly(time, 
n)|id), data=dataset).

My hope was that it can be done in some other way just to avoid 
potential errors that might occur when fiddling with transpositions like 
the one suggested (different number of observations per id and 
potentially missing x or y coordinate).
As for the chron object, since I'm trying to fit a polynomial, I have 
rescaled the time in a numeric form from 0 to 1 (the time is actually od 
the form YYYY/MM/DD HH:MM:SS an the span is of 3 weeks, but I am only 
interested in daily variation. So the beginning of the day is 0 and the 
end 1). The polynomial have a much more simple behavior in that range.



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