环境:ubuntu16.04
R语言版本:R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
出处:
需要的依赖:
进入R语言交互模式以后,如下:
>install.packages("forecast")
可以下载到各种tar.gz包
package.tar.gz
用q()可以退出交互模式
运行方式Rscript test.r
先上数据:
catering_sale.csv
日期 数据
2015/3/1 51
2015/2/28 2618.2
2015/2/27 2608.4
2015/2/26 2651.9
2015/2/25 3442.1
2015/2/24 3393.1
2015/2/23 3136.6
2015/2/22 3744.1
2015/2/21 6607.4
2015/2/20 4060.3
2015/2/19 3614.7
2015/2/18 3295.5
2015/2/16 2332.1
2015/2/15 2699.3
2015/2/14
2015/2/13 3036.8
2015/2/12 865
2015/2/11 3014.3
2015/2/10 2742.8
2015/2/9 2173.5
2015/2/8 3161.8
2015/2/7 3023.8
2015/2/6 2998.1
2015/2/5 2805.9
2015/2/4 2383.4
2015/2/3 2620.2
2015/2/2 2600
2015/2/1 2358.6
2015/1/31 2682.2
2015/1/30 2766.8
2015/1/29 2618.8
2015/1/28 2714.3
2015/1/27 2280.8
2015/1/26 2414
2015/1/25 3130.6
2015/1/24 2716.9
2015/1/23 2930.8
2015/1/22 2504.9
2015/1/21 2559.5
2015/1/20 2168.6
2015/1/19 2436.4
2015/1/18 3234.3
2015/1/17 3061
2015/1/16 2900.1
2015/1/15 2646.8
2015/1/14 2615.2
2015/1/13 2124.4
2015/1/12 1958
2015/1/8 2259.1
2015/1/7 2419.8
2015/1/6 2775
2015/1/5 2594.9
2015/1/4 2468.3
2015/1/3 3004.3
2015/1/2 3313.3
2015/1/1 3613.6
2014/12/31 2655.9
2014/12/30 2644.3
2014/12/29 2565.3
2014/12/27 2525.9
2014/12/26 2778
2014/12/25 2542.1
2014/12/24 2473.3
2014/12/23 2240.1
2014/12/22 2575
2014/12/21 3802.8
2014/12/18 2274.7
2014/12/17 2687.2
2014/12/16 2577.8
2014/12/15 2583
2014/12/14 3282.6
2014/12/13 3113.7
2014/12/12 2661.4
2014/12/11 2553.2
2014/12/10 2511.3
2014/12/9 2710.3
2014/12/8 2468.1
2014/12/7 3041.5
2014/12/6 3178.9
2014/12/5 2594.4
2014/12/4 2381.1
2014/12/3 2415
2014/12/2 2236.4
2014/11/30 3207.2
2014/11/29 3059.5
2014/11/28 3039.1
2014/11/26 2817.5
2014/11/25 2891.8
2014/11/24 2470.1
2014/11/23 3556.6
2014/11/22 3397.7
2014/11/20 2761.6
2014/11/19 2618.2
2014/11/18 2758.3
2014/11/17 2614.3
2014/11/16 3437.1
2014/11/15 3250
2014/11/14 3063.7
2014/11/13 2839.2
2014/11/12 2360.9
2014/11/11 2158.5
2014/11/10 2005.5
2014/11/9 3236.4
2014/11/8 22
2014/11/7 2452.6
2014/11/6 2265
2014/11/5 2566.1
2014/11/4 2527.2
2014/11/3 2326.5
2014/11/2 2941.9
2014/11/1 60
2014/10/31 2520.9
2014/10/30 2446.2
2014/10/29 2549.4
2014/10/28 2449.3
2014/10/27 2162.5
2014/10/26 2781.3
2014/10/25 3060.6
2014/10/24 2064
2014/10/22 2439.7
2014/10/21 2476.2
2014/10/20 2478.3
2014/10/19 2826.2
2014/10/18 2924.8
2014/10/17 2417.5
2014/10/16 2450.1
2014/10/15 2533
2014/10/14 2238.7
2014/10/13 2388.8
2014/10/12 3291.3
2014/10/11 2738.8
2014/10/10 2344.1
2014/10/9 2068.8
2014/10/8 3185.3
2014/10/7 2778.6
2014/10/6 2921.1
2014/10/5 2524.3
2014/10/4 3057.1
2014/10/3 3039.6
2014/10/2 3193.4
2014/10/1 3075.4
2014/9/30 2847.6
2014/9/29 2311.4
2014/9/28 2327.3
2014/9/27 9106.44
2014/9/26 2616.6
2014/9/25 2620.2
2014/9/24 2616.4
2014/9/23 2655.8
2014/9/22 2310.7
2014/9/21 2935.8
2014/9/20 3017.9
2014/9/19 2625.5
2014/9/18 2752.7
2014/9/17 2181.5
2014/9/16 2440.5
2014/9/15 2422.8
2014/9/14 2583.6
2014/9/13 2728.9
2014/9/12 2525.3
2014/9/11 2531.7
2014/9/10 2300.5
2014/9/9 2097.5
2014/9/8 4065.2
2014/9/7 3555.2
2014/9/6 3462.5
2014/9/5 3033.1
2014/9/4 2926.1
2014/9/3 2431.4
2014/9/2 2706
2014/9/1 3049.9
2014/8/31 3494.7
2014/8/30 3691.9
2014/8/29 2929.5
2014/8/28 2760.6
2014/8/27 2593.7
2014/8/26 2884.4
2014/8/25 2591.3
2014/8/24 3022.6
2014/8/23 3052.1
2014/8/22 2789.2
2014/8/21 2909.8
2014/8/20 2326.8
2014/8/19 2453.1
2014/8/18 2351.2
2014/8/17 3279.1
2014/8/16 3381.9
2014/8/15 2988.1
2014/8/14 2577.7
2014/8/13 2332.3
2014/8/12 2518.6
2014/8/11 2697.5
2014/8/10 3244.7
2014/8/9 3346.7
2014/8/8 2900.6
2014/8/7 2759.1
2014/8/6 2915.8
2014/8/5 2618.1
2014/8/4 2993
2014/8/3 3436.4
2014/8/2 2261.7
原书代码修改后,test.r如下:
paste("------------------2--------------------------------")
#####对缺失值用多重插补值得到的结果为2699.3
data<-read.csv("./catering_sale.csv",header=T)[,2]
data
#install.packages("forecast")
#install.packages("fUnitRoots")
library(forecast)
library(fUnitRoots)
sales=ts(data)
plot.ts(sales,xlab="时间",ylab="销量/元")
###单位根检验
unitrootTest(sales)
####自相关图
acf(sales,na.action = na.pass)
###一阶差分
difsales=diff(sales)
plot.ts(difsales,xlab="时间",ylab="销量残差/元")
###自相关图
acf(difsales,na.action = na.pass)
###单位根检验
unitrootTest(difsales)
###白噪声检验
Box.test(difsales,type="Ljung-Box")
####偏自相关图
pacf(difsales,na.action = na.pass)
###ARIMA(1,1,0)模型
arima=arima(sales,order=c(1,1,0))
arima
forecast=forecast(arima,h=5,level=c(99.5))
forecast
运行方式:
Rscript test.r
运行结果如下:
-------------------------------------------------------------------------------------------------------------------------------------------------
root@Ubuntu16:/home/appleyuchi/桌面/R语言# Rscript test.r
[1] "------------------2--------------------------------"
[1] 51.00 2618.20 2608.40 2651.90 3442.10 3393.10 3136.60 3744.10 6607.40
[10] 4060.30 3614.70 3295.50 2332.10 2699.30 NA 3036.80 865.00 3014.30
[19] 2742.80 2173.50 3161.80 3023.80 2998.10 2805.90 2383.40 2620.20 2600.00
[28] 2358.60 2682.20 2766.80 2618.80 2714.30 2280.80 2414.00 3130.60 2716.90
[37] 2930.80 2504.90 2559.50 2168.60 2436.40 3234.30 3061.00 2900.10 2646.80
[46] 2615.20 2124.40 1958.00 2259.10 2419.80 2775.00 2594.90 2468.30 3004.30
[55] 3313.30 3613.60 2655.90 2644.30 2565.30 2525.90 2778.00 2542.10 2473.30
[64] 2240.10 2575.00 3802.80 2274.70 2687.20 2577.80 2583.00 3282.60 3113.70
[73] 2661.40 2553.20 2511.30 2710.30 2468.10 3041.50 3178.90 2594.40 2381.10
[82] 2415.00 2236.40 3207.20 3059.50 3039.10 2817.50 2891.80 2470.10 3556.60
[91] 3397.70 2761.60 2618.20 2758.30 2614.30 3437.10 3250.00 3063.70 2839.20
[100] 2360.90 2158.50 2005.50 3236.40 22.00 2452.60 2265.00 2566.10 2527.20
[109] 2326.50 2941.90 60.00 2520.90 2446.20 2549.40 2449.30 2162.50 2781.30
[118] 3060.60 2064.00 2439.70 2476.20 2478.30 2826.20 2924.80 2417.50 2450.10
[127] 2533.00 2238.70 2388.80 3291.30 2738.80 2344.10 2068.80 3185.30 2778.60
[136] 2921.10 2524.30 3057.10 3039.60 3193.40 3075.40 2847.60 2311.40 2327.30
[145] 9106.44 2616.60 2620.20 2616.40 2655.80 2310.70 2935.80 3017.90 2625.50
[154] 2752.70 2181.50 2440.50 2422.80 2583.60 2728.90 2525.30 2531.70 2300.50
[163] 2097.50 4065.20 3555.20 3462.50 3033.10 2926.10 2431.40 2706.00 3049.90
[172] 3494.70 3691.90 2929.50 2760.60 2593.70 2884.40 2591.30 3022.60 3052.10
[181] 2789.20 2909.80 2326.80 2453.10 2351.20 3279.10 3381.90 2988.10 2577.70
[190] 2332.30 2518.60 2697.50 3244.70 3346.70 2900.60 2759.10 2915.80 2618.10
[199] 2993.00 3436.40 2261.70
载入需要的程辑包:timeDate
载入需要的程辑包:methods
载入需要的程辑包:timeSeries
载入需要的程辑包:fBasics
There were 30 warnings (use warnings() to see them)
Title:
Augmented Dickey-Fuller Test
Test Results:
PARAMETER:
Lag Order: 1
STATISTIC:
DF: -1.2631
P VALUE:
t: 0.1898
n: 0.4279
Description:
Fri Apr 13 22:51:30 2018 by user:
There were 42 warnings (use warnings() to see them)
Title:
Augmented Dickey-Fuller Test
Test Results:
PARAMETER:
Lag Order: 1
STATISTIC:
DF: -16.1785
P VALUE:
t: < 2.2e-16
n: 0.004452
Description:
Fri Apr 13 22:51:30 2018 by user:
Box-Ljung test
data: difsales
X-squared = 40.42, df = 1, p-value = 2.048e-10
Call:
arima(x = sales, order = c(1, 1, 0))
Coefficients:
ar1
-0.4765
s.e. 0.0644
sigma^2 estimated as 737874: log likelihood = -1627.1, aic = 3258.2
Point Forecast Lo 99.5 Hi 99.5
202 2821.403 410.1730 5232.633
203 2554.724 -166.9652 5276.414
204 2681.787 -586.6708 5950.245
205 2621.246 -995.6650 6238.157
206 2650.092 -1335.0917 6635.275
-------------------------------------------------------------------------------------------------------------------------------------------------
另外还会生成一个Rplots.pdf的文件,里面都是图形.
参考来自:
https://stackoverflow.com/questions/45193907/forecast-arima-function-missing-fromforecast-package