Announcement

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

  • creating variables by conditionals and inequalities

    Hi everyone,

    First time posting; this will hopefully be a simple fix. I'm a political scientist doing a separation-of-powers-based project, and the literature in this area typically features models with variables accounting for when institutions are 'constrained' and thus their behavior expected to be different compared to when they are not 'constrained'. So, I need a variable which is coded as 0 when the Court is within the Pareto set of the three other major political institutions (House, Senate, and President), and when it is not, coded as the absolute value of the ideological distance between the Court's ideal point and the nearest of those three.

    The variable needs to be coded as 'unconstrained' (=0) when we have a situation where the Court's ideal point is within the Pareto set, e.g. any one of the three other ideal points is to the left or the right of the Court (i.e. the Court is not the most extreme of the four);

    And constrained (=absolute value of Court and closest institution) when not within the Pareto set, e.g. when the Court's ideal point is to the left or the right of all three other ideal points.

    So I wrote the following code built on existing variables measuring the ideal points of the President, and the medians of the Court, House, and Senate, but it is not giving me what I need. Because all of these ideology measures are modeled as real numbers between [-1, 1], they are in the same space, and so I had the idea to use a set of inequalities to tell Stata what to do. I made one command for each scenario.

    (note: JCS measures the Supreme Court's ideal point)

    gen SOPconstraint=.

    *** unconstrained
    replace SOPconstraint=0 if house < JCS < senate < pres
    replace SOPconstraint=0 if house < JCS < pres < senate
    replace SOPconstraint=0 if house < pres < JCS < senate
    replace SOPconstraint=0 if senate < pres < JCS < house
    replace SOPconstraint=0 if senate < JCS < pres < house
    replace SOPconstraint=0 if senate < JCS < house < pres
    replace SOPconstraint=0 if pres < JCS < house < senate
    replace SOPconstraint=0 if pres < house < JCS < senate
    replace SOPconstraint=0 if pres < JCS < senate > house

    *** house
    replace SOPconstraint=abs(JCS-house) if JCS < house < pres < senate
    replace SOPconstraint=abs(JCS-house) if JCS < house < senate < pres
    replace SOPconstraint=abs(JCS-house) if JCS > house > senate > pres
    replace SOPconstraint=abs(JCS-house) if JCS > house > pres > senate

    *** sen
    replace SOPconstraint=abs(JCS-senate) if JCS < senate < pres < house
    replace SOPconstraint=abs(JCS-senate) if JCS < senate < house < pres
    replace SOPconstraint=abs(JCS-senate) if JCS > senate > house > pres
    replace SOPconstraint=abs(JCS-senate) if JCS > senate > pres > house

    *** pres
    replace SOPconstraint=abs(JCS-pres) if JCS < pres < senate < house
    replace SOPconstraint=abs(JCS-pres) if JCS < pres < house < senate
    replace SOPconstraint=abs(JCS-pres) if JCS > pres > house > senate
    replace SOPconstraint=abs(JCS-pres) if JCS > pres > senate > house

    The problem seems to be that the commands are overlapping and thus overwriting one another for some reason. The variable I get depends on which commands get run first. Second, the commands modeling the unconstrained scenarios do not leave any results. There are no entries coded as 0 once the commands are all run. And of course there should be a great many such scenarios.

    I'm getting the sense that my commands are structured incorrectly. I'm not sure if I can use four-term inequalities as I'm trying to do here, or perhaps I need more detailed commands built from other terms like & or |. Any thoughts?


    P.S.: a note on theory--
    The theory is based on a simple formal-modeling setup where, for example, the Supreme Court is represented as a single point in a one-dimensional ideological space (defined by the median member of the institution) along with other institutions with power to punish the Court for untoward behavior. The Senate, House, and President are also all represented as single points in ideological space. The Court, for example, is defined as 'unconstrained' when its ideal point is within the Pareto set--that is, when it sits between the ideal points of the other three institutions. That means that the Court can behave as it likes, and given that generally all three institutions would have to agree to punish the Court for its behavior, at least one of them will prefer the Court's ideal point to any status quo point in the space. If the Court is outside of that set, then there are status quos which the Court cannot move because all three will prefer an alternative to the Court's ideal point. Thus, constrained when outside of the three other institutions' ideal points, and unconstrained when between them. Because these SOP models are often trying to find changes in behavior due to other institutions' actions or preferences, accounting for these constrained/unconstrained scenarios empirically becomes important.
    Last edited by Gordon Ballingrud; 27 Aug 2024, 16:11.

  • #2
    Maybe something like this.

    Code:
    clear all
    
    ** generate data
    
    set obs 100
    foreach var in house JCS senate pres {
        g `var' = runiform(-1,1)
    }
    
    ** get values of interest
    egen groupmin = rowmin(house JCS senate pres)
    egen groupmax = rowmax(house JCS senate pres)
    g outside  = (JCS==groupmin) | (JCS==groupmax)
    foreach var in house senate pres {
        g JCS_`var' = abs(JCS - `var')
    }
    egen closest = rowmin(JCS_house JCS_senate JCS_pres)
    g desired = outside*closest
    Last edited by George Ford; 27 Aug 2024, 16:45.

    Comment


    • #3
      Your code is not working because although things like -JCS < house < pres < senate- are legal Stata syntax, they don't mean what you intend: they are not chained inequalities. I'll get to what that code actually means later. But first, here is some code that I believe does what you were trying to do:

      Code:
      rename (house JCS senate pres) ideol=
      gen `c(obs_t)' = _n
      
      reshape long ideol, i(obs_no) j(inst) string
      
      by obs_no (ideol), sort: gen JCS_order = max(cond(inst == "JCS", _n, .))
      by obs_no (ideol): gen SOPconstraint = 0 if !inlist(JCS_order, 1, _N)
      by obs_no (ideol): replace SOP_constraint = ideol[2]-ideol[1] if JCS_order == 1
      by obs_no (ideol): replace SOP_constraint = ideol[_N]-ideol[_N-1] if JCS_order == _N
      drop JCS_order
      
      reshape wide
      rename ideol* *
      Note: You did not provide example data, so this code is untested and may contain typos or other errors. If it does not work properly with your data set and you need assistance to debug it, when posting back provide example data from your data set. (If there are particular situations where the code produces incorrect results, please be sure to include some of those observations in your example.) To be sure that your example data will be usable for testing and further code development, be sure to use the -dataex- command to post it. If you are running version 18, 17, 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.

      Now, here's an explanation of why your original code produced incomprehensible results. In Stata if you write - if a < b < c < d-, Stata does not interpret this as a chain of inequalities among a, b, c, and d. Rather, it interprets it as -if (((a < b) < c) < d)-. Now, what does that mean? Well, to Stata, (a < b) is a logical expression, something that is either true or false. And it represents true as 1 and false as 0. So, at the first stage, this becomes -if((1 < c) < d)- if a < b, or as -if((0 < c) < d)- if a >= b. At the next stage it compares c to the 0 or 1 that it got from evaluating (a < b). And c might either by > 0 or <= 0, which would result in (0 < c) being either true or false, respectively. And so on. As you can see, these results will only occasionally by chance correspond to a correct evaluation of a transitive chain of inequalities among a, b, c, and d.

      Added: Crossed with #2.
      Last edited by Clyde Schechter; 27 Aug 2024, 17:27.

      Comment


      • #4
        For more discussion of essentially the same pitfall with logical operators & and | see

        Code:
        SJ-23-1 st0710  . . . . . . Stata tip 151: Puzzling out some logical operators
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q1/23   SJ 23(1):293--297                                (no commands)
                surveys various common confusions about logical operators
                and explains how to handle them
        
        https://journals.sagepub.com/doi/pdf/10.1177/1536867X231162009
        The main point is the same: faced with an expression with chained operators, Stata doesn't evaluate them all at once; it evaluates one operator with two operands at a time.

        Comment


        • #5
          Thank you, gents, for your replies. I will try to run the code(s) that you have written to test them out; for the moment, I wanted to do 2 things: extend my thanks and gratitude for taking the time to help out. And I wanted to provide some clarity to Clyde's question about my data.

          I ran the dataex command, and selected a few important variables from a large set which the command itself could not handle. I'm using the Songer dataset, which codes a large sample of cases from the US Courts of Appeals, and structures the entries around the individual case. So each entry codes data for one case selected by the sampling procedure from the universe of cases resolved by the Courts of Appeals in the specified time frame (1925-1996). There are tons of variables in the dataset which don't pertain to the project. The ones I selected below do (except for "year" which only matters inasmuch as it helps to establish the structure of the data for this example).

          Notice below that the variables in some cases do not have observations. That's true of the JCS measure, which only goes back as far as 1937. The measure "distance" codes the distance between the President and the Supreme Court median, so it has no observations where JCS has none. That just means that none of these observations will end up being included in my models. But plenty will.

          For JCS, just imagine some decimal between -1 and 1, just like for the house, senate, and Pres measures. They're all coded on the same scale.

          "usg" codes whether the case in question involves the federal government, or a person or department within it, as a party to the case. "fedwin" codes whether the federal government won the case, given that usg=1. The other measures, "senate" , and "house", code the ideology of the median of the House or Senate chamber at the time the case's outcome was announced. "pres" is just the President's ideology score.

          (I tried to put the data into a nice table but the tools here are resisting me)

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(JCS year usg fedwin pres distance senate house)
          . 1925 1 1 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 0 .396 . .123 .108
          . 1925 1 0 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 0 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 0 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 0 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 . .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 0 .396 . .123 .108
          . 1925 1 0 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 . .396 . .123 .108
          . 1925 1 0 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 . .396 . .134 .072
          . 1925 1 0 .396 . .123 .108
          . 1925 1 0 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 1 1 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .123 .108
          . 1925 1 0 .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 1 1 .396 . .123 .108
          . 1925 0 . .396 . .134 .072
          . 1925 0 . .396 . .123 .108
          end

          Anyhoo, I will run those code samples, which you kindly wrote for me, when I have a moment to do so and observe the outcomes. I just wanted to say thank you, and to keep the conversation going.

          --Gordon
          Last edited by Gordon Ballingrud; 28 Aug 2024, 12:19.

          Comment


          • #6
            OK. Thank you for posting the example data. (Don't worry about putting it in a clean table: the exact output of -dataex- is the best for enabling helpers to construct a replica of your table. A clean table is nice to look at, but sometimes can be very difficult to actually work with.)

            So, I have made several changes to the code. One of them arises from having learned that the ideology scores can be missing. My earlier code tacitly assumed that the scores are always between -1 and 1, which implies that they are never missing. In addition, having had the opportunity to try running the earlier code with the example data, I now can see that there are many typos therein. These are now fixed.

            Code:
            rename (house JCS senate pres) ideol=
            gen `c(obs_t)' obs_no = _n
            
            reshape long ideol, i(obs_no) j(inst) string
            drop if missing(ideol)
            
            by obs_no (ideol), sort: egen JCS_order = max(cond(inst == "JCS", _n, .))
            by obs_no (ideol): gen SOPconstraint = 0 if !inlist(JCS_order, 1, _N)
            by obs_no (ideol): replace SOPconstraint = ideol[2]-ideol[1] if JCS_order == 1
            by obs_no (ideol): replace SOPconstraint = ideol[_N]-ideol[_N-1] if JCS_order == _N
            drop JCS_order
            
            reshape wide
            rename ideol* *

            Comment


            • #7
              kinda hard to check it when JCS is always missing.

              Comment


              • #8
                George Ford When I saw #5, in addition to running my code from #2 after modifying it to account for missing values, which enabled me to pick up numerous typos, I then overwrote the missing values of JCS with random numbers in the [-1, 1] interval and then verified that the results reflected the conditions required in #1.

                Comment


                • #9
                  Here is an updated draw. I dropped everything from the years prior to where JCS exists. This might be better.

                  Clyde Schechter George Ford

                  [CODE]
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float(JCS year usg fedwin pres distance senate house)
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                  -.17614196 1937 1 0 -.368 .19185804 -.131 -.136

                  Comment


                  • #10
                    Ok so I can't edit the last post for some reason. I didn't print the whole thing.

                    Also, let me know if it would be better to get rid of the missing values for "fedwin" too.


                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float(JCS year usg fedwin pres distance senate house)
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 1 -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 0 . -.368 .19185804 -.131 -.136
                    -.17614196 1937 1 0 -.368 .19185804 -.131 -.136
                    end

                    Comment


                    • #11
                      So I ran Clyde's code, but this is what I got. It didn't seem to give what I needed; the resulting variable is coded 0 across the board.

                      If it would be helpful, I can provide more info on the variables in question.

                      If this project is taking up too much time, please feel free to drop out. I don't want this to become an inconvenience to anyone.

                      So here is what I ran with the results copied from the results window:



                      Code:
                      rename (house JCS senate pres) ideol=
                      
                      gen `c(obs_t)' obs_no = _n
                      
                      reshape long ideol, i(obs_no) j(inst) string
                      (note: j = JCS house pres senate)
                      
                      Data                               wide   ->   long
                      -----------------------------------------------------------------------------
                      Number of obs.                    20354   ->   81416
                      Number of variables                 481   ->     479
                      j variable (4 values)                     ->   inst
                      xij variables:
                          ideolJCS ideolhouse ... ideolsenate   ->   ideol
                      -----------------------------------------------------------------------------
                      
                      drop if missing(ideol)
                      (11,186 observations deleted)
                      
                      by obs_no (ideol), sort: egen JCS_order = max(cond(inst == "JCS", _n, .))
                      (5070 missing values generated)
                      
                      by obs_no (ideol): gen SOPconstraint = 0 if !inlist(JCS_order, 1, _N)
                      
                      by obs_no (ideol): replace SOPconstraint = ideol[2]-ideol[1] if JCS_order == 1
                      (0 real changes made)
                      
                      by obs_no (ideol): replace SOPconstraint = ideol[_N]-ideol[_N-1] if JCS_order == _
                      > N
                      (0 real changes made)
                      
                      drop JCS_order
                      
                      reshape wide
                      (note: j = JCS house pres senate)
                      
                      Data                               long   ->   wide
                      -----------------------------------------------------------------------------
                      Number of obs.                    70230   ->   18195
                      Number of variables                 480   ->     482
                      j variable (4 values)              inst   ->   (dropped)
                      xij variables:
                                                        ideol   ->   ideolJCS ideolhouse ... ideolsenate
                      -----------------------------------------------------------------------------
                      
                      rename ideol* *
                      
                      **** and now I did this to show what the outcome was:
                      su SOPconstraint
                      
                          Variable |        Obs        Mean    Std. Dev.       Min        Max
                      -------------+---------------------------------------------------------
                      SOPconstra~t |     18,195           0           0          0          0
                      
                      end



                      Comment


                      • #12
                        Well, in the example data in #10, 0 across the board is, indeed, the correct answer. In that example data, JCS, pres, senate, and house do not vary across observations: they are each constant with values -.176142, -.368, -.131, and -.136. This puts JCS at -.176142 between house(-.136) and pres (-.368) so there is no SOP_constraint.

                        As for fedwin, this is something you have just introduced and, at least as far as you have explained, has nothing to do with the SOP_constraint variable. You don't even say what it means. So I have no idea whether having missing values in that variable is a problem or not.

                        Comment


                        • #13
                          SOP constraint depicted.gph

                          So I don't know if this is going to work but I'm trying to post a scatterplot of all the ideology scores. It should show that the JCS score is sometimes to the extreme of all three of the other scores, meaning that there should be some entries for SOP which are non-zero.

                          Comment


                          • #14
                            Your graph very plainly shows that the JCS score is at the extreme pretty regularly in the period between about 1945 and 1960. Outside that period, it is only sporadically extreme. Why don't you post back with new data example that comes from that time period? I do think my code should handle it properly, but if it doesn't, I'll fix it.

                            Comment


                            • #15
                              In the dataex, JCS is always the extreme.

                              Comment

                              Working...
                              X