Announcement

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

  • Sub sample regression with panel data

    Hi guys,

    I am running an event study, trying to measure the impact of rating changes on the stock market for different countries and I am stuck with this easy procedure:

    My variables are: date, index return and downgrade. I am basically studying the effect that changes in rating (downgrades) have in the stock market (index). I have data from 01.01.1998 until 31.12.2017 and I would like to run a sub-sample regression for the crisis period (01.01.2008 - 31.12.2012) and for the non-crisis period (01.01.1998 - 31.12.2007 & 01.01.2013 - 31.12.2017)

    The variable downgrade is a dummy variable.

    My data looks like this:

    Click image for larger version

Name:	Screen Shot 2018-04-10 at 4.20.52 PM.png
Views:	1
Size:	44.0 KB
ID:	1438580

    I am basically using the following command for the sub-sample regression for the crisis period:

    xtreg indexreturn downgrade if date>=01012008 & date<=31122012, re

    And I get the error: no observations


    Can anyone please help me?
    Last edited by Michael Scott; 10 Apr 2018, 08:24.

  • #2
    Well, on its face, your -if- condition is not compatible with your data. But there is no way to tell you how to fix it because the example you show of your data is a screenshot (which the FAQ explicitly says you should not post). It does not include the crucial information about data storage types and formatting, and it is not importable to Stata to test code.

    If you would like help with this, please repost your example using the -dataex- command. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Please read the entire Forum FAQ for excellent advice about the most useful ways to show data examples (-dataex-), Stata code and Stata output (code delimiters).

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Sorry, attached you can find the excel with the data, I hope it helps.

      Attached Files

      Comment


      • #4
        No, please read the FAQ, and for that matter, re-read what I wrote in #2. Excel file attachments are not helpful. You are asked to use the -dataex- command to properly post an example of your data.

        Comment


        • #5
          I hope now is the right format, sorry for the trouble.

          Downgrade_total_20 is a dummy variable that takes value of 1 when a downgrade happened and 0 otherwise
          Is a panel data with 23 countries (country_id from 1 to 23)

          Data:
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int date byte country_id double(indexreturn downgrade_total_20)
          13881 1    .01219616233551239 0
          13884 1   .018577836439265447 0
          13885 1                     0 0
          13886 1  -.008218431228353907 0
          13887 1  -.006734695368911848 0
          13888 1  -.012528846769329385 0
          13891 1   -.04167351522910437 0
          13892 1 -.0003932789481920286 0
          13893 1   .007748722334384798 0
          13894 1   .009103082399085482 0
          13895 1  -.002006969755274541 0
          13898 1   .018897756011018684 0
          13899 1   .024396502647357803 0
          13900 1  -.009459411729975534 0
          13901 1   -.00890922384918859 0
          end
          format %td date

          Code:
          xtreg indexreturn downgrade if date>=01012008 & date<=31122012, re

          Comment


          • #6
            I note that the crucial part of the FAQ was flagged previously to you in your earlier thread https://www.statalist.org/forums/for...dow-with-loops

            The error here is easy to spot: your dates are daily dates like 13900, such as

            Code:
            . di %td 13900
            21jan1998
            Evidently, none of your dates is more than the large integer 01012008 which is not in your dataset. but a couple of millennia away:

            Code:
            . di %td  01012008
            14oct4730
            You're thinking of 01012008 as 1 Jan 2008 but Stata just sees a large integer, which as said it can interpret as a daily date, but not as you wish.

            Your condition is more easily expressed as

            Code:
             xtreg indexreturn downgrade if inrange(year(date), 2008, 2012), re
            where the function year() does precisely what you will guess it to do.

            There are other ways to do it, such as

            Code:
             xtreg indexreturn downgrade if inrange(date, mdy(1, 1, 2008), mdy(12, 31, 2012)), re

            If you have dates, you need to keep thinking of how Stata handles them. No other way to do it!

            Code:
            help datetime
            is the port to call at.





            Last edited by Nick Cox; 10 Apr 2018, 13:00.

            Comment


            • #7
              Yes, that's much better, thank you. Your date variable is, in fact, a Stata internal format date variable. When you want to reference specific Stata internal format date values in code you either have to know their numeric equivalents, or, more commonly, use the td() function. Thus

              Code:
              xtreg indexreturn downgrade if date>= td(1jan2008) & date<= td(31dec2012), re
              By the way, you can simplify this slightly as:

              Code:
              xtreg indexreturn downgrade if inrange(date, td(1jan2008), td(31dec2012)), re
              or (I'm not sure this next one is really simpler):

              Code:
              xtreg indexreturn downgrade if inrange(yofd(date), 2008, 2012), re
              If you are going to be using Stata regularly in financial work, you absolutely need to get comfortable with how Stata handles date and time variables. To that end, you must go to -help datetime- and then click on the link to the PDF documentation. Read that entire chapter. It's long, and you won't remember everything. But you will at least understand how Stata represents dates and times, the various functions available to convert from one type of datetime variable to another, and you will also learn about display formats that make the numerical representation readable by humans in Stata outputs and in the browser. Once you have been through it, a small amount of experience using them will enable you to rapidly use those date representations that come up often in your work. The ones that you rarely use you will forget, but you will be able to handle them easily when they pop up by referring to the help files or back to the PDF documentation.

              Added: Crossed with #6 whose suggestions overlap mine, but include some other approaches too.

              Comment


              • #8
                Thanks a lot for your quick answers. That really help a lot.


                Regards,
                Michael

                Comment


                • #9
                  Hello All,

                  I have been running regression sub-sample wise for my estimation using the following code:
                  Code:
                  bysort Dummy_Large_Firm: reg Market_Share Debt Control Variables
                  The code successfully gives me results for (Large Firm =1 and otherwise cases). While I try the similar code with lagged values of Leverage using the same code:
                  Code:
                  bysort Dummy_Large_Firm: reg Market_Share L1.Debt Control Variables
                  I get an error stating as
                  "not sorted" "not sorted"
                  .

                  I have already xtset my data. I am using panel data; panel Id is Unique_Identifier, time is set yearly. Here is an example:

                  input long Unique_Identifier int Year float Market_Share double Debt
                  521 2012 .0006814565 613.3
                  531 2008 .0020748428 999.1
                  217 2004 .005539388 511.3
                  251 2017 .007485001 2131.1
                  616 2009 .00013656271 9
                  139 2012 .0023027079 542.3
                  204 2005 .01412031 960
                  217 2012 .005248568 1774.6
                  492 2015 .00007311576 21.2
                  72 2004 .0006172744 6.2
                  145 2010 .00003958025 166.6
                  510 2006 .0009377174 598.9
                  533 2011 .0018658226 1502.3
                  446 2005 .000711036 141.2
                  50 2014 .0007052323 186.8
                  31 2014 .001243356 775.4
                  219 2011 .0017951766 1206.8
                  128 2016 .00011265253 17
                  128 2004 .0002935728 6.3
                  559 2007 .0006496811 126.1
                  end

                  Can someone tell me where am I going wrong?
                  regards,
                  Mohina

                  Comment


                  • #10
                    Your post is confusing. You show code referring to Large_Firm_Dummy, but your data has no such variable. Did you mean Unique_Identifier? Also, you give no information about how you -xtset- your data.

                    Anyway, the key principle is that in order to use the time-series operators (L, F, D), the data must be sorted on the same two variables (panel identifier and time variable) that were specified in the -xtset- command. If they are sorted in any other way, you will get the not sorted error message.

                    Comment


                    • #11
                      I am so much sorry, I missed out on that. Here are the details:
                      I use xtset in the following way:
                      Code:
                      xtset Unique_Identifier Year, yearly
                      Unique_Identifier is my panel id.

                      input long Unique_Identifier int Year float Market_Share double Debt float Dummy_Large_Firm
                      145 2004 .00009369344 10.5 0
                      145 2005 .00022590444 15.1 0
                      145 2006 .0003135598 51.7 0
                      145 2007 .0003180892 101.3 0
                      145 2008 .0003936128 141.5 0
                      145 2009 .0001660433 166 0
                      145 2010 .00003958025 166.6 0
                      141 2008 .0004917706 6.8 0
                      141 2009 .0004732062 15 0
                      141 2010 .0007376175 46.3 0
                      144 2001 .00854411 641.9 1
                      144 2002 .0085564535 690.7 1
                      144 2003 .007989856 835.5 1
                      144 2004 .008733699 1068.1 1
                      144 2005 .008838594 1412.8 1
                      144 2006 .0079933405 1820.9 1
                      144 2007 .007113301 1925.6 1
                      144 2008 .00659326 2051.5 1
                      144 2009 .007042057 1784.8 1
                      144 2010 .007867921 1776.4 1
                      end
                      format %ty Year

                      regards,
                      Mohina

                      Comment


                      • #12
                        OK. You can't do this the way you coded it. When you -bysort Dummy_Large_Firm:...- you break the sort order that -xtset- created. In order to use the L1 operator, the data must be sorted by Unique_Identifier and Year. So this has to be done by looping over the levels of Dummy_Large_Firm instead:

                        Code:
                        clear
                        input long Unique_Identifier int Year float Market_Share double Debt float Dummy_Large_Firm
                        145 2004 .00009369344 10.5 0
                        145 2005 .00022590444 15.1 0
                        145 2006 .0003135598 51.7 0
                        145 2007 .0003180892 101.3 0
                        145 2008 .0003936128 141.5 0
                        145 2009 .0001660433 166 0
                        145 2010 .00003958025 166.6 0
                        141 2008 .0004917706 6.8 0
                        141 2009 .0004732062 15 0
                        141 2010 .0007376175 46.3 0
                        144 2001 .00854411 641.9 1
                        144 2002 .0085564535 690.7 1
                        144 2003 .007989856 835.5 1
                        144 2004 .008733699 1068.1 1
                        144 2005 .008838594 1412.8 1
                        144 2006 .0079933405 1820.9 1
                        144 2007 .007113301 1925.6 1
                        144 2008 .00659326 2051.5 1
                        144 2009 .007042057 1784.8 1
                        144 2010 .007867921 1776.4 1
                        end
                        format %ty Year
                        
                        
                        xtset Unique_Identifier Year, yearly
                        
                        levelsof Dummy_Large_Firm, local(dlfs)
                        foreach dlf of local dlfs {
                            display _newline(3) "Dummy_Large_Firm == `dlf'"
                            regress Market_Share L1.Debt if Dummy_Large_Firm == `dlf'
                        }

                        Comment


                        • #13
                          Many Many Thanks Sir,

                          The code worked to solve my issues.
                          regards,
                          Mohina

                          Comment


                          • #14
                            Clyde Schechter , I have similar issue with subsampling, I have already created a variable called sector with value labels tourism and manufacturing. I want to run a panel regression the sector separately
                            The code I used is
                            eststo:reg ret marketcap if sector==tourism, robust

                            The error I get is type mismatch.

                            Comment


                            • #15
                              Well, you didn't use -dataex-, so I can't tell if sector is actually a string variable or a numeric variable with value labels. If it's a string variable, the correct syntax wold be -if sector == "tourism"-. The quotes are required. If, however, it is truly a numeric variable with value labels, and if the name of the value label is sector_label, the syntax would be -if sector == "tourism":sector_label-.

                              In the future when asking for help with code, always show example data, and always use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                              Comment

                              Working...
                              X