Announcement

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

  • #16
    Here is some toy data I made up, along with code that does the calculations:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(region date rainfall)
    1 1935  8.032198
    1 1936  8.447136
    1 1937  8.591763
    1 1938  7.934585
    1 1939  8.369435
    1 1940   8.44331
    1 1941  8.423146
    1 1942  8.084593
    1 1943  8.307385
    1 1944 8.3846445
    1 1945  8.484856
    1 1946  8.497174
    1 1947  8.168061
    1 1948  8.087855
    1 1949  8.216142
    1 1950  8.231004
    1 1951  8.483223
    1 1952  8.502059
    1 1953  8.739008
    1 1954  8.629378
    2 1935  7.217003
    2 1936  7.499479
    2 1937   7.89219
    2 1938  7.496597
    2 1939  7.579321
    2 1940   7.69753
    2 1941  7.775066
    2 1942  7.681837
    2 1943  7.593424
    2 1944  7.503234
    2 1945  7.523049
    2 1946  7.634192
    2 1947  7.493707
    2 1948  7.393755
    2 1949  7.418781
    2 1950     7.425
    2 1951  7.736089
    2 1952  7.677586
    2 1953  7.616431
    2 1954  7.657046
    3 1935  7.065272
    3 1936  7.608771
    3 1937  7.938552
    3 1938  7.620558
    3 1939  7.721437
    3 1940  7.664909
    3 1941  7.514309
    3 1942  7.370231
    3 1943  7.467028
    3 1944  7.430826
    3 1945  7.604745
    3 1946  7.699978
    3 1947  7.412583
    3 1948  7.380505
    3 1949  7.266688
    3 1950    7.3843
    3 1951  7.506262
    3 1952  7.639979
    3 1953   7.77132
    3 1954   7.92295
    4 1935  6.034285
    4 1936  6.730779
    4 1937  6.784344
    4 1938  6.081991
    4 1939  6.521652
    4 1940  6.590026
    4 1941  6.467077
    4 1942   6.01835
    4 1943  6.377407
    4 1944  6.548792
    4 1945  6.740992
    4 1946  6.795482
    4 1947  6.361302
    4 1948  6.543336
    4 1949  6.380631
    4 1950  6.541751
    4 1951  6.695799
    4 1952  6.588926
    4 1953  6.909254
    4 1954  6.555641
    5 1935  5.060695
    5 1936  5.123369
    5 1937  5.262172
    5 1938  5.054333
    5 1939  5.254365
    5 1940  5.223055
    5 1941  5.296315
    5 1942  5.244389
    5 1943  5.018603
    5 1944  5.234845
    5 1945  5.369242
    5 1946  5.450609
    5 1947  5.517453
    5 1948  5.413876
    5 1949  5.469325
    5 1950  5.481056
    5 1951  5.790877
    5 1952  5.884436
    5 1953  5.987456
    5 1954  5.901814
    end
    format %ty date
    
    capture program drop one_region
    program define one_region
        centile rainfall, centile(20 80)
        gen pctile20 = r(c_1)
        gen pctile80 = r(c_2)
        exit
    end
    
    runby one_region, by(region)
    gen int shock = 0 if !missing(rainfall)
    replace shock = -1 if rainfall < pctile20
    replace shock = 1 if rainfall > pctile80
    -runby- is written by Robert Picard and me, and is available from SSC.

    In the future, when asking for help with code, please use the -dataex- command to provide example data, as I have done here.
    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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #17
      Dear Clyde,

      I am very grateful for your example and help. :-)

      Thank you very much. I will try these codes on my data and let you know how it goes!!

      Regards!

      Comment


      • #18
        Dear Clyde,

        Below is a copy of my monthly rainfall data by region 1. I have a total of 10 regions. I would like to calculate these monthly rainfall to yearly rainfall before using the codes in #16. Can you please help me with codes for converting into yearly rainfall. Given that there are missing data for some months, can you tell me what is the best way to calculate the yearly rainfall for the entire data set.

        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float reg int year byte month float rainfall
        1 1982 1 15.425
        1 1982 2 19.445
        1 1982 3 29.96667
        1 1982 4 19.5
        1 1982 5 41.37333
        1 1982 6 51.52333
        1 1982 7 33.415
        1 1982 8 26.58333
        1 1982 9 12.12
        1 1982 10 12.27
        1 1982 11 21.735
        1 1982 12 33.93
        1 1983 1 19.01
        1 1983 2 6.32
        1 1983 3 5.646667
        1 1983 4 22.58
        1 1983 5 43.19333
        1 1983 6 45.82667
        1 1983 7 26.26
        1 1983 8 25.256666
        1 1983 9 21.05
        1 1983 10 14.465
        1 1983 11 .
        1 1983 12 .
        1 1984 1 .
        1 1984 2 6.62
        1 1984 3 4.79
        1 1984 4 3.835
        1 1984 5 34.01
        1 1984 6 45.31001
        1 1984 7 32.870007
        1 1984 8 29.33
        1 1984 9 18.56
        1 1984 10 19.055
        1 1984 11 24.445
        1 1984 12 27.72
        1 1985 1 14.14
        1 1985 2 6.2
        1 1985 3 3.79
        1 1985 4 5.36
        1 1985 5 24.84
        1 1985 6 33.140007
        1 1985 7 43.81
        1 1985 8 27.52
        1 1985 9 23.25
        1 1985 10 35.840004
        1 1985 11 31.38
        1 1985 12 29.92
        1 1986 1 13.31
        1 1986 2 10.61
        1 1986 3 6.94
        1 1986 4 4.06
        1 1986 5 20.325
        1 1986 6 32.783333
        1 1986 7 26.28
        1 1986 8 23.653334
        1 1986 9 21.43
        1 1986 10 24.113335
        1 1986 11 26.76
        1 1986 12 13.786667

        end
        [/CODE]
        ------------------ copy up to and including the previous line -----------------
        [/CODE]

        Thank you in advance.

        Regards!

        Comment


        • #19
          When you say calculate yearly rainfall, do you mean the total yearly rainfall (i.e. add up the monthly rainfalls within the year)? Or do you mean the average yearly rainfall? The code would be:
          Code:
          by region year, sort: egen yearly_total_rainfall = total(rainfall)
          by region year, sort: egen yearly_average_rainfall = mean(rainfall)
          As for handling the missing values, I cannot advise you. I imagine that there are conventions in the meterological community about how to handle this, but I am not a meteorologist (nor do I work in any other discipline that normally works with rainfall data) so I wouldn't know what that approach is. There are several approaches used for dealing with missing values in general statistics, but the choice depends on facts from the scientific discipline, not just statistical considerations. You need to consult with somebody who uses rainfall data professionally for this. There are a few people on the Forum that do that. But I doubt they will check on this thread given that it's title does not suggest it would be of interest to them. I suggest you repost the question (including the example data) as a New Topic and give it a title that specifically refers to dealing with missing values and rainfall data.

          Comment


          • #20
            Dear Clyde,

            Thanks for your reply.

            I have used the average yearly rainfall which is works for my analysis. I don't have too many missing data, so I think the yearly average should suffice. However, I will post this under a New Topic.
            Thanks so Clyde.

            Kind Regards!

            Comment

            Working...
            X