R中的时间序列:如何计算R中多个时间序列变量相对于固定年份的百分比变化?

R中的时间序列:如何计算R中多个时间序列变量相对于固定年份的百分比变化?,r,matrix,time-series,percentage,R,Matrix,Time Series,Percentage,对于多个时间序列变量,如何计算相对于固定年份随时间变化的百分比 structure(list(haiarYear = 2009:2012, anyInf = c(25914L, 23601L, 22713L, 22654L), haiarPatDays = c(10402161L, 10289079L, 10212208L, 10033090L), rate = c(2.49121312388839,

对于多个时间序列变量,如何计算相对于固定年份随时间变化的百分比

structure(list(haiarYear = 2009:2012, 
               anyInf = c(25914L, 23601L, 22713L, 22654L), 
               haiarPatDays = c(10402161L, 10289079L, 10212208L, 10033090L), 
               rate = c(2.49121312388839, 
                        2.29379131018432, 
                        2.22410276014746, 
                        2.25792851454537)), 
               .Names = c("haiarYear", "anyInf", "haiarPatDays", "rate"), 
               row.names = c(NA, -4L), 
               class = "data.frame")
tsInfPatDays <- ts(tInfPatDays[,-1], start=2009)
options(digits=2)
我想计算每个变量相对于2009年的百分比变化:
anyInf
haiarPatDays
rate

对于一个变量,我可以将百分比变化计算为:

transform(tsInfPatDays, since2009 = (rate-rate[1])/rate[1]*100)
屈服:

anyInf haiarPatDays rate since2009
 25914     10402161 2.49      0.00
 23601     10289079 2.29     -7.92
 22713     10212208 2.22    -10.72
 22654     10033090 2.26     -9.36
以下计算相对于上一年的百分比变化,并对每个变量进行操作:

100*(tsInfPatDays/lag(tsInfPatDays, -1)-1)
给予:

Time Series:
Start = 2010 
End = 2012 
Frequency = 1 
     tsInfPatDays.anyInf tsInfPatDays.haiarPatDays tsInfPatDays.rate
2010               -8.93                    -1.087             -7.92
2011               -3.76                    -0.747             -3.04
2012               -0.26                    -1.754              1.52
将此作为一个模型,我希望能够通过索引2009年参考数据
tsInfPatDays[1,]

  anyInf haiarPatDays         rate 
2.59e+04     1.04e+07     2.49e+00
然后:

第一行似乎计算正确,但后面的行显然是错误的

我见过一种用于行减法的转置矩阵方法。虽然不是百分比,但作为概念证明,我尝试从时间序列行中减去参考行的值。我得到了以下错误:

t(tsInfPatDays-t(tsInfPatDays[1,]))

Error in `-.default`(tsInfPatDays, t(tsInfPatDays[1, ])) : 
  non-conformable arrays
如果在使用转置矩阵方法之前尝试提取数据,则会出现相同的错误:

 t(tsInfPatDays-t(drop(coredata(tsInfPatDays[1,]))))

 Error in `-.default`(tsInfPatDays, t(drop(coredata(tsInfPatDays[1, ])))) : 
 non-conformable arrays

可以在列上循环:

 ts(sapply(tsInfPatDays,function(x)(x-x[1])/x[1]*100), start= 2009)
Time Series:
Start = 2009 
End = 2012 
Frequency = 1 
         anyInf haiarPatDays       rate
2009   0.000000     0.000000   0.000000
2010  -8.925677    -1.087101  -7.924726
2011 -12.352396    -1.826092 -10.722100
2012 -12.580073    -3.548022  -9.364298

查看
data.table
包,然后查看
?data.table
中的
roll
mult
参数。虽然您的解决方案解决了我的问题,但我想我希望了解如何操作ts结构,以及为什么它们在某些方面像矩阵一样工作,而不是在其他方面。由于diff()和lag()是相对于其他时间点进行操作的,因此我认为将函数应用于具有绝对时间点引用的ts结构应该很容易。@penguinv22个人而言,我没有使用太多的
ts
,确切地说,对于这种“奇怪/没有统一”的行为。我更喜欢使用
xts
包。
 t(tsInfPatDays-t(drop(coredata(tsInfPatDays[1,]))))

 Error in `-.default`(tsInfPatDays, t(drop(coredata(tsInfPatDays[1, ])))) : 
 non-conformable arrays
 ts(sapply(tsInfPatDays,function(x)(x-x[1])/x[1]*100), start= 2009)
Time Series:
Start = 2009 
End = 2012 
Frequency = 1 
         anyInf haiarPatDays       rate
2009   0.000000     0.000000   0.000000
2010  -8.925677    -1.087101  -7.924726
2011 -12.352396    -1.826092 -10.722100
2012 -12.580073    -3.548022  -9.364298