[R-sig-ME] time delayed response as a covariate in lme4
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:
>> '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,
> 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)
> -----END PGP SIGNATURE-----
> R-sig-mixed-models at r-project.org mailing list
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
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:
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