File: ivpanel.inp

package info (click to toggle)
gretl 2021a-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,556 kB
  • sloc: ansic: 394,030; sh: 4,494; makefile: 2,856; cpp: 2,776; xml: 590; perl: 364
file content (431 lines) | stat: -rw-r--r-- 11,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
function void ivp_print (bundle *b)
  matrix result = b["coeff"] ~ b["stderr"]
  scalar case = b["case"]
  scalar N = 0
  if case == 1
    printf "\nFixed-effects TSLS, using %d observations\n", b["nobs"]
  elif case == 2
    printf "\nBetween TSLS, N = %d\n", b["nobs"]
  else 
    printf "\nG2SLS random effects, using %d observations\n", b["nobs"]
  endif
  printf "Dependent variable: %s\n", b["ystr"]
  printf "Endogenous: %s\n", b["Estr"]
  printf "Instruments: %s\n", b["Istr"]

  string S = b["modstr"]
  modprint result S

  printf "  SSR = %g\n", b["SSR"]
  printf "  sigma-hat = %g (df = %d)\n", b["sigma"], b["df"]
  printf "  R-squared = corr(y, yhat)^2 = %f\n", b["rsq"]  
  if (case == 1 || case == 3) 
    matrix dims = b["dims"] 
    N = dims[1]
    printf "  Included units = %d\n", N
    printf "  Time-series length: min = %d, max = %d\n", dims[2], dims[3]
  endif
  scalar X2 = b["wald"]
  scalar df = rows(b["coeff"]) - 1
  scalar pv = pvalue(X, df, X2)
  printf "  Wald chi-square(%d) = %g [%.4f]\n", df, X2, pv 

  if inbundle(b, "Fpool")
    # poolability F-test for fixed effects
    scalar Fp = b["Fpool"] 
    scalar dfn = N-1
    scalar dfd = b["df"]
    pv = pvalue(F, dfn, dfd, Fp)
    printf "  Null hypothesis: The groups have a common intercept\n"
    printf "  Test statistic: F(%d, %d) = %g [%.4f]\n", dfn, dfd, Fp, pv 
  endif

  printf "\n"
end function

/* computes R-squared as the squared correlation between
   y and yhat: we may want to do something else, or nothing
*/

function scalar tsls_rsq (const matrix y, const matrix yh)
  scalar r = corr(y, yh)
  return r^2
end function 

/*
  panel_means_matrix: given the NT x k data matrix X and the 
  NT x 1 matrix u, which records the panel unit ID associated 
  with each row of X, returns an N x k matrix holding the
  unit/group means of the columns of X. We want this for the
  between model.
*/

function matrix panel_means_matrix (const matrix X, const matrix u)
  scalar NT = rows(X)
  matrix e = seq(1, NT)'

  matrix vU = values(u)
  scalar N = rows(vU)
  matrix umean = zeros(N, cols(X))
    
  loop i=1..N -q
      matrix sel = selifr(e, u .= vU[i])
      umean[i,] = meanc(X[sel,])
  endloop	

  return umean
end function

/* Replace the columns of X with group mean minus grand mean:
   we want this for the poolability F-test for fixed effects
*/

function void get_panel_means (matrix *X, const matrix u)
  matrix grand = meanc(X)
  matrix e = seq(1, rows(X))'
  matrix vU = values(u)
  scalar N = rows(vU)
  scalar k = cols(X)
  scalar Ti
   
  loop i=1..N -q
      matrix sel = selifr(e, u .= vU[i])
      Ti = rows(sel)
      matrix tmp = X[sel,]
      X[sel,] = zeros(Ti, k) .+ (meanc(tmp) - grand)
  endloop	
end function

/*
  real_panel_demean: removes the group means and adds back
  the grand mean for all columns of the NT x k matrix X. The 
  input matrix u (NT x 1) must record the panel unit ID associated 
  with each row of X.

  Fills out dims with the number of included groups and the
  minimum and maximum group time-series lengths.
*/

function void real_panel_demean (matrix *X, const matrix u,
                                 matrix *dims)
  matrix grand = meanc(X)
  scalar NT = rows(X)
  matrix e = seq(1, NT)'

  matrix vU = values(u)
  scalar N = rows(vU)
  scalar minT = NT
  scalar maxT = 0
    
  loop i=1..N -q
      matrix sel = selifr(e, u .= vU[i])
      scalar Ti = rows(sel)
      minT = (Ti < minT) ? Ti : minT
      maxT = (Ti > maxT) ? Ti : maxT
      matrix tmp = X[sel,]
      X[sel,] = tmp .+ (grand - meanc(tmp))
  endloop	

  dims = {N, minT, maxT}  
