I have list of products: P1, P2, P3, P4 belonging to different categories, as per df CAT . For each product there are two time series associated: ts_sales and ts_ofs (representing sales and out of stock).
I want to correlate sales time series of P1 with out of stock of P2, P3 (belonging to the same category)
This code shows how I correlate sales time series from products on the same category:
rm(list=ls())
CAT <- data.frame(PROD = c('P1','P2','P3','P4'),CAT = c('C1','C1','C1','C2'))
ts_sales <- list()
ts_sales$'P1' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P2' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P3' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_sales$'P4' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs <- list()
ts_ofs$'P1' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P2' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P3' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
ts_ofs$'P4' <- ts(runif(10,0,1), start=c(2019,1), frequency = 12)
sales <- with(CAT,split(as.character(PROD), CAT))
sales <- lapply(sales, function(x) ts_sales[x])
ofs <- with(CAT,split(as.character(PROD), CAT))
ofs <- lapply(ofs, function(x) ts_ofs[x])
cor(do.call(ts.intersect, sales$C1))
I tried:
c(ofs$C1,sales$C1$P1)
but sales$C1$P1 does not get appended on the same format
Appreciate:)
We can use
out <- lapply(Map(c, ofs, sales), function(x) do.call(ts.intersect, x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.