Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Loop fails to replace missing values


From   "Francis, Richard N" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Loop fails to replace missing values
Date   Wed, 4 Dec 2013 16:59:03 +0000

Thank you Nick, I really appreciate your patience.

Rick 

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Wednesday, December 04, 2013 7:32 AM
To: [email protected]
Subject: Re: st: Loop fails to replace missing values

Your loop

qui foreach v in tstat {
      replace `v' = r(`v') in `i'
}

reduces to a single statement

replace tstat = r(tstat) in `i'

Now r(tstat) is evidently not defined. That is not an error in Stata, as Stata merely substitutes missing. In your case, therefore, nothing is changed.

You are evidently confusing -r(tstat)- with the scalar -tstat-.

Your code simplifies to

destring gvkey, replace
egen id = group(gvkey), label
gen date1 = yq(year, fqtr)
tsset id date1, quarterly
su id, meanonly
local limit=r(max)
g tstat = .
gen which = ""

forval i = 1/`limit' {
     display "Iteration of `i' of `limit"
     replace which = "`: label (id) `i''" in `i'
     dfgls fcf if id == `i' , maxlag(4) ers
     matrix b = e(b)
     matrix v1 = e(V)
     qui replace tstat = b[1,1] / sqrt(v1[1,1]) in `i'
 }

Following also an earlier comment by Kieran McCaul I replaced loops over one element by single statements. It's possible that these loops were intended to be expanded by you, in which case you would need to reinstate them.

Note also, primarily as a matter of style, that there is little point in putting values into named scalars if all you want to do is take out the value immediately afterwards. That's like getting a box, putting your pen in it, and then taking it out the pen to use just afterwards.
The boxing up is harmless, but redundat.

The -destring- statement also appears harmless, but redundant.  -egen,
group()- will accept a string variable.

Nick
[email protected]


On 4 December 2013 14:07, Francis, Richard N <[email protected]> wrote:
> Hi Statalist users,
>
> Have a loop with a REPLACE command which fails to update the missing values with valid numeric values.
>
> The loop is as follows:
>
> destring gvkey, replace
>  egen id = group(gvkey), label
>  gen date1 = yq(year, fqtr)
>  tsset id date1, quarterly
>  su id, meanonly
>  local limit=r(max)
>  foreach v in tstat {
>       g `v' = .
>  }
>  gen which = ""
>
>  forval i = 1/`limit' {
>          display "Iteration of `i' of `limit"
>          replace which = "`: label (id) `i''" in `i'
>          dfgls fcf if id == `i' , maxlag(4) ers
>                  matrix b = e(b)
>                  matrix v1 = e(V)
>                  scalar coeff = b[1,1]
>                  scalar se1 = sqrt(v1[1,1])
>                  scalar tstat = coeff/se1
>                  qui foreach v in tstat {
>
>                 replace `v' = r(`v') in `i'
>
>          }
> }
>
> The -dfgls- program generates the e(b) and e(V) matrices, but I'm unsure if my use of "scalar" in this situation is correct.
>
> Any help is greatly appreciated.
>
> Thank you!
>
> Rick
>
> *
> *   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/
*
*   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/

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index