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]

st: using loop to generate distributions with different means and standard deviations


From   Lance Wiggains <[email protected]>
To   [email protected]
Subject   st: using loop to generate distributions with different means and standard deviations
Date   Fri, 20 May 2011 14:28:37 -0400

Sorry for the vagueness. Right now I'm just using simulated data for 3
different products. Here is my code:
My data looks like this
Week      Product 1        Product 2      Product 3
1                  50                 45                 50
2                  60                 50                 40
3                  70                 55                 30
4                  80                 50                 20
5                  90                 45                 10
6                  100               50                  0

tsset week
gen n=_n
egen max_n=max(n)

ds week n max_n, not
foreach var in `r(varlist)'{
       tssmooth ma ms_`var'= `var', weights(1 1<2>1)
       }

ds ms*
foreach var in `r(varlist)' {
       gen week3_`var'=`var' if n==max_n
       egen max_week3_`var'=max(week3_`var')
       drop week3*

}
drop ms*

ds week n max_*, not
foreach var in `r(varlist)' {
       gen max_`var'=max_week3_ms_`var'
}
drop max_week*

keep if n+3>=max_n
ds week n max*, not
foreach var in `r(varlist)'{
       egen sd_`var'=sd(`var')
}

rename max_n maximum_n

ds max_* sd* week, not
foreach var in `r(varlist)'{
       drop `var'
}

drawnorm product1, m(max_product1) sd(sd_product1)

Thanks,

Lance
- Hide quoted text -

On Wed, May 18, 2011 at 1:51 PM, Sarah Edgington <[email protected]> wrote:
> Lance,
> That's a different problem.  From your original post I assumed you had all
> the variables already created.
> One strategy for writing loops is to write out the code for the first two
> examples of something repetitive you want to do.  Then identify the parts of
> the example that remain the same across the examples.
> If you post the code your trying to repeat we may be able to help you but
> your current description is too vague for me to do much more than offer
> vague suggestions of how to think about loops.
> -Sarah
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Lance Wiggains
> Sent: Wednesday, May 18, 2011 10:36 AM
> To: [email protected]
> Subject: Re: st: RE: using loop to generate distributions with different
> means and standard deviations
>
> I've tried that but the problem is that I'm pre-calculating the means and
> sd's for the variable because I'm only using the last 3-4 observations for
> each variable to calculate those values. I'm doing this because I want it to
> reflect the changes that happen recently. My mean function uses tssmooth,
> with weights (1 1<2>), to average the last 3 weeks of sales. So if sales
> were 70,80,90, and 100 I get a value of 92.5 for my mean. It also calculates
> a SD for the last 3-4 observations. Then I want to plug those numbers into
> the drawnorm function using a loop. Any idea about how that would work?
>
> Lance
>
> On Wed, May 18, 2011 at 1:16 PM, Sarah Edgington <[email protected]> wrote:
>> Lance,
>> Try something like this:
>>
>>        forv i=1/3 {
>>                 drawnorm name`i', m(mean_var`i') sd(sd_var`i')
>>        }
>>
>> You'll run into problems, though, if your data actually includes the
>> variable names you list since there isn't a sd_var1.
>>
>> -Sarah
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Lance
>> Wiggains
>> Sent: Wednesday, May 18, 2011 10:09 AM
>> To: [email protected]
>> Subject: st: using loop to generate distributions with different means
>> and standard deviations
>>
>> Statalist members,
>>
>> I'm trying to get Stata to generate a distribution of data from
>> variables in my data set.
>>
>> My appended data looks like this
>> mean_var1=90
>> standard_deviation_var1=5
>> mean_var2=100
>> sd_var2=10
>> mean var3=110
>> sd_var3=15
>> and so on
>>
>> I'm need a loop that will take my variables and create the
>> distributions for me.
>> I've been using the drawnorm command
>>     drawnorm name1, m(mean_var1) sd(sd_var1) but I can't get it to
>> recognize more than 1 variable at a time
>>
>> I want it to perform the distribution command for each pair of my
> variables.
>> I.e. (m_var1, sd_var1), (m_var2, sd_var2) , (m_var3, sd_var3)
>>
>> Thanks for your consideration,
>>
>> Lance
*
*   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