[R] How to generate “aggregated” time lags?
Faradj Koliev
faradj.g at gmail.com
Fri Jul 15 01:19:29 CEST 2016
Dear all,
I hope you’re enjoying your summer!
I've been asked to "aggregate" my time lags from simple 1 year time lag to 1-3 year time lag. This could be done --I've been told --by simply taking the sum or mean of time lag 1,2, and 3.
I need your help here. How can I generate this "aggregated" time lag variable?
This is how far I've come:
First, my (example) logistic model:
print( model<- lrm(Y~X+A2, data=mydata))
Second, I created time lags 1,2,3, for the covariate X in the model.
mydata$lag1X <- Lag(mydata$X, +1)
mydata$lag2X <- Lag(mydata$X, +2)
mydata$lag3X <- Lag(mydata$X, +3)
Not sure how to go further....How do I take the sum or mean of these lag variables? How to create ”aggregated” time lag 1-3? All suggestions are very welcome!
A reproducible example (with lagged variables included )
dput(mydata)
structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("A",
"B", "C", "D"), class = "factor"), Year = c(1990L, 1991L, 1992L,
1993L, 1994L, 1995L, 1990L, 1991L, 1992L, 1993L, 1991L, 1992L,
1993L, 1994L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L
), X = c(1L, 1L, 2L, 3L, 4L, 4L, 0L, 1L, 1L, 2L, 1L, 2L, 3L,
3L, 1L, 2L, 3L, 4L, 5L, 5L, 6L), A1 = c(1L, 0L, 1L, 1L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L),
Y = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), A2 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L,
0L), lag1X = c(NA, 1L, 1L, 2L, 3L, 4L, 4L, 0L, 1L, 1L, 2L,
1L, 2L, 3L, 3L, 1L, 2L, 3L, 4L, 5L, 5L), lag2X = c(NA, NA,
1L, 1L, 2L, 3L, 4L, 4L, 0L, 1L, 1L, 2L, 1L, 2L, 3L, 3L, 1L,
2L, 3L, 4L, 5L), lag3X = c(NA, NA, NA, 1L, 1L, 2L, 3L, 4L,
4L, 0L, 1L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 4L)), .Names = c("Subject",
"Year", "X", "A1", "Y", "A2", "lag1X", "lag2X", "lag3X"), row.names = c(NA,
-21L), class = "data.frame")
[[alternative HTML version deleted]]
More information about the R-help
mailing list