Unable to forecast linear model in R -
i'm able forecasts arima model, when try forecast linear model, not actual forecasts - stops @ end of data set (which isn't useful forecasting since know what's in data set). i've found countless examples online using same code works fine, haven't found else having same error.
library("stats") library("forecast") y <- data$mfg.shipments.total..usa. model_a1 <- auto.arima(y) forecast_a1 <- forecast.arima(model_a1, h = 12)
the above code works perfectly. however, when try linear model....
model1 <- lm(y ~ mfg.no.total..usa. + mfg.inv.total..usa., data = data ) f1 <- forecast.lm(model1, h = 12)
i error message saying must provide new data set (which seems odd me, since documentation forecast package says optional argument).
f1 <- forecast.lm(model1, newdata = x, h = 12)
if this, able function work, forecast predicts values existing data - doesn't predict next 12 periods. have tried using append function add additional rows see if fix issue, when trying forecast linear model, stops @ recent point in time series.
here's data i'm using:
+------------+---------------------------+--------------------+---------------------+ | | mfg.shipments.total..usa. | mfg.no.total..usa. | mfg.inv.total..usa. | +------------+---------------------------+--------------------+---------------------+ | 2110-01-01 | 3.59746e+11 | 3.58464e+11 | 5.01361e+11 | | 2110-01-01 | 3.59746e+11 | 3.58464e+11 | 5.01361e+11 | | 2110-02-01 | 3.62268e+11 | 3.63441e+11 | 5.10439e+11 | | 2110-03-01 | 4.23748e+11 | 4.24527e+11 | 5.10792e+11 | | 2110-04-01 | 4.08755e+11 | 4.02769e+11 | 5.16853e+11 | | 2110-05-01 | 4.08187e+11 | 4.02869e+11 | 5.18180e+11 | | 2110-06-01 | 4.27567e+11 | 4.21713e+11 | 5.15675e+11 | | 2110-07-01 | 3.97590e+11 | 3.89916e+11 | 5.24785e+11 | | 2110-08-01 | 4.24732e+11 | 4.16304e+11 | 5.27734e+11 | | 2110-09-01 | 4.30974e+11 | 4.35043e+11 | 5.28797e+11 | | 2110-10-01 | 4.24008e+11 | 4.17076e+11 | 5.38917e+11 | | 2110-11-01 | 4.11930e+11 | 4.09440e+11 | 5.42618e+11 | | 2110-12-01 | 4.25940e+11 | 4.34201e+11 | 5.35384e+11 | | 2111-01-01 | 4.01629e+11 | 4.07748e+11 | 5.55057e+11 | | 2111-02-01 | 4.06385e+11 | 4.06151e+11 | 5.66058e+11 | | 2111-03-01 | 4.83827e+11 | 4.89904e+11 | 5.70990e+11 | | 2111-04-01 | 4.54640e+11 | 4.46702e+11 | 5.84808e+11 | | 2111-05-01 | 4.65124e+11 | 4.63155e+11 | 5.92456e+11 | | 2111-06-01 | 4.83809e+11 | 4.75150e+11 | 5.86645e+11 | | 2111-07-01 | 4.44437e+11 | 4.40452e+11 | 5.97201e+11 | | 2111-08-01 | 4.83537e+11 | 4.79958e+11 | 5.99461e+11 | | 2111-09-01 | 4.77130e+11 | 4.75580e+11 | 5.93065e+11 | | 2111-10-01 | 4.69276e+11 | 4.59579e+11 | 6.03481e+11 | | 2111-11-01 | 4.53706e+11 | 4.55029e+11 | 6.02577e+11 | | 2111-12-01 | 4.57872e+11 | 4.81454e+11 | 5.86886e+11 | | 2112-01-01 | 4.35834e+11 | 4.45037e+11 | 6.04042e+11 | | 2112-02-01 | 4.55996e+11 | 4.70820e+11 | 6.12071e+11 | | 2112-03-01 | 5.04869e+11 | 5.08818e+11 | 6.11717e+11 | | 2112-04-01 | 4.76213e+11 | 4.70666e+11 | 6.16375e+11 | | 2112-05-01 | 4.95789e+11 | 4.87730e+11 | 6.17639e+11 | | 2112-06-01 | 4.91218e+11 | 4.87857e+11 | 6.09361e+11 | | 2112-07-01 | 4.58087e+11 | 4.61037e+11 | 6.19166e+11 | | 2112-08-01 | 4.97438e+11 | 4.74539e+11 | 6.22773e+11 | | 2112-09-01 | 4.86994e+11 | 4.85560e+11 | 6.23067e+11 | | 2112-10-01 | 4.96744e+11 | 4.92562e+11 | 6.26796e+11 | | 2112-11-01 | 4.70810e+11 | 4.64944e+11 | 6.23999e+11 | | 2112-12-01 | 4.66721e+11 | 4.88615e+11 | 6.08900e+11 | | 2113-01-01 | 4.51585e+11 | 4.50763e+11 | 6.25881e+11 | | 2113-02-01 | 4.56329e+11 | 4.69574e+11 | 6.33157e+11 | | 2113-03-01 | 5.04023e+11 | 4.92978e+11 | 6.31055e+11 | | 2113-04-01 | 4.84798e+11 | 4.76750e+11 | 6.35643e+11 | | 2113-05-01 | 5.04478e+11 | 5.04488e+11 | 6.34376e+11 | | 2113-06-01 | 4.99043e+11 | 5.13760e+11 | 6.25715e+11 | | 2113-07-01 | 4.75700e+11 | 4.69012e+11 | 6.34892e+11 | | 2113-08-01 | 5.05244e+11 | 4.90404e+11 | 6.37735e+11 | | 2113-09-01 | 5.00087e+11 | 5.04849e+11 | 6.34665e+11 | | 2113-10-01 | 5.05965e+11 | 4.99682e+11 | 6.38945e+11 | | 2113-11-01 | 4.78876e+11 | 4.80784e+11 | 6.34442e+11 | | 2113-12-01 | 4.80640e+11 | 4.98807e+11 | 6.19458e+11 | | 2114-01-01 | 4.56779e+11 | 4.57684e+11 | 6.36568e+11 | | 2114-02-01 | 4.62195e+11 | 4.70312e+11 | 6.48982e+11 | | 2114-03-01 | 5.19472e+11 | 5.25900e+11 | 6.47038e+11 | | 2114-04-01 | 5.04217e+11 | 5.06090e+11 | 6.52612e+11 | | 2114-05-01 | 5.14186e+11 | 5.11149e+11 | 6.58990e+11 | | 2114-06-01 | 5.25249e+11 | 5.33247e+11 | 6.49512e+11 | | 2114-07-01 | 4.99198e+11 | 5.52506e+11 | 6.57645e+11 | | 2114-08-01 | 5.17184e+11 | 5.07622e+11 | 6.59281e+11 | | 2114-09-01 | 5.23682e+11 | 5.24051e+11 | 6.55582e+11 | | 2114-10-01 | 5.17305e+11 | 5.09549e+11 | 6.59237e+11 | | 2114-11-01 | 4.71921e+11 | 4.70093e+11 | 6.57044e+11 | | 2114-12-01 | 4.84948e+11 | 4.86804e+11 | 6.34120e+11 | +------------+---------------------------+--------------------+---------------------+
edit - here's code used adding new datapoints forecasting.
library(xts) library(mondate) d <- as.mondate("2115-01-01") d11 <- d + 11 seq(d, d11) newdates <- seq(d, d11) new_xts <- xts(order.by = as.date(newdates)) new_xts$mfg.shipments.total..usa. <- na new_xts$mfg.no.total..usa. <- na new_xts$mfg.inv.total..usa. <- na x <- append(data, new_xts)
not sure if ever figured out, in case thought i'd point out what's going wrong.
the documentation forecast.lm
says:
an optional data frame in variables predict. if omitted, assumed variables trend , season, , h forecasts produced.
so it's optional if trend
, season
only predictors.
the arima model works because it's using lagged values of time series in forecast. linear model, uses given predictors (mfg.no.total..usa.
, mfg.inv.total..usa.
in case) , needs corresponding future values; without these, there no independent variables predict from.
in edit, added variables future dataset, still have values of na future points, forecasts na.
Comments
Post a Comment