st: Generate a random variable with a range constraint

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

st: Generate a random variable with a range constraint

David Collins
I apologise for what may be a fairly rudimentary question.  I want to
generate a random variable (wt), n = 5000; as follows;

  set obs 5000
  set seed 1000
  g wt = 70 * rnormal(1, 0.15)

However, wt also needs to be constrained as follows, 45 < wt < 95.  I am
having difficulty incorporating the constraint into the generate
function, and would appreciate any help.

--David

--
David M Collins, Ph.D.
Dean & Professor, Faculty of Pharmacy
University of Manitoba, Apotex Centre
750 McDermot Avenue, Winnipeg  MB  R3E 0T5
CANADA.

Tel. 204-474-8794  Fax: 204-474-7617
Email: [hidden email]

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s)
is intended for the addressee(s) only and may contain confidential
information. If you are not an intended recipient of this message or an
authorized assistant to an intended recipient, please notify the sender
by replying to this message  and then delete  it from your system.
Use, dissemination, distribution, or reproduction of this message and
any of its attachments (if any) is not authorized and may be unlawful.

*
*   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/
Reply | Threaded
Open this post in threaded view
|

st: Re: Generate a random variable with a range constraint

Martin Weiss-5
Well, you are asking for a normal, equivalent to

g wt2=rnormal(70, 10.5)


which could theoretically range from minus to plus infinity. Why do you want
it restricted? You can of course


drop if wt<45 | wt >95

and you would not lose too many observations, as 45 and 95 are far out in
the tails of N(70, 10.5)...

HTH
Martin
_______________________
----- Original Message -----
From: "David Collins" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, December 06, 2008 6:35 PM
Subject: st: Generate a random variable with a range constraint


>I apologise for what may be a fairly rudimentary question.  I want to
>generate a random variable (wt), n = 5000; as follows;
>
>  set obs 5000
>  set seed 1000
>  g wt = 70 * rnormal(1, 0.15)
>
> However, wt also needs to be constrained as follows, 45 < wt < 95.  I am
> having difficulty incorporating the constraint into the generate function,
> and would appreciate any help.
>
> --David
>
> --
> David M Collins, Ph.D.
> Dean & Professor, Faculty of Pharmacy
> University of Manitoba, Apotex Centre
> 750 McDermot Avenue, Winnipeg  MB  R3E 0T5
> CANADA.
>
> Tel. 204-474-8794  Fax: 204-474-7617
> Email: [hidden email]
>
> CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s)
> is intended for the addressee(s) only and may contain confidential
> information. If you are not an intended recipient of this message or an
> authorized assistant to an intended recipient, please notify the sender
> by replying to this message  and then delete  it from your system. Use,
> dissemination, distribution, or reproduction of this message and
> any of its attachments (if any) is not authorized and may be unlawful.
>
> *
> *   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/
>


*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Generate a random variable with a range constraint

Antoine Terracol
In reply to this post by David Collins
Hello David,

I'm not sure this is what you want, but the following code

clear
set obs 5000
scalar sig=70
scalar mu=0.15
scalar up=95
scalar low=45
g ///
wt=sig*invnorm((normal((up-mu)/sig)-normal((low-mu)/sig))*runiform()+normal((low-mu)/sig))+mu
su wt

generates a random variable tuncated at 'low' and 'up', and whose
*untruncated* distribution is normal with mean 'mu' and standard
deviation 'sig'

hope this helps,

Antoine


David Collins wrote:

> I apologise for what may be a fairly rudimentary question.  I want to
> generate a random variable (wt), n = 5000; as follows;
>
>  set obs 5000
>  set seed 1000
>  g wt = 70 * rnormal(1, 0.15)
>
> However, wt also needs to be constrained as follows, 45 < wt < 95.  I am
> having difficulty incorporating the constraint into the generate
> function, and would appreciate any help.
>
> --David
>

*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: st: Generate a random variable with a range constraint

Antoine Terracol
Of course, it's

g wt= ...

and not

g /// wt= ...

Antoine

Antoine Terracol wrote:

> Hello David,
>
> I'm not sure this is what you want, but the following code
>
> clear
> set obs 5000
> scalar sig=70
> scalar mu=0.15
> scalar up=95
> scalar low=45
> g ///
> wt=sig*invnorm((normal((up-mu)/sig)-normal((low-mu)/sig))*runiform()+normal((low-mu)/sig))+mu
>
> su wt
>
> generates a random variable tuncated at 'low' and 'up', and whose
> *untruncated* distribution is normal with mean 'mu' and standard
> deviation 'sig'
>
> hope this helps,
>
> Antoine
>
>
> David Collins wrote:
>> I apologise for what may be a fairly rudimentary question.  I want to
>> generate a random variable (wt), n = 5000; as follows;
>>
>>  set obs 5000
>>  set seed 1000
>>  g wt = 70 * rnormal(1, 0.15)
>>
>> However, wt also needs to be constrained as follows, 45 < wt < 95.  I
>> am having difficulty incorporating the constraint into the generate
>> function, and would appreciate any help.
>>
>> --David
>>
>
> *
> *   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/
>

*
*   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/