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.
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
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
Comment