Stack Overflow Asked by Davide on March 2, 2021
I have a question on how to convert a df to a time series. I am new with R and I am struggling with this operation.
These are some rows of my df which is named “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
My date variable is “week”, which doesn’t have necessarily a frequency equal to 7 because some dates are missing. I would like to convert this df to a time series, where “week” is the date to consider. My aim is to use this df for forecasting purposes. In particular, I would like to use multiple linear regression applied to time series
####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")
Thank you for your help
from the back of m mind there is xts and zoo what about this?
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]))
Answered by Will on March 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP