Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Return (triangle) distribution as -gen var = rtriangle()- |

Date |
Tue, 8 Jan 2013 15:28:10 +0000 |

You can't write new user-written functions in Stata. Here is a rough command. -egen- is not needed here. *! 1.0.0 Ryan Turner plus Statalist 8 Jan 2013 program define rtriangle version 8.2 syntax [if] [in] , GENerate(str) [a(real 0.0) b(real 1.0) c(real 0.5)] marksample touse local g "`generate'" gen `g' = runiform() if `touse' quietly replace `g' = /// cond(`g' < (`c'-`a')/(`b'-`a'), `a' + sqrt(`g'*(`b'-`a')*(`c'-`a')), `b' - sqrt((1-`g')*(`b'-`a')*(`b'-`c'))) end Examples: . rtriangle, gen(t1) . rtriangle, gen(t2) a(10) b(30) c(20) Nick On Tue, Jan 8, 2013 at 3:11 PM, Ryan Turner <rjturner@cmu.edu> wrote: > I am trying to generate a (triangle) distribution for use in the same form as the other standard distributions, e.g.: > > set obs 100 > gen myuni = runiform() > > I have written the following program below to do so, but it returns 'triangle not found'. I understand there is a difference between a user defined program (series of stata commands) and (user-defined?) functions e.g. for use with egen. I think I need to return a matrix, I'm not sure. > > The best I could do with the manual was from -help egen-: "Here fcn() is a function specifically written for egen, as documented below or as written by users"; but nothing regarding user-defined functions was documented below. > capture program drop triangle > program define triangle > syntax [, a(real 0.0) b(real 1.0) c(real 0.5)] > > local U = runiform() > > if `U' < (`c'-`a')/(`b'-`a') { > local x = `a' + sqrt(`U'*(`b'-`a')*(`c'-`a')); > } > else { > local x = `b' - sqrt((1-`U')*(`b'-`a')*(`b'-`c')); > } > > return `x' > end > > . set obs 100 > . gen mytri = triangle, a(10) b(20) c(15) > triangle not found * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Return (triangle) distribution as -gen var = rtriangle()-***From:*Ryan Turner <rjturner@cmu.edu>

- Prev by Date:
**st: Return (triangle) distribution as -gen var = rtriangle()-** - Next by Date:
**st: Automatic Model Fitting** - Previous by thread:
**st: Return (triangle) distribution as -gen var = rtriangle()-** - Next by thread:
**st: Automatic Model Fitting** - Index(es):