Announcement

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

  • Problem with -if inrange-

    Hello everyone,

    For my Master's thesis I have a panel data set comprising 55 periods (1960 to 2014) and 46 Asian countries. Below you'll find an extract from my dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20 country int year byte confrontation_incidence float(temp temp_lagged rainfalllog rainfall_laggedlog)
    "Afghanistan" 1960 0 12.1 12.5 2.5701926  3.130655
    "Afghanistan" 1961 0 12.6 12.1  2.510277 2.5701926
    "Afghanistan" 1962 0 12.2 12.6 2.3968964  2.510277
    "Afghanistan" 1963 0 13.2 12.2  2.541579 2.3968964
    "Afghanistan" 1964 0 11.6 13.2 2.5166676  2.541579
    "Afghanistan" 1965 0 12.5 11.6 2.5684364 2.5166676
    "Afghanistan" 1966 0 12.8 12.5  2.463893 2.5684364
    "Afghanistan" 1967 0 12.2 12.8  2.585912  2.463893
    "Afghanistan" 2013 1 13.9   13 2.5264685 2.6026025
    "Afghanistan" 2014 1 13.7 13.9 2.4852955 2.5264685
    "Armenia"     1991 0  7.2    .  2.711723         .
    "Armenia"     1992 0    6  7.2  2.778658  2.711723
    end

    The topic is the relationship between climate change and civil conflict. To that end I perform the following regression:

    Code:
    encode country, gen(country1)
    xtset country1 year
    regress confrontation_incidence temp temp_lagged rainfalllog rainfall_laggedlog iccountry* icyear*,vce(cluster country1)
    where iccountry* and icyear* are country-specific fixed effects and time-trends respectively.

    As a robustness test I now want to perform the exact same regression but for different time periods covering thirty years i.e. 1960 to 1989, 1961 to 1990 etc. To do so, I chose the following command:

    Code:
    if inrange(year,1960,1989) regress confrontation_incidence temp temp_lagged rainfalllog rainfall_laggedlog iccountry* icyear*,vce(cluster country1)
    While this works fine, Stata refuses to execute the same command when I change the years, for example when I use 1961 and 1990 instead of 1960 and 1989. This would not be as much of a problem if Stata at least gave me an error message. However the only thing it does is reproduce my command in the result window without any further information.

    I have checked the manual as well as the forum but could not find anything related to this problem.
    As an alternative I also tried the -rolling- command with a thirty-year window but I run into problems there too. But that's the topic for another thread.

    Has anybody run into the same problem before and knows a solution?

    I thank you for your consideration.

  • #2
    You're confusing the if command and the if qualifier. The if command you give as example will execute the regression with all the data if and only if

    Code:
    inrange(year[1], 1960, 1989)
    is true. It doesn't select observations. See http://www.stata.com/support/faqs/pr...-if-qualifier/

    There is no error message with other years because your command is perfectly legal. It just fails to match what you want.

    You want the if qualifier. See e.g. http://www.stata.com/help.cgi?if and http://www.stata.com/help.cgi?ifcmd which do make the crucial distinction between the different ideas. Nevertheless you're very far from the first to be confused on this.

    Use of the inrange() function is not the issue here.
    Last edited by Nick Cox; 19 Nov 2016, 15:25.

    Comment


    • #3
      Hello Nick,

      thank you very much for your answer. After reading the three sources you provided I changed my command in the following way:

      Code:
      regress confrontation_incidence temp temp_lagged rainfalllog rainfall_laggedlog iccountry* icyear* if 1960<=year<=1989,vce(cluster country1)
      Unfortunately the results were again equivalent to those I obtained when using the entire sample. After reading the FAQ on the -if qualifier- versus the -if command-, I was under the impression that you need to place the -if- behind your regression command to use it as a qualifier. But, apparently that does not solve my problem.

      I then checked what would happen if instead of the -regress- command I used -list-:

      Code:
      list  confrontation_incidence if 1960<=year<=1989
      Here too, instead of only giving me the observations up to 1989 for each country, Stata listed all the observations of my sample. I then thought that there is something wrong with my year variable. After checking I found that it is of the numeric type int so Stata should be able to perform a <= test.

      I am at a bit of a loss here. Do you think changing my year variable into a date variable by using the -date- function might help?

      By the way I am a total beginner and using Stata 12.0 on Windows.

      Comment


      • #4
        your code is illegitimate; why not use code such as shown by Nick Cox?

        Comment


        • #5
          You've created a new bug.

          Code:
          ... if 1960<=year<=1989
          as a condition is perfectly legal, but is treated by Stata as equivalent to

          Code:
          ... if (1960 <= year) <= 1989
          which for your data evidently collapses to

          Code:
          if 1 <= 1989
          which is always true, with the result you noted.

          Code:
          ... if inrange(year,1960,1989)
          is on the other hand a single condition, and what you want.
          Last edited by Nick Cox; 20 Nov 2016, 08:48.

          Comment


          • #6
            Rich Goldstein That was of course the first thing I tried this morning but it didn't work. What I didn't realize was that I left a space between -inrange- and the bracket and Stata told me that it couldn't find the variable inrange. I now tried it again and it finally works. That was rather stupid, I must admit!

            Nick Cox Like I explained to Rich above I left a space between -inrange- and the bracket so Stata treated -inrange- as a variable it couldn't find. As I didn't realize my mistake I resorted to the command I showed you in my last post. After deleting the space -if inrange- works perfectly fine. Thank you again for your help and patience and enjoy the rest of your Sunday!
            Last edited by Tom Englaro; 20 Nov 2016, 10:29.

            Comment

            Working...
            X