2016-03-29 2 views
11

कुछ पूर्वानुमानों की साजिश करते समय मैं एक्स-अक्ष को बदलना चाहता हूं। मॉडल दैनिक crontab के माध्यम से अद्यतन किया जाता है। एक्स पैमाने पर तारीख में है कि दैनिक वृद्धि शामिल करना चाहिए:साजिश() का उपयोग करते हुए एक्स-अक्ष को कैसे समायोजित करें जब सीमा प्रतिदिन बदल रही हो?

उदाहरण डेटा:

# 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)) 

enter image description here

मैं 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) 

लेकिन है कि एक्स पैमाने पर साजिश नहीं है।

+0

आप भूखंडों के आधार के लिए प्रतिबंधित या भी खुला एक 'ggplot' समाधान के लिए कर रहे हैं? – RHA

+0

मैं प्रतिबंधित नहीं हूं। मैं सिर्फ आधार का उपयोग करता हूं क्योंकि 'लाइब्रेरी (पूर्वानुमान) '' ggplot' का समर्थन नहीं करता है, कम से कम मैं इसका उपयोग नहीं कर सका। – Mamba

+0

यह भी देखें [यहां] (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

उत्तर

7

कारण है कि अपने axis() प्रदर्शित नहीं करता है, क्योंकि include =30 केवल विंडो में 30 पिछले तत्वों से पता चलता है, लेकिन वास्तव अक्ष पर 1.

शुरू होता है आप तो वर्तमान विंडो के लिए अपने अक्ष स्लाइड यदि यह दिखाएगा:

plot(xaxt="n",forecast(fit,h=length(df[testSlices[[1]],]$x-1)),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)) 
at <- seq(1,as.integer(date3),length.out=4) 
dates <- seq(date1, date2, by="days")[at] 
axis(1, at = at+400, labels = dates) 

enter image description here

संबंधित मुद्दे