【问题标题】:automating forecasting graphs in rr中的自动化预测图
【发布时间】:2012-08-03 14:45:23
【问题描述】:

我正在处理一堆不同的文件集,这些文件集是每月数据,但它们都是不同的月份。其中一些包含过去 2 年的月度数据,其中一些包含几个月的月度数据。

我喜欢使用这些数据来创建预测图表。如果所有数据都是一致的,那将是直截了当的。但数据是每月的,每个文件的开始和结束时间不同。

z 输出:

    Month   Value
1 2010-01-01 100
2 2010-02-01 200
3 2010-03-01 600
4 2010-04-01 400
5 2010-05-01 300
6 2010-06-01 700

我尝试这样做以将 start 设置为 s 对象,但不工作

s <- head(z,1)$Month
s <- sub("-", "," s)
t.ser <- ts(z$Value, start=s,freq=12)

由于我处理的数据各不相同,我不知道开始日期。

这可行,但是由于我不知道我的数据集的开始日期,我可以在开始时放置一个变量吗?

t.ser <- ts(z$Value, start=c(2010,1),freq=12)
t.ets <- ets(t.ser) 
t.fc <- forecast(t.ets,h=12)
plot(t.fc, xaxt="n")

我想格式化 x 轴以显示实际数据的日期和预测值的日期。因为我不知道开始日期,我也不能这样做。无论数据如何,我都想预测 12 个月。

如果我知道开始时间,这将有效。

a = seq(as.Date("2011-01-01"), by="month", length=36)
axis(1, at = decimal_date(a), labels = format(a, "%Y %b %d"), cex.axis=0.6) 

当我这样做时:

 t.ser <- ts(z$Value/1000000, start(z[1,1]),freq=12) 

我明白了

t.ser
  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1  45  51  56  56  59  60  60  60  64  65  75  73
2  74  80  87  91  92  96 109 108 123 129 133 143
3 127 127 123 121 130        

但是当我这样做时:

t.ser <- ts(z$Value/1000000, start=c(2010,1),freq=12) 

我得到了这个,这是我在开始时使用变量时想要的结果,而不是明确地输入日期。任何人都有任何想法,我将如何完成这个?

t.ser
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2010  45  51  56  56  59  60  60  60  64  65  75  73
2011  74  80  87  91  92  96 109 108 123 129 133 143
2012 127 127 123 121 130             

【问题讨论】:

    标签: r forecasting


    【解决方案1】:

    试试这个(如果您的数据来自文件,请指定类似 file = "mydata.dat" 代替 text=Lines,如果这不是您的数据格式,请适当修改):

    Lines <- "Month   Value
    1 2010-01-01 100
    2 2010-02-01 200
    3 2010-03-01 600
    4 2010-04-01 400
    5 2010-05-01 300
    6 2010-06-01 700"
    
    library(zoo)
    z <- read.zoo(text = Lines, header = TRUE, FUN = as.yearmon)
    t.ser <- as.ts(z)
    

    这给出了:

    > t.ser
         Jan Feb Mar Apr May Jun
    2010 100 200 600 400 300 700
    

    【讨论】:

    • Gorhendieck 在 z data.frame 中有我的数据,我这样做了:b
    • 如果DF 是您的数据框,请在您的问题中提供dput(head(DF)) 的输出。
    • 我得到了它:z1
    • plot(z) z 在我的回复中。
    • 我让它像这样工作:l
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2017-04-18
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多