Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

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


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: How to loop over non-integers?
Date   Sat, 09 Jun 2007 14:34:36 -0400

I think you want:

  gen yy=.
  forvalues i = 1/100 {
    replace yy=y-(`i'/100)*x1
    reg yy x2
    estimates store tmp`i'
  }

hth,
Jeph

[email protected] wrote:
Dear all,

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 have tried to use the forvalues command to loop over values of a = i/n,
where n goes from 1 to (say) 100, which would give a span of values for a
equal to: 0.01, 0.02, ...,0.98, 0.99, 1.00. I cannot work it out (it would
be trivial with matlab). At any rate, I have a feeling that stata codes
most efficiently without forvalues.

Needless to say, I have looked up the "forvalues" help and Nick Cox's faq
http://www.stata.com/support/faqs/data/sequence2.html.

 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. I have tried countless variations of it. I
just don't get it. I have chosen to submit this particular form of the code
because it is pretty close to the English language, and should be
understandable. I know that the [i] and the `i' are a mess.

I'll appreciate any pointers!

Patrick
[email protected]

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index