Announcement

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

  • New Variable

    Hi, I want to create a new variable based on a year variable already within the dataset. The years in my dataset are 2008, 2009,2016, and 2017. I want to combine the years 2008 and 2009 so it forms wave 1. Then combine 2016 and 2017 so it forms wave 2. I'm not sure how to do this? Thanks

  • #2
    Code:
    assert inlist(year, 2008, 2009, 2016, 2017)
    gen byte wave = cond(inlist(year, 2008, 2009), 1, 2)

    Comment


    • #3
      Thank you! That worked perfectly

      Comment


      • #4
        If i wanted to regress the mental health on the wave 2 would I type in:
        regress mental health Wave==2

        Comment


        • #5
          No. Assuming, as is suggested in #1, that there are only waves 1 and 2, you would do:
          Code:
          regress mental_health i.wave
          If there are more than 2 waves and you want to specifically contrast mental health in wave 2 with all other waves, it would be:
          Code:
          regress mental_health 2.wave
          If, as appears to be the case from what is said in #1 there are only the two waves, then either approach will give you the same results.

          Comment


          • #6
            Right. So if i had an interaction term say age with the wave would I then write it as:
            regress mental_health age i.wave i(age wave)

            Comment


            • #7
              You need to read -help fvvarlist- and get factor-variable notation down straight.
              Code:
              regress mental_health c.age##i.wave
              This assumes that age is a continuous variable. If you have age groups forming a discrete variable then instead of -c.age##i.wave- it's -i.age##i.wave-.

              Comment


              • #8
                Age is a binary variable that takes the value of 1 or 0 if age is above 50. Because I want to first include age and wave as an independent variable. Then have an interaction term between the two.
                So would it be then
                regress mental_health age wave i.age##i.wave

                Comment


                • #9
                  No. Just
                  Code:
                  regress mental_health i.age##i.wave
                  Read -help fvvarlist- to understand why.

                  Comment


                  • #10
                    I was wondering if I wanted to do this comparison for different countries. Would the regression be:
                    regress mental_health i.Country##i.age##i.wave

                    I do not have a country dummy but do have the names for all the countries within the dataset

                    Comment


                    • #11
                      So you would first have to create a numeric variable that encodes the names of the countries:

                      Code:
                      encode Country, gen(n_country)
                      regress mental_health i.n_country##i.wave##i.age
                      margins n_country#wave
                      margins n_country, dydx(wave)
                      Regressions with a three-way interaction are difficult to interpret from the regression output. So I've added the two -margins- commands at the end to give you a simpler view of the results. The first one will give you the expected value of your mental_health variable in each wave in each country. The second one will give you the effect of wave (difference between waves 1 and 2) in each country.

                      If your data set has people from many different countries, I think it is likely that there is some country-level clustering of the mental-health outcomes. So, unless you have a small number of countries (say, fewer than 20), I would probably add the -vce(cluster n_country)- option to the -regress- command to correct for that.

                      Comment


                      • #12
                        There is roughly 32 countries with my dataset, so would I add at the end of my regression cluster(Country)

                        Comment


                        • #13
                          Yes, but you have to put a comma between the rest of the -regress- command and that.

                          Comment


                          • #14
                            yes understood thank you!

                            Comment


                            • #15
                              When running the first code for generating n_country it says it is not possible with numeric variables. The original form of the countries are numbers so would I use this instead. so:
                              I would skip the encode code line and just do

                              regress mental_health i.Country##i.wave##i.age
                              margins Country#wave
                              margins Country, dydx(wave)
                              Last edited by Taiba Chau; 30 Mar 2022, 03:32.

                              Comment

                              Working...
                              X