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

st: Re: matrix with averages and p-values

From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: matrix with averages and p-values
Date   Thu, 2 Oct 2003 11:37:24 -0400

Presumably, you've thought about the issues with testing multiple hypotheses

If you just wanted this once, you could do it using a simple loop within
your do file, but to make it multi-purpose, a simple program would be best.
Here's one that will do what you ask (assuming you want the p values from
simple t-tests) and put the results in a matrix.  I adapted some code I
already had that did something somewhat similar -- it's pretty basic but
does include using value labels for labeling rows/columns when they are

program define mymat
version 8
syntax varlist(numeric max=1) , by(str) mat(str)
confirm numeric var `by'
qui levels `by', local(bys)
local size: word count `bys'
mat `mat'=J(`size',`size',0)
forvalues i=1/`size' {
 local ival: word `i' of `bys'
 local ilab: label (`by') `ival'
 if real("`ilab'")<. local ilab "`by'`i'"
 local ilabs "`ilabs' `ilab'"
 forvalues j=`=`i'+1'/`size' {
 local jval: word `j' of `bys'
 qui ttest `varlist' if inlist(`by',`ival',`jval'), by(`by')
 mat `mat'[`i',`i']=r(mu_1)
 mat `mat'[`j',`j']=r(mu_2)
 mat `mat'[`i',`j']=r(p)
 mat `mat'[`j',`i']=r(p)
matrix rownames `mat'= `ilabs'
matrix colnames `mat'= `ilabs'

you would use it for your example as:

mymat Price, by(State) mat(pricebystate)

and it would create a matrix called pricebystate with the results you seek.

(assuming that State is a numeric var with value labels --if not, encode it

I'm not sure how displaying the resulting matrix with 50 columns may look,
it may be best to drop _all and use svmat to create a dataset with the

Michael Blasnik
[email protected]

----- Original Message ----- 
From: <[email protected]>
To: <[email protected]>
Sent: Thursday, October 02, 2003 9:51 AM
Subject: st: matrix with averages and p-values

> I have a data set that looks like the following:
>        State      Price
> 1.     AL         40
> 2.     AL         50
> 3.     AL         40
> 4.     AZ         20
> 5.     AZ         20
> 6.     AZ         30
> 7.     AZ         40
> 8.     CA         20
> 9.     CA         50
> 10.    CA         20
> 11.    CA         40
> 12.    CA         50
> I would like to get a table with averages (on the
> diagonal), and p-value for the test that two averages
> are equal (off-diagonal):
>        AL         AZ         CA
> AL     avg(AL)    p(AL=AZ)   p(AL=CA)
> AZ       ---      avg(AZ)    p(AZ=CA)
> CA                           avg(CA)
> Since I have 50 states and I have to repeat the same
> analysis for different variables and different data
> sets, I was wondering whether there is an easy way for
> doing this.
> Thank you,
> Raffaella Baldi

*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index