Examples / SWAMY.RPF |
SWAMY.RPF is an example of Swamy random coefficients estimator for panel data. This is for illustration of the techniques for combining separate linear regressions for individuals—in practice, you would use the @SWAMY procedure (or @MEANGROUP, which does straight averages across cross sections rather than the matrix-weighted averages).
The basic idea in the random coefficients model is that the cross-section coefficient vectors are “drawn” from a distribution with a common mean. This is sometimes known as Swamy’s (1970) model. An almost identical analysis from a purely Bayesian standpoint is given in Leamer (1978), pp 272-5. The assumptions underlying the model are
\begin{equation} \beta _i = \beta + u_i \end{equation}
with
\begin{equation} {\mathop{\rm var}} (u_i ) = \Delta \end{equation}
If \(\Delta\) is small, the coefficients will be almost identical. If it were extremely large, the individual coefficient vectors would be, in effect, unconstrained. With a moderate value, the estimator suggests that they have similar, but not identical, values. The Swamy procedure is to estimate \(\Delta\) using the covariance of the differences in separate least squares estimates for the individuals.
This uses the grunfeld.dat data file, with \(N=10\), \(T=20\). Because the Swamy procedure averages across individuals, the size of \(T\) relative to \(N\) isn't as important as in some other methods.
cal(panelobs=20) 1935
all 10//1954:1
open data grunfeld.dat
data(format=prn,org=cols)
The @SWAMY procedure can be used to do the analysis below.
@swamy invest
# constant firmvalue cstock
This requires three passes. The first collects information on the individual regressions. The second determines the precision of the overall estimate, and the final one takes a matrix weighted average of the individual estimates to get the grand estimate.
compute nindiv=10
compute ncoeff=3
dec vect[symm] xxmats(nindiv)
dec vect[vect] betas(nindiv)
dec vect sigmas(nindiv)
dec symm delta(ncoeff,ncoeff)
dec symm xxgls(ncoeff,ncoeff)
dec vect betagls(ncoeff)
dec integer k
First pass, run OLS regressions over each individual. Save %XX, %BETA and %SEESQ. Also accumulate the sum of the coefficients and their outer product.
compute delta=%const(0.0)
compute betagls=%const(0.0)
do i=1,nindiv
linreg(noprint,smpl=%indiv(t)==i) invest
# constant firmvalue cstock
compute xxmats(i)=%xx
compute betas(i)=%beta
compute sigmas(i)=%seesq
ewise delta(j,k)=delta(j,k)+%beta(j)*%beta(k)
compute betagls=betagls+%beta
end do i
Estimate the covariance matrix of beta's
ewise delta(j,k)=1.0/(nindiv-1)*$
(delta(j,k)-(1.0/nindiv)*betagls(j)*betagls(k))
Second pass. Compute the GLS covariance matrix. While we're at it, replace XXMATS with the precision.
compute xxgls=%const(0.0)
do i=1,nindiv
compute xxmats(i)=inv(delta+sigmas(i)*xxmats(i))
compute xxgls=xxgls+xxmats(i)
end do i
compute xxgls=inv(xxgls)
Final pass. Compute the grand coefficient matrix. Display the joint estimation using LINREG with CREATE.
compute betagls=%const(0.0)
do i=1,nindiv
compute betagls=betagls+xxgls*xxmats(i)*betas(i)
end do i
linreg(create,title="Swamy Random Coefficients",$
coeffs=betagls,covmat=xxgls,form=chisquared) invest
# constant firmvalue cstock
Full Program
cal(panelobs=20) 1935
all 10//1954:1
open data grunfeld.dat
data(format=prn,org=cols)
*
* The @SWAMY procedure does the analysis below.
*
@swamy invest
# constant firmvalue cstock
*
compute nindiv=10
compute ncoeff=3
*
dec vect[symm] xxmats(nindiv)
dec vect[vect] betas(nindiv)
dec vect sigmas(nindiv)
dec symm delta(ncoeff,ncoeff)
dec symm xxgls(ncoeff,ncoeff)
dec vect betagls(ncoeff)
dec integer k
*
* First pass, run OLS regressions over each individual. Save %xx, %beta
* and %seesq. Also accumulate the sum of the coefficients and their
* outer product.
*
compute delta=%const(0.0)
compute betagls=%const(0.0)
do i=1,nindiv
linreg(noprint,smpl=%indiv(t)==i) invest
# constant firmvalue cstock
compute xxmats(i)=%xx
compute betas(i)=%beta
compute sigmas(i)=%seesq
ewise delta(j,k)=delta(j,k)+%beta(j)*%beta(k)
compute betagls=betagls+%beta
end do i
*
* Estimate the covariance matrix of beta's
*
ewise delta(j,k)=1.0/(nindiv-1)*$
(delta(j,k)-(1.0/nindiv)*betagls(j)*betagls(k))
*
* Second pass. Compute the GLS covariance matrix. While we're at it,
* replace xxmats with the precision.
*
compute xxgls=%const(0.0)
do i=1,nindiv
compute xxmats(i)=inv(delta+sigmas(i)*xxmats(i))
compute xxgls=xxgls+xxmats(i)
end do i
compute xxgls=inv(xxgls)
*
* Final pass. Compute the grand coefficient matrix
*
compute betagls=%const(0.0)
do i=1,nindiv
compute betagls=betagls+xxgls*xxmats(i)*betas(i)
end do i
linreg(create,title="Swamy Random Coefficients",$
coeffs=betagls,covmat=xxgls,form=chisquared) invest
# constant firmvalue cstock
Output
These are identical, with the first being generated by the @SWAMY procedure, while the second is done using the code in the program.
Linear Regression - Estimation by Swamy Random Coefficients
Dependent Variable INVEST
Panel(20) of Annual Data From 1//1935:01 To 10//1954:01
Usable Observations 200
Degrees of Freedom 197
Mean of Dependent Variable 145.95825000
Std Error of Dependent Variable 216.87529623
Standard Error of Estimate 105.53917534
Sum of Squared Residuals 2194287.9538
Durbin-Watson Statistic 0.3258
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. Constant -9.62928514 17.03503951 -0.56526 0.57189459
2. FIRMVALUE 0.08458734 0.01995591 4.23871 0.00002248
3. CSTOCK 0.19941840 0.05265336 3.78738 0.00015224
Linear Regression - Estimation by Swamy Random Coefficients
Dependent Variable INVEST
Panel(20) of Annual Data From 1//1935:01 To 10//1954:01
Usable Observations 200
Degrees of Freedom 197
Mean of Dependent Variable 145.95825000
Std Error of Dependent Variable 216.87529623
Standard Error of Estimate 105.53917534
Sum of Squared Residuals 2194287.9538
Durbin-Watson Statistic 0.3258
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. Constant -9.62928514 17.03503951 -0.56526 0.57189459
2. FIRMVALUE 0.08458734 0.01995591 4.23871 0.00002248
3. CSTOCK 0.19941840 0.05265336 3.78738 0.00015224
Copyright © 2025 Thomas A. Doan