Bookmark and Share

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]

Re: st: use random values in a calculation


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: use random values in a calculation
Date   Sat, 2 Jul 2011 04:48:49 +0000

<>

Here's one approach:

************************
clear
set obs 625
g n = _n
tostring n, replace 
g str4 var = "s" + n

**create fake v1-v4 variables for example:
forv nn = 1/4 {
	g v`nn' = runiform()*10
	}

**create random opt1-opt4 to multiply by v1-v4:
forv n = 1/4 {
	g opt`n' = int(runiform()*5)
	replace opt`n' = .05 if opt`n'==1
	replace opt`n' = .1 if opt`n'==2
	replace opt`n' = .15 if opt`n'==3
	replace opt`n' = .2 if opt`n'==4
	}

**create sum of all v*opt vars:
g s = (v1*opt1)+(v2*opt2)+(v3*opt3)+(v4*opt4)
order var s
l  in 1/9
**
keep var s

**use -sxpose- to create s1-s625 variables:
		**-sxpose- is from SSC**
		cap which sxpose
		if _rc ssc install sxpose, replace
sxpose , clear firstnames force destring
************************

- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]



On Jul 1, 2011, at 9:46 PM, Zhi Su wrote:

> Dear Statalists,
> 
>  I need to generate a set of variables that are som of four variables
> v1 v2 v3 v4 multipled by a ratio r1 r2 r3 r4 which are randomly chosen
> from (0, 0.05,0.1,0.15,0.2). For example,
> s0=v1*0+v2*0+v3*0+v4*0
> s1=v1*0.05+v2*0+v3*0_v4*0
> s2=v1*0.05+v2*0.05+v3*0+v4*0
> .
> .
> .
> s625=v1*0.2+v2*0.2+v3*0.2+v4*0.2
> 
> How can I use commands to generate these 625 variables?
> 
> Thank you!
> 
> -- 
> Zhi Su
> 348 Holmes Hall
> Northeastern University
> 360 Huntington Avenue
> Boston, MA 02115
> Office:1-617-373-2316
> email:[email protected]
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index