导入工具包
library(plyr)
library(dplyr)
library(forecast)
library(data.table)
library(tseries)
options(warn=-1)
读取数据文件
user_balance=read.csv("user_balance_table.csv")
user_balance$report_date=as.Date(as.character(user_balance$report_date),format="%Y%m%d")
temp=order(user_balance$report_date)
user_balance=user_balance[temp,]
data=ddply(user_balance,.(report_date),function(D){
colwise(sum)(D[,c(-1,-2)])})
train_data = data[274:396,]
test_data = data[397:427,]
get_score<- function(purchase_pred,redeem_pred,purchase_true,redeem_true,h=0.3){
out = data.frame(purchase_pred ,redeem_pred,purchase_true,redeem_true )
out$purchase_mape = abs(out$purchase_pred-out$purchase_true)/
out$purchase_true
out$redeem_mape = abs(out$redeem_pred-out$redeem_true)/
out$redeem_true
score = sum(exp(-out$purchase_mape/h)*10)* 0.45 +
sum(exp(-out$redeem_mape/h)*10)* 0.55
return(score)
}
purchase_ts=ts(train_data$total_purchase_amt,
frequency=7,start=c(1,1))
purchase_stl_res = stl(purchase_ts,s.window="periodic",robust=TRUE)
purchase_pred_test = forecast(purchase_stl_res,h = 31)$mean
redeem_ts = ts(train_data$total_redeem_amt,
frequency=7,start=c(1,1))
redeem_stl_res = stl(redeem_ts,s.window="periodic",robust=TRUE)
redeem_pred_test = forecast(