Announcement

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

  • Creating a variable based on the forward values of another variable conditional on birth-year and residence

    Dear Statalist Users,

    I have attached an example of my dataset. Variable plate denotes the city of residence it takes values from 1 to 81. Refpop is my treatment variable. Refpop_1, Refpop_2, .... , Refpop_8 are the forward values of my treatment variables constructed based on origional treatment variable (i.e., Refpop).

    I want to create another variable, let's say fake_treatment. For example, if refpop_1 is 0.1 for children residing in city 1 and born in 2013, I want fake_treatment to take value of 0.1 for all children born in 2003 and reside in city 1. Or, if refpop_1 is 0.2 for children residing in city 2 and born in 2014, I want this fake_treatment to take value of 0.2 for all children born in 2004 and reside in city 2. It goes like that. Precesily, I want 2013 refpop_1 values to be replaced with children born in 2003, 2014 refpop_1 values to be replaced with children born in 2004, 2015 refpop_1 values to be replaced with children born in 2005, 2016 refpop_1 values to be replaced with children born in 2006, 2017 refpop_1 values to be replaced with children born in 2007, and 2018 refpop_1 values to be replaced with children born in 2008 conditional on their city of residence.

    Indeed, there are 81 cities in my dataset (denoted by plate) and I try to create a fake treatment variable based on the forward values of my actual treatment variable. Since there are 81 cities, I could not figure out the exact code. Thank you in advance.




    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float child_birth_year double refpop float plate double(refpop_1 refpop_2 refpop_3 refpop_4 refpop_5 refpop_6 refpop_7)
    2003                     0 1                     .                     .                     .                     .                   . . .
    2004                     0 1                     0                     .                     .                     .                   . . .
    2005                     0 1                     0                     0                     .                     .                   . . .
    2006                     0 1                     0                     0                     0                     .                   . . .
    2007                     0 1                     0                     0                     0                     0                   . . .
    2008                     0 1                     0                     0                     0                     0                   0 . .
    2009                     0 1                     0                     0                     0                     0                   0 0 .
    2010                     0 1                     0                     0                     0                     0                   0 0 0
    2011                     0 1                     0                     0                     0                     0                   0 0 0
    2012                     . 1                     0                     0                     0                     0                   0 0 0
    2013   .009273339807454824 1                     .                     0                     0                     0                   0 0 0
    2014    .02936181946792312 1   .009273339807454824                     .                     0                     0                   0 0 0
    2015     .0655555155918426 1    .02936181946792312   .009273339807454824                     .                     0                   0 0 0
    2016     .0718090373940462 1     .0655555155918426    .02936181946792312   .009273339807454824                     .                   0 0 0
    2017    .07464182522269777 1     .0718090373940462     .0655555155918426    .02936181946792312   .009273339807454824                   . 0 0
    2018    .09233631862269061 1    .07464182522269777     .0718090373940462     .0655555155918426    .02936181946792312 .009273339807454824 . 0
    2004                     0 2                     .                     .                     .                     .                   . . .
    2005                     0 2                     0                     .                     .                     .                   . . .
    2006                     0 2                     0                     0                     .                     .                   . . .
    2007                     0 2                     0                     0                     0                     .                   . . .
    2008                     0 2                     0                     0                     0                     0                   . . .
    2009                     0 2                     0                     0                     0                     0                   0 . .
    2010                     0 2                     0                     0                     0                     0                   0 0 .
    2011                     0 2                     0                     0                     0                     0                   0 0 0
    2012                     . 2                     0                     0                     0                     0                   0 0 0
    2013    .01724893600819041 2                     .                     0                     0                     0                   0 0 0
    2014   .056442755971468944 2    .01724893600819041                     .                     0                     0                   0 0 0
    2015   .037366207789350785 2   .056442755971468944    .01724893600819041                     .                     0                   0 0 0
    2016    .04196231396008021 2   .037366207789350785   .056442755971468944    .01724893600819041                     .                   0 0 0
    2017    .04458019917588229 2    .04196231396008021   .037366207789350785   .056442755971468944    .01724893600819041                   . 0 0
    2018    .04995811722101417 2    .04458019917588229    .04196231396008021   .037366207789350785   .056442755971468944  .01724893600819041 . 0
    2004                     0 3                     .                     .                     .                     .                   . . .
    2005                     0 3                     0                     .                     .                     .                   . . .
    2006                     0 3                     0                     0                     .                     .                   . . .
    2007                     0 3                     0                     0                     0                     .                   . . .
    2008                     0 3                     0                     0                     0                     0                   . . .
    2009                     0 3                     0                     0                     0                     0                   0 . .
    2010                     0 3                     0                     0                     0                     0                   0 0 .
    2011                     0 3                     0                     0                     0                     0                   0 0 0
    2012                     . 3                     0                     0                     0                     0                   0 0 0
    2013                     0 3                     .                     0                     0                     0                   0 0 0
    2014  .0007167852468393355 3                     0                     .                     0                     0                   0 0 0
    2015                     . 3  .0007167852468393355                     0                     .                     0                   0 0 0
    2016  .0059937582340705235 3                     .  .0007167852468393355                     0                     .                   0 0 0
    2017                     . 3  .0059937582340705235                     .  .0007167852468393355                     0                   . 0 0
    2018                     . 3                     .  .0059937582340705235                     .  .0007167852468393355                   0 . 0
    2003                     0 4                     .                     .                     .                     .                   . . .
    2004                     0 4                     0                     .                     .                     .                   . . .
    2005                     0 4                     0                     0                     .                     .                   . . .
    2006                     0 4                     0                     0                     0                     .                   . . .
    2007                     0 4                     0                     0                     0                     0                   . . .
    2008                     0 4                     0                     0                     0                     0                   0 . .
    2009                     0 4                     0                     0                     0                     0                   0 0 .
    2010                     0 4                     0                     0                     0                     0                   0 0 0
    2011                     0 4                     0                     0                     0                     0                   0 0 0
    2012                     . 4                     0                     0                     0                     0                   0 0 0
    2013                     0 4                     .                     0                     0                     0                   0 0 0
    2014 .00018449435631764024 4                     0                     .                     0                     0                   0 0 0
    2015  .0014685750762884163 4 .00018449435631764024                     0                     .                     0                   0 0 0
    2016  .0015811166336421772 4  .0014685750762884163 .00018449435631764024                     0                     .                   0 0 0
    2017  .0018597031116818136 4  .0015811166336421772  .0014685750762884163 .00018449435631764024                     0                   . 0 0
    2018  .0020958558877683937 4  .0018597031116818136  .0015811166336421772  .0014685750762884163 .00018449435631764024                   0 . 0
    2003                     0 5                     .                     .                     .                     .                   . . .
    2004                     0 5                     0                     .                     .                     .                   . . .
    2005                     0 5                     0                     0                     .                     .                   . . .
    2006                     0 5                     0                     0                     0                     .                   . . .
    2007                     0 5                     0                     0                     0                     0                   . . .
    2008                     0 5                     0                     0                     0                     0                   0 . .
    2009                     . 5                     0                     0                     0                     0                   0 0 .
    2010                     . 5                     0                     0                     0                     0                   0 0 0
    2011                     0 5                     .                     .                     0                     0                   0 0 0
    2012                     . 5                     0                     .                     .                     0                   0 0 0
    2013                     0 5                     .                     0                     .                     .                   0 0 0
    2014  .0002986982729265859 5                     0                     .                     0                     .                   . 0 0
    2015                     . 5  .0002986982729265859                     0                     .                     0                   . . 0
    2016                     . 5                     0  .0002986982729265859                     0                     .                   0 . .
    2017  .0012007670571648754 5                     .                     .  .0002986982729265859                     0                   . 0 .
    2018                     . 5  .0012007670571648754                     .                     .  .0002986982729265859                   0 . 0
    2003                     0 6                     .                     .                     .                     .                   . . .
    2004                     0 6                     0                     .                     .                     .                   . . .
    2005                     0 6                     0                     0                     .                     .                   . . .
    2006                     0 6                     0                     0                     0                     .                   . . .
    2007                     0 6                     0                     0                     0                     0                   . . .
    2008                     0 6                     0                     0                     0                     0                   0 . .
    2009                     0 6                     0                     0                     0                     0                   0 0 .
    2010                     0 6                     0                     0                     0                     0                   0 0 0
    2011                     0 6                     0                     0                     0                     0                   0 0 0
    2012                     . 6                     0                     0                     0                     0                   0 0 0
    2013                     0 6                     .                     0                     0                     0                   0 0 0
    2014   .006287046421035958 6                     0                     .                     0                     0                   0 0 0
    2015   .011310815647871755 6   .006287046421035958                     0                     .                     0                   0 0 0
    2016   .014070619458492507 6   .011310815647871755   .006287046421035958                     0                     .                   0 0 0
    2017   .016816801335201005 6   .014070619458492507   .011310815647871755   .006287046421035958                     0                   . 0 0
    2018   .020153965575486883 6   .016816801335201005   .014070619458492507   .011310815647871755   .006287046421035958                   0 . 0
    2003                     0 7                     .                     .                     .                     .                   . . .
    2004                     0 7                     0                     .                     .                     .                   . . .
    2005                     0 7                     0                     0                     .                     .                   . . .
    2006                     0 7                     0                     0                     0                     .                   . . .
    2007                     0 7                     0                     0                     0                     0                   . . .
    2008                     0 7                     0                     0                     0                     0                   0 . .
    end

  • #2
    It seems to me that you are already cluttering up your data set with a bunch of variables that you have no need of in any case. And whatever method you are using to do it seems to have some errors. You say, for example, that refpop_1 through refpop_8 are the forward values of refpop. But inspection easily reveals that they are the lagged values, not the forward values. Moreover, a few are computed incorrectly. For example, plate 5 in 2010 has refpop_1 = 0, when it should be missing value (.).

    Stata has time-series operators that calculate lagged (and forward) values of variables "on the fly" when used in commands, sparing you the trouble of creating such variables. So, say, for example, you want to do some kind of regression analysis using what you call refpop_1 and refpop_2 as explanatory variables. You have no need for the refpop_1 and refpop_2 variables themselves. Instead:
    Code:
    xtset plate child_birth_year
    regression_command outcome_variable ... L1.refpop L2.refpop ...
    does the job.

    Most Stata commands in which it might be relevant to use lagged or forward variables will accept this kind of notation. So unless you are gong to be using some specific commands that won't accept these time-series operators, making these variables has only downsides: it clutters your data set with variables that contain no new information, and you risk making mistakes when you create them. See -help tsvarlist- for more information on time-series operators in Stata.

    As for your fake treatment "variable" it seems to me, in the same vein, that it is simply L10.refpop.

    Comment


    • #3
      Dear Professor,

      Thank you very much for your reply.

      Indeed, I am exploring the effect of refugee inflow exposure on native children's health outcomes as measured by height-for-age z score and weight-for-age z score. The dataset I use is the DHS. I appended 3 rounds of DHS and as you might be familiar it is a cross sectional dataset. In the appended dataset, the children born between 2003 to 2018. To understand children's exposure status to the refugee shock, I mainly utilize their year of birth and city of residence at the time of survey. The treatment var (refpop) is as follows: Refpop= Refufee Number(y,c)/Initial popluation(c). Refugee Numbery,c is the number of refugees in city c at the year of birth of children, y. Then, Initial Populationc stands for the population in city c in 2010 as the population data starts from 2010.The refugee shock started in 2011 and city-level refugee number in the host country is available from 2013. In, 2012 it's missing. So I cant have any repop for children born in 2012. The refpop takes value of zero for all children born between 2003 to 2011 as there were no refugees in the host country in these years.

      However, due to endogeneity of refugees' location decisions, I use a distance-based IV. I instrument refpop with the distance based IV. So, after using IV, I need to conduct a placebo test with the forward values of the treatment var. My aim is to use forward values of treatment var as if non-treated ones (born between 2003 and 2011) are treated. So, I am not sure using xtreg is okay. Or am I wrong?


      Originally posted by Clyde Schechter View Post
      It seems to me that you are already cluttering up your data set with a bunch of variables that you have no need of in any case. And whatever method you are using to do it seems to have some errors. You say, for example, that refpop_1 through refpop_8 are the forward values of refpop. But inspection easily reveals that they are the lagged values, not the forward values. Moreover, a few are computed incorrectly. For example, plate 5 in 2010 has refpop_1 = 0, when it should be missing value (.).

      Stata has time-series operators that calculate lagged (and forward) values of variables "on the fly" when used in commands, sparing you the trouble of creating such variables. So, say, for example, you want to do some kind of regression analysis using what you call refpop_1 and refpop_2 as explanatory variables. You have no need for the refpop_1 and refpop_2 variables themselves. Instead:
      Code:
      xtset plate child_birth_year
      regression_command outcome_variable ... L1.refpop L2.refpop ...
      does the job.

      Most Stata commands in which it might be relevant to use lagged or forward variables will accept this kind of notation. So unless you are gong to be using some specific commands that won't accept these time-series operators, making these variables has only downsides: it clutters your data set with variables that contain no new information, and you risk making mistakes when you create them. See -help tsvarlist- for more information on time-series operators in Stata.

      As for your fake treatment "variable" it seems to me, in the same vein, that it is simply L10.refpop.

      Comment


      • #4
        With respect to the original question posed, it now sounds as if what you want for your fake treatment variables is to use F10.refpop. I had understood you to want to use 2003 values in 2013 etc., but from #3 it sounds as if you want to use the 2013 values in 2003. So those would be the forward values.

        I have almost no experience with instrumental variables analysis. In my field, epidemiology, there is little use for it as it is pretty rare to be able to defend a variable as meeting the requirements for an instrument, except perhaps in the context of genetics (and I don't do genetic epidemiology). So I can't advise you on what regression command to use for your analysis. I know there is an official Stata command, -xtivreg- that is used for instrumental variables modeling of panel data. And there are several user-written commands as well. But I do not know the domain of applicability or limitations of any of these. Perhaps somebody else will join in and help you with this.

        Comment

        Working...
        X