Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: Simplification of mata syntax to determine CI possible?


From   Dirk Enzmann <dirk.enzmann@uni-hamburg.de>
To   statalist@hsphsun2.harvard.edu
Subject   st: Simplification of mata syntax to determine CI possible?
Date   Wed, 06 Jul 2011 12:57:59 +0200

I would like to know whether it is possible to simplify the syntax / computations of the following mata syntax:

The syntax calculates likelihood based confidence intervals of a binomial parameter given sample size n, successes x and confidence level ci (I hope the lines will not get broken in the mail):

* ---- start ---------
mata:
void propci(real scalar x, real scalar n, real scalar ci)
{
  real rowvector pp
  real scalar k
  real rowvector lik
  real scalar ci_l
  real scalar ci_u
  real scalar i
  pp = range(0.000001,0.999999,1/99999)
  k = exp(1)^(-invchi2(1, ci/100)/2)
  lik = binomialp(n,x,pp)
  lik = lik/max(lik)
  ci_l = .
  ci_u = 0
  for (i=1; i <= 100000; i++) {
    if (lik[i,1] > k & ci_l == .) {
      ci_l = pp[i,1]
    }
    else if (lik[i,1] > k & ci_u < pp[i,1]) {
      ci_u = pp[i,1]
    }
    else if (ci_u > 0 & lik[i,1] < k) {
      i = 100000
    }
  }
  st_numscalar("r(ci_l)", ci_l)
  st_numscalar("r(ci_u)", ci_u)
}
end
* ---- end -----------

The 13 lines between -lik = lik/max(lik)- and -st_numscalar("r(ci_l)", ci_l)- (exclusive) can be (re)written in R using only one line of commands:

# --- start R snippet -------

conf.int=range(pp[lik > k])

# --- end R snippet ------

I wonder whether something similar is possible using mata.

========================================
Dirk Enzmann
email: dirk.enzmann@uni-hamburg.de
========================================
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index