11
いくつかの予測をプロットするときにx軸を変更したいと思います。モデルは毎日crontabで更新されます。 xスケールは、日付のその日々増加含まれている必要があります範囲が毎日変化しているときに、plot()を使ってx軸を調整するには?
例をデータ:
# where dates changes according to Sys.Date-1
dates <- seq(as.Date("2015-01-01"), Sys.Date()-1, by = "days")
# where x is updated daily
x <- diffinv(rnorm(length(dates)-1))
df<-data.frame(dates,x)
# split data and train model
df$x<-as.ts(df$x)
# required libraries
library(caret)
library(forecast)
library(plyr)
# the time series is updated on daily basis
date1 <- strptime("2016-02-04", format="%Y-%m-%d")
date2 <- strptime(Sys.time()-1, format="%Y-%m-%d")
date3<-difftime(date2,date1,units="days")
# here I split data into time and test data according to initialWindow "2016-02-04"
timeSlices <- createTimeSlices(1:nrow(df),
initialWindow = 400, horizon = date3, fixedWindow = TRUE)
#extract data for fitting the model
trainSlices <- timeSlices[[1]]
testSlices <- timeSlices[[2]]
# here I calculate the fit and forecast
fit <- tbats(df[trainSlices[[1]],]$x, seasonal.periods=c(7,365), use.trend=TRUE, use.parallel=TRUE)
pred <- forecast(fit,h=length(df[testSlices[[1]],]$x))
# here I plot actual vs. predicted values
plot(forecast(fit,h=length(df[testSlices[[1]],]$x)),ylab = "x ",xlab="Daily units from 2015-01-01 to CurrentDate", main="Forecast", include=30)
lines(x = as.numeric(rownames(df[testSlices[[1]],])), df[testSlices[[1]],]$x, col = "red")
legend(x = "topleft", legend = c("Prediction", "Actual Data"), col = c("blue", "red"), lty = c(1, 1))
Iでもあるので、上の別名dates scale
2015-07-01
とに日々の単位を変更したいです毎日更新に反映されます。私はxaxt="n"
を使用しようとSys.Date()-1
でdates scale
を追加:
dates <- seq(Sys.Date()-30, Sys.Date()-1, by = "days")
plot(xaxt="n",forecast(fit,h=length(df[testSlices[[1]],]$x)),ylab = "x ",xlab="Daily units from 2015-01-01 to CurrentDate", main="Forecast", include=30)
lines(x = as.numeric(rownames(df[testSlices[[1]],])), df[testSlices[[1]],]$x, col = "red")
legend(x = "topleft", legend = c("Prediction", "Actual Data"), col = c("blue", "red"), lty = c(1, 1))
# here I wanted to add the new scale
axis(1, at = dates,labels = TRUE)
しかし、それは、xスケールをプロットしません。
をあなたは基本プロットに制限するかも 'ggplot'溶液に開いていますか? – RHA
私は制限されていません。私は 'ライブラリ(予測)'が 'ggplot'をサポートしていないので、ベースを使用しています。少なくとも使用できませんでした。 – Mamba
[こちら](http://davenportspatialanalytics.squarespace.com/blog/2012/3/14/plotting-forecast-objects-in-ggplot-part-1-extracting-the-da.html)と[ここをクリックしてください](http://davenportspatialanalytics.squarespace.com/blog/2012/3/21/plotting-forecast-objects-in-ggplot-part-2-visualize-observa.html)。 – Axeman