Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Re: How to determine if a variable is a dummy in mata


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Re: How to determine if a variable is a dummy in mata
Date   Sun, 17 Nov 2013 11:01:29 +0000

(2) better as

: all((x :== 0) :| (x :== 1))
Nick
[email protected]


On 17 November 2013 10:09, Nick Cox <[email protected]> wrote:
> Here are some other solutions.
>
> (1)
>
> Only for 0 and 1 is a number equal to its own square. Hence
>
> : x = (0,0,0,0,1,1,1)
>
> : x == x:^2
>   1
>
> A graph such as
>
> twoway function x^2, ra(-3  3) || function x, ra(-3 3)
>
> may help make this vivid.
>
> (2)
>
>  sum((x :== 0) :| (x :== 1)) == length(x)
>
> Nick
> [email protected]
>
> On 17 November 2013 05:30, Joseph Coveney <[email protected]> wrote:
>> Paul Corral wrote:
>>
>> I'm been trying to determine if a variable is a dummy or not within mata.
>>
>> Is there an easy way to do this without iterating over each row of a
>> matrix's column and check if it is either 0 or 1. I was trying to use
>> the allof() command, however it can only check if all are a scalar.
>> So, while I may check if all are ones, it won't allow me to check if
>> all are 0 or 1.
>>
>> --------------------------------------------------------------------------------
>>
>> Something like that below should work.  If you don't like the verbosity, you can
>> condense it all to a single line of code, although it might make it difficult to
>> follow.
>>
>> Other ways come to mind to accomplish the same thing, for example, to covert the
>> column to text and then use a string search (glance through Mata's string
>> functions for guidance) for characters other than "0" and "1".  Some of them
>> might be more efficient, too.
>>
>> Joseph Coveney
>>
>> : mata set matastrict on
>>
>> :
>> : real scalar function indicator(
>>>                 real matrix TestedMatrix,
>>>                 real scalar tested_column_index) {
>>>
>>>         real colvector TestedColumn
>>>         TestedColumn = TestedMatrix[., tested_column_index]
>>>
>>>         TestedColumn = uniqrows(TestedColumn)
>>>
>>>         real scalar result
>>>         result = (
>>>                 (TestedColumn[1] == 0) &&
>>>                 (TestedColumn[2] == 1) &&
>>>                 (rows(TestedColumn) == 2)
>>>                 )
>>>
>>>                 return(result)
>>> }
>>
>> :
>> : A = (0, 1, 2, 3 \ 1, 4, 5, 6 \ 0, 7, 8, 9)
>>
>> :
>> : indicator(A, 1)
>>   1
>>
>> : indicator(A, 2)
>>   0
>>
>> :
>> : A = A \ (10, 11, 12, 13)
>>
>> :
>> : indicator(A, 1)
>>   0
>>
>> :
>> : end
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index