我想计算对数增长率,我正努力使它在data.table的by
- 子句中使用两个变量。
我确实有一个data.table,它涵盖了一段时间内的生产情况,我想计算一段时间内每组的对数增长率。 p>
library(zoo)
library(data.table)
library(ggplot2)
library(dplyr)
DT <- structure(list(Year.Quarter = structure(c(2015, 2015, 2015, 2015,
2015, 2015.25, 2015.25, 2015.25, 2015.25, 2015.25, 2015.5, 2015.5,
2015.5, 2015.5, 2015.5, 2015.75, 2015.75, 2015.75, 2015.75, 2015.75,
2016, 2016, 2016, 2016, 2016, 2016.25, 2016.25, 2016.25, 2016.25,
2016.25), class = "yearqtr")
,Group = structure(c(2L, 1L, 4L,
3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA), .Label = c("1", "2", "3", "4"), class = "factor")
, Conventional.Prod = c(11.78, 7.31, 7.34, 9.44, 28.72, 11.32, 5.27, 7.47, 8.08, 27.14, 11.49,
4.65, 7.63, 7.07, 25.93, 10.69, 3.68, 6.96, 6.72, 18.31, 9.28,
3.69, 6.86, 6.34, 19.14, 9.25, 3.69, 6.9, 6.16, 17.7)
, Unconventional.Prod = c(15.22, 10.69, 7.66, 15.56, 30.28, 15.68, 10.73, 7.53, 15.92, 29.86,
13.51, 10.35, 7.37, 15.93, 28.07, 13.31, 10.32, 7.04, 16.28,
25.69, 12.72, 9.31, 7.14, 16.66, 25.86, 12.75, 9.31, 7.1, 16.84, 24.3))
, .Names = c("Year.Quarter", "Group", "Conventional.Prod", "Unconventional.Prod"), row.names = c(NA, -30L), class = c("data.table",
"data.frame"))
DT[, .( Conventional.Prod
, d.log.Conventional.Prod = log(Conventional.Prod, base = exp(1)) - shift(log(Conventional.Prod, base = exp(1)), n = 1L , fill = NA, type = "lag")
, Log.Conventional.Prod = log(Conventional.Prod, base = exp(1))
, Lag.Log.Conventional.Prod = shift(log(Conventional.Prod, base = exp(1)), n = 1L , fill = NA, type = "lag")
), by = list(Group, Year.Quarter)]
我不知道,为什么它没有被Group变量正确分组和排序,以及为什么不能计算生产的滞后值。我不认为因子变量存在问题,因为排序工作得很好。
DT[order(Group, Year.Quarter)]
Year.Quarter Group Conventional.Prod Unconventional.Prod
1: 2015 Q1 1 7.31 10.69
2: 2015 Q2 1 5.27 10.73
3: 2015 Q3 1 4.65 10.35
4: 2015 Q4 1 3.68 10.32
5: 2016 Q1 1 3.69 9.31
6: 2016 Q2 1 3.69 9.31
7: 2015 Q1 2 11.78 15.22
8: 2015 Q2 2 11.32 15.68
9: 2015 Q3 2 11.49 13.51
10: 2015 Q4 2 10.69 13.31
[...]
答案 0 :(得分:1)
你可以这样做:
setkey(DT, Group, Year.Quarter)
logG = function(x) c(NA, diff(log(x)))
DT[!is.na(Group), .(Year.Quarter, logG(Conventional.Prod), logG(Unconventional.Prod)), by='Group']
# Group Year.Quarter V2 V3
# 1: 1 2015 Q1 NA NA
# 2: 1 2015 Q2 -0.327212911 0.0037348316
# 3: 1 2015 Q3 -0.125163143 -0.0360570369
# 4: 1 2015 Q4 -0.233954467 -0.0029027597
# 5: 1 2016 Q1 0.002713706 -0.1029946688
# 6: 1 2016 Q2 0.000000000 0.0000000000
# 7: 2 2015 Q1 NA NA
# 8: 2 2015 Q2 -0.039832105 0.0297756625
# 9: 2 2015 Q3 0.014906019 -0.1489558630
# 10: 2 2015 Q4 -0.072168367 -0.0149145196
# 11: 2 2016 Q1 -0.141447178 -0.0453400745
# 12: 2 2016 Q2 -0.003237995 0.0023557137
# ...
答案 1 :(得分:0)
扩展@sirallen的答案我确实得到了没有任何附加功能的解决方案,只有data.table
工具。
setkey(DT, Group, Year.Quarter)
DT[, .(Year.Quarter, Conventional.Prod
, d.log.Conventional.Prod = log(Conventional.Prod, base = exp(1)) - shift(log(Conventional.Prod, base = exp(1)), n = 1L , fill = NA, type = "lag")
, Log.Conventional.Prod = log(Conventional.Prod, base = exp(1))
, Lag.Log.Conventional.Prod = shift(log(Conventional.Prod, base = exp(1)), n = 1L , fill = NA, type = "lag")
), by = list(Group)]
如果有人可以解释为什么在按两个变量分组时它不起作用,那就太好了。