绘制ARIMA模型时,日期未显示在ggplot中的x轴上

时间:2018-10-01 18:45:45

标签: r ggplot2

我对R编程还是有些陌生,尽管我已经弄清楚了如何使某些必需的预测模型能够正常工作。我遇到的一个问题是,无法从数据集中获取“日期”列以在最终ARIMA ggplot渲染的x轴上进行打印。它只显示索引从0到200到400到600的时间间隔。除日期显示外,其他所有功能都有效。这是我的完整代码:

sentiments = read.csv('trollscores.csv', stringsAsFactors = FALSE)
sentiments$Date <- as.Date(sentiments$Date, format = "%m/%d/%Y")

library(ggplot2)
library(forecast)
library(tseries)
library(timetk)

ggplot(sentiments, aes(Date, sentiments$SentimentLM)) + geom_line() + 
scale_x_date('month')  + ylab("Sentiment Scores") +
xlab("")

count_ts = ts(sentiments[,c('SentimentLM')])
sentiments$clean_cnt = tsclean(count_ts)

ggplot() +
geom_line(data = sentiments, aes(x = Date, y = clean_cnt)) +
ylab('Cleaned Bicycle Count')

sentiments$cnt_ma = ma(sentiments$clean_cnt, order=7) # using the clean 
count with no outliers
sentiments$cnt_ma30 = ma(sentiments$clean_cnt, order=30)

ggplot() +
geom_line(data = sentiments, aes(x = Date, y = clean_cnt, colour = 
"Counts")) 
+
geom_line(data = sentiments, aes(x = Date, y = cnt_ma,   colour = "Weekly 
Moving Average"))  +
geom_line(data = sentiments, aes(x = Date, y = cnt_ma30, colour = "Monthly 
Moving Average"))  +
ylab('Sentiment Score')

count_ma = ts(na.omit(sentiments$cnt_ma), frequency=30)
decomp = stl(count_ma, s.window="periodic")
deseasonal_cnt <- seasadj(decomp)
plot(decomp)

adf.test(count_ma, alternative = "stationary")

Acf(count_ma, main='')

Pacf(count_ma, main='')

count_d1 = diff(deseasonal_cnt, differences = 1)
plot(count_d1,col="blue")
adf.test(count_d1, alternative = "stationary")

Acf(count_d1, main='ACF for Differenced Series')
Pacf(count_d1, main='PACF for Differenced Series')

auto.arima(deseasonal_cnt, seasonal=FALSE)

fit<-auto.arima(deseasonal_cnt, seasonal=FALSE)
tsdisplay(residuals(fit), lag.max=45, main='(0,1,2) Model Residuals')

fit2 = arima(deseasonal_cnt, order=c(1,1,7))
tsdisplay(residuals(fit2), lag.max=15, main='Seasonal Model Residuals')

fcast <- forecast(fit2, h=30)
plot(fcast)

hold <- window(ts(deseasonal_cnt), start=700)

fit_no_holdout = arima(ts(deseasonal_cnt[-c(700:725)]), order=c(1,1,7))

fcast_no_holdout <- forecast(fit_no_holdout,h=25)
plot(fcast_no_holdout, main=" ")
lines(ts(deseasonal_cnt))

library(sweep)
library(tidyquant)

ne_sweep <- sw_sweep(fcast,timetk_idx = TRUE,rename_index = "date")

# Visualizing the forecast
ne_sweep %>%
ggplot(aes(x = date, y = value, color = key)) +
# Prediction intervals
geom_ribbon(aes(ymin = lo.95, ymax = hi.95), 
          fill = "#D5DBFF", color = NA, size = 0) +
geom_ribbon(aes(ymin = lo.80, ymax = hi.80, fill = key), 
          fill = "#596DD5", color = NA, size = 0, alpha = 0.8) +
# Actual & Forecast
geom_line(size = 1) + 
geom_point(size = 2) +
# Aesthetics
theme_tq(base_size = 16) +
scale_color_tq() +
labs(title = "Sentiment 3-Year Forecast", x = "", y = "Level of sentiment") 

dput(head(ne_sweep))
structure(list(date = c(1, 1.03333333333333, 1.06666666666667, 
1.1, 1.13333333333333, 1.16666666666667), key = c("actual", "actual", 
"actual", "actual", "actual", "actual"), value = c(-0.00117229792495284, 
-0.00204034959504821, -0.00293998225125085, -0.00263003238897274, 
-0.00176165038488553, -0.00190213131023263), lo.80 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), lo.95 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), hi.80 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), hi.95 = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

enter image description here

1 个答案:

答案 0 :(得分:0)

我怀疑您需要添加一个比例项来告诉ggplot如何将名为date的索引列转换为x轴上的日期:

scale_x_yearmon(n = 12, format = "%Y %m")

编辑:将样本数据添加到问题后的替代解决方案。

看起来索引列以1.0开头,然后为随后的每一行添加1/30。我假设这些编码天数已转换为day_num,然后可以将其添加到start_date中,以在新的date2列中获得ggplot友好的日期。

library(dplyr)
start_date <- as.Date("2015-01-01")  # Replace with first day of data range
ne_sweep_dates <- ne_sweep %>%
  as_tibble() %>%
  mutate(day_num = (date-1)*30) %>%
  mutate(date2 = start_date + day_num)

#library(ggplot2); library(tidyquant)
ne_sweep_dates %>%
  ggplot(aes(x = date2, y = value, color = key)) +
  # Prediction intervals
  geom_ribbon(aes(ymin = lo.95, ymax = hi.95), 
              fill = "#D5DBFF", color = NA, size = 0) +
  geom_ribbon(aes(ymin = lo.80, ymax = hi.80, fill = key), 
              fill = "#596DD5", color = NA, size = 0, alpha = 0.8) +
  # Actual & Forecast
  geom_line(size = 1) + 
  geom_point(size = 2) +
  # Aesthetics
  theme_tq(base_size = 16) +
  scale_color_tq() +
  labs(title = "Sentiment 3-Year Forecast", x = "", y = "Level of sentiment") 

enter image description here