time series analysis with covariates

Viewed 4286

I have a dataset with data from thousands of individuals with measurement of a parameter X measured yearly the last 9 years.

Basicly they are in a dataframe df

id,year,x,feature
A,2016,376,female
A,2015,391,female
A,2014,376,female
A,2013,373,female
A,2012,347,female
A,2011,330,female
B,2016,398,male
B,2015,391,male
B,2014,410,male
B,2013,393,male
B,2012,408,male
B,2011,288,male
C,2016,2464,male
C,2015,2465,male
C,2014,2500,male
C,2013,2215,male
C,2012,2228,male
C,2011,1839,male

etc.

I want to estimate different models on these timeseries

like predict(x(t)) = f(x(t-1),x(t-2),...,x(t-n),feature, id (taken as a random factor))

I can see how to use ts for autoregressive modelling but it will calculate thosands of indvidual models, and I want a global prediction (with its inherent problems) based on the time history and the features.

lm is not a good idea since the data is highly autocorrelated. Any good ideas?

2 Answers

There are many possible models but here is a mixed effects model with AR1 structure that you can try.

library(nlme)

fm <- lme(x ~ year + feature, random = ~ year | id, DF,
    correlation = corAR1(form = ~ year | id))
summary(fm)

and here is a plot of the data:

library(ggplot2)

ggplot(DF, aes(year, x, group = id, col = feature)) + geom_line() + geom_point()

screenshot

Note: We have assumed this input data:

Lines <- "
id,year,x,feature
A,2016,376,female
A,2015,391,female
A,2014,376,female
A,2013,373,female
A,2012,347,female
A,2011,330,female
B,2016,398,male
B,2015,391,male
B,2014,410,male
B,2013,393,male
B,2012,408,male
B,2011,288,male
C,2016,2464,male
C,2015,2465,male
C,2014,2500,male
C,2013,2215,male
C,2012,2228,male
C,2011,1839,male"
library(zoo)
DF <- read.csv(text = Lines, strip.white = TRUE)
Related