Old*_*Pro 3 r time-series data.table
我有多组时间序列数据,并希望帮助找出将它们导入R并用R分析它们的最佳方法.我对data.table非常熟悉,但对R的ts类支持时间序列分析并不熟悉.
特别是,我想知道如何ts在这种情况下使用,或者是否存在限制ts(例如聚合一组ts对象的问题),使其适合在此处使用.
有很多商店.对于每个商店,我每天都有多个数据点,例如以美元计的销售量,交易数量的销售量和商店流量(进入商店的人数).(实际上我所拥有的是一个包含列存储ID,日期以及该商店和日期数据的表.)
我一直在做的是使用data.table,每个商店有一行,按商店将数据聚合成几个月,并将每个月的值存储在一个单独的命名列中(例如jan14_dollars,feb14_dollars ......)但这很笨重出于很多原因,特别是当我想看几周或几周时.
我认为处理这个问题的正确方法是拥有类型的列,ts这样每一行都只是store, dollars_ts, transactions_ts, traffic_ts但是(a)如何将数据转换为该格式;(b)可以ts将整数的方式组合起来给我结果我想要?如果您只能回答(a)或(b)但不能回答两者,请尽量回答.
我无法提供逼真的数据集,但您可以生成一个随机的数据集,如下所示:
require("data.table")
storeData <- CJ(store = toupper(letters), date = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by="day"))
storeData$dollars = sample(100:100000, nrow(storeData), replace = TRUE)/100
storeData$transactions <- sample(0:1000, nrow(storeData), replace = TRUE)
storeData$traffic <- storeData$transactions + sample(0:1000, nrow(storeData), replace = TRUE)
head(storeData)
store date dollars transactions traffic
1: A 2012-01-01 48.60 409 990
2: A 2012-01-02 996.89 36 428
3: A 2012-01-03 69.35 647 1103
4: A 2012-01-04 334.56 953 973
5: A 2012-01-05 692.99 958 1753
6: A 2012-01-06 973.32 724 1086
Run Code Online (Sandbox Code Playgroud)
我想回答诸如"有多少商店的美元销售增长积极?"之类的问题.并且"美元/交易的变化与交通变化之间是否存在关系?" 并将数据分成时间段并比较各时期的答案(例如今年第一季度与去年第一季度相比).
可以使用这些问题来回答这些问题ts吗?如果是这样,我如何将这些数据放入一组适当的列中,或者是否存在除了data.table我应该使用的其他结构?
请展示如何组织数据,然后如何使用数据回答示例问题"2014年1月与2013年1月相比,有多少商店的销售额增长为正?" 并且"过去3个月每笔交易的总体趋势是多少?"
你在这里问了很多问题.我建议你花时间阅读data.table可以做的涉及连接和聚合数据的所有事情.以下是您如何在第一季度获得每家商店年增长率的示例.
#get the first day of the first month for your binning
minDate<-min(storeData$date); month(minDate)<-1; day(minDate)<-1
#get the first day of the last month for your binning
maxDate<-max(storeData$date); month(maxDate)<-12; day(maxDate)<-1
#Build some bins
yearly<-data.table(leftBound=seq.Date(minDate,maxDate,by="year"))
quarterly<-data.table(leftBound=seq.Date(minDate,maxDate,by="3 months"))
monthly<-data.table(leftBound=seq.Date(minDate,maxDate,by="month"))
#Example for quarterly data
quarterly[, rollDate:=leftBound]
storeData[, rollDate:=date]
setkey(quarterly,"rollDate")
setkey(storeData,"rollDate")
temp<-quarterly[storeData, roll=TRUE] #associate each (store, date) pair with a quarter
#create a "join table" containing each quarter for each store
jt<-CJ(leftBound=quarterly$leftBound, store=unique(storeData$store))
setkey(temp,"leftBound","store")
dt<-temp[jt, allow.cartesian=TRUE]
dt[, `:=`(year=year(leftBound), quarter=quarter(leftBound))]
qSummary<-dt[,list(dollars=sum(dollars, na.rm=TRUE),
transactions=sum(transactions, na.rm=TRUE),
traffic=sum(traffic, na.rm=TRUE)),
by=list(year,quarter,store)] #Summarize the data by quarter
#Get year/year growth for Q1
qSummary[,list(dollarGrowth = dollars[which(year==2014 & quarter==1)] / dollars[which(year==2013 & quarter==1)]), by=store]
#First five rows...
store dollarGrowth
1: A 0.0134860
2: B 0.0137215
3: C 0.0188249
4: D 0.0163887
5: E 0.0037576
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4679 次 |
| 最近记录: |