滞后变量的内存错误

问题描述 投票:1回答:2

编辑:假数据例如

df = matrix(runif(50*507), nrow = 50, ncol = 507)
df = data.frame(df)
df[,1] = seq(as.Date("2017/1/1"), as.Date("2017/2/19"), "days")
names(df) = paste0("var", 1:507)
names(df)[505:507] = c("mktrf", "smb", "hml")
names(df)[1] = "Date"

所有的dep都是

x = df[,505:507]

所有的indep var

y <- df[,2:504]

我有一个名为shift的函数我想应用于df的每一列。该函数滞后于变量。该功能如下,并将指定的列移动指定的数字。

shift<-function(x,shift_by){
  stopifnot(is.numeric(shift_by))
  stopifnot(is.numeric(x))

  if (length(shift_by)>1)
    return(sapply(shift_by,shift, x=x))

  out<-NULL
  abs_shift_by=abs(shift_by)
  if (shift_by > 0 )
    out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
  else if (shift_by < 0 )
    out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
  else 
    out<-x
  out
}

当我使用像这样的sapply函数时,y是一个由时间序列变量组成的数据帧,我想要滞后:

y_lag <- sapply(y,shift,-1 )

我收到以下错误:

Error: cannot allocate vector of size 54.2 Mb
In addition: Warning messages:
1: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)
2: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)
3: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)
4: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)
5: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)
6: In unlist(x, recursive = FALSE) :
  Reached total allocation of 8072Mb: see help(memory.size)

我的问题:我可以使用不同的方法来延迟列的每个元素,同时仍然使用lm包吗?或者我如何解决我遇到的内存问题?我不能使用不同的电脑。

r recursion memory lag sapply
2个回答
0
投票

我能够使用其他问题中描述的lagpad函数使其工作:

lagpad <- function(x, k=1) {
  i<-is.vector(x)
  if(is.vector(x)) x<-matrix(x) else x<-matrix(x,nrow(x))
  if(k>0) {
    x <- rbind(matrix(rep(NA, k*ncol(x)),ncol=ncol(x)), matrix(x[1:(nrow(x)-k),], ncol=ncol(x)))
  }
  else {
    x <- rbind(matrix(x[(-k+1):(nrow(x)),], ncol=ncol(x)),matrix(rep(NA, -k*ncol(x)),ncol=ncol(x)))
  }
  if(i) x[1:length(x)] else x
}

这基本上是r2evans描述的,改变了整个df。


0
投票

在这种情况下,有几种选择可以避免使用sapply。一种选择是使用mutate_all

library(dplyr)

y_lag <- mutate_all(y, shift, shift_by = -1)

tail(y_lag)
#var2      var3      var4      var5       var6      var7      var8       var9      var10
#45 0.26817677 0.9664805 0.2849259 0.6375189 0.20889115 0.1530204 0.6500325 0.78397715 0.32936124
# many more rows to follow
© www.soinside.com 2019 - 2024. All rights reserved.