# Re: Re: st: How to loop over non-integers?

 From PatrickT@umac.mo To statalist@hsphsun2.harvard.edu Subject Re: Re: st: How to loop over non-integers? Date Mon, 11 Jun 2007 04:16:41 +0800

```Thank you Michael, this is very helpful.

instrumental variable quantile regression (IVQR) estimates along the lines
of Chernozhukov

and Hansen (2006, see ref. below).

To make a long story short, we have x1 endogenous, x2 exogenous, and z1 an
instrument, the IVQR procedure has 2 steps: ---STEP 1--- qreg of y -
alpha*x1 on x2 and

z1, ---STEP 2--- find the alpha which minimizes the Wald statistic
associated with z1. Denote this value alpha_hat: it is a consistent
quantile estimate.

The code below nearly gets there. From the plot of the Wald statistic
against alpha, the smallest element of alpha (on this coarse grid) is
alpha_hat = 29. I could

not work out how to use the minimum function to extract this value. I tried
things like --- scalar alpha_hat = min(alpha) --- but that won't work.
What's the trick

I'm missing?

*----------- begin example -------------
use auto.dta
rename mpg y
rename gear x1
rename weight x2
rename foreign z1

set matsize 800
scalar a=100
local n 100
matrix A = J(`n',1,.)
matrix W = J(`n',1,.)
forval i = 10/`n' {
gen yy`i' = y - a*(`i'/`n')*x1
quietly qreg yy`i' x2 z1
quietly test z1
matrix A[`i',1] = a*(`i'/`n')
matrix W[`i',1] = r(F)*r(df)
drop yy`i'
}

svmat A, names(alpha)
label variable alpha "candidate values of alpha_hat"
svmat W, names(Wstat)
label variable Wstat "Wald statistic for H0: gamma=0"

graph twoway scatter Wstat alpha
graph export Wstat.ps,replace

*scalar alpha_hat = min(alpha)
scalar alpha_hat = 29
gen yy = y - alpha_hat*x1
quietly qreg yy x2 z1
matrix beta_hat = e(b)
matrix list beta_hat
exit
*----------- example end ---------------

Reference.
Chernozhukov, Victor & Hansen, Christian, 2006."Instrumental quantile
regression inference for structural and treatment effect models," Journal
of Econometrics,

Elsevier, vol. 127(2), pages 491-525, June.

Many many thanks Michael for your help.
Patrick.

Michael Hanson <mshanson@mac.com>
Sent by: owner-statalist@hsphsun2.harvard.edu

To   statalist@hsphsun2.harvard.edu
cc
bcc
Subject   Re: st: How to loop over non-integers?

On Jun 9, 2007, at 12:55 PM, PatrickT@umac.mo wrote:

> My problem is the following. I would like to generate a variable
> (yy) as a
> linear combination of two existing variables (y and x1), of the form
> yy=y-a*x1, where a is a scalar which spans a set of real values
> such as
> [0,1]. I then want to regress that new variable (yy) on another
> variable
> (x2), store the estimates and compare some statistics for various
> values of
> a. For instance, to find the value of a which maximizes the R
> squared. (my
> real problem is quite a bit more complicated than that -- here I am
> merely
> illustrating).

I certainly hope so -- I presume you are aware of the myriad of
reasons why maximizing an R^2 is a poor model selection methodology.
(Any decent applied regression text should contain several of those
reasons.)

>  gen yy=.
>  gen n=100
>  forvalues i = 1/n {
>  replace yy[i]=y-(`i'/n)*x1
>  reg yy[i] x2
>  estimates store tmp[i]
>  }
>
> The above is a complete mess.

Agreed!

> I have tried countless variations of it.

[snip]

Have you considered:

local n 20
forval i = 1/`n' {
gen yy`i' =y - (`i'/`n')*x1
quietly reg yy`i' x2
est store tmp`i'
}

Note that the reason that n = 20 above is that, according to -help
estimates-, "You may store up to 20 estimation sets."  If instead you
simply need to access certain statistics from a -regress- command
(say, the R^2), you could place those into an (n x 1) matrix:

local n 100
matrix r2 = J(`n',1,.)
forval i = 1/`n' {
gen yy`i' = y - (`i'/`n')*x1
quietly reg yy`i' x2
matrix r2[`i',1] = e(r2)
}
mat list r2

> I'll appreciate any pointers!

Hope this helps.

-- Mike

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```