Solved – R: Fitting a model with periodic, nonlinear and categorical components

nonlinear regressionrregressionseasonalitytime series

Can anyone give me some advice on how to fit a model with linear (some categorical), non-linear and time series components in R? I don't want to use a non-parametric model like a Loess smooth or similar, as I want a simple and well defined confidence interval and the ability to extrapolate. I also don't have sufficient data to justify a non-parametric model. The model form should be as below, note that there are time series components that exhibit seasonal behavior (and my notation probably doesn't express this well!):

$$\hat{Y} = \beta_0 +\beta_1 X_1 + e^{\beta_2X_{1}}+\beta_3 f_{harmonic}(X_t) + \epsilon_i
$$
Where $\epsilon_i \sim N(\mu, \sigma) $.

I know that the nls package can fit non-linear models, and I could use that to fit a harmonic function to get the time series part of the model. But I understand that it doesn't work for factor type variables, so I'm reluctant to use it.

Equally, I understand that the dynlm package can be used to fit dynamic linear models (and could therefore account for seasonality), but it cannot fit dynamic models of the form specified above to the best of my knowledge.

Would the preferred approach be to simply perform each model sequentially on the residuals of the previous procedure? If I did this, how would I ultimately combine them together to create predictions on new data?

Best Answer

Not attempting to give a complete answer, but I note that you don't mention the nlme package, which provides for both nonlinear models and time-series structures on the errors. It is associated with a book by Pinheiro and Bates (2000), Mixed-Effects Models in S and S-PLUS (Springer), which includes a lot of examples.