> Date: Sat, 10 Feb 2007 11:47:28 +0000 (GMT)
> From: Maarten buis <[email protected]>
> Subject: Re: st: creating a new user function in Stata
>
> - --- Carlos Madeira <[email protected]> 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
goal
> > 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
can't
> 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
> manageble
> 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
see
> 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
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.
Stephen
-------------------------------------------------------------
Professor Stephen P. Jenkins <[email protected]>
Institute for Social and Economic Research
University of Essex, Colchester CO4 3SQ, U.K.
Tel: +44 1206 873374. Fax: +44 1206 873151.
http://www.iser.essex.ac.uk
Survival Analysis using Stata:
http://www.iser.essex.ac.uk/teaching/degree/stephenj/ec968/
Downloadable papers and software: http://ideas.repec.org/e/pje7.html
*
* 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/