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

st: programming tips, was Pairwise comparisons repeated ANOVA


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: programming tips, was Pairwise comparisons repeated ANOVA
Date   Wed, 9 Apr 2003 12:35:34 -0400

You can avoid needing the cond() or if/then/else constructs by taking
advatage of the nullmat function available in commands that combine
matrices.  In particular, you can code:

matrix A = nullmat(A) \ (`i', `k', r(p))

and it will not balk on the first time through even if A does not exist.
This approach solves your immediate problem best.

The concept of setting up a local macro to control other commands often
comes in handy, I tend to use it sometimes to comment out or modify
commands.  For example, if you have an option -noisily- in your command, you
can use the macro created in the syntax command directly, such as `noisily'
regress `varlist', where it will then issue the noisily prefix as needed.
Another use for this I have found is rather than coding many if/else chunks,
simply define a local macro equal to * if a certain condition does or does
not exist...e.g.,

local showN=cond("`counts'"!="","","*")

then later in your code you might have:

`showN' noi display "count=`n'"

This code will only execute the dislay statement if local counts is not
blank.

I've also used similar code for adding to the end of a command or providing
options.  As you remarked, overusing such approaches runs the risk of
becoming too "cute" and can make your ado code more difficult to follow, but
in many circumstances it comes in handy and improves readability.

Michael Blasnik
[email protected]


----- Original Message -----
From: "Joseph Coveney" <[email protected]>
To: "Statalist" <[email protected]>
Sent: Wednesday, April 09, 2003 11:57 AM
Subject: Re: st: Pairwise comparisons repeated ANOVA (redux)


> Sven-Erik Johansson had asked about pairwise comparisons in conjunction
with a one-
> way repeated-measures analysis of variance.  George Hoffman and I
responded, and
> later Al Feiveson provided a program for the Scheff� test using the pooled
error term.
>
> After posting my response, I noted that I had initialized a cumulator
matrix
> unnecessarily.  In addition, I had neglected to clean up
afterward--creating an interim
> matrix and not dropping it before exiting the do-file.  In making the
do-file more
> efficient, I discovered two features of Stata's programming language
(perhaps
> rediscovered if others have observed them before).  I'm posting them
because I wonder
> whether it would be good practice to use them.
>
> The first is the ability to substitute a parenthetical presentation of a
matrix in lieu of a
> matrix name as in the following:  the online help file for matrix
operations indicates that,
> to append a row vector to the bottom of an existing matrix A, first create
matrix B and
> then -matrix A = A \ B-.  But I found that I could use -matrix A = A \
(element1,
> element2, . . . ).  This has the benefit of avoiding having to create two
matrixes
> explicitly, possibly economizing very slightly.
>
> The second is that, in lieu of an -if . . . else- construction, I could
define a macro in a -
> cond()- statement that contains for its value a Stata command as a string.
And then I
> could "execute" the macro.  (I suspect that this ability has been observed
before.)  I
> don't know off-hand of any advantage to this tactic unless -cond()- saves
execution time
> vis-�-vis an -if . . . else- equivalent.
>
> Both of these features are illustrated below in the modified do-file,
which runs and gives
> the same results as that in the earlier posting.  My question is as to the
wisdom of using
> either of these tactics in Stata programming.  They both strike me as do a
lot of
> analogous tricks in C, namely, clever-looking, but liable to create
pitfalls.
>
> Joseph Coveney
>
> --------------------------------------------------------------------------
------
>
> set more off
> infile byte rctime1 byte rctime2 byte rctime3 byte rctime4 using ///
>   http://www.uni-koeln.de/themen/Statistik/data/winer/win_228.txt, clear
> // ANOVA skipped this time
> local testno = 1
> forvalues i = 1/3 {
>     local j = `i' + 1
>     forvalues k = `j'/4 {
>         quietly ttest rctime`i' = rctime`k'
>         local command = cond(`testno' == 1, ///
>           "matrix A = (`i', `k', r(p))", ///
>           "matrix A = A \ (`i', `k', r(p))")
>         `command'
>         local rownamesA = "`rownamesA'" + "testpair`testno++' "
>     }
> }
> matrix rownames A = `rownamesA'
> matrix colnames A = onedrug otherdrug unadjustp
> _mtest adjust A, mtest(holm) pindex(3) append
> matrix list r(result)
> exit


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