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: dot product across many variables


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: dot product across many variables
Date   Tue, 29 Nov 2011 19:08:28 +0000

Two line solution, FWIW. 

gen dotproduct = . 
mata: st_store(., "dotproduct", rowsum(st_data(., "first1 first2 first3"):*st_data(., "second1 second2 second3")))

Any one-liners? 

Nick 
[email protected] 

Nick Cox

Talking of which, 

. edit

. set obs 10
obs was 0, now 10

. forval i = 1/3 {
  2. gen first`i' = runiform() > `i' * 0.2
  3. gen second`i' = runiform() < `i' * 0.2
  4. }

. l

     +--------------------------------------------------------+
     | first1   second1   first2   second2   first3   second3 |
     |--------------------------------------------------------|
  1. |      0         0        0         0        1         1 |
  2. |      1         1        0         0        0         0 |
  3. |      1         0        1         1        0         1 |
  4. |      1         0        1         1        0         1 |
  5. |      1         0        1         0        1         1 |
     |--------------------------------------------------------|
  6. |      0         0        1         1        0         1 |
  7. |      1         0        1         0        0         1 |
  8. |      0         0        1         1        1         1 |
  9. |      1         0        0         0        0         1 |
 10. |      1         0        1         1        1         0 |
     +--------------------------------------------------------+

. mata
------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------
: x = st_data(., "first1 first2 first3")

: y = st_data(., "second1 second2 second3")

: x :* y
        1   2   3
     +-------------+
   1 |  0   0   1  |
   2 |  1   0   0  |
   3 |  0   1   0  |
   4 |  0   1   0  |
   5 |  0   0   1  |
   6 |  0   1   0  |
   7 |  0   0   0  |
   8 |  0   1   1  |
   9 |  0   0   0  |
  10 |  0   1   0  |
     +-------------+

: rowsum(x :* y)
        1
     +-----+
   1 |  1  |
   2 |  1  |
   3 |  1  |
   4 |  1  |
   5 |  1  |
   6 |  1  |
   7 |  0  |
   8 |  2  |
   9 |  0  |
  10 |  1  |
     +-----+

Nick 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Paul Burkander
Sent: 29 November 2011 18:12
To: [email protected]
Subject: Re: st: dot product across many variables

ah, that's great.  Much better than having to learn mata in day.

Thanks!

On Tue, Nov 29, 2011 at 1:03 PM, Nick Cox <[email protected]> wrote:
> Consider a loop
>
> gen newdummy = 0
>
> forval j = 2000/2010 {
> replace newdummy = newdummy + firstdummy`j' * seconddummy`j'
> }
>
> On Tue, Nov 29, 2011 at 5:11 PM, Paul Burkander <[email protected]> wrote:
>
>> I have a data set that has two binary variables in each of several
>> years.  I'd like to create a new variable that is the sum across all
>> years of the product of the two within year.
>>
>> For example,
>> firstdummy2000=1
>> seconddummy2000=0
>> firstdummy2001=1
>> seconddummy2001=1
>>
>> newdummy=(firstdummy2000*seconddummy2000+firstdummy2001*seconddummy2001)=1
>>
>> There are so many years that it seems cumbersome to do it as above,
>> moreover the years might change over time and I'd like to be flexible.
>>
>> I thought perhaps a matrix command would work, doing a dot product?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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