Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: flagging a specific pattern of values within observations


From   "Sergiy Radyakin" <[email protected]>
To   <[email protected]>
Subject   Re: st: flagging a specific pattern of values within observations
Date   Tue, 24 Apr 2007 21:23:41 +0200

Hello Friedrich,

this will work only for small number of options, i.e. 3 as here. Notice that in the list
you specify not all the "options", but all "combinations". This will grow very fast (n!).
So it will take 120 combinations to list (and to check!) for n=5.
With just n=6 (n!=720) one can already forget about it.



The following will check if v1, v2, and v3 are different (c==1 if not):
gen c=1-inlist(v1, v2, v3)*inlist(v2,v1,v3)*inlist(v3,v1,v2)

or using built-in function diff:

egen c=diff(v1 v2 v3)

:)

(there will be 5 terms in the RHS for 5 variables, etc -- the number grows as n)
There is no check that 1<=vi<=n. It is unnecessary if the data stems from, say users choosing
options from drop-down lists. But if necessary, this can be easily accomplished by also checking
if inlist(v1, 1, 2,...,n)*inlist(v2, 1, 2,...,n)*...*inlist(vn, 1, 2,...,n) is equal to 1.
(n more conditions, totalling 2n (or n+1 if the shortcut is used), instead of n!)

But ideally I would like to write something like:
gen k= set(v1 v2 v3)=set(1 2 3)
(order of elements does not matter for sets)

or alternatively
gen k= sorted(v1 v2 v3)=(1 2 3)
(to have a sorted list compared with the benchmark).

Stata allows to compare lists:
di (1 2 3)==(1 2 3)

di (1 2 3)==(1 3 2)

but does not allow this:

gen k= (v1 v2 v3)==(1 2 3)


So some questions for the Gurus:
-- how to subtract an element from a list?
-- does Stata support sets? (e.g. at the level of Pascal sets)
-- v1 v2 ...vn are chars: "a" "b" etc. Need X=concatenated sorted v1..vn. E.g:

v1 v2 v3 X
a b c abc
b a c abc
a b a aab
a a a aaa

What is the shortest way? What if vi-s can be strings (not just chars)?
ab aa ca aaabca

Best regards,
Sergiy






----- Original Message ----- From: "Friedrich Huebler" <[email protected]>
To: <[email protected]>
Sent: Tuesday, April 24, 2007 8:08 PM
Subject: Re: st: flagging a specific pattern of values within observations



This should do it.

input fid q11a q11b q11c
55 2 3 1
21 1 2 1
end
gen q = string(q11a) + string(q11b) + string(q11c)
gen tag = !(inlist(q,"123","132","213","231","312","321"))

Friedrich

--- Alan Neustadtl <[email protected]> wrote:
Makes sense...thanks for finding the flaw.

Best,
Alan

On 4/24/07, Sergiy Radyakin <[email protected]> wrote:
> No, this will not work(2-2-2):
>
> ----------------------------
> fid     q11a    q11b    q11c
> 55      2        3       1
> 21      1        2       1
> 22      2        2       2
> ----------------------------
>
> Prime numbers seem to be a better idea.
>
> Best,
>   Sergiy
>
>
> ----- Original Message -----
> From: "Alan Neustadtl" <[email protected]>
> To: <[email protected]>
> Sent: Tuesday, April 24, 2007 7:30 PM
> Subject: Re: st: flagging a specific pattern of values within
observations
>
>
> > How about something like this:
> >
> > clear
> > input fid q11a q11b q11c
> > 55  2 3 1
> > 21  1 2 1
> > end
> >
> > tempvar checkit
> > egen `checkit'=rowtotal(q11a q11b q11c)
> > generate byte flag=1 if `checkit' !=6
> > list if flag==1
> >
> > Best,
> > Alan
> >
> >
> > On 4/24/07, Anirudh V. S. Ruhil <[email protected]> wrote:
> >> A colleague is trying to accomplish something in a different
stats
> >> package
> >> and asked if STATA could effect the solution. "Of course",
said I. Now I
> >> am
> >> scratching my head for the code.  Individuals were asked to
rank-order 3
> >> choices. Person 55 did fine, but not Person 21. How can I
generate a flag
> >> for such respondent-errors?
> >>
> >> ----------------------------
> >> fid     q11a    q11b    q11c
> >> 55      2               3       1
> >> 21      1               2       1
> >> ----------------------------
> >>
> >> thanks in advance
> >>
> >> Ani

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index