Statalist The Stata Listserver


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

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


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Re: How to loop over non-integers
Date   Sun, 10 Jun 2007 14:57:26 +0100 (BST)

--- on Jun 9, 2007, at 12:55 PM, [email protected] wrote:
> 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.

Michael Hanson and Jeph Herrin both suggested using -estimates store-,
though Michael noted that the help file claims that that option is
limited to 20 estimation results. Kit Baum than noted that this limited
has been raised to 300 without changing the help file for -estimates-.

If you want to bypass the limit of 300 estimates you can use -post-,
see the example below:

*------------- begin example ------------
set more off
drop _all

set obs 100
gen x1 = invnorm(uniform())
gen x2 = invnorm(uniform())
gen y = .5*x1 + x2 + invnorm(uniform())
gen yy = .

tempname memhold
tempfile results
postfile `memhold' prop r2 using `results'

forvalues prop = 0(.001)1 {
	replace yy = y - `prop'*x1
	reg yy x2
	post `memhold' (`prop') (e(r2))
}
postclose `memhold'
use `results', clear
twoway line r2 prop
*--------------- end example -------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Also note if you look at how this simulation generates data it is
pretty clear that there is a much easier way of estimating the scalar
a: just use the original y and add x1 and x2 as a predictor, and the
coefficient of x1 is the scalar a you are looking for.

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________ 
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html 
*
*   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