# \donttest{
## The function is currently defined as
plot_trend_and_forecast <- function(data, x_col, y_col, group_col) {
data$Date <- as.Date(data$Date)
data_groups <- split(data, data[[group_col]])
for (group in names(data_groups)) {
group_data <- data_groups[[group]]
time_series <- ts(group_data[[y_col]], frequency = 1, start = min(group_data[[x_col]]))
arima_model <- auto.arima(time_series)
forecast <- forecast(arima_model, h = 6)
forecast_dates <- seq(max(group_data[[x_col]]) + 1,
to = max(group_data[[x_col]]) + length(forecast$mean),
by = "day")
forecast_data <- data.frame(Date = forecast_dates, Forecast = forecast$mean)
print(ggplot() +
geom_line(data = group_data, aes(x = !!rlang::sym(x_col), y = !!rlang::sym(y_col))) +
geom_line(data = forecast_data, aes(x = Date, y = Forecast), color = "blue") +
geom_ribbon(data = forecast_data, aes(x = Date, ymin = forecast$lower[, 2],
ymax = forecast$upper[, 2]), fill = "blue", alpha = 0.2) +
labs(x = x_col, y = y_col, title = group) +
theme_minimal())
}
}
## Example of use:
data <- data.frame(
Date = c("2022-07-01", "2022-07-02", "2022-07-03", "2022-07-29", "2022-07-30", "2022-07-31"),
Block = c("1_234", "1_234", "1_234", "1_235", "1_235", "1_235"),
Requests = c(372234, 268816, 291224, 1928854, 1928290, 786539),
Impressions = c(18537, 12432, 13764, 2839269, 2682648, 1114773),
Revenue = c(13.5, 9.13, 8.85, 1669.0, 1654.0, 739.0),
Clicks = c(1167, 720, 856, 214451, 196657, 93178),
Viewable = c(13320, 8214, 9768, 2446884, 2243865, 1063158)
)
plot_trend_and_forecast(data, "Date", "Impressions", "Block")
#Any value instead of Impressions can be here
# }Run the code above in your browser using DataLab