使用as.POSIXlt/ct进行R预测 [英] R Forecasting with as.POSIXlt/ct

查看:104
本文介绍了使用as.POSIXlt/ct进行R预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天

我在这里的一篇文章中读到,函数Forecast :: plot.forecast不适用于axis.Date或axis.POSIXct(在软件包预测中不使用)."可以在此处看到:绘制预测的自定义轴标签在R

I read on one of the posts here that "the function forecast::plot.forecast is not designed to be used with axis.Date or axis.POSIXct (which are not used in the package forecast)." This can be seen here:custom axis labels plotting a forecast in R

尽管如此,他们还是设法使用了预测包和一些代码来获取正确的轴标签.但是,此示例适用于季度数据.此外,此处使用"as.POSIXlt"的示例用于每周数据:预测时间序列数据

Nevertheless, they managed to use the forecast package and some code to get the correct axis labels. However, this example is for quarterly data. Also, this example here using 'as.POSIXlt' is for weekly data: Forecasting time series data

我尝试使用该代码,但无法使其用于每月数据.因此我的轴标签仍然错误.我很困惑请您帮忙告知我如何使用预测包使轴标签正确反映吗?

I've tried playing with the code but I can't get it to work for monthly data. So my axis labels are still wrong. I'm stumped. Please would you help advise how I get the axis labels to reflect correctly using the forecast package?

示例

library(forecast)
headcount<-c(2475,2468,2452,2464,2500,2548,2536,2565,2590,2608,2625,2663)
date<-c("2013/01/31","2013/02/28","2013/03/31","2013/04/30","2013/05/31","2013/06/30",
"2013/07/31","2013/08/31","2013/09/30","2013/10/31","2013/11/30","2013/12/31")

x<-data.frame(headcount,date)
t<-ts(x$headcount,start=c(2013,1),end=c(2013,12),frequency=12)
fit<-forecast(t,h=12)
plot(forecast(fit))

这样做,轴标签将显示为2013.0、2013.5、2014.5

By doing this, the axis labels come out as 2013.0, 2013.5, 2014.5

我知道这只是一年的数据.我只是对如何固定每月数据的轴标签感兴趣,

I know this is only a year's worth of data. I'm just interested in how to fix the axis labels for monthly data,

亲切的问候 D

推荐答案

以下是使用提供的链接的可能解决方案

Here's a possible solution using the links provided

plot(forecast(fit), axes = FALSE)
a <- seq(as.Date(date[1]) + 1, by = "months", length = length(date) + 11)
axis(1, at = as.numeric(a)/365.3 + 1970, labels = format(a, format = "%m/%Y"), cex.axis = 0.9)
axis(2, cex.axis = 0.9)

这篇关于使用as.POSIXlt/ct进行R预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