end function

/* panel-demean X "in place" and return dims vector */

function matrix panel_demean_1 (matrix *X, const matrix *u)
  matrix dims
  real_panel_demean(&X, u, &dims)
  return dims
end function

/* panel-demean X into ret, preserving X; return ret
   and fill out dims argument. 
*/

function matrix panel_demean_2 (const matrix X, const matrix u,
                                matrix *dims)
  matrix ret = X
  real_panel_demean(&ret, u, &dims)
  return ret
end function

/*
  Note: Z may be rank-deficient; this is not a problem  _per se_, 
  as long as \hat{X} is full rank. In order to accommodate 
  potentially rank-deficient instrument matrices, we use the QR 
  decomposition so redundant columns can be softly killed (à la
  Burt Bacharach). 

  We then guard against the possibility of \hat{X} not having 
  full column rank by using the generalised inverse. This, of 
  course, is not particularly useful if we want an estimator 
  (it's under-identified) but comes in handy if all we need is 
  the residuals vector.
*/

function matrix matrix_tsls (const matrix y, matrix *X, matrix *Z, 
                             matrix *V[null], matrix *S1[null])
    matrix R 
    matrix Q = qrdecomp(Z, &R)
    if exists(S1)
      # write first-stage fitted values into S1
      S1 = Q*Q'S1
    endif
    matrix XQ = X'Q
    matrix Qy = Q'y
    matrix nonzero = (abs(R[diag]) .> 1.0e-12)'
    if minr(nonzero) == 1
        V = invpd(XQ * XQ')
        ret = V * (XQ * Qy)
    else        
        R = selifc(R, nonzero)
	P = qform(R, invpd(R'R))
        V = ginv(qform(XQ, P))
        ret = V * (XQ * (P * Qy))
    endif

    return ret
end function

/* Compute restricted SSR for poolability test */

function scalar get_SSRr (const matrix y, const matrix X)
  matrix u
  matrix b = mols(y, X, &u)
  return u'u
end function

function scalar panel_sigma (const matrix All, int k,
                             const matrix endocols,
                             const matrix exocols,
			     int ng, int code)
  # break out the various required sub-matrices
  matrix my = All[,1]
  matrix mX = All[,2:k+1]
  matrix Endo = mX[,endocols]
  matrix mZ = mX[,exocols] ~ All[,k+2:]

  # compute IV estimator
  matrix beta = matrix_tsls(my, &mX, &mZ)

  n = rows(mX)  
  dfk = cols(mX)
  loop i=1..k -q
    dfk -= isconst(mX[,i])
  endloop 

  # compute residuals etc
  matrix U = my - mX * beta
  if code == 1   # fixed effects
    scalar df = n - dfk - ng
  elif code == 2 # between
    scalar df = ng - dfk - 1
  else           # straight TSLS
    scalar df = n - dfk - 1
  endif
  
  return sqrt(U'U / df)
end function

function scalar all_varying (list X, list Z)
  scalar ret = 1
  X -= 0
  loop foreach i X -q
    if isconst(X.$i, 0)
      printf "$i is not time-varying\n"
      ret = 0
    endif
  endloop
  Z -= 0
  loop foreach i Z -q
    if isconst(Z.$i, 0)
      printf "$i is not time-varying\n"
      ret = 0
    endif
  endloop  
  return ret
end function

function bundle ivpanel (series y "dependent variable", 
                         list X "regressors",
                         list Z "instruments", 
                         int case[1:3:1] {"Fixed effects", "Between model", "G2SLS"}, 
                         bool quiet[0])
  bundle b
  scalar Fpnum = NA

  # ensure we have a constant in first place in both lists
  X = 0 || X
  Z = 0 || Z

  scalar nendo = nelem(X - Z)
  scalar ninst = nelem(Z - X)
  if nendo > ninst 
     funcerr "Order condition for identification is not satisfied"
  endif

  # fixed effects: test for time variation  
  if case == 1 && !all_varying(X, Z)
     funcerr "Fixed effects: all regressors and instruments must be time-varying"
  endif

  # construct string for printing coeffs
  string Xstr = varname(X)

  # and the name of the dependent variable
  string ystr = argname(y)
  if strlen(ystr) == 0
    ystr = "anonymous"
  endif

  # record cols for endogenous and exogenous regressors
  scalar k = nelem(X)
  matrix endocols = zeros(1, nendo)
  matrix exocols = zeros(1, k - nendo)
  scalar ce = 1
  scalar cx = 1
  loop foreach i X -q
    if (inlist(Z, X.$i))
      exocols[cx] = i
      cx++
    else 
      endocols[ce] = i
      ce++
    endif
  endloop

  # list of all vars
  list Lbig = y X || Z

  # create big data matrix and transform it
  series msk = ok(Lbig)
  set matrix_mask msk
  matrix All = {Lbig}
  matrix u = {$unit}
  if (u[1] > 1) 
    # handle sample offset
    u -= u[1] - 1
  endif
  if case == 1 # within, fixed-effects
    matrix dims = panel_demean_1(&All, &u)
    scalar N = dims[1]
    scalar NT = rows(All)
  elif case == 2 # between
    All = panel_means_matrix(All, u)
    scalar N = rows(values(u))
    NT = N
  elif case == 3 # G2SLS
    matrix dims
    matrix X1 = panel_demean_2(All, u, &dims)
    scalar N = dims[1]
    scalar NT = rows(X1)
    sw = panel_sigma(X1, k, endocols, exocols, N, 1)
    matrix X2 = panel_means_matrix(All, u)
    sb = panel_sigma(X2, k, endocols, exocols, N, 2)
    if !quiet
      printf "sigma-hat(within)  = %.8g\n", sw
      printf "sigma-hat(between) = %.8g\n", sb
    endif    
    # total s^2 for comparison
    # stot = panel_sigma(All, k, endocols, exocols, N, 3)
    # printf "stot = %.8g\n", stot
    All = (X1/sw) | (X2/sb)
  endif    

  # break out the various required sub-matrices
  matrix my = All[,1]
  matrix mX = All[,2:k+1]
  matrix Endo = mX[,endocols]
  matrix mZ = mX[,exocols] ~ All[,k+2:]
  k = cols(mX)
  delete All

  # compute IV estimator plus residuals etc
  matrix V 
  if case == 1
    matrix S1 = mX[,endocols]
    matrix beta = matrix_tsls(my, &mX, &mZ, &V, &S1)
  else
    matrix beta = matrix_tsls(my, &mX, &mZ, &V)
  endif
  matrix U = my - mX * beta

  if case == 1
    # FE: components for poolability F-test
    mX[,endocols] = S1
    matrix u1 = my - mX * beta
    scalar SSRu = u1'u1
    matrix y0 = {y}
    matrix X0 = {X}
    matrix S0 = X0[,endocols]
    get_panel_means(&S0, u)   
    X0[,endocols] = S1 + S0
    scalar SSRr = get_SSRr(y0, X0)
    Fpnum = SSRr - SSRu
    # end F-test stuff
    scalar df = NT - k - N + 1
  elif case == 2
    scalar df = N - k
  else
    scalar df = NT - k
  endif

  scalar SSR = U'U
  scalar s2 = SSR / df
  matrix V *= s2
  matrix se = sqrt(diag(V)) 
  scalar wald = qform(beta[2:,]', invpd(V[2:,2:]))
  
  if case == 3
    my = my[1:NT]
    U = U[1:NT]
  endif
    
  matrix yhat = my - U
  scalar rsq = tsls_rsq(my, yhat)

  # lists of endog vars and additional instruments
  list tmp = X - Z
  string Estr = strsub(varname(tmp), ",", " ")
  tmp = Z - X   
  string Istr = strsub(varname(tmp), ",", " ")

  # assemble bundle for return
  b["nobs"] = NT
  b["coeff"] = beta
  b["stderr"] = se
  b["vcv"] = V
  b["SSR"] = SSR
  b["sigma"] = sqrt(s2)
  b["df"] = df
  b["rsq"] = rsq
  b["wald"] = wald 
  b["modstr"] = Xstr
  b["ystr"] = ystr
  b["Estr"] = Estr
  b["Istr"] = Istr
  b["uhat"] = U
  b["yhat"] = yhat
  b["case"] = case
  if (case == 1 || case == 3) 
    b["dims"] = dims
  endif
  if ok(Fpnum) 
     b["Fpool"] = (Fpnum/SSR) * df/(N-1)
  endif

  if quiet
    print "ivpanel: OK"
  else
    ivp_print(&b)
  endif  

  return b
end function

function bundle GUI_ivpanel (series y "dependent variable", 
                             list X "regressors",
                             list Z "instruments", 
                             int case[1:3:1] {"Fixed effects", "Between model", "G2SLS"}) 
  bundle b = ivpanel(y, X, Z, case)
  return b
end function