Announcement

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

  • Loop over multiple observations in one variable

    Hi,

    I am trying to find out mean of rainfall for period of 30 years for each of the 140 countries. For this, I'm using loop.

    Variables that I'm using are: year (1984-2016), country( Names of 140 countries) and rainfall(for 30 years 140 countries).
    Generating rain30 - 30 year average for each country
    devrain- deviation of rainfall from 30 year average

    Code:
    foreach year = 1984/2016{
    by(country): egen rain30 = mean(rainfall)
    gen devrain = rain30- rainfall
    }
    I get invalid syntax as an error message.

    Could someone please tell me where am I going wrong?

    Thanks.

  • #2
    The invalid syntax comes from -foreach year = 1984/2016-. That should be -forvalues year = 1984/2016-. But then you also need to add an -if- condition to your -egen- command so it only applies to the given year in an iteration.

    All of that said, there is no need to use a loop to get what you want here anyway:

    Code:
    by country, sort: egen rain30 = mean(rainfall)
    gen devrain = rain30-rainfall
    alone will do what you are looking for, with no surrounding loop.

    Comment


    • #3
      Several problems here. I spotted three and there may be others.

      1. You are mushing together syntax for foreach and forvalues. That's what Stata is choking on first. Or so I guess.

      2. You're asking for the same calculation each time around the loop. That's not always illegal, but here second time around the loop, the variables you are asking for would already exist and the loop would fail even if you correct #1. So it would be illegal.

      3. But you don't need a loop at all.

      Code:
      egen rain30 = mean(rainfall), by(country)
      will automatically average over years. Trying to separate the calculation by year will at best give you the mean rainfall in each year in each country, which would be a matter of averaging over stations in each country if you had such data, but you don't, so your calculation could only return the original data in a new variable.

      That said, my climatologist persona (and I do have one) is queasy about mean rainfall in a country, unless it's a very small country. Mean rainfall over a period of years even for a station is a less secure concept than it seemed to be say 50 years ago.

      What do you want to do with these means?

      PS: in climatology, as in science generally, deviations (conventionally called "anomaly" as a term of art) would always be (value - mean) not (mean - value).
      Last edited by Nick Cox; 28 Nov 2018, 12:56.

      Comment


      • #4
        Thank you Clyde and Nick for correcting me.

        I'm using these means to see how change in temperature and rainfall affects natural disasters, thereby affecting political factors. That's why estimating precipitation and temperature anomaly (I hope the usage is correct).
        I understand that mean rainfall or temperature of a country as rainfall and temperature vary greatly across a country. In literature, I have seen many authors using global mean temperature and precipitation.
        So, I thought of using range of temperature to account for vast difference in temperature in a country. It turned out that range is just average temperature.

        Could you please suggest me how else can I account for climate change affecting natural disasters in regression analysis?

        Please ignore if this is not the right platform to ask such a question.

        Comment


        • #5
          The correlation between value - mean and value is precisely 1, so calculating the mean first won't do anything important to a regression-type analysis.

          Comment


          • #6
            Dear Statalist,

            I am working with rainfall and child health data. I have the month, year, historical mean and the mean rainfall for a calendar month. I am trying to generate a variable to define the "normal rainfall, as well as to measure the prenatal exposure to normal rainfall (please see below). In addition, I would like to consider the trimesters.

            I am grateful for your help with the following:

            1. I would like to define the normal rainfall for a given month if rainfall fell within one standard deviation of historical mean for that particular calendar month, however the normal rainfall should not be taken in an absolute sense as I will not be comparing the regions. Is there away to generate the normal rainfall using this approach?

            2. I have data on children born within a flood period and would like to measure prenatal exposure as the fraction of normal rainfall months occurring in the 9 months before birth.

            3. I would like to consider the first, second and third trimesters. I would like to define the 1st Trimester as the fraction of months during the 6-8 months before birth that the normal rainfall indicator equal 1. 2nd trimester as the fraction of months during the 3-5 months before birth that the normal rainfall indicator equal 1. Finally, the 3rd trimester as the fraction of months during the 0-2 months before birth that the normal rainfall indicator equal 1.

            I am very grateful for your help!

            Below is an example of the data.

            month year meanrain raininyear
            7 2004 26.33825 28.85667
            7 2004 26.33825 28.85667
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            8 2004 21.87548 21.63
            9 2004 16.78079 13.12667
            9 2004 16.78079 13.12667
            9 2004 16.78079 13.12667
            9 2004 16.78079 13.12667
            9 2004 16.78079 13.12667
            10 2004 18.43741 19.59
            11 2004 20.09833 19.315
            11 2004 20.09833 19.315
            11 2004 20.09833 19.315
            11 2004 20.09833 19.315
            11 2004 20.09833 19.315
            12 2004 26.05887 22.55

            Thank you.

            Regards!

            Comment


            • #7
              #6 doesn't really fit the thread title and is wasted here. You have also posted the same question under an equally irrelevant title in another thread.

              What you could do is start a new thread and give it a relevant title.

              That said, I don't think that the title is the major issue. I doubt that anyone can do much with the questions as they stand.

              I have worked with rainfall data but #1 is not clear to me.

              I doubt that #2 or #3 are clear to anyone as your example data say nothing about pregnancy or births. At that point a reader is just guessing wildly at what those medical data might be, and I doubt that they will want to do that

              You need Stata help, but your best way to get it is to find someone at your workplace who knows more.

              Comment

              Working...
              X