[Gretl-users] Nonlinear switching regression vs Loop constructs

Allin Cottrell cottrell at wfu.edu
Sat Jan 29 11:30:25 EST 2011

On Fri, 28 Jan 2011, GREg Ory wrote:

> I need some tips on estimating panel data using nonlinear switching
> regression technique. AFAIK there are two ways of solving it.

I've had one more go at this problem, because it seems like quite
a nice showcase for what one can now do with gretl scripting. I'm
attaching a script which combines the grid-search approach with
NLS estimation. That is, I first do the grid-search, over what
you stated were acceptable values for mu and sigma, then I
initialize NLS using the best values from the grid. The script
includes a "switch" to turn fixed-effects on or off.

What I'm getting from this (with fixed effects on) is:

(mu,sigma) = (0.40, 0.20); SSR = 25.6531, R^2 = 0.803620

NLS (unconstrained)
(mu,sigma) = (0.18, 0.03); SSR = 25.4212, R^2 = 0.806201

That is, NLS finds a slightly better fit with an "out of bounds"
mu value. NLS also shows that the sigma estimate is statistically
indistinguishable from zero, which suggests that the logistic term
may not be appropriate.

Allin Cottrell
-------------- next part --------------
set echo off
set messages off

function series GDV(series x, scalar m, scalar s)
     series den = exp(pi*(m-x)/(s*sqrt(3)))
     return 1/(1+den)
end function

open gretl_plant.gdt -q

# CHOICE here: do we want fixed effects or not?
scalar FE = 1
# scalar FE = 0

scalar SSRmin = 1e10
scalar SSRmax = 0
scalar optmu
scalar optsig
scalar badmu
scalar badsig
scalar nvals = 0

# apply scaling (though not for HHI)
Price /=100
Domestic_Sales /= 10000
Export_Sales /= 10000
Total_import /= 10000

# range and step for mu
scalar m_min = 0.28
scalar m_max = 0.501
scalar m_step = 0.01

# range and step for sigma
scalar s_min = 0.01
scalar s_max = 0.201
scalar s_step = 0.01

# regular regressors: include Cost?
list X = const Cost Domestic_Sales Export_Sales Soda PCFT Total_import
# logistic term
series Log
# dependent variable
series y

/**** GRID SEARCH ****/

loop for (mu=m_min; mu<m_max; mu+=m_step) --quiet
  loop for (sig=s_min; sig<s_max; sig+=s_step) --quiet
    Log = GDV(HHI, mu, sig)
    y = Price - Log
    if FE
      panel y X --quiet
      ols y X --quiet
    SSR = $ess
    if SSR < SSRmin
      optmu = mu
      optsig = sig
      SSRmin = SSR
    if SSR > SSRmax
      badmu = mu
      badsig = sig
      SSRmax = SSR

printf "*** Estimated via GRID SEARCH\n\n"
printf "mu in [%g,%g), sigma in [%g,%g)\n", m_min, m_max, 
  s_min, s_max
printf "number of points evaluated: %d\n", nvals
printf "min(SSR): %g for mu = %.3f, sigma = %.3f\n",
  SSRmin, optmu, optsig
printf "max(SSR): %g for mu = %.3f, sigma = %.3f\n",
  SSRmax, badmu, badsig

Log = GDV(HHI, optmu, optsig)
y = Price - Log
if FE
  panel y X
  ols y X
/**** NLS ****/

# initialize NLS from grid search results
scalar m = optmu
scalar s = optsig

printf "*** Estimated via NLS\n"

if FE
  series u = $unit
  list D = dummify(u)
  matrix b = $coeff
  matrix g = zeros(nelem(D),1)
  nls Price = lincomb(X,b) + lincomb(D,g) + Log
     series Log = GDV(HHI, m, s)
     params b g m s
  end nls -q 
  b |= g
  vnam = varname(X) ~ "," ~ varname(D) 
  matrix b = $coeff
  nls Price = lincomb(X,b) + Log
     series Log = GDV(HHI, m, s)
     params b m s
  end nls -q
  vnam = varname(X)

# print out NLS results with nice formatting
matrix cf = b | {m; s}
matrix se = sqrt(diag($vcv))
vnam = vnam ~ ",mu,sigma"
cfse = cf ~ se
modprint cfse vnam
printf "SSR = %g, R-squared = %g\n\n", $ess, $rsq

if m < m_min || m > m_max
  printf "NLS: mu (%g) is not in [%g,%g)\n", m, m_min, m_max
if s < s_min || s > s_max
  printf "NLS: sigma (%g) is not in [%g,%g)\n", s, s_min, s_max

More information about the Gretl-users mailing list