Hi,
Austin's solution is very elegant. But I think that the following
codes could help in different cases
* For teaching Stata
sysuse auto, clear
qui {
tab rep78, matrow(A)
tab head, matrow(B)
local na = rowsof(A)
local nb = rowsof(B)
mat C=J(`na',`nb',.)
}
forv ka=1/`na' {
forv kb=1/`nb' {
sum price if rep78==A[`ka',1] & head==B[`kb',1], meanonly
mat C[`ka',`kb']=r(mean)
}
}
mat l C
* For efficiency, use a vector instead of a matrix
sysuse auto, clear
qui {
gen aux = 100*headroom + rep78
tab aux, matrow(A)
local na = rowsof(A)
mat C=J(`na',1,.)
}
forv ka=1/`na' {
sum price if aux==A[`ka',1], meanonly
mat C[`ka',1]=r(mean)
}
}
mat l C
In both cases, we drop the missings in rep78
Rodrigo.
-----Mensaje original-----
De: [email protected]
[mailto:[email protected]] En nombre de Austin
Nichols
Enviado el: Jueves, 08 de Mayo de 2008 11:56 a.m.
Para: [email protected]
Asunto: Re: st: Teach an old dog new tricks
Sergiy Radyakin <[email protected]>:
Easier in Mata, perhaps, but still easy to put in an ado:
_______________________________________
prog tabmat
version 8.2
syntax varname [fw aw iw], by(varlist min=1 max=2) Matname(string)
[replace] cap conf matrix `matname'
if _rc!=111 & "`replace'"=="" {
di as err "Matrix `matname' already defined;" _n "specify" _c di as
txt " replace " as err "option to overwrite"
error 110
}
cap matrix drop `matname'
tokenize `by'
tempvar iv jv
tempname v
egen long `iv'=group(`1')
su `iv', meanonly
loc maxi=r(max)
egen long `jv'=group(`2')
su `jv', meanonly
loc maxj=r(max)
forv j=1/`maxj' {
su `2' if `jv'==`j', meanonly
loc col: label (`2') `=r(mean)'
loc colc: subinstr local col "." "`=char(183)'", all loc colnam
`"`colnam' "`colc'""'
}
forv i=1/`maxi' {
su `1' if `iv'==`i', meanonly
loc row: label (`1') `=r(mean)'
loc rowc: subinstr local row "." "`=char(183)'", all loc rownam
`"`rownam' "`rowc'""'
forv j=1/`maxj' {
su `varlist' [`weight'`exp'] if `iv'==`i' & `jv'==`j', meanonly
if r(N)>0 loc m=r(mean)
else loc m=.
mat `v'=nullmat(`v') , `m'
}
mat `matname'=nullmat(`matname') \ `v'
mat drop `v'
}
matrix rownames `matname' = `rownam'
matrix colnames `matname' = `colnam'
matrix roweq `matname' = `1':
matrix coleq `matname' = `2':
mat li `matname'
end
_______________________________________
sysuse auto, clear
table rep78 head, c(mean price)
tabmat price, by(rep78 head) mat(A)
On Thu, May 8, 2008 at 10:16 AM, Sergiy Radyakin
<[email protected]> wrote:
> Hello Statalisters,
>
> I am looking for a way to get the matrix of means, which is displayed
> but not saved by, e.g.:
>
> sysuse auto
> table rep78 head, c(mean price)
>
> The (relatively new) svy commands allow this, but they are quite slow
> for the purpose. -table- was in Stata for ages, but it still does not
> save any matrix results.
>
> I am aware of an option replace, which saves the means in the dataset,
> but it destroys the data, - something I am trying to avoid. Adding
> preserve/restore will slow the program down sufficiently enough to
> reject this way of doing it.
>
> Is it possible to obtain a matrix of means from table (or another
> command with a similar functionality) (preferred) ? Or is it possible
> to tell table,replace to keep the existing data and add the computed
> statistics as new variables (good)?
>
> Note that tabstat does not allow what I am looking, since it only
> supports one var in the by() clause, and I will need to call it N
> times (where N is the number of categories in the second var) and
> combine the columns together, which is not fast either.
>
> Also note, that while
>
> tabulate rep78 foreign, matcell(A)
>
> is a valid syntax,
>
> tabulate rep78 foreign, matcell(A) summarize(price) mean
>
> which presumably could be the solution I am looking for, is not valid:
> r(198); option matcell() not allowed
>
> Any suggestions are welcomed.
>
> Thank you,
> Sergiy Radyakin
> *
> * 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/
********************************************************************************
ADVERTENCIA: La informaci�n contenida en esta transmisi�n, y en cualquier archivo adjunto, est� sujeta a reserva legal conforme a la normativa aplicable al Banco Central de Chile, y no puede ser usada o difundida por personas distintas de su o sus destinatarios. Si usted ha recibido esta transmisi�n por error, por favor notifique inmediatamente al remitente respondiendo por este mismo medio y elim�nela de su sistema.
El Banco Central de Chile no se har� responsable de la exactitud y veracidad de la informaci�n contenida en este mensaje, as� como de su modificaci�n, copia, divulgaci�n o reenv�o, total o parcial. Su uso no autorizado puede ser sancionado de conformidad con las leyes chilenas.
El Banco Central de Chile transmite sus decisiones a trav�s de comunicados oficiales, los que pone a disposici�n del p�blico en su p�gina de Internet: www.bcentral.cl
DISCLAIMER: The information contained in this email or any attached file, is subject to legal privilege pursuant to the laws and regulations applicable to the Central Bank of Chile , and may not be used or disseminated by any person other than its intended recipients. If you have received this transmission in error, please notify the sender immediately by reply to this email address and delete it from your system.
The Central Bank of Chile shall not be liable for the accuracy or authenticity of the contents of this message, whether amended, copied, forwarded or disclosed in any form, in whole or in part. Please note that unauthorized use may be penalized in conformity with the Chilean law.
The Central Bank of Chile communicates its decisions by official releases, and
makes them available to the public in its WebPages: www.bcentral.cl
*
* 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/