Statalist


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

Re: st: Mata question


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   Re: st: Mata question
Date   Mon, 6 Jul 2009 09:49:24 +0900

Christian Bustamante wrote:

I'm trying to do a loot with a nested -ttesti- but I couldn't do it. I
have two vector with different means (m1 and m2) and another two with
each mean's standard deviation (s1 and s2). This vectors are quite
large, so do -ttesti- element for element could be very costly. Also,
I have each sample's size on two variables: n1 and n2. All this values
are obtained from a logistic regresion. After run -ttesti- I want to
save the t-statistic -r(t)- and the probability for two-sided p-value
-r(p)-. I'm doing something like this:

forvalues i=1/size(m1) {    /* how can i get vector's size? */
		ttesti n1 m1[1,`i'] s1[1,`i'] n1 m1[1,`i'] s1[1,`i']
		mat tvalues[1,`i']=r(t)
		mat pvalues[1,`i']=r(p)
	}

But appears a lot of errors: 1) n1 should be integer. 2) 'm1' found
where number expected. 3) varlist not allowed.
How can I solve this?

--------------------------------------------------------------------------------

I'm not sure how you got sample means and standard deviations from a logistic
regression, but the following will work for the loop you're trying to do.

You can get the size of a Stata matrix with the functions -colsof()- and
-rowsof()-.  If you want to reference these values as integers, such as in a
-forvalues- statement, then you would use the macro evaluation syntax (`=...')
that I've used in the illustration do-file below, i.e., you'd type

Forvalues index = 1/`=colsof(MyMatrix)' {
. . .

Joseph Coveney

*
* Create array of fictional summary statistics
*
clear *
set more off
set seed `=date("2009-07-06", "YMD")'
drawnorm m1 m2, n(520)
generate byte sample = mod(_n, 52)
collapse (mean) m1 = m1 m2 = m2 (sd) sd1 = m1 sd2 = m2 ///
    (count) n1 = m1 n2 = m2, by(sample)
*
* Begin here
*
generate double tvalues = .
generate long df = .
generate double pvalues = .
quietly forvalues sample = 1/`=_N' {
    ttesti `=n1[`sample']' `=m1[`sample']' ///
        `=sd1[`sample']' `=n2[`sample']' ///
        `=m2[`sample']' `=sd2[`sample']'
    replace tvalues = r(t) in `sample'
    replace df = r(df_t) in `sample'
    replace pvalues = r(p) in `sample'
}
*
* Done
*
list tvalues-pvalues in 1/5, noobs
exit




*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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