因此,我正在处理一个444天内每日数据的数据框。我有几个变量,我想在回归模型中使用滞后(lm
)。我想每次滞后7次。我目前正在产生这样的滞后......
email_data$email_reach1 <- lag(ts(email_data$email_reach, start = 1, end = 444), 1)
email_data$email_reach2 <- lag(ts(email_data$email_reach, start = 1, end = 444), 2)
email_data$email_reach3 <- lag(ts(email_data$email_reach, start = 1, end = 444), 3)
email_data$email_reach4 <- lag(ts(email_data$email_reach, start = 1, end = 444), 4)
email_data$email_reach5 <- lag(ts(email_data$email_reach, start = 1, end = 444), 5)
email_data$email_reach6 <- lag(ts(email_data$email_reach, start = 1, end = 444), 6)
email_data$email_reach7 <- lag(ts(email_data$email_reach, start = 1, end = 444), 7)
然后,我为每个我想要滞后的变量重复这个。
这似乎是实现这一目标的可怕方式。还有更好的东西吗?
我已经考虑过落后整个数据框架了,但是我不知道你如何为结果分配变量名称并将其合并回原始数据框。 / p>
答案 0 :(得分:7)
您也可以使用data.table
。 (HT到@akrun)
set.seed(1)
email_data <- data.frame(dates=1:10, email_reach=rbinom(10, 10, 0.5))
library(data.table)
setDT(email_data)[, paste0('email_reach', 1:3) := shift(email_reach, 1:3)][]
# dates email_reach email_reach1 email_reach2 email_reach3
# 1: 1 4 NA NA NA
# 2: 2 4 4 NA NA
# 3: 3 5 4 4 NA
# 4: 4 7 5 4 4
# 5: 5 4 7 5 4
# 6: 6 7 4 7 5
# 7: 7 7 7 4 7
# 8: 8 6 7 7 4
# 9: 9 6 6 7 7
#10: 10 3 6 6 7
答案 1 :(得分:3)
另一种方法是使用xts
库。接下来是一个小例子,我们从:
x <- ts(matrix(rnorm(100),ncol=2), start=c(2009, 1), frequency=12)
head(x)
Series 1 Series 2
[1,] -1.82934747 -0.1234372
[2,] 1.08371836 1.3365919
[3,] 0.95786815 0.0885484
[4,] 0.59301446 -0.6984993
[5,] -0.01094955 -0.3729762
[6,] -0.19256525 0.3137705
将其转换为xts
,调用lag()
,此处有0,1,2滞后以最小化输出:
library(xts)
head(lag(as.xts(x),0:2))
Series.1 Series.2 Series.1.1 Series.2.1 Series.1.2 Series.2.2
jan 2009 -1.82934747 -0.1234372 NA NA NA NA
feb 2009 1.08371836 1.3365919 -1.82934747 -0.1234372 NA NA
mar 2009 0.95786815 0.0885484 1.08371836 1.3365919 -1.8293475 -0.1234372
apr 2009 0.59301446 -0.6984993 0.95786815 0.0885484 1.0837184 1.3365919
maj 2009 -0.01094955 -0.3729762 0.59301446 -0.6984993 0.9578682 0.0885484
jun 2009 -0.19256525 0.3137705 -0.01094955 -0.3729762 0.5930145 -0.6984993
答案 2 :(得分:2)
对于任何给定的n
,我认为这与上面的代码相同。
n <- 7
for (i in 1:n) {
email_data[[paste0("email_reach", i)]] <- lag(ts(email_data$email_reach, start = 1, end = 444), i)
}
答案 3 :(得分:1)
基于Molx的答案,但是对于任何变量列表都进行了推广,并修补了一下......感谢Molx!
do_lag <- function(the_data, variables, num_periods) {
num_vars <- length(variables)
num_rows <- nrow(the_data)
for (j in 1:num_vars) {
for (i in 1:num_periods) {
the_data[[paste0(variables[j], i)]] <- c(rep(NA, i), head(the_data[[variables[j]]], num_rows - i))
}
}
return(the_data)
}
答案 4 :(得分:1)
这不是一个真正的答案,只是使用答案格式作为上述警告的详细说明:
email_data <- data.frame( email_reach=ts(email_data$email_reach, start = 1, end = 444))
然后你的代码就是你得到的:
> head(email_data, 10)
email_reach email_reach1 email_reach2 email_reach3 email_reach4
1 4 4 4 4 4
2 4 4 4 4 4
3 5 5 5 5 5
4 7 7 7 7 7
5 4 4 4 4 4
6 7 7 7 7 7
7 7 7 7 7 7
8 6 6 6 6 6
9 6 6 6 6 6
10 3 3 3 3 3
email_reach5 email_reach6 email_reach7
1 4 4 4
2 4 4 4
3 5 5 5
4 7 7 7
5 4 4 4
6 7 7 7
7 7 7 7
8 6 6 6
9 6 6 6
10 3 3 3
答案 5 :(得分:0)
collapse::flag
提供了解决此问题的常规且快速(基于C ++)的解决方案:
library(collapse)
# Time-series (also supports xts and others)
head(flag(AirPassengers, -1:2))
## F1 -- L1 L2
## Jan 1949 118 112 NA NA
## Feb 1949 132 118 112 NA
## Mar 1949 129 132 118 112
## Apr 1949 121 129 132 118
## May 1949 135 121 129 132
## Jun 1949 148 135 121 129
# Time-series matrix
head(flag(EuStockMarkets, -1:2))
## Time Series:
## Start = c(1991, 130)
## End = c(1998, 169)
## Frequency = 260
## F1.DAX DAX L1.DAX L2.DAX F1.SMI SMI L1.SMI L2.SMI F1.CAC CAC L1.CAC L2.CAC F1.FTSE FTSE L1.FTSE L2.FTSE
## 1991.496 1613.63 1628.75 NA NA 1688.5 1678.1 NA NA 1750.5 1772.8 NA NA 2460.2 2443.6 NA NA
## 1991.500 1606.51 1613.63 1628.75 NA 1678.6 1688.5 1678.1 NA 1718.0 1750.5 1772.8 NA 2448.2 2460.2 2443.6 NA
## 1991.504 1621.04 1606.51 1613.63 1628.75 1684.1 1678.6 1688.5 1678.1 1708.1 1718.0 1750.5 1772.8 2470.4 2448.2 2460.2 2443.6
## 1991.508 1618.16 1621.04 1606.51 1613.63 1686.6 1684.1 1678.6 1688.5 1723.1 1708.1 1718.0 1750.5 2484.7 2470.4 2448.2 2460.2
## 1991.512 1610.61 1618.16 1621.04 1606.51 1671.6 1686.6 1684.1 1678.6 1714.3 1723.1 1708.1 1718.0 2466.8 2484.7 2470.4 2448.2
## 1991.515 1630.75 1610.61 1618.16 1621.04 1682.9 1671.6 1686.6 1684.1 1734.5 1714.3 1723.1 1708.1 2487.9 2466.8 2484.7 2470.4
# Data frame
head(flag(airquality[1:3], -1:2))
## F1.Ozone Ozone L1.Ozone L2.Ozone F1.Solar.R Solar.R L1.Solar.R L2.Solar.R F1.Wind Wind L1.Wind L2.Wind
## 1 36 41 NA NA 118 190 NA NA 8.0 7.4 NA NA
## 2 12 36 41 NA 149 118 190 NA 12.6 8.0 7.4 NA
## 3 18 12 36 41 313 149 118 190 11.5 12.6 8.0 7.4
## 4 NA 18 12 36 NA 313 149 118 14.3 11.5 12.6 8.0
## 5 28 NA 18 12 NA NA 313 149 14.9 14.3 11.5 12.6
## 6 23 28 NA 18 299 NA NA 313 8.6 14.9 14.3 11.5
# Panel lag
head(flag(iris[1:2], -1:2, iris$Species))
## Panel-lag computed without timevar: Assuming ordered data
## F1.Sepal.Length Sepal.Length L1.Sepal.Length L2.Sepal.Length F1.Sepal.Width Sepal.Width L1.Sepal.Width L2.Sepal.Width
## 1 4.9 5.1 NA NA 3.0 3.5 NA NA
## 2 4.7 4.9 5.1 NA 3.2 3.0 3.5 NA
## 3 4.6 4.7 4.9 5.1 3.1 3.2 3.0 3.5
## 4 5.0 4.6 4.7 4.9 3.6 3.1 3.2 3.0
## 5 5.4 5.0 4.6 4.7 3.9 3.6 3.1 3.2
## 6 4.6 5.4 5.0 4.6 3.4 3.9 3.6 3.1
类似地,collapse::fdiff
和collapse::fgrowth
支持在(多元)时间序列和面板上适当地滞后/领先和迭代(准,对数)差异和增长率。 p>