Announcement

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

  • Replace missing observation before first observation of section

    Hi all,

    I want to summarize my fragility variable for different types of growth episodes, so first i generated a variable equal to fragility if that episode dummy = 1 and that's great. But for this type of variable, the year before the start of the episode is also relevant and I need to include that in my summary statistics of fragility. so I want to replace the 1 missing observation of crisisfrag preceding each chunk of data in crisisfrag and i want to replace it with the corresponding row frag_index (by corresponding i mean the row preceding each chunk)

    I'll also have to do this for 3 other types of episodes, but for now I'm focussing on crisis for the example.

    so far I have:
    Code:
    gen crisisfrag = frag_index if crisis_dummy == 1
    when I tried
    Code:
    replace crisisfrag[_n-1] = fragindex[_n-1] if crisisfrag[_n-1]==.
    I got an error that said "weights not allowed"

    Any help is appreciated!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str52 country_name double year float(frag_index crisis_dummy crisisfrag)
    "Angola"     1981 1.1493217 1 1.1493217
    "Angola"     1982 1.1500545 1 1.1500545
    "Angola"     1983 1.1507871 1 1.1507871
    "Angola"     1984 1.1518463 1 1.1518463
    "Angola"     1985 1.1521727 1 1.1521727
    "Angola"     1986 1.1529053 1 1.1529053
    "Angola"     1987 1.1522527 1 1.1522527
    "Angola"     1988 1.2388158 1 1.2388158
    "Angola"     1989  1.239795 1  1.239795
    "Angola"     1990 1.1879622 1 1.1879622
    "Angola"     1991 1.1899204 1 1.1899204
    "Angola"     1992  1.127124 1  1.127124
    "Angola"     1993 1.1236141 1 1.1236141
    "Angola"     1994 1.1281831 1 1.1281831
    "Angola"     1995  1.130794 0         .
    "Angola"     1996  1.133485 0         .
    "Angola"     1997 1.1338112 0         .
    "Angola"     1998 1.1316068 0         .
    "Angola"     1999 1.1369085 0         .
    "Angola"     2000 1.1416377 0         .
    "Angola"     2001 1.0815322 0         .
    "Angola"     2002  .9746989 0         .
    "Angola"     2003 .56646764 0         .
    "Angola"     2004 .56915855 0         .
    "Angola"     2005  .5735613 0         .
    "Angola"     2006 .57731134 0         .
    "Angola"     2007  .5823668 0         .
    "Angola"     2008  .5867696 0         .
    "Angola"     2009  .5912523 1  .5912523
    "Angola"     2010 .53220606 1 .53220606
    "Angola"     2011   .492501 1   .492501
    "Angola"     2012  .4969837 1  .4969837
    "Angola"     2013  .5011401 1  .5011401
    "Angola"     2014 .50568944 1 .50568944
    "Angola"     2015 .50937295 1 .50937295
    "Angola"     2016  .5537676 1  .5537676
    "Angola"     2017  .4810795 1  .4810795
    "Angola"     2018  .3022385 1  .3022385
    "Angola"     2019 .37884295 1 .37884295
    "Azerbaijan" 1991  .4470737 1  .4470737
    "Azerbaijan" 1992  .3963463 1  .3963463
    "Azerbaijan" 1993  .7526202 1  .7526202
    "Azerbaijan" 1994  .6688801 1  .6688801
    "Azerbaijan" 1995   .770782 1   .770782
    "Azerbaijan" 1996  .7729334 1  .7729334
    "Azerbaijan" 1997  .7190617 0         .
    "Azerbaijan" 1998  .7223123 0         .
    "Azerbaijan" 1999  .7262556 0         .
    "Azerbaijan" 2000  .7307451 0         .
    "Azerbaijan" 2001   .734835 0         .
    "Azerbaijan" 2002   .794482 0         .
    "Azerbaijan" 2003   .796647 0         .
    "Azerbaijan" 2004   .799498 0         .
    "Azerbaijan" 2005  .8018028 0         .
    "Azerbaijan" 2006  .8052664 0         .
    "Azerbaijan" 2007  .7496784 0         .
    "Azerbaijan" 2008  .6955136 0         .
    "Azerbaijan" 2009  .6978116 0         .
    "Azerbaijan" 2010  .6996368 0         .
    "Azerbaijan" 2011  .7013153 0         .
    "Azerbaijan" 2012   .703247 0         .
    "Azerbaijan" 2013  .6681315 0         .
    "Azerbaijan" 2014  .6695903 0         .
    "Azerbaijan" 2015  .6709757 0         .
    "Azerbaijan" 2016  .7688444 0         .
    "Azerbaijan" 2017  .7698235 0         .
    "Azerbaijan" 2018  .6767297 0         .
    "Azerbaijan" 2019  .6840762 0         .
    "Bahrain"    1981 .52876216 1 .52876216
    "Bahrain"    1982 .52938837 1 .52938837
    "Bahrain"    1983  .5296149 1  .5296149
    "Bahrain"    1984 .52994806 1 .52994806
    "Bahrain"    1985 .53151333 1 .53151333
    "Bahrain"    1986  .5303212 1  .5303212
    "Bahrain"    1987  .5293088 0         .
    "Bahrain"    1988  .5292023 0         .
    "Bahrain"    1989 .52927554 0         .
    "Bahrain"    1990 .58570516 0         .
    "Bahrain"    1991  .5858517 0         .
    "Bahrain"    1992 .58596504 0         .
    "Bahrain"    1993   .587204 0         .
    "Bahrain"    1994  .5886627 0         .
    "Bahrain"    1995   .590268 0         .
    "Bahrain"    1996  .5918733 0         .
    "Bahrain"    1997 .59333205 0         .
    "Bahrain"    1998 .59504384 0         .
    "Bahrain"    1999  .4296117 0         .
    "Bahrain"    2000  .5564309 0         .
    "Bahrain"    2001  .3337166 0         .
    "Bahrain"    2002   .241985 0         .
    "Bahrain"    2003  .2422048 0         .
    "Bahrain"    2004 .24307735 0         .
    "Bahrain"    2005  .2455817 0         .
    "Bahrain"    2006  .2477597 0         .
    "Bahrain"    2007 .25073698 0         .
    "Bahrain"    2008 .25298825 0         .
    "Bahrain"    2009 .25563914 0         .
    "Bahrain"    2010  .4839083 0         .
    "Bahrain"    2011  .5924911 0         .
    "Bahrain"    2012  .5934369 0         .
    end

  • #2
    Consider

    .
    Code:
    clonevar safecopy = crisisfrag
    
    bysort country (year) : replace safecopy = frag_index[_n+1] if missing(crisisfrag) & crisis_dummy[_n+1]
    where the principle of keeping different panels separate in calculations is respected.

    Stata is telling you indirectly that subscripts are not allowed before equals (meaning, assignment) signs with replace, as is also implicit in its help. The minimal syntax

    replace oldvar = exp

    cannot be varied.
    Last edited by Nick Cox; 17 Nov 2021, 06:09.

    Comment


    • #3
      Hi Nick Cox,

      Love to hear from you! Thanks for that, you certainly got me closer to it! The observation that you created is where I need the new observation to be, but right now it's equal to the first observation of the chunk. I need it to be equal to the preceding observation value of frag_index, not the same as the first observation.

      I'm playing around with your code to see if I can figure it out but if you have any next step ideas, I'm all years!

      And i was wondering if you would mind explaining why [_n+1] worked, and not [_n-1] since I'm talking about the preceding observation? I've been working on this all night and it never occurred to me to use [_n+1].



      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str52 country_name double year float(frag_index crisis_dummy crisisfrag safecopy)
      "Angola"     1981 1.1493217 1 1.1493217 1.1493217
      "Angola"     1982 1.1500545 1 1.1500545 1.1500545
      "Angola"     1983 1.1507871 1 1.1507871 1.1507871
      "Angola"     1984 1.1518463 1 1.1518463 1.1518463
      "Angola"     1985 1.1521727 1 1.1521727 1.1521727
      "Angola"     1986 1.1529053 1 1.1529053 1.1529053
      "Angola"     1987 1.1522527 1 1.1522527 1.1522527
      "Angola"     1988 1.2388158 1 1.2388158 1.2388158
      "Angola"     1989  1.239795 1  1.239795  1.239795
      "Angola"     1990 1.1879622 1 1.1879622 1.1879622
      "Angola"     1991 1.1899204 1 1.1899204 1.1899204
      "Angola"     1992  1.127124 1  1.127124  1.127124
      "Angola"     1993 1.1236141 1 1.1236141 1.1236141
      "Angola"     1994 1.1281831 1 1.1281831 1.1281831
      "Angola"     1995  1.130794 0         .         .
      "Angola"     1996  1.133485 0         .         .
      "Angola"     1997 1.1338112 0         .         .
      "Angola"     1998 1.1316068 0         .         .
      "Angola"     1999 1.1369085 0         .         .
      "Angola"     2000 1.1416377 0         .         .
      "Angola"     2001 1.0815322 0         .         .
      "Angola"     2002  .9746989 0         .         .
      "Angola"     2003 .56646764 0         .         .
      "Angola"     2004 .56915855 0         .         .
      "Angola"     2005  .5735613 0         .         .
      "Angola"     2006 .57731134 0         .         .
      "Angola"     2007  .5823668 0         .         .
      "Angola"     2008  .5867696 0         .  .5912523
      "Angola"     2009  .5912523 1  .5912523  .5912523
      "Angola"     2010 .53220606 1 .53220606 .53220606
      "Angola"     2011   .492501 1   .492501   .492501
      "Angola"     2012  .4969837 1  .4969837  .4969837
      "Angola"     2013  .5011401 1  .5011401  .5011401
      "Angola"     2014 .50568944 1 .50568944 .50568944
      "Angola"     2015 .50937295 1 .50937295 .50937295
      "Angola"     2016  .5537676 1  .5537676  .5537676
      "Angola"     2017  .4810795 1  .4810795  .4810795
      "Angola"     2018  .3022385 1  .3022385  .3022385
      "Angola"     2019 .37884295 1 .37884295 .37884295
      "Azerbaijan" 1991  .4470737 1  .4470737  .4470737
      "Azerbaijan" 1992  .3963463 1  .3963463  .3963463
      "Azerbaijan" 1993  .7526202 1  .7526202  .7526202
      "Azerbaijan" 1994  .6688801 1  .6688801  .6688801
      "Azerbaijan" 1995   .770782 1   .770782   .770782
      "Azerbaijan" 1996  .7729334 1  .7729334  .7729334
      "Azerbaijan" 1997  .7190617 0         .         .
      "Azerbaijan" 1998  .7223123 0         .         .
      "Azerbaijan" 1999  .7262556 0         .         .
      "Azerbaijan" 2000  .7307451 0         .         .
      "Azerbaijan" 2001   .734835 0         .         .
      "Azerbaijan" 2002   .794482 0         .         .
      "Azerbaijan" 2003   .796647 0         .         .
      "Azerbaijan" 2004   .799498 0         .         .
      "Azerbaijan" 2005  .8018028 0         .         .
      "Azerbaijan" 2006  .8052664 0         .         .
      "Azerbaijan" 2007  .7496784 0         .         .
      "Azerbaijan" 2008  .6955136 0         .         .
      "Azerbaijan" 2009  .6978116 0         .         .
      "Azerbaijan" 2010  .6996368 0         .         .
      "Azerbaijan" 2011  .7013153 0         .         .
      "Azerbaijan" 2012   .703247 0         .         .
      "Azerbaijan" 2013  .6681315 0         .         .
      "Azerbaijan" 2014  .6695903 0         .         .
      "Azerbaijan" 2015  .6709757 0         .         .
      "Azerbaijan" 2016  .7688444 0         .         .
      "Azerbaijan" 2017  .7698235 0         .         .
      "Azerbaijan" 2018  .6767297 0         .         .
      "Azerbaijan" 2019  .6840762 0         .         .
      "Bahrain"    1981 .52876216 1 .52876216 .52876216
      "Bahrain"    1982 .52938837 1 .52938837 .52938837
      "Bahrain"    1983  .5296149 1  .5296149  .5296149
      "Bahrain"    1984 .52994806 1 .52994806 .52994806
      "Bahrain"    1985 .53151333 1 .53151333 .53151333
      "Bahrain"    1986  .5303212 1  .5303212  .5303212
      "Bahrain"    1987  .5293088 0         .         .
      "Bahrain"    1988  .5292023 0         .         .
      "Bahrain"    1989 .52927554 0         .         .
      "Bahrain"    1990 .58570516 0         .         .
      "Bahrain"    1991  .5858517 0         .         .
      "Bahrain"    1992 .58596504 0         .         .
      "Bahrain"    1993   .587204 0         .         .
      "Bahrain"    1994  .5886627 0         .         .
      "Bahrain"    1995   .590268 0         .         .
      "Bahrain"    1996  .5918733 0         .         .
      "Bahrain"    1997 .59333205 0         .         .
      "Bahrain"    1998 .59504384 0         .         .
      "Bahrain"    1999  .4296117 0         .         .
      "Bahrain"    2000  .5564309 0         .         .
      "Bahrain"    2001  .3337166 0         .         .
      "Bahrain"    2002   .241985 0         .         .
      "Bahrain"    2003  .2422048 0         .         .
      "Bahrain"    2004 .24307735 0         .         .
      "Bahrain"    2005  .2455817 0         .         .
      "Bahrain"    2006  .2477597 0         .         .
      "Bahrain"    2007 .25073698 0         .         .
      "Bahrain"    2008 .25298825 0         .         .
      "Bahrain"    2009 .25563914 0         .         .
      "Bahrain"    2010  .4839083 0         .         .
      "Bahrain"    2011  .5924911 0         .         .
      "Bahrain"    2012  .5934369 0         .         .
      end
      Last edited by Sam Volpe; 17 Nov 2021, 07:31.

      Comment


      • #4
        I've already explained why your code didn't work, perhaps too concisely. The subscript on the left-hand side of the equals sign is illegal in replace as that command only allows

        Code:
        replace varname =
        as a start to the command; there is more flexibility over what you can say after that start. The error message arises from an incorrect guess by the parser: the guess is that the square brackets mean that you are trying to specify weights. The real reason the code is illegal as far as you are concerned is that subscripts are not allowed in that position.

        Otherwise use [_n-1] to refer to preceding observations and [_n+1] to refer to following observations. There is an asymmetry as Stata will willingly copy downwards in a cascade, but the reverse doesn't apply. More at https://www.stata.com/support/faqs/d...issing-values/

        Comment

        Working...
        X