Announcement

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

  • Checking wether the ordering of the observation of a variable is monotone

    Helllo Statalist community.

    I would like to find a way to check which of the individuals in my unabalanced panel dataset have an almost monotone ordering of the observations.
    The variable of interest is Brand.
    Brand can only go from 1 to 3, and I would like to create a dummy that tells wether my desired ordering is attained.

    My desired ordering should follow these rules:

    a) Brand=1 if Preference==1 for each ID
    b) Brand=2 can only happen for Preference>=2
    c) Brand=3 can only happen if lagged values of Brand are equal to 1 & 2. That is, Brand=3 only if Brand is equal to 1 or 2 in previous observations of the same individual.
    d) After the points in a) b) c) are respected for an individual, the ordering does not matter anymore, and it is always considered respected.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(ID Preference Brand)
    1  1 1
    1  2 1
    1  3 1
    1  4 1
    1  5 2
    1  6 2
    1  7 2
    1  8 2
    1  9 3
    1 10 3
    1 11 3
    1 12 3
    2  1 2
    2  2 2
    2  3 1
    2  4 1
    2  5 3
    2  6 1
    3  1 1
    3  2 3
    3  3 1
    3  4 1
    3  5 3
    3  6 2
    3  7 3
    3  8 1
    3  9 1
    4  1 3
    4  2 1
    4  3 1
    4  4 2
    4  5 1
    4  6 1
    4  7 2
    4  8 1
    5  1 1
    5  2 1
    5  3 1
    5  4 1
    5  5 1
    5  6 1
    5  7 1
    5  8 1
    5  9 3
    5 10 1
    5 11 1
    5 12 2
    6  1 1
    6  2 1
    6  3 2
    6  4 1
    6  5 1
    6  6 2
    6  7 2
    6  8 3
    end

    In the toy dataset I provided:
    ID=1 respects the ordering;
    ID=2 does not respect the ordering as it breaks rules a) and b)
    ID=3 does not respect the ordering as it breaks rule c)
    ID=4 does not respect the ordering as it breaks rules a) and c)
    ID=5 does not respect the ordering as it breaks rule c)
    ID=6 respects the ordering

    Then my dummy variable would be equal to 1 for ID=1, and ID=6

    I would like to know how to frame this problem in a general way, as my original dataset is much larger then the one I attached.

    Technical Note:
    I am using Stata16 on Windows10


  • #2
    Code:
    by ID (Preference), sort: egen byte criterion_a = min(!(Preference == 1 & Brand != 1))
    by ID (Preference): egen byte criterion_b = min(!(Preference < 2 & Brand == 2))
    by ID (Preference): gen int preceding_1 = min(sum(Brand == 1), 1)
    by ID (Preference): gen int preceding_2 = min(sum(Brand == 2), 1)
    by ID (Preference): egen byte criterion_c = min((preceding_1 & preceding_2) | Brand != 3)
    gen byte respects_ordering = criterion_a & criterion_b & criterion_c
    Note: I found your description of rule c confusing. I have interpreted it as follows: when the observations for an ID are ordered in increasing order of Preference, any observation with Brand == 3 must follow at least one observation with Brand == 1 and also follow at least one observation with Brand == 2. This appears to be consistent with the desired results you showed for your toy data set. But your wording doesn't really say that as far as I can tell: it seems to say that under the same ordering, any observation by Brand == 3 must have Brand = 1 or Brand = 2 in the immediately preceding observation.

    Comment

    Working...
    X