【问题标题】:How can I create a list of time series from a dataframe based off a column's unique values?如何根据列的唯一值从数据框中创建时间序列列表?
【发布时间】:2021-09-11 04:13:46
【问题描述】:

创建的数据示例

date = seq(as.Date("2019/01/01"), by = "month", length.out = 48)
productB = rep("B",48)
productB = rep("B",48)
productA = rep("A",48)
productA = rep("A",48)
subproducts1=rep("1",48)
subproducts2=rep("2",48)
subproductsx=rep("x",48)
subproductsy=rep("y",48)
b1 <- c(rnorm(30,5), rep(0,18))
b2 <- c(rnorm(30,5), rep(0,18))
b3 <-c(rnorm(30,5), rep(0,18))
b4 <- c(rnorm(30,5), rep(0,18))

创建数据框

dfone <- data.frame("date"= rep(date,4),
         "product"= c(rep(productB,2),rep(productA,2)),
         "subproduct"= c(subproducts1,subproducts2,subproductsx,subproductsy),
         "actuals"= c(b1,b2,b3,b4))

如何根据上述数据帧上的子产品创建具有训练/测试拆分的时间序列列表?有 192 行和 4 个子产品,因此每个子产品 48 行意味着 4 个时间序列,但由于训练和测试拆分,我希望列表中有 8 个元素。

编辑:

for(i in unique(dfone$subproduct)) {
    nam <- paste("df", i, sep = ".")
    assign(nam, dfone[dfone$subproduct==i,])
}

list_df <- list(df.1,df.2,df.x,df.y) %>%
lapply( function(x) x[(names(x) %in% c("date", "actuals"))])

for (i in 1:length(list_df)) {
   assign(paste0("df", i), as.data.frame(list_df[[i]]))
  }
combined_dfs <- merge(merge(merge(df1, df2, by='date', all=T), df3, 
by='date', all=T),df4,by="date",all=T)
colnames(combined_dfs) <-  
c("date","actualB1","actualB2","actualAx","actualAy")


list_ts <- lapply(combined_dfs, function(t) 
ts(t,start=c(2019,1),end=c(2021,6), frequency = 12)) %>%
              lapply( function(t) ts_split(t,sample.out= 
(0.2*length(t))))    # creates my train test split
list_ts <- do.call("rbind", list_ts)  #Creates a list of time series

上面几乎是我想要的,但是有没有更简单的方法来做 merge(merge() 部分?

【问题讨论】:

    标签: r dplyr time-series data-manipulation train-test-split


    【解决方案1】:

    这样的?

    library(dplyr)
    train_frac = 0.8
    dfone_split <- dfone %>%
      mutate(set = sample(c("train", "test"), n(), replace = TRUE, 
                          prob = c(train_frac, 1 - train_frac)))
    dfone_train <- dfone_split %>% filter(set == "train")
    dfone_test <- dfone_split %>% filter(set == "test")
    

    您还可以查看rsample 包,它提供了多种控制拆分的方法,例如在 windows 中使用时间采样。

    【讨论】:

    • 嘿,我已经更新了我上面的帖子。我唯一想改变的是 combine_dfs 部分。有没有办法更改代码,所以如果我说 100 个独特的子产品,我不需要不断添加 merge()?
    • 这似乎是一个不同的问题,我在您的帖子中没有看到足够的信息来重现它。我看到类似的问题是这样回答的:stackoverflow.com/questions/14096814/…
    • Opps,我添加了使该代码块运行所需的功能。问题是它并不比我目前拥有的解决方案更好。在 R 中使事情自动化/不那么硬的代码具有挑战性
    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2023-02-23
    • 2014-01-06
    • 2020-03-31
    • 1970-01-01
    相关资源
    最近更新 更多