【问题标题】:linear spine interpolation of time-series by groups按组对时间序列进行线性脊柱插值
【发布时间】:2019-01-16 18:00:55
【问题描述】:

我正在处理从 1990 年到 2018 年的数千家上市公司资产负债表的时间序列。在某些年份我只有年度资产负债表,而在其他年份我有 5 个资产负债表,包括 1 个年度和 4 个季度资产负债表。我正在尝试使用所有可用的信息。资产负债表日期始终为 xxxx-01-01/xxxx-03-31/xxxx-06-30/xxxx-09-30/xxxx-12-31。我选择代号、日期、长期负债和短期负债。我想首先计算长期和短期负债的总和作为新列,并按每个月的代码号对新列进行线性脊柱插值。日期的格式为月年。

code       date type           cl        ll       
 1   1990-12-31    A     56280000         0        
 1   1991-12-31    A     77230000         0        
 1   1992-12-31    A    195893200         0        
 1   1993-01-01    A            0         0        
 1   1994-06-30    A            0         0        
 1   1994-12-31    A            0         0        
 1   1996-12-31    A            0         0        
 2   1991-12-31    A    374334527   3500000   
 2   1992-12-31    A    688472115  19820785  
 2   1993-12-31    A   1135584690  70268722  
 2   1994-12-31    A   1442120726  85175588  
 2   1995-06-30    A   1571620470         0 

我知道如何在使用 splinefun 和 na.approx 的时间间隔恒定时做到这一点。但我不知道如何处理非常量的时间间隔。谢谢!

【问题讨论】:

    标签: r interpolation zoo linear-interpolation


    【解决方案1】:

    我刚刚得到了想要的结果。这个想法来自一个类似的问题https://stackoverflow.com/a/31383995/10714457。值得注意的一件事是月份列是字符形式的。我需要as.numeric(month) 首先将它们转换为数字。

     DF$month <- format(as.Date(DF$date), "%m")
     DF$year <- format(as.Date(DF$date), "%Y")
     res <- setDT(DF)[, .SD[match(1:12, as.numeric(month))], by = .(year, code)]
     cols <- c("ll", "cl", "ncl")
     Interpolation <- res[, (cols) := 
     lapply(.SD, na.approx, na.rm = FALSE), .SDcols = cols]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-01
      • 2018-07-11
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2016-01-16
      • 2020-10-19
      • 1970-01-01
      相关资源
      最近更新 更多