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

 From Michael Hanson To statalist@hsphsun2.harvard.edu Subject Re: st: How to loop over non-integers? Date Sat, 9 Jun 2007 15:11:01 -0400

```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/
```