Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ignore insufficient observations and run the loop without breaking

    Hi, I would like to calculate cumulative security residual return, which is residual in a regression of a firm's monthly stock return(variable"ret") on the market monthly returns (variable "vwretd") over the 12-month period ( estimation_window) before the date of filing of the financial statement (event_window). I have prepared my data and am working on the regression loop. my codes are shown below:

    gen predicted_return=.
    egen id=group(company_id)

    forvalues i=1(1)N { /*note: replace N with the highest value of id */

    reg ret vwretd if id==`i' & estimation_window==1
    predict p if id==`i'
    replace predicted_return = p if id==`i' & event_window==1
    drop p
    }


    initially, it worked well, but when it comes to id==15, it showed the following error and stopped running the loop:

    (option xb assumed; fitted values)
    (8,074,563 missing values generated)
    (0 real changes made)

    insufficient observations
    (option xb assumed; fitted values)
    (8,074,563 missing values generated)
    (0 real changes made)


    I search the forum and tried to fix the problem by using the codes below but it does work. Can I get some help?

    forvalues i=1(1)N { /*note: replace N with the highest value of id */

    capture noisily reg ret vwretd if id==`i' & estimation_window==1
    capture noisily predict p if id==`i'
    capture noisly replace predicted_return = p if id==`i' & event_window==1
    capture noisly drop p
    }

  • #2
    Better answers can be provided if you give more detail, such as pasting the exact code you used along with Stata's output, about how exactly those commands are not working.

    My guess is that the typos on the fourth and fifth lines are causing problems - it should be "capture noisily" rather than "capture noisily".

    Comment


    • #3
      Building off the advice from the second post (especially about the data and code), it is actually possible to capture noisily a group of commands. I can't remember why I recently had to do this, but you can do

      Code:
          foreach v of var `unit' `time' `depvar' {
          cap {    
              conf numeric v `v', ex
              
              as !mi(`v')
              
              qui: su `v'
              
              as r(sd) ~= 0
          }
          }
      Which would prevent spelling errors for large chunks of code that need to be captured.

      Comment


      • #4
        I am assuming the insufficient observations error message was generated by the reg command for id==15.

        Starting with the first block of code in post #1, the following is the most common way of solving problems with code subsequent to a regression that fails to run for a particular subset of the data.
        Code:
        gen predicted_return=.
        egen id=group(company_id)
        
        forvalues i=1(1)N { /*note: replace N with the highest value of id */
            capture noisily reg ret vwretd if id==`i' & estimation_window==1
            if rc==0 {
                predict p if id==`i'
                replace predicted_return = p if id==`i' & event_window==1
                drop p
            }
        }

        Comment


        • #5
          Thank you so much for everyone's help.I fixed the typos but it still does not work. below are the details about my steps:
          Since this is an event study. I have two datasets: one for stock montly returns, and the other for my event of interest, the filing of the financial statements. The first one is called stockdata, the second one is called eventdates. In the stock data, I have matching company id (tic), monthly stock return date (date), stock return (ret), and market return value (vwretd). In the eventdates data, I have company id (tic) and the date of filing of the annual financial statement (fdate) as variables. Since the research period is from 2002 to 2017, so for each company (tic), I have multiple event dates (fdate) (usually it has 16 event dates, one for each year because a company files its financial statement annually). So it is necessary to create a duplicate set of observations for each event date/company combination.

          Step 1: combining event and stock data: Now, I need to find out how many event dates(fdate) there are for each company(tic). Use the dataset of event dates and generate a variable that counts the number of event dates per company. The codes are:
          use eventdates, clear
          by tic: gen eventcount=_N


          Then I cut the dataset down to just one observation for each company. Each company observation is associated with the count of event dates for that company. Save this as a new dataset .
          by tic: keep if _n==1
          sort tic
          keep tic eventcount
          save eventcount


          The next step is to merge the new 'eventcount' dataset with the stock data.
          use stockdata, clear
          sort tic
          merge tic using eventcount
          tab _merge
          keep if _merge==3
          drop _merge


          Now I use Stata's 'expand' command to create the duplicate observations. The 'eventcount' variable has been merged onto each stock observation and tells Stata how many copies of that observation are needed.
          expand eventcount

          Then I create a variable that indicates which "set" of observations within the company each observation belongs to. Then sort the dataset to prepare for another merge.
          drop eventcount
          sort tic date
          by tic date: gen set=_n
          sort tic set
          save stockdata2


          Back in my original event dates dataset. I create a matching set variable to identify the different event dates within each company. The final step is to use the set variable to match each event date with a set of stock observations.
          use eventdates, clear
          sort tic fdate
          by tic: gen set=_n
          sort tic set
          save eventdates2
          use stockdata2, clear
          merge tic set using eventdates2
          keep if _merge==3
          drop _merge


          Finally, create a new variable that groups company_id and set so that I have a unique identifier to use in the rest of the analysis.
          egen group_id = group(tic set)

          Step 2: I need to calculate the event windows and estimation windows. For each company in each year, the event window is the filing date of the annual financial statement. so it is only one day. The estimation window has 12 months, which is the 12 month period before the filing date of the annual financial statement.Note that, typically a record will be identified as falling in the eventwindow when "date"="fdate". However, my stock return data is monthly (I could not use daily stock return data because the data size is too huge for my research period), so for each company, there is only one observation of the stock return(ret) /market return(vwretd) each month. The variable "date" is typically the last day of the month, but the "fdate" in the event data could be any day in a year. In other words, "date" usually won't exactly match with "fdate" in my data. So I identify a record as falling in the event window as long as the "date" and "fdate" are in the same month in the same year and ignore the potential difference in day. Then the estimation window is the 12 months(i.e., 365 days) before the identified "date".here are my codes:

          sort tic date
          gen yr_date = year( date )
          gen month_date=month(date)
          gen yr_fdate=year(fdate)
          gen month_fdate=month(fdate)
          by tic: gen event_window=1 if yr_date== yr_fdate& month_date== month_fdate
          gen dif=date-fdate
          by tic: gen estimation_window=1 if dif<0 & dif>=-365
          replace event_window=0 if event_window==.
          replace estimation_window=0 if estimation_window==.

          Step 3 is the regression that I have problems with. I also tried the codes William suggested, but there is an error. It says rc not found. My codes are:
          gen predicted_return=.
          egen id=group(group_id)

          forvalues i=1(1)N { /*note: replace N with the highest value of id */

          capture noisily reg ret vwretd if id==`i' & estimation_window==1
          if rc==0{
          predict p if id==`i'
          replace predicted_return = p if id==`i' & event_window==1
          drop p
          }
          }


          Originally posted by Ali Atia View Post
          Better answers can be provided if you give more detail, such as pasting the exact code you used along with Stata's output, about how exactly those commands are not working.

          My guess is that the typos on the fourth and fifth lines are causing problems - it should be "capture noisily" rather than "capture noisily".

          Comment


          • #6
            I also tried the codes William suggested, but there is an error. It says rc not found
            Correct the typographical error
            Code:
            if rc==0{
            to
            Code:
            if _rc==0{
            as you could deduce from the output of help capture and the PDF documentation linked to from it.

            Comment

            Working...
            X