二. 销量关于时间的预测

SQL代码

SELECT substr(T.period_wid,1,6),sum(T.qty)
from dmk.dmk_sal_actual_sales_dtl T
WHERE T.qty<=100 and T.period_wid>=20151201
GROUP BY substr(T.period_wid,1,6)
销量关于时间的预测

1、导入数据

install.packages(“xlsx”)
library(xlsx)
x1=read.xlsx(“salemon1.xlsx”,sheetIndex=1)

2、异常值检测和处理

plot(x1 m o n , x 1 mon,x1 sale,type=“l”)
out <-boxplot.stats(x1 s a l e ) sale) out
a=which(x1 s a l e x = r e a d . x l s x ( " s a l e m o n . x l s x " , s h e e t I n d e x = 1 ) p l o t ( x sale %in% out) 查看销量数据明细,出现异常的数据存在于环保科技事业部(无唯一码的垃圾箱之类) 销量数据部分剔除环保科技事业部 x=read.xlsx("salemon.xlsx",sheetIndex=1) plot(x mon,x s a l e , t y p e = " l " ) b o x p l o t ( x sale,type="l") boxplot(x sale)
boxplot.stats(x$sale)

3、绘制时间序列图,了解销量趋势

xsale <- ts(x$sale, frequency=12, start=c(2016,1))
ts.plot(xsale)
abline(lm(xsale~time(xsale)),col=“red”)

4、模型的建立

第一种:线性回归
(1)销量数据的平稳性检验
install.packages(“tseries”)
library(tseries)
adf.test(xsale)
(2)线性回归和残差检验
使用spss拟合结果,选择拟合度和显著性较好的线性拟合
modelm=lm(xsale~time(xsale))
resid=residuals(modelm)
plot.ts(resid)
adf.test(resid)
Box.test(resid, type=“Ljung-Box”)
第二种:AR模型
(1)AR模型确定
因为原始数据平稳,故 ARIMA(p,d,q)中d的值为0,即模型确定为ARMA模型,然后通过相关图和偏相关图寻找合适的 p 值和 q 值。( ##p是AR阶数,d是差分阶数,q是MA阶数)
自相关图:(确定p=1)
acf(xsale,lag.max=12)
acf(xsale,lag.max=12,plot=FALSE)
或者:a.resid=residuals(a) #适用于截面数据,a为回归方程
acf(a.resid, lag.max = NULL,type = “correlation”)
偏相关图::(确定q=0)
pacf(xsale,lag.max=12)
pacf(xsale,lag.max=12,plot=FALSE)
最终确定模型为AR模型。
(2)模型估计与检验

模型估计

install.packages(“quantmod”)
library(xts)
library(zoo)
library(quantmod)
model<-arima(xsale,order=c(1,0,0))
model

模型检验

resid=residuals(model)
plot.ts(resid)
adf.test(resid)
Box.test(resid, type=“Ljung-Box”)

预测

预测后3个月的销量
install.packages(“forecast”)
library(forecast)
saleforecast<-forecast(model,h=3,level=c(99.5))
saleforecast

发布了30 篇原创文章 · 获赞 0 · 访问量 351

猜你喜欢

转载自blog.csdn.net/hua_chang/article/details/105034019
今日推荐