[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: creating a new user function in Stata
> Date: Sat, 10 Feb 2007 11:47:28 +0000 (GMT)
> From: Maarten buis <email@example.com>
> Subject: Re: st: creating a new user function in Stata
> - --- Carlos Madeira <firstname.lastname@example.org> wrote:
> > I had a problem writing a user-written function in Stata and was
> > wondering if you could give me a help about how to write it. My
> > is to create a program that generates a variable with cdf
> values of a
> > bivariate-beta function in a similar way that the function i
> > beta(a1,b1,x1) would do.
> Short answer is that functions are hard coded into Stata, so you
> write your own function. You can write an -egen- program. But given
> your example I suspect you want this to be part of a ml program. In
> that case I wouldn't bother and just break the equation up in
> parts and create temporary variables. So looking at the equation you
> gave us:
> > cdf_biv_beta =
> > ibeta(a1,b1,x1)(1+w(x1-a1/(a1+b1))(x2-a2/(a2+b2))ibeta(a2,b2,x2) +
> > (....)
> I would do:
> tempvar mean1 mean2 temp
> gen double `mean1' = `a1'/(`a1' + `b1')
> gen double `mean2' = `a2'/(`a2' + `b2')
> gen double `temp' = 1 + `w'*(`x1'-`mean1')*(`x2'-`mean2')
> replace `lnf' = ln(ibeta(`a1',`b1',`x1') * /*
> */ `temp' * /*
> */ ibeta(`a2',`b2',`x2') + /*
> */ ... )
> I have two additional comments:
> 1. When writing down the log likelihood it is worth the effort to
> if taking the log simplifies the equation (turns multiplications in
> additions, and the like). An example is shown in a short
> technical text
> on http://home.fsw.vu.nl/m.buis/software/betafit.html .
> 2. the part 1 + `w'*(`x1'-`mean1')*(`x2'-`mean2') worries me from a
> numerical point of view. `x1'-`mean1' is the difference between an
> observed and expected proportion, so this is usually a small number.
> You multiply it with a similarly small number: `x2'-`mean2'. Adding
> such a small number to 1 can lead to big rounding errors. The
> last Mata
> matters column in the Stata Journal (volume 6, number 4) by William
> Gould was very clear piece on this issue.
> Hope this helps,
Maarten has already provided some helpful advice. I would add that if
your goal really is to create a new user function in Stata, then the
way that users other than Statacorp typically do this is by writing an
-egen- function (and the ado file name is prefixed with "_g"). You
might get some tips on how to structure a file by looking at previous
examples. Type -findit _gmvnp- at your prompt and follow the link.
The program cited is a "function" (in your sense) for calculating
multivariate normal probablilities.
Professor Stephen P. Jenkins <email@example.com>
Institute for Social and Economic Research
University of Essex, Colchester CO4 3SQ, U.K.
Tel: +44 1206 873374. Fax: +44 1206 873151.
Survival Analysis using Stata:
Downloadable papers and software: http://ideas.repec.org/e/pje7.html
* For searches and help try: