Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Maarten Buis <maartenlbuis@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: ambiguity in -if- qualifier |
Date | Tue, 25 Mar 2014 09:49:14 +0100 |
On Mon, Mar 24, 2014 at 5:36 PM, Yu Chen, PhD wrote: > Suppose I want to do a regression only on foreign cars, using the > auto.dta data set. I have two possible ways to do that. (1). I can > -drop- the domestic cars at the beginning and then do the regression. > This way the regression is performed only on the foreign cars. (2) I > can use an -if- qualifier in the regression command to restrict the > sample to foreign cars. > Do you think these two methods produce the same results? > > Try the code below, and you will see that results differ. > > Code for method (1). > sysuse auto,clear > gen n=_n > tsset n > drop if foreign==0 > reg price L.mpg headroom > > > Code for method (2). > sysuse auto,clear > gen n=_n > tsset n > reg price L.mpg headroom if foreign==1 The problem is not with the -if- condition, but with the way you specified -tsset-. In addition to time, you have two panels (foreign and domestic cars) and in your -tsset- specification you ignored that. To solve that you just specify -tsset foreign n- instead of -tsset n-. In the example below I used -savedresults- to make sure that the results from the two methods are exactly equivalent, and they are (-savedresults- doesn't return an error message): *------------ begin example ----------------- // method 1 sysuse auto,clear gen n=_n tsset foreign n drop if foreign==0 reg price L.mpg headroom savedresults save mustbetrue e() // method 2 sysuse auto,clear gen n=_n tsset foreign n reg price L.mpg headroom if foreign==1 savedresults compare mustbetrue e(), /// exclude(macros: cmdline) *------------- end example ------------------ Hope this helps, Maarten --------------------------------- Maarten L. Buis WZB Reichpietschufer 50 10785 Berlin Germany http://www.maartenbuis.nl --------------------------------- * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/