Announcement

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

  • #16
    Dear Clyde,

    Thank you very much.

    Sorry for the error, it was meant to be a zero.

    Thanks again!

    Regards!

    Comment


    • #17
      Dear Clyde,

      I am trying use two (2) standard deviations instead of one (1) in #12 and #13. I used the codes:

      bys reg: egen stdr= 2sd(meanrain)
      However, I am getting an error. Can you please let me know how this can be done?

      Kind Regards,
      Nichola



      Comment


      • #18
        -egen- is not like -gen- in that you cannot put arbitrary expressions on the right hand side of the equals sign. All you can put there is an egen function call. Also, even if this were a -gen- command, in Stata you cannot right 2X to mean 2 times x: you must make it explicit, 2*X. Putting these problems together, what you need is:

        Code:
        bys reg: egen stdr = sd(meanrain)
        replace stdr = 2*stdr

        Comment


        • #19
          Dear Clyde,

          Thank you for your reply.

          Regards!

          Comment


          • #20
            Dear Clyde and Statalisters,,

            I would like to try a new measure of defining a rainfall shock variable. I have data from the 1980 to 2010 and would like to define the shock as a level of rainfall that is one standard deviation above or below the region-specific mean (calculated over the 10 years prior to the birth year). I have the data for 10 regions. I would also like to create a dummy variable for "normal rainfall" to represent the absence of a negative shock. Therefore, this dummy should equal 1 if the rainfall in an individual's locality or region during their year of birth fell within a standard deviation of the region-specific mean.

            I am grateful for your help with this.

            Kind Regards!

            Comment


            • #21
              Code:
              sum rainfall
              gen byte normal_rainfall = inrange(rainfall, r(mean)-r(sd), r(mean)+r(sd))
              There is no reason to have separate variables for shock and normal rainfall as they are simply the opposite of each other. Where you would use a shock variable, !normal_rainfall will serve.

              Comment


              • #22
                Dear Clyde,

                Thanks for your reply.
                year month reg mean rainfall sd normrain
                1983 10 1 18.43741 14.465 6.891529 1
                1983 11 1 20.09833 . 6.891529 0
                1983 12 1 26.05887 . 6.891529 0
                1984 1 1 16.82793 . 6.891529 0
                1984 2 1 9.908793 6.62 6.891529 1
                1984 3 1 7.752069 4.79 6.891529 1
                I have tried using the commands, however I am getting 0 only where there is no data for rainfall in that particular month. I am not sure why this is happening. With regard to #20, I would expect a different results.

                Thank you for your help with this.

                Regards!

                Comment


                • #23
                  OK, I didn't anticipate the possibility of a month with no rainfall data. You are getting a 0 result there because when there is no data, the value of rainfall is missing value (.), which is never going to lie between mean - 1SD and mean + 1SD. So we need to modify that last command to deal with this special case:

                  Code:
                   
                  sum rainfall gen byte normal_rainfall = inrange(rainfall, r(mean)-r(sd), r(mean)+r(sd)) if !missing(rainfall)
                  Now you will get a missing value for normal_rainfall if there is no data on rainfall. That will be more appropriate.

                  Comment


                  • #24
                    Dear Clyde,

                    Thanks for your help.

                    I would like to create a new dummy variable for drought = 1 if there were 5 consecutive months of droughts. I have already defined a drought variable. Can you please assist me with this?

                    Regards

                    Comment


                    • #25
                      Yes, I can assist you with this if you use -dataex- to supply example data. If you are running version 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


                      • #26
                        region year month drought
                        1 2005 1 0
                        1 2005 2 0
                        1 2005 3 0
                        1 2005 4 0
                        1 2005 5 0
                        1 2005 6 1
                        1 2005 7 1
                        1 2005 8 0
                        1 2005 9 0
                        1 2005 10 0
                        1 2005 11 1
                        1 2005 12 0
                        Dear Clyde,

                        Here is an example of the dataset. I would like to create a new dummy for drought (drought2 =1) when there 2 consecutive months of drought during pregnancy (for example June and July in the dataset). I tried using
                        Code:
                        gen drought2 =0
                        Code:
                        replace drought2==1 if drought =1 & month=1 & month=2
                        but this gives an error.

                        Thanks for your assistance.

                        Regards

                        Comment


                        • #27
                          I specifically asked you to use -dataex- to show your example data. You did not do that. It will take me longer to get you data into Stata from the table you show than it will take me to write and test the code. Please repost using the -dataex- command to show the example data. (Instructions for -dataex- were given in #25.)

                          Code:
                          replace drought2==1 if drought =1 & month=1 & month=2
                          has == where you should have just =, and has = where you should have ==. In addition, it is semantically incorrect: no observation will ever have month = 1 and month = 2 at the same time, so a different logic is needed. If you post back using -dataex- to show your example data, I will show you a solution to your problem.

                          Comment


                          • #28
                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input int year byte month float(reg drought)
                            2005  1 1 0
                            2005  2 1 0
                            2005  3 1 0
                            2005  4 1 0
                            2005  5 1 0
                            2005  6 1 1
                            2005  7 1 1
                            2005  8 1 0
                            2005  9 1 0
                            2005 10 1 0
                            2005 11 1 1
                            2005 12 1 0
                            2006  1 1 0
                            2006  2 1 0
                            2006  3 1 0
                            2006  4 1 1
                            2006  5 1 1
                            2006  6 1 0
                            2006  7 1 0
                            2006  8 1 0
                            2006  9 1 1
                            2006 10 1 1
                            2006 11 1 0
                            2006 12 1 0
                            end
                            Dear Clyde,

                            I have made a mistake with the '='. We see above the data using dataex.

                            Regards

                            Comment


                            • #29
                              Code:
                              //  CREATE A MONTHLY DATE VARIABLE
                              gen mdate = ym(year, month)
                              format mdate %tm
                              assert missing(mdate) == missing(year, month)
                              
                              //  IDENTIFY SPELLS OF DROUGHT
                              sort mdate
                              gen spell = sum(drought == 1 & drought[_n-1] != 1)
                              replace spell = 0 if !drought
                              by spell (mdate), sort: gen duration = _N if drought
                              by spell (mdate): gen byte two_consec_months = (duration >= 2) & !missing(duration)
                              sort mdate

                              Comment


                              • #30
                                Dear Clyde,

                                Much thanks.

                                Regards

                                Comment

                                Working...
                                X