用RIM中的ARIMA /指数平滑法找到拟合预测

时间:2017-11-14 17:29:40

标签: r forecasting smoothing ets arima

我已经编写了一些代码来对索赔日期进行排序,每月/每年计算一次,我试图用ARIMA /指数平滑参数进行预测。

参见索赔清单:

            2012-01-31   82
            2012-02-29   65     
            2012-03-31   64
            2012-04-30   73
            2012-05-31   71
            2012-06-30   79
            2012-07-31   72
            2012-08-31   82
            2012-09-29   64
            2012-10-31   72
            2012-11-30   63
            2012-12-31   80
            2013-01-31   67             
            2013-02-27   65
            2013-03-31   84
            2013-04-30   68
            2013-05-31   68
            2013-06-29   66
            2013-07-30   64
            2013-08-31   69
            2013-09-29   66
            2013-10-31   65
            2013-11-30   56
            2013-12-31   76
            2014-01-31   75
            2014-02-28   58
            2014-03-29   80
            2014-04-30   76
            2014-05-31   80
            2014-06-28   68
            2014-07-31   82
            2014-08-30   79
            2014-09-27   60
            2014-10-31   85
            2014-11-30   60
            2014-12-31   76
            2015-01-31   75
            2015-02-28   84
            2015-03-31   77
            2015-04-30   79
            2015-05-30   91
            2015-06-30   82
            2015-07-31   98
            2015-08-31   65
            2015-09-30   77
            2015-10-31  115
            2015-11-30   79
            2015-12-31   80
            2016-01-30   91
            2016-02-29  105
            2016-03-31   77
            2016-04-30  107
            2016-05-31   85
            2016-06-30   89
            2016-07-30  112
            2016-08-31   88
            2016-09-30   90
            2016-10-30   79
            2016-11-30   85
            2016-12-31   66

我的代码所面临的问题是,我得到的是平均预测,而不是类似于此示例的所需拟合数据:https://stats.stackexchange.com/questions/115506/forecasting-a-seasonal-time-series-in-r

请参阅R代码:

  1. 对索赔日期进行排序并计算

            library(forecast)
            library(ggplot2)
            library(xts)
            library(reshape2)
            library(zoo)
            library(lubridate)
    
            data = read.csv('Claims1.csv')
            data$DISABILITYDATE <- as.Date(data$DISABILITYDATE, "%m/%d/%Y")
            data
    
            str(data)
            as.Date(data[,1])
            xts(x=data[,-1], order.by = data[,1])
    
            data = read.csv('Claims1.csv')
            data$DISABILITYDATE <- as.Date  (data$DISABILITYDATE, "%m/%d/%Y")
            df <- xts(rep(1,length(data$DISABILITYDATE)),order.by=data$DISABILITYDATE)
            df1 <- apply.monthly(df,function(x) length(x))
            df1
            t(df1)
    
            str(df1)
            df2 <- data.frame(df1=c("Jan 2012","Feb 2012","Mar 2012","Apr 2012","May 2012","Jun 2012","Jul 2012","Aug 2012","Sep 2012","Oct 2012","Nov 2012","Dec 2012","Jan 2013","Feb 2013","Mar 2013","Apr 2013","May 2013","Jun 2013","Jul 2013","Aug 2013","Sep 2013","Oct 2013","Nov 2013","Dec 2013","Jan 2014","Feb 2014","Mar 2014","Apr 2014","May 2014","Jun 2014","Jul 2014","Aug 2014","Sep 2014","Oct 2014","Nov 2014","Dec 2014","Jan 2015","Feb 2015","Mar 2015","Apr 2015","May 2015","Jun 2015","Jul 2015","Aug 2015","Sep 2015","Oct 2015","Nov 2015","Dec 2015","Jan 2016","Feb 2016","Mar 2016","Apr 2016","May 2016","Jun 2016","Jul 2016","Aug 2016","Sep 2016","Oct 2016","Nov 2016","Dec 2016"),score=c(df1))
            df2
            t(df2)
            df2[-1]
    

    2.1使用ETS预测(指数平滑)

            library(forecast)
            x.ts <- as.ts(df2[2])
            x.ts
            x.ets <- ets(x.ts)
            x.ets
            x.fore <- forecast(x.ets$fitted, h=12)
            x.fore
    
            x <- ts(df2[2], start = 2012, frequency = 12)
            plot(forecast(ets(x), 24))
            x
    
            plot(forecast(x, h=12))
            date1 <- ymd("2012-01-01","2013-01-01","2014-01-01","2015-01-01","2016-01-01","2017-01-01")
            abline(v=decimal_date(date1), col="blue")
    
  2. ETS Forecast  2.2使用ARIMA进行预测

                ARIMAfit = auto.arima(x, approximation=FALSE,trace=FALSE)
                summary(ARIMAfit)
                plot(ARIMAfit)
    
                pred = predict(ARIMAfit, n.ahead = 48)
                round(as.numeric(pred$fitted,0))
                pred
    
                library(TSPred)             
                plotarimapred(pred$pred,x, xlim=c(2012, 2020), range.percent = 0.05)
    

    我的输出是这样的: I have forecasted the claims but I need to get more #fitted results than the straight line method

    所需输出的示例 Example of desired output

0 个答案:

没有答案