基于 ID 的时间序列插补

Time series Imputation based on ID

我正在处理时间序列数据。数据集是:

datALL <- read.table(header=TRUE, text="
                   ID  Year Align
                   A01    2017  329
                   A01    2016  NA
                   A01    2015  NA
                   A01    2014  314
                   A01    2013  NA
                   A01    2012  NA
                   A01    2011  432
                   A02    2017  4536
                   A02    2016  NA
                   A02    2015  NA
                   A02    2014  2345
                   A02    2013  NA
                   A02    2012  NA
                   A02    2011  1932
                   ")
datALL
    ID Year Align
1  A01 2017   329
2  A01 2016    NA
3  A01 2015    NA
4  A01 2014   314
5  A01 2013    NA
6  A01 2012    NA
7  A01 2011   432
8  A02 2017  4536
9  A02 2016    NA
10 A02 2015    NA
11 A02 2014  2345
12 A02 2013    NA
13 A02 2012   NA
14 A02 2011  1932

我想使用 imputeTS 包来估算缺失值。该软件包适用于个人 ID.

datA01 <- read.table(header=TRUE, text="
                   ID  Year Align
                   A01    2017  329
                   A01    2016  NA
                   A01    2015  NA
                   A01    2014  314
                   A01    2013  NA
                   A01    2012  NA
                   A01    2011  432
                   ")
datA01
   ID Year Align
1 A01 2017   329
2 A01 2016    NA
3 A01 2015    NA
4 A01 2014   314
5 A01 2013    NA
6 A01 2012    NA
7 A01 2011   432


### install.packages("imputeTS")
library(imputeTS)
datA01$Year <- ts(datA01[, c(2)])
datA01$Align1 <- na_kalman(datA01$Align)
dat1
   ID Year Align   Align1
1 A01 2017   329 329.0000
2 A01 2016    NA 318.9847
3 A01 2015    NA 312.7852
4 A01 2014   314 314.0000
5 A01 2013    NA 347.2150
6 A01 2012    NA 387.7720
7 A01 2011   432 432.0000

对于A02,它也可以完美运行:

datA02 <- read.table(header=TRUE, text="
                   ID  Year Align
                   A02    2017  4536
                   A02    2016  NA
                   A02    2015  NA
                   A02    2014  2345
                   A02    2013  NA
                   A02    2012  NA
                   A02    2011  1932
                   ")
datA02$Year <- ts(datA02[, c(2)])
datA02$Align1 <- na_kalman(datA02$Align)
datA02 
   ID Year Align   Align1
1 A02 2017  4536 4536.000
2 A02 2016    NA 3510.613
3 A02 2015    NA 3168.817
4 A02 2014  2345 2345.000
5 A02 2013    NA 2485.226
6 A02 2012    NA 2143.431
7 A02 2011  1932 1932.000

对于所有数据加在一起,它不会起作用,因为它需要所有 14 年作为一个连续的时间序列。根据ID,应该是每七年一次。我需要帮助来获取可以处理该问题的循环函数。

datALL$Year <- ts(datALL[, c(2)])
datALL$Align1 <- na_kalman(datALL$Align)
#### WRONG IMPUTATION DUE TO FAILUE IN SEPARATING YEARS BY ID
datALL
    ID Year Align    Align1
1  A01 2017   329  329.0000
2  A01 2016    NA  808.8287
3  A01 2015    NA  968.7716
4  A01 2014   314  314.0000
5  A01 2013    NA 1288.6573
6  A01 2012    NA 1448.6002
7  A01 2011   432  432.0000
8  A02 2017  4536 4536.0000
9  A02 2016    NA 1928.4289
10 A02 2015    NA 2088.3718
11 A02 2014  2345 2345.0000
12 A02 2013    NA 2408.2575
13 A02 2012    NA 2568.2004
14 A02 2017  1932 1932.0000

正确的数据应该是这样的

   ID Year Align   Align1
1 A01 2017   329 329.0000
2 A01 2016    NA 318.9847
3 A01 2015    NA 312.7852
4 A01 2014   314 314.0000
5 A01 2013    NA 347.2150
6 A01 2012    NA 387.7720
7 A01 2011   432 432.0000
8 A02 2017  4536 4536.000
9 A02 2016    NA 3510.613
10 A02 2015    NA 3168.817
11 A02 2014  2345 2345.000
12 A02 2013    NA 2485.226
13 A02 2012    NA 2143.431
14 A02 2011  1932 1932.000

dplyr是你的朋友:

library(imputeTS)
library(dplyr)

datALL %>%
  group_by(ID) %>%
  mutate(Align1 = na_kalman(Align))

结果:

    ID Year Align    Align1
1  A01 2017   329  329.0000
2  A01 2016    NA  318.9847
3  A01 2015    NA  312.7852
4  A01 2014   314  314.0000
5  A01 2013    NA  347.2150
6  A01 2012    NA  387.7720
7  A01 2011   432  432.0000
8  A02 2017  4536 4536.0000
9  A02 2016    NA 3510.6131
10 A02 2015    NA 3168.8175
11 A02 2014  2345 2345.0000
12 A02 2013    NA 2485.2262
13 A02 2012    NA 2143.4306
14 A02 2011  1932 1932.0000

请注意,如果我们在 mutate 中将 year 变为 ts,我们会收到以下警告消息,因为我们正试图将两个不同的时间序列组合在一起:

Warning messages: 1: In mutate_impl(.data, dots) : Vectorizing 'ts' elements may not preserve their attributes 2: In mutate_impl(.data, dots) : Vectorizing 'ts' elements may not preserve their attributes

year 转换为 ts 实际上没有必要(而且不正确),因为 na_kalman 将数字向量作为输入,而 Align 变量是采用的变量时间序列的值。