Suppose you want to estimate a time series model with threshold effects. Let's work with this model: \[y_{t} = \alpha + \beta_{1} y_{t-1} + \beta_{2} y_{t-2} + \beta_{3} \delta_{1t} y_{t-1} + \beta_{4} \delta_{2t} y_{t-2} + \varepsilon_{t}\] This can be done using the tstools package in one of two ways.

Begin by simulating some data and loading the tstools package.

```
library(tstools)
set.seed(100)
y <- ts(rnorm(500))
```

**Threshold I**

The most common way to set up a threshold model is based on the regressors. \(\delta_{1t} = 1\) if \(y_{t-1}\) is above a threshold and \(\delta_{2t} = 1\) if \(y_{t-2}\) is above a (potentially different) threshold.

If we want to use zero as the threshold for both \(y_{t-1}\) and \(y_{t-2}\), you can do:

```
rhs <- thrlags(y, 1:2, 0.0)
fit <- tsreg(y, rhs)
```

If you want to use a different threshold for each variable, you'll need to send a vector of threshold values. Let's say the threshold for \(y_{t-1}\) is 0 and the threshold for \(y_{t-1}\) is 0.02. Then we'd have

```
rhs <- thrlags(y, 1:2, c(0.0, 0.02))
fit <- tsreg(y, rhs)
```

**Threshold II**

You can also send a logical vector or matrix directly to `thrlags`

. It will use that vector/matrix as the dummy variable. If you send a vector, it will apply that vector to each variable.

```
d <- y > 0.0
rhs <- thrlags(y, 1:2, d)
fit <- tsreg(y, rhs)
```

**Stratified Sampling**

You need to be concerned with bias resulting from stratified sampling if you create the dummy variables based on the dependent variable in the regression. This is a form of selection bias that can cause your results to be uninteresting, particularly to journal referees.

*Last updated: July 31, 2017*

Index Home