Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Matrix problem


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Matrix problem
Date   Mon, 21 Jul 2003 18:42:18 +0100

Eric Cahuzac

> Does anyone know how to solve this pb
> Here is my data set (a sample)
> 
>      +----------------+
>      | v1    v2    v3 |
>      |----------------|
>   1. |  2   100   101 |
>   2. |  2   100   102 |
>   3. |  2   100   104 |
>   4. |  3   100   105 |
>   5. |  2   100   106 |
>      |----------------|
>   6. |  3   100   125 |
>   7. |  2   100   131 |
>   8. |  2   100   138 |
>   9. |  2   101   105 |
>  10. |  2   101   106 |
>      |----------------|
>  11. |  3   101   125 |
>  12. |  2   101   131 |
>  13. |  2   102   104 |
>  14. |  2   102   105 |
>  15. |  2   102   106 |
>      +----------------+
> 
> a table of v2 v3, missing gives that
> 
> ----------------------------------------------------------
>           |                       v3
>        v2 |  101   102   104   105   106   125   131   138
> ----------+-----------------------------------------------
>       100 |    1     1     1     1     1     1     1     1
>       101 |    .     .     .     1     1     1     1     .
>       102 |    .     .     1     1     1     .     .     .
> ----------------------------------------------------------
> 
> I want to obtain a matrix:
> 
> A[3,8]
>      101  102  104  105  106  125  131  138
> 100    .    .    .    .    .    .    .    .
> 101    .    .    .    .    .    .    .    .
> 102    .    .    .    .    .    .    .    .
> 
> where dots are replaced by corresponding values of v1
> 
> I try something like the following:
> 
> vallist v2, g(v2)
> qui tab v2
> global nbli=r(r)
> vallist v3, g(v3)
> qui tab v3
> global nbco=r(r)
> 
> matrix A=J($nbli,$nbco,.)
> mat rown A = $v2
> mat coln A = $v3
> 
> local i = 0
> local j = 0
> foreach k of num $v2 {
>  local i = `i' + 1
> foreach m of num $v3 {
>  local j = `j' + 1
> matrix A[`i',`j']=????????
> }
> local j = 0
> }
> 
> Do you have any idea on how to cope with this problem?

In your example, each row and column combination occurs
at most once. I'm going to assume that is generic. 

(1) Canned solution using -tabcount- from SSC
=============================================

. ssc inst tabcount 
. levels v2 , local(l2)
. levels v3 , local(l3)

. tabcount v2 v3 [w=v1] , v1(`l2') v2(`l3') matrix(mymatrix)
(frequency weights assumed)

----------------------------------------------------
          |                    v3                   
       v2 |  102   104   105   106   125   131   138
----------+-----------------------------------------
      100 |    2     2     3     2     3     2     2
      101 |                2     2     3     2      
      102 |          2     2     2                  
----------------------------------------------------

. mat li mymatrix

mymatrix[3,7]
     102  104  105  106  125  131  138
100    2    2    3    2    3    2    2
101    0    0    2    2    3    2    0
102    0    2    2    2    0    0    0

(You have to fix 0 to missing...) 

(2) Your code, modified a bit 
=============================

vallist v2, g(v2)
qui tab v2
global nbli=r(r)
vallist v3, g(v3)
qui tab v3
global nbco=r(r)
 
matrix A=J($nbli,$nbco,.)
mat rown A = $v2
mat coln A = $v3
 
local i = 0
foreach k of num $v2 {
	local i = `i' + 1
	local j = 0 
	foreach m of num $v3 {
		local j = `j' + 1
		local v2 : word `i' of $v2 
		local v3 : word `j' of $v3 
		su v1 if v2 == `v2' & v3 == `v3', meanonly 
		matrix A[`i',`j'] = r(min) 
	}
}

(3) Your code, modified more, official Stata 8 only 
===================================================

levels v2, local(l2)
levels v3, local(l3) 
matrix A = J(`: word count `l2'',`: word count `l3'',.)
mat rown A = `l2'
mat coln A = `l3'
 
local i = 1
foreach k of local l2 {
	local j = 1
	foreach m of local l3 {
		su v1 if v2 == `k' & v3 == `m', meanonly 
		matrix A[`i++',`j++'] = r(min) 
	}
}

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


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index