# Implementation of Gonzalo & Pitarakis (2002, JoE)

Full reference: Jesús Gonzalo, Jean-Yves Pitarakis, "Estimation and model selection based inference in single and multiple threshold models", Journal of Econometrics, vol. 110 (2002), pp. 319-352.

The main idea here is a regression model where the coefficients change when the observed values of an indicator variable (series) lie in certain separate ranges, which are called regimes. These regimes are separated by the threshold values and are determined endogenously through a search algorithm. 

(So in a sense this is the "dumb" brute-force approach which is presented first in the G&P paper, not the sequential refined algorithm that the authors also develop. This approach here can become computationally intensive for many thresholds and/or observations. Currently there are no plans to implement the sequential alternative.)

This approach is a general regression model, so in principle it is valid for cross sectional, time series, or even panel data. However, in the panel case it would then be a pooled model, without the usual unit-specific effects (see another function package Threshold_Panel that is applicable to fixed-effects models). Also, non-stationary data are not allowed (Assumption A2).

All coefficients will in general differ across regimes, it is not possible to restrict a subset to remain unchanged.

# main function GP_thresh_estim

## Arguments:

- dep (series) - dependent / left-hand side variable

- indep (list) - regressors / right-hand side variables

- thresh (series) - regime-determining variable

- M (int), default 2 - max number of thresholds

- robust (bool), default TRUE - use robust inference?

- trim (scalar), default 0.15 (= 15 %) - min fraction of obs in regime

- verbose (bool), default TRUE - print out estimates?

Here only the first three arguments - 'dep', 'indep', and 'thresh' - are obvious and required inputs. In principle, the threshold indicator series can also be part of the regressor list. A constant will be automatically added to the regressors.

Among the optional further arguments, 'M' is the maximum considered number of thresholds, so there are at most M+1 regimes. The 'robust' setting switches on a heteroskedasticity-consistent Wald test version (and possibly also autocorrelation-consistent / HAC, depending on whether the current dataset is time series or not; this choice is inherited behavior from gretl's '--robust' option to the ols command). And 'trim' determines the minimum number of observations that are required in each regime (= between thresholds; expressed as a fraction of the sample size). If 'verbose' is set to TRUE, the final (best) model will be printed directly.

## Returns:

... a bundle with the following members:

- numthresh: number of estimated thresholds (int)

- infocrit: the value of the info criterion (scalar)

- thresholds: col vector (m rows) with the chosen threshold values (matrix)

- mregressors: all the regime-specific regressors for the full threshold model (matrix)

- model: a copy of the $model bundle of the full threshold model (bundle)

- whichregime: indicates for each obs to which regime (1...m+1) it belongs (series)

Note that the information criterion is defined in Gonzalo & Pitarakis and is not the same thing as the standard info.crit. from the final regime-based model. 

The 'mregressors' columns are constructed as the interactions of the original regressors with the constructed regime dummies based on the estimated regimes. This includes those regime dummies themselves, but not the plain, un-interacted, original regressors. For example, a constant term is not included because it would form a linearly dependent set with all regime dummies. The interaction terms' names contain the suffixes "r_1", "r_2" and so forth as regime indications (in the column labels). 
(The matrix object is used because a gretl list is a somewhat fragile construct in the sense that it depends on the suitable dataset being active. If needed, users should be able to use the mat2list function to convert this matrix to a gretl list.) 

Finally, 'model' contains all the members from the standard $model accessor bundle after an OLS estimation; see the general gretl documentation for details. Again, the list sub-members like 'xlist' are expected to be fragile and perhaps not directly usable by themselves.

## Changelog

- November 2024, v0.1 initial public release
