**Hyndsight » R**, and kindly contributed to R-bloggers)

There are quite a few R packages available for nonlinear time series analysis, but sometimes you need to code your own models. Here is a simple example to show how it can be done.

The model is a first order threshold autoregression:

r$} \end{cases}\]" title="Rendered by QuickLaTeX.com"/>

where is a Gaussian white noise series with variance . The following function will generate some random data from this model.

simnlts <- function(n, alpha, beta, r, sigma, gamma, burnin=100) { # Generate noise e <- rnorm(n+burnin, 0, sigma) # Create space for y y <- numeric(n+burnin) # Generate time series for(i in 2:(n+burnin)) { if(y[i-1] <= r) y[i] <- alpha*y[i-1] + e[i] else y[i] <- beta*y[i-1] + gamma*e[i] } # Throw away first burnin values y <- ts(y[-(1:burnin)]) # Return result return(y) } |

The “burn-in” parameter allows the first value of the series to be a random draw from the stationary distribution of the process (assuming it is stationary).

We will estimate the model by minimizing the sum of squared errors across both regimes. Other approaches that take account of the different variances in the two regimes may be better, but it is useful to keep it simple, at least initially. The following function uses a non-linear optimizer to estimate , and . To ensure good starting values, we begin the optimization with and set the initial value of to be the mean of the observed data.

fitnlts <- function(x) { ss <- function(par,x) { alpha <- par[1] beta <- par[2] r <- par[3] n <- length(x) # Check that each regime has at least 10% of observations if(sum(x<=r) < n/10 | sum(x>r) < n/10) return(1e20) e1 <- x[2:n] - alpha*x[1:(n-1)] e2 <- x[2:n] - beta*x[1:(n-1)] regime1 <- (x[1:(n-1)] <= r) e <- e1*(regime1) + e2*(!regime1) return(sum(e^2)) } fit <- optim(c(0,0,mean(x)),ss,x=x,control=list(maxit=1000)) if(fit$convergence > 0) return(rep(NA,3)) else return(c(alpha=fit$par[1],beta=fit$par[2],r=fit$par[3])) } |

There are a few things to notice here.

- I have used a function inside a function. The
`ss`

function computes the sum of squared errors. It would have been possible to define this outside`fitnlts`

, but as I don’t need it for any other purpose it is neater to define it locally. - Occasionally the optimizer will not converge, and then
`fitnlts`

will return missing values. - I have avoided the use of a loop by computing the errors in both regimes. This is much faster than the alternatives.
- If is outside the range of the data, then either or will be unidentifiable. To avoid this problem, I force to be such that each regime must contain at least 10% of the data. This is an arbitrary value, but it makes the estimation more stable.

The functions can be used as follows (with some example parameters):

y <- simnlts(100, 0.5, -1.8, -1, 1, 2) fitnlts(y) |

A similar approach can be used for other time series models.

**leave a comment**for the author, please follow the link and comment on their blog:

**Hyndsight » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...