简体   繁体   English

协变量的时间序列分析

[英]time series analysis with covariates

I have a dataset with data from thousands of individuals with measurement of a parameter X measured yearly the last 9 years. 我有一个数据集,其中包含来自数千个人的数据,测量了过去9年每年测量的参数X.

Basicly they are in a dataframe df 基本上它们在数据帧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(tn),feature, id (taken as a random factor)) 像predict(x(t))= f(x(t-1),x(t-2),...,x(tn),feature,id(作为随机因子))

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. 我可以看到如何使用ts进行自回归建模,但它会计算单个模型的thosands,我想要根据时间历史和特征进行全局预测(及其固有的问题)。

lm is not a good idea since the data is highly autocorrelated. lm不是一个好主意,因为数据是高度自相关的。 Any good ideas? 有什么好主意吗?

There are many possible models but here is a mixed effects model with AR1 structure that you can try. 有许多可能的模型,但这里有一个AR1结构的混合效果模型,您可以尝试。

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()

截图

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)

The statement about the function f() arises many choices. 关于函数f()的陈述产生了许多选择。

However, within the linear class, you can use vector generalized linear models (via vglm()) to fit generalized linear models with ARMA (or GARCH) structures, incorporating covariates. 但是,在线性类中,您可以使用向量广义线性模型(通过vglm())来拟合具有ARMA(或GARCH)结构的广义线性模型,并结合协变量。

For example, assuming the (presupposed) random errors are normally distributed, you can use the family function ARff() from package VGAMextra , as follows. 例如,假设(预先假定的)随机错误是正态分布的,您可以使用包VGAMextra的族函数ARff() ,如下所示。

The second option, however, uses the non-parametric version, ie, VGAMs, via smart prediction. 然而,第二选项通过智能预测使用非参数版本,即VGAM。 The only drawback is that vglms/vgams do not handle random effects. 唯一的缺点是vglms / vgams不处理随机效果。

library(VGAM)
library(VGAMextra)
# Fitting a linear model to the mean of the normal distribution
# allowing an AR(3) struture. Use the modelling function vglm() and
# the family functions ARff()
df.read <- DF   # DF as given by G.G.
fit.Lines <- vglm(x ~  feature , ARff(order = 3,
                                       zero = c("Var", "ARcoeff")), 
              data = df.read, trace = TRUE)
coef(fit.Lines, matrix = TRUE)
summary(fit.Lines, HD = FALSE)

with(df.read, plot(fitted.values(fit.Lines) ~ year,
               ylim = c(0, 3000),
 pch = 19, col = as.factor(feature)))


# Using VGAMs, here, the family function uninormal() is utilized. 
#

df.read2 <- data.frame(embed(df.read$x, 4))
names(df.read2) <- c("x", "xLag1", "xLag2", "xLag3")
df.read2 <- transform(df.read2, year = df.read$year[-c(1:3)],
                        feature = df.read$feature[-c(1:3)])
fit.Lines.vgams <- vgam(x ~ sm.bs(xLag1) + sm.bs(xLag2) + 
                      sm.bs(xLag3) + feature + year, 
                    uninormal, data = df.read2, trace = TRUE)

with(df.read2, plot(fitted.values(fit.Lines.vgams) ~ year,
               ylim = c(0, 3000),
               pch = 19, col = as.factor(feature)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM