I am still somewhat new to R programming although I have figured out how to get some of my required forecasting models to work. The one problem that I have is that I can't get the Date column from my dataset to print on the x-axis of my final ARIMA ggplot rendering. It just shows an index going in intervals from 0 to 200 to 400 to 600. Everything else works except for the date display. Here is my full code:
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('Sentiment 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")