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]

st: RE: Create a row matrix with weighted frequencies from various dummy variables


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Create a row matrix with weighted frequencies from various dummy variables
Date   Fri, 1 Jul 2011 11:58:57 +0100

Note that the reference to -tabcount- should have been explained. But you don't need -tabcount-. 

clear 
set seed 10235
set obs 1000
forv i=1/20 {
	gen d`i' = round(uniform())
}
forv j=1/5 {
	gen z`j' = round(uniform())
}
generate wt = uniform()

matrix count = J(1, 100, .) 
local k = 0 
qui forv i = 1/20 { 
	forv j = 1/5 { 
		su wt if d`i' == 1 & z`j' == 1, meanonly 
		matrix count[1, `++k'] = r(sum) 
	}
}

Nick 
n.j.cox@durham.ac.uk 

Nikolaos Kanellopoulos

I have two sets of dummy variables (say d1,...,d20 and z1,...,z5) and I want to 
create a 1x100 matrix where each element will be the weighted frequency for each 
combination between each d-variable and each z-variable when both equal 1 (lets 
say something like this [d1=1&z1=1(wt), 
d1=1&z2=1(wt),...,d200=1&z4=1(wt),d200=1&z5=1(wt)].

My thought is to use the following:
********************************************************************
clear *
set seed 10235
drop _all
set obs 1000
/*Generate data*/
forv i=1/20 {
        gen d`i' = round(uniform())
 }
forv i=1/5 {
        gen z`i' = round(uniform())
 }
generate wt = uniform()
/*generate 100 variables=1 if both dummy vars=1*/
forv i=1/20 {
forv j=1/5  {
        ta d`i' if d`i'==1 & z`j'==1 , gen(d`i'z`j')
}
}
/*Check no missing variables and replace with zero if missing*/
forv i=1/20 {
    forv j=1/5 {
          capt su d`i'z`j'1 
              if _rc!=0 {
                   gen d`i'z`j'1=0
}
}
}
/*use tabcount to create 1x1 matrix with frequency for each dz combination*/
forv i=1/20 {
     forv j=1/5 {
          tabcount d`i'z`j'1 [iw=wt], v1(1) matrix(d`i'z`j')
}
}
/*Put in a 1x100 matrix*/
mat D1=d1z1
forv i=2/5 {
     mat D1=D1,d1z`i'
}
mat D=D1
forv i=2/20 {
     forv j=1/5{
         mat D=D,d`i'z`j'
}
}
/*Round the elements of D*/
mata
d=st_matrix("D")
D=round(d)
st_matrix("D2",D)
end
********************************************************************

Is there a more efficient way to do this? Any suggestion is welcome.

*
*   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