Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Need help making an ado for Tamhane test

From   Maarten Buis <>
Subject   Re: st: Need help making an ado for Tamhane test
Date   Fri, 31 May 2013 10:09:04 +0200

On Fri, May 31, 2013 at 9:22 AM, Marta García-Granero
<> wrote:
> Anyway, I have attempted
> (perpetrated?) to write some code for Tamhane's T2 multiple comparison of
> means with heterogeneous variances.
> To avoid having to edit it every time I want to use it, to replace "Hb" by
> the dependent variable name and "Status" by the grouping variable, I would
> like to turn it into a program, like this:
> tamhane Hb, by(Status)
> Unfortunately, I have hit the ceiling of my knowledge in Stata programming
> as it is right now.

Here is how I would start:

*------------------ begin example ------------------
clear all

program define tamhane, rclass
    syntax varname, by(varname)

    // take care of possible missing values
    marksample touse
    markout `touse' `by', strok
    qui count if `touse'
    if r(N) == 0 error 2000

    // collect the levels of by
    qui levelsof `by' if `touse',local(levels)

    // count the number of levels
    // see: -help extended_fcn-
    local k : word count `levels'

    // count the number of comparisons
    local comp=`k'*(`k'-1)/2

    // create a matrix to store (and return) the results
    tempname res
    matrix `res' = J(`comp',1,.)

    // collect the test results in that matrix
    local row = 1
    forvalues i = 1/ `k' {
        local lev_i : word `i' of `levels'
        forvalues j = `=`i'+1'/`k' {
            local lev_j : word `j' of `levels'
            quietly ttest `varlist'                             ///
                if `touse' & ((`by'==`lev_i')|(`by'==`lev_j')), ///
                by(`by') unequal

            matrix `res'[`row',1] = 1-(1-r(p))^`comp'
            local row = `row' + 1
            local rowname "`rowname' `lev_i'_vs_`lev_j'"

    // give the rows and collumn names
    matrix rownames `res' = `rowname'
    matrix colnames `res' = "p_value"

    // display the matrix
    matlist `res', underscore format(%7.4f)

    // return the matrix
    return matrix result = `res'

// try it out
sysuse auto
tamhane price, by(rep78)
*------------------- end example -------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Reichpietschufer 50
10785 Berlin

*   For searches and help try:

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