0

I hope the title makes sense. Basically, I have been working on data for a while and I'd like to forecast future values and plot them. My regression mode is this: lm(Total~ Rank+ Market), when I do regression analysis I can see the coefficients and everything. My data has the following columns: total, market, rank, date. But with forecasting function I believe the regression model did not work. I tried converting Total into a time-series and plotting it but in that case, the other dependent variables have no effect on the total value.

I have been trying to figure this out for a while and I looked into a lot of forecasting methods but they only include one variable, not the regression model itself. Can you please provide any resources or syntax of how to forecast my multivariable regression model?

https://rpubs.com/Mentors_Ubiqum/tslm

I thought this was what I'm looking for but they did not include the other variables in the function and I tried my model with tslm but it didn't work. I think tslm can be only forecasted with trend and season variables?

Thank you in advance

edit:

So far what I have tried :

ts <- ts(originalfile$Total, frequency = 365, start decimal_date(as.Date("2020-08-01")))

ts_dataframe <- (Total = ts, originalfile$market, originalfile$rank)

mymodel <- tslm(Total ~ Rank +Market, ts_dataframe)

and then I tried to plot mymodel with auto.arima and with forecast. Auto arima did not work nad forecast worked but graph looks weird visually there are no lines just a straight line in a rectangle. I cannot share the picture it's on my work laptop but what seems to be wrong here?

James Z
  • 12,209
  • 10
  • 24
  • 44
raistlinn
  • 1
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 30 '21 at 20:24
  • thank you will do that. – raistlinn Nov 30 '21 at 21:39
  • does it make more sense now? please let me know if you need more input – raistlinn Nov 30 '21 at 21:52
  • No. This still does not help since we can't actually run the code and see the output ourselves. It's not clear what you would expect the output to look like when you have three variables in involved in your model. Stack Overflow is for specific programming question. If you have modeling questions or seek data visualization advice, perhaps [stats.se] is a better venue. – MrFlick Nov 30 '21 at 23:19
  • I see thank you. I can't post the whole thing since it's work related. Could you tell me in my case to forecast multivariable time series what package is usually used? – raistlinn Nov 30 '21 at 23:24
  • 1
    To forecast with a model that uses exogenous regressors (like `Rank` and `Market`) you'll need to also provide the future values of these variables. Some examples of how this can be done with the `tslm()` model can be found here: https://otexts.com/fpp2/forecasting-regression.html – Mitchell O'Hara-Wild Dec 01 '21 at 00:59

1 Answers1

0

Good news, multivariable regression with time series works perfectly fine in R. I very highly recommend the book Forecasting Principles and Practice, by Rob J. Hyndman and George Athanasopoulos. It can be viewed for free via the author's web site: https://otexts.com/fpp3/, or purchased via Amazon.

A good example of multiple linear regression with a time series is shown on this page: https://otexts.com/fpp3/regression-intro.html. It uses TSLM to do multiple linear regression.

First, let's do the time series, then let's look at the graphs:

us_change %>% 
  model(TSLM(Consumption ~ Income + Production + Savings + Unemployment + season() + trend())) %>% 
  report()

Let's look at that report:

report

Let's look at some graphs of the data. It's possible to view all the graphs of the features (Production, Savings and Unemployment) at once:

us_change %>%
  pivot_longer(-Quarter) %>%
  ggplot(aes(Quarter, value, color = name)) +
  geom_line() +
  facet_grid(name ~ ., scales = "free_y") +
  guides(colour = "none") +
  labs(y="% change")

Plot of all features simultaneously

All well and good, but here's the best part: You can forecast, and graph the forecasts, too. Our forecasts will also include season and trend, which improves accuracy.

train <- us_change %>% 
  filter(Quarter <yearquarter("2009 Q4"))
test <- us_change %>% 
  filter(Quarter>=yearquarter("2009 Q4"))

fit <- train %>% 
  model(TSLM(Consumption ~ Income + Production + Savings + Unemployment + season() + trend()))

forecast_consumption <- forecast(fit, new_data = test)

forecast_consumption

Now let's look at a graph:

forecast_consumption %>% 
  autoplot(us_change)

Plot of predictions

This should give you the resources (the free online book, or print version via Amazon) and sample code to work with multiple regression time series in R. The sample code here is different than the sample code in the book, so you have two examples to use. Best of luck to you!

Russ Conte
  • 124
  • 6