繁体   English   中英

将 data.frame 转换为时间序列

[英]Convert data.frame to time series

我有一个关于如何将 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")

感谢您的帮助

在我的脑海里有 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]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM