Announcement

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

  • How to store the time fixed effects please?

    Hello,

    I have learned that with these commands, I can store the entity fixed effects:

    xtreg y x1 x2 i.year, fe
    predict fe, u


    However, is there a way I can save the time fixed effects(the results for i.year) please?
    (Since this seems to be a general question, I did not include data here. Look forward to your kind reply!)

  • #2
    To my knowledge there is no single command that will do this, but a few lines of simple code does the trick:

    Code:
    levelsof year, local(years)
    gen year_effect = .
    foreach y of local years {
        replace year_effect = _b[`y'.year] if year == `y'
    }

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      To my knowledge there is no single command that will do this, but a few lines of simple code does the trick:

      Code:
      levelsof year, local(years)
      gen year_effect = .
      foreach y of local years {
      replace year_effect = _b[`y'.year] if year == `y'
      }


      Hi Clyde, thanks for your kind reply! However, I tried these commands and seems `y'.YM doesn't seem to be recognized as the time-fixed effect for each year-month(here I use YM rather than year since I have monthly panel data). I'm not sure what's going wrong here.

      Code:
      . levelsof YM, local(YMs)
      663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
      >  697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 
      > 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      
      . gen YM_effect = .
      (48,552 missing values generated)
      
      . foreach y of local YMs {
        2.     replace YM_effect = _b[`y'.YM] if YM == `y'
        3. }
      [663.YM] not found
      r(111);

      Comment


      • #4
        Guo:
        you may want to take at the
        Save the FEs as variables
        . reghdfe ln_w grade age ttl_exp tenure not_smsa south , absorb(FE1=idcode FE2=year)
        reported in the help file of the community-contributed module -reghdfe-.
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Guo:
          you may want to take at the

          reported in the help file of the community-contributed module -reghdfe-.
          Many thanks Carlo! The command works quite well! Thanks again for your kind reply!

          Comment


          • #6
            You have found a solution to your problem from Carlo's great post. So you may not want to bother troubleshooting. But the approach that I used in #2 works in the general situation, and I don't understand what went wrong when you tried it. Here's a demonstration of how it works using the StataCorp online grunfeld.dta:

            Code:
            webuse grunfeld, clear
            
            xtreg mvalue kstock i.year, fe
            
            levelsof year, local(years)
            gen year_effect = .
            foreach y of local years {
                replace year_effect = _b[`y'.year] if year == `y'
            }
            
            browse
            My best guess is that the problem is that there actually aren't any observations with YM == 663 in your estimation sample. There are such observations in your data set (otherwise 663 would not appear in the output of -levelsof YM-.) But it seems that they are all omitted, probably due to missing values in the other variables in your regression model. Remember that any observation with a missing value for any variable mentioned in the regression command is omitted from the estimation sample. So if there are no YM == 663 observations in the estimation sample, there will be no _b[663.YM].

            Actually, my code should have anticipated the possibility that not all time periods in the data set would necessarily appear in the estimation sample. So the code should have been:
            Code:
            levelsof YM if e(sample), local(YMs)
            gen YM_effect = .
            foreach y of local YMs {
                replace YM_effect = _b[`y'.YM] if YM == `y'
            }
            Sorry about that.

            Comment

            Working...
            X