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
Tue, 5 Jul 2011 23:18:14 +0000
<>
My response to Zhi Su's off-list reply...
Again, a snapshot/example of your data would have made things more clear, but I think updated version of my latest example gets you what you need:
***********************
clear
set obs 1000
**I have to create fake v1-v4 b/c I don't know what yours look like:
forv nn = 1/4 {
g v`nn' = runiform()*10
}
l in 1/9 //view fake v1 - v4 vars
**
loc i = 1
**multiply each v1 by each ratio:
foreach n1 in 0 05 1 15 2 {
foreach n2 in 0 05 1 15 2 {
foreach n3 in 0 05 1 15 2 {
foreach n4 in 0 05 1 15 2 {
*g s`var'_`n1'_`n2'_`n3'_`n4' = (v1*`n1')+(v2*`n2')+(v3*`n3')+(v4*`n4')
g s`i' = (v1*`n1')+(v2*`n2')+(v3*`n3')+(v4*`n4')
loc `++i'
} //end n1 loop
} //end n2 loop
} //end n3 loop
} //end n4 loop
desc s*
************************
- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
On Jul 5, 2011, at 5:16 PM, Zhi Su wrote:
> Hi, Eric,
>
> I do not quite understand your thought about it.
> 1. why you suggest to generate a string varaible n and var="s"+n?
> 2. I need to clarify that v1, v2, v3, v4 are known variables. They are
> not random. Only the ratios that multiply them change. The ratio for
> each variable could be 0, 0.05, 0.1, 0.15, 0.2.
> 3. And you suggest to
> **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)
>
> The command using "int(runiform()*5)" randomly generate integer [0,4).
> It could possible generate the same value for some "s=
> (v1*opt1)+(v2*opt2)+(v3*opt3)+(v4*opt4)".
> Actually, the 625 sum variables should be mutually different from each
> other, because the (r1, r2, r3, r4) used in one sum should have at
> least one value different from another set of (r1, r2, r3, r4).
> used to calculate the sum.
>
> 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
> s3=v1*0.05+v2*0.05+v3*0.05+v4*0
> .
> .
> s624=v1*0.2+v2*0.2+v3*0.2+v4*0.15
> s625=v1*0.2+v2*0.2+v3*0.2+v4*0.2
>
> I have not come up with good ideas about generate this set of sum.
> Do you have any good ideas?
>
> Thank you!
>
> Su
>
> 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/