【问题标题】:Convert data.frame to time series将 data.frame 转换为时间序列
【发布时间】:2020-03-21 16:37:52
【问题描述】:

我有一个关于如何将 df 转换为时间序列的问题。我是 R 新手,我正在为这个操作而苦苦挣扎。

这些是我的 df 的一些行,名为“test”:

> test
SM    weekY   week       art   cat   flagP Woy   year  ispromo    yval  yqta price ln_yval ln_price
   <chr> <chr>   <date>     <chr> <chr> <chr> <chr> <chr> <chr>     <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 11111 2016/01 2016-01-03 Z0005 C10   0     01    2016  0        59839.  4060  14.7   11.0      2.69
 2 11111 2016/02 2016-01-10 Z0005 C10   0     02    2016  0        38186.  2640  14.5   10.6      2.67
 3 11111 2016/03 2016-01-17 Z0005 C10   0     03    2016  0        38986.  2660  14.7   10.6      2.68

我的日期变量是“星期”,它的频率不一定等于 7,因为缺少某些日期。我想将此 df 转换为时间序列,其中“周”是要考虑的日期。我的目标是将此 df 用于预测目的。特别是,我想使用应用于时间序列的多元线性回归

####example where XXXXXXX is the converted df to time series and I am using some variables for lin. regr.

fit_test <- tslm(ln_yval ~ SM + cat + ispromo, data=XXXXXXX)
autoplot(XXXXXXX[,'ln_yval '], series="Data") +
  autolayer(fitted(fit_test), series="Fitted")

感谢您的帮助

【问题讨论】:

    标签: r dataframe time-series forecast


    【解决方案1】:

    在我的脑海里有 xts 和 zoo 这个呢?

    library(xts)
    library(zoo)
    library(forecast)
    
    df <- data.frame(week = c("2020-01-01", "2020-01-08", "2020-01-18"),
                     SM = c(1, 2, 3),
                     ispromo = c(0,0,0),
                     cat = c("Z005", "Z005","Z005"),
                     yval = c(1.0, 2.0, 3.0),
                     ln_yval = c(3.4, 4.5, 4.6))
    time_series_xts <- xts(df[,-1], order.by=as.Date(df[,1]))
    time_series_zoo <- zoo(df[,-1], order.by=as.Date(df[,1]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      相关资源
      最近更新 更多