Latest TSP Examples
Examples Sorted by Date
By Date
Examples Sorted by Group
The examples below are the latest bits of code that have
arisen from technical support questions, plus a few which
were volunteered by TSP users.
A few of them are relatively rough and may not be fully
commented. Others are extensively commented, and some are
in PROC form, quite ready for general use.
Some of the examples are cross-referenced in a second
category. The first category will then be given in
parentheses. For example, panunit is listed first
under U (Unit root), and second under Panel.
Example Categories
G - Panel - GMM models
- probitac (Q)
Probit with SEs robust to autocorrelation (uses GMM). (May-05-98)
- arelbond
Simple Arellano-Bond example, 1-step and 2-step estimators (balanced panel data). Uses cov1step and premaskg. Uses very simple artificial data. (originally written 9/19/97; revised 12/97 for cov1step change; revised 4/98 to correct the COVOC=OWN behavior in the 2-step estimator, and add a comparative table of 1-step, 2-step, and default GMM coefficients) (Apr-17-98)
- cov1step
Arellano-Bond 1-step COVOC matrix - GMM/panel (originally released 8/4/97; revised to add mask argument to work around GMM bug in applying mask to an1) (Dec-17-97)
- firstdif
Automates creation of first-differenced variables for GMM panel models, where the variables are named by time period, like y1 y2 y3, etc. (Nov-11-97)
- lm2test
like Arellano-Bond m2 statistic, tests AR(1) and AR(2), by Bronwyn Hall. Originally written 3/97, revised for 4.3 compatibility. (Oct-08-97)
- lm2test2
Alternative version of lm2test, using explicit lags. Unfortunately, it seems to be about 20% slower than lm2test. (Oct-08-97)
- cov2step
Arellano-Bond 2-step COVOC matrix - GMM/panel (Sep-18-97)
- premaskg
creates mask for GMM with panel data (Aug-04-97)
- premaskc
2 examples of calling premaskg (Aug-04-97)
- mask2
2 different ways to set up a large sparse MASK matrix for use in GMM (different instruments in different equations). (Feb-27-96)
E - Panel - Error Components models
- ec3sls
3SLS with 2-way error components, balanced panel. Uses transformed data with 3SLS commands. (Nov-26-97)
- ec2sls
2SLS with 2-way error components, balanced panel. Uses transformed data with 2SLS commands. (Nov-26-97)
- p3s
Example of using ec3sls on a real model (data files are not included). (Nov-26-97)
- ec2sm1
First matrix algebra version of ec2sls. Does not use transformed data. Notation differs a bit from Hsiao. (Original version 9/97). (Nov-18-97)
- ec2sm2
Second matrix algebra version of ec2sls. Uses transformed data; notation is close to Hsiao. (Nov-18-97)
- h3b
Hsiao's Appendix 3B - verifies 2-way EC Omega inverse (Sep-11-97)
- apd
creates artificial panel data. balanced, one-way random effects, 2 Xs correlated with random effects (May-20-97)
- ub2wfere
unbalanced 2-way fixed and random effects follows Wansbeek and Kapteyn (J of E, 7/1989). Not tested against any real-world benchmark results. (Dec-18-96)
- data2way
generates artificial data for 2-way model (Dec-18-96)
P - Panel - other than GMM and Error Components
- panunit2 (U)
Unit root test for panel data. Revised version of panunit, which has an explicit list of variables to be tested and a flag for generating synthetic data. (Sep-18-98)
- fixedubmv
How to convert PANEL's @FIXED matrix to a series, for the most general case, when the data is unbalanced and has missing values. (originally written 6/98; revised 8/98 to replicate fixed effects from initial nonmissing observations to all observations) (Aug-03-98)
- panchow
Panel Chow test, example of running restricted and unrestricted regressions for different years in the sample. (May-13-98)
- ar1hetub
version of ar1het for unbalanced data (Apr-29-98)
- panunit (U)
unit root test for panel data, following Pesaran, by Bronwyn Hall (originally written 8/97; revised 3/98 to use less memory) (Mar-31-98)
- logitshp (L)
Mixed Logit on (24) shares, panel data. Calculates predicted shares. Handles missing X values for zero shares (in a rather ad hoc way). (Feb-06-98)
- unbalsu4 (M)
(ML) SUR with 4 equations, in a nested pattern of missing data. In this example, there are 4 drugs that were invented at different times, and then observed up to the present (artificial data are used). The code can be used for 1-4 equations in this type of pattern. (Nov-26-97)
- ar1het
AR(1) model with heteroskedasticity (rho(i), sigma2(i)). This follows Kmenta's "Elements of Econometrics" (1986) p.618-620. (Nov-21-97)
- logitfe3 (L)
fixed effects logit for panel data with 3 time periods (Jun-20-97)
- garcia
recursive eqsub, for stochastic DEQ growth model, unbalanced panel (Jun-12-97)
- fixediv
2SLS with fixed effects. Efficient computation, by using PANEL in 2 stages. (May-13-97)
- panhaus (B)
reproduces Hausman test of RE vs. FE in panel data (Feb-13-97)
- bal2wfe
balanced 2-way fixed effects (Dec-18-96)
- fixedse
standard errors for the intercept terms in a fixed effects model (balanced or unbalanced) (Nov-24-96)
- coxpanel
ML estimation of Cox proportional hazards model, balanced data example with 3 time periods. Uses lagged EQSUB feature. (May-16-96)
M - ML and ML PROC, and general nonlinear estimation
- boxcoxar
Box-Cox with AR(1) residual (Jul-28-98)
- boxcox0
Box-Cox transformation when Y is zero (May-29-98)
- student (R)
regression with Student t residuals; see also stacklos example. (May-26-98)
- kalmanhp (K)
Kalman filter HyperParameter estimation, using ML PROC. (originally written 1/97; revised 4/98 to clarify that only SEs for the final state vector are inconsistent) (Apr-13-98)
- rancoef
ML estimation of a regression with a single random coefficient. Outlines how to extend to multiple random coefficients. (Mar-11-98)
- spatial
Proc which estimates Spatial Autocorrelation and Spatial Autoregressive models. User supplies W = spatial proximity weight matrix. (Dec-05-97)
- spatcal
Example of running spatial.tsp on California plant species data. (Dec-05-97)
- olsme
OLS with measurement errors on the dependent variable, and known variances for these measurement errors (obtained from a first stage estimation). That is, the composite variance is made from these known variances that differ across observations, plus a residual variance that is equal across observations. (Dec-04-97)
- unbalsu4
(ML) SUR with 4 equations, in a nested pattern of missing data. In this example, there are 4 drugs that were invented at different times, and then observed up to the present (artificial data are used). The code can be used for 1-4 equations in this type of pattern. (Nov-26-97)
- regarma (T)
regression with ARMA(8,2) errors (uses ML PROC). Much easier to modify than previous codes like armax7. (Oct-27-97)
- diseq104
Disequilibrium model with sample separation known. Section 10.4 of Maddala(1983). (Sep-16-97)
- stacklos (R)
LAD and Student's t residuals on classic Stack Loss dataset (Sep-12-97)
- fiml5
FIML via ML with 5 equations (like fiml4 and fiml11) (Jul-21-97)
- garcia (P)
recursive eqsub, for stochastic DEQ growth model, unbalanced panel (Jun-12-97)
- ml2stage
calculates corrected VCOV for second stage ML estimator. Automated differentiation. No example model or data. (Apr-22-97)
- unbalsur1
unbalanced SUR -- 2 equations; some observations missing for the second equation. ML version only (easiest way to get estimates and proper standard errors). (Dec-02-96)
- grid
checks a 3-parameter nonlinear model for multiple local optima. 2 different ways of choosing starting values: 1. full grid 2. random draw within bounds (like simulated anealling) reports back optimum found (May-23-96)
- coxpanel (P)
ML estimation of Cox proportional hazards model, balanced data example with 3 time periods. Uses lagged EQSUB feature. (May-16-96)
- kfml (K)
estimates Kalman Filter transition matrix via grid search (2 parameters). Compare with kalmanhp above. (Mar-07-96)
- bilinear (T)
ML estimation of simple bilinear time series model. Uses recursive derivative code; would be simpler with ML PROC. (Jan-30-96)
- fiml11
11-equation FIML estimation, via the ML command. Just an extension of the old fiml4 example. (Dec-22-95)
- unbalml
slightly more complicated version of unbalsur1. Probably not needed. (Dec-15-95)
- unbalsur
does unbalanced SUR estimation (2 equations) in about 4 different ways. Fairly complicated. Users who don't want to compare minimum distance, pairwise deletion, etc. should just use unbalsur1. (Aug-10-95)
L - Logit models
- pr2 (Q)
McFadden's pseudo R-squared for Probit/Logit. In TSP 4.4 as "Kullback-Leibler R-squared" (@KLRSQ). (originally written 12/96; revised 4/98 to add Arturo Estrella's improved R-squared) (Apr-29-98)
- logitsh
Logit with shares as dependent variables. Was previously corrupted on examples disk and web page. (originally released 7/96, revised 2/98 to add more comments) (Feb-25-98)
- logitshp
Mixed Logit on (24) shares, panel data. Calculates predicted shares. Handles missing X values for zero shares (in a rather ad hoc way). (Feb-06-98)
- logitfe3
fixed effects logit for panel data with 3 time periods (Jun-20-97)
- testnlog
2 level (5 x 10) nested logit, with artificial test data, by Bronwyn Hall (Apr-01-97)
- wclogit
weighted conditional logit. This example was previously corrupted on the web page and examples disk. (Oct-31-96)
- logitmix
mixed logit via ML command; a standard TSP testrun (Sep-25-96)
- logiteach
data setup code for conditional logit, where each alternative is chosen exactly once. (Sep-12-96)
- logitf
forecasts from a multinomial logit model (picks most likely Y value for a set of X values). not tested. (May-20-96)
Q - QDV - Qualitative Dependent Variables (other than Logit)
- tob2lim
2-Limit Tobit (with both upper and lower bounds). Uses globally concave parameterization for iterations, to avoid convergence problems. (originally written 10/97; minor changes to comments on 7/98) (Jul-24-98)
- nbsim
Using RANDOM(NEGBIN) when the mean is different for each observation (Jun-11-98)
- probitac
Probit with SEs robust to autocorrelation (uses GMM). (May-05-98)
- pr2
McFadden's pseudo R-squared for Probit/Logit. In TSP 4.4 as "Kullback-Leibler R-squared" (@KLRSQ). (originally written 12/96; revised 4/98 to add Arturo Estrella's improved R-squared) (Apr-29-98)
- tobrncf
Tobit with random coefficients, Ionnatos, JBES July 1995 (Apr-02-97)
- switch
switching regression model from 4.3 User's Guide, by Bronwyn Hall (Nov-11-96)
- tobendog
ML estimation of a 2-equation model, where one equation is a tobit (truncated at zero), and the second equation has this variable on the RHS. (Feb-13-96)
- hetprob
Proc: test for heteroscedasticity after Probit estimation (Oct-16-95)
- cn4
4-dimension cumulative normal integral. (Aug-24-93)
R - Robust (resistant to outliers or heteroskedasticity) and non-parametric
- jb
Jarque-Bera normality test, starting from small-sample versions of @SKEW and @KURT stored by MSD. (Sep-04-98)
- student
regression with Student t residuals; see also stacklos example. (May-26-98)
- wtdsampl (I)
Randomly sample from a vector with non-uniform weights. Similiar to random(draw=). Proc WSM, with example of using it. (Dec-12-97)
- stacklos
LAD and Student's t residuals on classic Stack Loss dataset (Sep-12-97)
- kercc
gaussian kernel regression - version with kronecker product. Fast and concise but memory intensive - only good for relatively small samples (up to 500 obs?) Also illustrates DOT trick for using GRAPH in a loop with different TITLE strings (up to 8 chars). (Aug-04-97)
- mbbjex3
moving blocks bootstrap (OLS or LAD), by Bernd Fitzenberger (Jun-17-97)
- ktau
computes Kendall's tau-b (nonparametric correlation), and its standard error. Compares with Spearman rank correlation, and regular Pearson correlation. Includes Proc to compute number of ties, and an improved Rank Proc which accounts for ties. (Mar-27-96)
- kernel
gaussian kernel density estimation and regression by Joao Santos-Silva (compare with kercc above) (Dec-14-95)
- hetprob (Q)
Proc: test for heteroscedasticity after Probit estimation (Oct-16-95)
- rankcorr
revised version of old rankcorr example. This one uses the RANK option in the SORT command, instead of defining a separate RANK Proc. See the ktaub example for an improved rank correlation, which handles ties correctly. (Jun-30-95)
A - Autoregressive residuals with LSQ (nonlinear/multiequation)
- boxcoxar (M)
Box-Cox with AR(1) residual (Jul-28-98)
- pdlar (D)
single PDL variable with AR(p) residuals (4.4) (Jul-11-97)
- pdlar2 (D)
example of calling pdlar (Jun-13-97)
- pdlar243 (D)
4.3 code version of pdlar2 (calls pdlar43) (Jun-12-97)
- pdlar43 (D)
4.3 code version of pdlar (Jun-12-97)
- surar1
nonlinear SUR with AR(1) residuals, different RHO for each equation, conditional or exact ML (Apr-07-97)
- ar4nl
AR(4) on single nonlinear equation (Feb-05-97)
K - Kalman filter
- kald
Kalman filter with dummy variables that are singular in the initial observations (used to test new recursive residuals that no longer assume initial observations are nonsingular). (Sep-11-98)
- kalmanhp
Kalman filter HyperParameter estimation, using ML PROC. (originally written 1/97; revised 4/98 to clarify that only SEs for the final state vector are inconsistent) (Apr-13-98)
- kfllt
Kalman Filter on Local Linear Trend (Harvey p.170) (Sep-05-96)
- kfarma11
Evaluation of conditional likelihood function for ARMA(1,1) via the KALMAN command. (Jul-22-96)
- kfma1
Ditto, but for MA(1) model. Harvey, TSM, 1981, p.103 (Jul-18-96)
- kfloop2
Kalman filter in a DO loop, to compute SEs for state vector at each period. 2 parameters in state vector, with prior computed from initial observations. (Jun-21-96)
- kfloop
Kalman filter in a DO loop, to compute SEs for state vector at each period. 1 parameter in state vector, with user-supplied prior. (Jun-21-96)
- kfml
estimates Kalman Filter transition matrix via grid search (2 parameters). Compare with kalmanhp above. (Mar-07-96)
U - Unit root and cointegration
- shinfull
Shin-Fuller ARMA(p+1,q) unit root test from Journal of Time Series Analysis 1998. Uses exact ML ARMA estimation with multiplicative seasonal. Computes P-value of test statistic by interpolating critical value table from paper. (Oct-02-98)
- sft
Tests shinfull on artificial data. Test P-values against published table of critical values. (Oct-02-98)
- panunit2
Unit root test for panel data. Revised version of panunit, which has an explicit list of variables to be tested and a flag for generating synthetic data. (Sep-18-98)
- vratio
Variance ratio test for unit root. (Jul-20-98)
- panunit
unit root test for panel data, following Pesaran, by Bronwyn Hall (originally written 8/97; revised 3/98 to use less memory) (Mar-31-98)
- adfgls
GLS version of ADF unit root test (Elliott, Rothenberg, Stock (1992)), by Yin-Wong Cheung (May-06-97)
- joh1
revised version of old joh1 example. Sorts the eigenvectors into same order as the sorted eigenvalues. (Jan-30-97)
- ppmex
Phillips-Perron "z hat sub t" unit root test on mexican data. Uses ppzt.tsp. (Oct-24-96)
- ppenders
ppzt on data from 4 countries. Mostly reproduces table on p.263 of Enders, "Applied Economic Time Series", 1995 (Oct-24-96)
- ppzt
defines Proc ppzt, for "z hat sub t" test. Differs from "z hat sub alpha" test in TSP 4.3. (Oct-24-96)
- kwunit2
revised version of KWUNIT Proc by a TSP user. Adds argument to control taking log of input series. Compare with Clint's revised version (kwunit) below. (Mar-29-96)
- kwunit
revised version of old kwunit example: - handles data of any frequency - uses current SMPL to determine range of data (Jan-08-96)
V - VAR - Vector AutoRegression
- varbq
VAR Blanchard-Quah decomposition (AER 1989). This is an alternative way of factoring Sigma (vs. the arbitrary Cholesky shocks) for impulse responses. The user orders the equations so that the first variable can have a long-run effect on all variables, and the last variable can have a long-run effect only on itself. Includes an example with 2 variables and 4 lags. (Oct-26-98)
- bernanke
VAR: Bernanke-Sims decomposition. This is a way of factoring Sigma, where the user specifies zero restrictions on particular elements of the factorization. Based on RATS code, and includes test examples. (Oct-26-98)
- varmc
VAR with Monte Carlo. Runs VAR in a loop with bootstrapped residuals to compute empirical distributions of any item in VAR's output. This example computes standard errors for variance decompositions. (Sep-14-98)
- varsiml
Use SIML to create impulse responses, after a VAR command. Corrects the original version dated 8/94. (Jun-11-98)
- varforc
forecasting after VAR. (originally written 5/97; revised 6/98 to note that FORCST now works after VAR, so this code is obsolete) (Jun-11-98)
- sbic
Shows 3 different normalizations for SBIC and AIC, and which one TSP uses in OLSQ (Feb-19-98)
- varsbici
Chooses optimal lag orders for VAR by minimizing @SBIC. Allows for different lags on different variables. Example with just 2 variables. (Sep-25-97)
- varsbic
chooses optimal lag order for VAR by minimizing @SBIC (Sep-11-97)
- varst32
Uses BJEST to check polynomial roots for stationarity of a VAR (3 variables, 2 lags). Could be revised slightly to use BJEST feature that computes polynomial roots (leave out the series argument, and put polynomial coefficients into @START). (Nov-13-95)
- varst21
same as varst32, but 2 variables with 1 lag (much easier to read and understand) (Nov-13-95)
D - PDL - Polynomial Distributed Lags
- pdlar
single PDL variable with AR(p) residuals (4.4) (Jul-11-97)
- pdlar2
example of calling pdlar (Jun-13-97)
- pdlar243
4.3 code version of pdlar2 (calls pdlar43) (Jun-12-97)
- pdlar43
4.3 code version of pdlar (Jun-12-97)
T - Time series (miscellaneous)
- calendar
Procs which use calendar information to convert IDs like 981231 to year,month,day variables; find which weekday, week, and month a particular day of the year is, etc. (Jul-21-98)
- regarma
regression with ARMA(8,2) errors (uses ML PROC). Much easier to modify than previous codes like armax7. (Oct-27-97)
- hptrend44
Faster version of HPtrend (Hodrick-Prescott trend decomposition). Uses the MFORM(BAND) option in TSP 4.4 for maximum speed. Also includes modular versions of the Proc for faster use with multiple series. (Oct-21-97)
- qtomw
Quarterly to Monthly conversion, using ratio to average (Jun-20-97)
- qtomw2
Quarterly to Monthly conversion, using ratio to average (Jun-20-97)
- ma
simple Proc to calculate moving average of length n (Feb-25-97)
- gapbal
creates gapped smpl for balanced AR1(TSCS) (obsolete in 4.4) (Feb-21-97)
- dh (B)
reproduces Durbin's h statistic (Feb-11-97)
- plotac
Proc which can be called after BJIDENT to print the autocorrelation function (and its 95% bounds) with color graphics. (Sep-03-96)
- bilinear
ML estimation of simple bilinear time series model. Uses recursive derivative code; would be simpler with ML PROC. (Jan-30-96)
- ar1tscs
easy way of creating the gapped SMPL for AR1(TSCS), from an ID variable, for balanced or unbalanced data. (Jun-21-95)
B - Benchmarks - reproducing results
- jb (R)
Jarque-Bera normality test, starting from small-sample versions of @SKEW and @KURT stored by MSD. (Sep-04-98)
- condnum (I)
Condition number of X'X matrix (version which is independent of the scale of X). Example using Klein-Goldberger consumption data. The condition number is a measure of multicollinearity. (originally written 1/98; revised 3/98 to add comment about condition number of Longley regression) (Mar-23-98)
- kleinlmc
Klein-I LIML benchmarks on consumption equation. Reproduces Greene's coefficients, but not his standard errors. Explores alternative standard errors to those produced by the LIML command. (Sep-27-97)
- longley
Longley benchmark with double precision data; compares precision of default orthonormalized regression (11+ digits) with plain (X'X)"(X'Y) regression (7-9 digits) (Aug-12-97)
- prin
reproduces results from PRIN, via MAT commands (Jun-05-97)
- wtdrsq
reproduces @RSQ from a weighted OLS regression (Jun-02-97)
- panhaus
reproduces Hausman test of RE vs. FE in panel data (Feb-13-97)
- dh
reproduces Durbin's h statistic (Feb-11-97)
I - Miscellaneous
- cubic
Equations for real roots of cubic equation, with positive constraints. (Aug-12-98)
- percent
Arbitrary quantiles (such as 5%, 95%), using sort. (Jul-27-98)
- listsort
Creates a LIST (R8 R5 R6 ...) from a vector of integers (8 5 6 ...). (Jul-09-98)
- interp
Linear interpolation of missing values, for very simple case of single isolated missing values. Discusses why this is not a very good idea. (Apr-28-98)
- condnum
Condition number of X'X matrix (version which is independent of the scale of X). Example using Klein-Goldberger consumption data. The condition number is a measure of multicollinearity. (originally written 1/98; revised 3/98 to add comment about condition number of Longley regression) (Mar-23-98)
- sbic (V)
Shows 3 different normalizations for SBIC and AIC, and which one TSP uses in OLSQ (Feb-19-98)
- wtdsampl
Randomly sample from a vector with non-uniform weights. Similiar to random(draw=). Proc WSM, with example of using it. (Dec-12-97)
- tab2
Forms and print 2 x 2 contingency table, with ChiSq(1) test for independence. Inputs are 2 dummy variables. (Oct-16-97)
- tab2t
Examples of calling tab2. (Oct-16-97)
- finance
Financial applications, by Sotiris Staikouras: 1. saving and sorting 60 betas 2. estimates of variance in rolling sample 3. standard deviation of sequential portfolios 4. t-stat for correlation coefficient (Aug-21-97)
- analyzr
using the restricted coefs stored by ANALYZ (May-23-97)
- stack2
Procs which stack 2 matrices (vertically or horizontally) Note: MMAKE will stack (2 or more) matrices horizontally, if they have the same number of rows (they don't have to have the same number of columns). This feature was added to TSP 4.3 on 4/25/95. (Jan-30-97)
- plotac (T)
Proc which can be called after BJIDENT to print the autocorrelation function (and its 95% bounds) with color graphics. (Sep-03-96)
- merge
Proc which merges two samples by ID variables (Jul-31-96)
- fisher
seems to be a revised version of the old fisher example. I'm confused; there seem to be about 3-4 different versions of this, 1 named pindex, and the others named fisher. This one is missing the BHH comments. (Dec-18-95)
- bandm
creates a banded symmetric matrix from its first column. (Dec-15-95)
- analyzf
computes an F-statistic for testing a group of variables with ANALYZ. Compares with the method of running the restricted regression. (Nov-02-95)
- anova
Proc which prints a (formatted) "ANOVA table" after OLS (Oct-26-95)
- listsave
writes a LIST into an external file, formatted as a TSP command. Messy, but the only way to save a list in an external file at present. Could be useful if you are saving an @VCOV matrix in a databank, and you also want to save the @RNMS parameter names for later use by ANALYZ in a separate run. (Aug-04-95)
- sortl
sorts a list of variable names, by the variables' values in a given period. Coding similar to listsave. (Jun-12-95)
- freqsav
a way to save and restore the frequency (for use at start and end of a Proc). Outdated in most releases of TSP 4.3, in which the FREQ command can take a variable (with value 0 1 4 12, etc.) as an argument, and @FREQ is stored with just such a value by FREQ. (Apr-24-95)
If you have any questions about these examples, please send email to
Clint Cummins
clint@leland.stanford.edu.
If you have any questions or comments about TSP please send an
email to info@tspintl.com. Comments or questions about this website
should be sent to the webmaster. Lost? Please consult the site map.