Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Going through each observation of a variable


From   Derya Karaci <dkaraci@yahoo.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Going through each observation of a variable
Date   Fri, 7 Jun 2013 09:47:24 -0700 (PDT)

David, thanks a lot for your answer. You are right that my question was not clear. The data set has about 80,000 individuals, it is just these particular variables that represent the price scenarios have 500 observations. I would like to go through each of the 500 prices one by one, and evaluate the expression for each individual, and create a new variable for each replication (Y_`k' where k=1,..,500). Each Y_`k' will have 80,000 observations. Then I would like to summarize horizontally across replications, not across individuals. At the end, each individual will have the mean and standard deviation of the 500 different price effects. Mean (wmean) and standard deviation (wsd) will also be variables with 80,000 observations.

The code below tried to follow http://www.stata.com/statalist/archive/2002-06/msg00112.html. But actually I don't want to randomly select from 500, I want the program to go through each observation one by one, create a new variable for each replication, and take a horizontal mean and sd across replications.   

I hope this is something feasible to do... 

Derya 


----- Original Message -----
From: David Kantor <kantor.d@att.net>
To: statalist@hsphsun2.harvard.edu
Cc: 
Sent: Friday, June 7, 2013 10:09 AM
Subject: Re: st: Going through each observation of a variable

At 10:52 AM 6/7/2013, Derya wrote:
>Dear Statalist users,
>
>I am newbie in Stata programming and I am stuck on something that is 
>probably very simple - but I could not find the answer on the list server.
>
>I would like to compute the mean and standard deviation of an 
>expression (P1*os1+P2*os2) that is computed for prices under 
>different scenarios. I have 500 scenarios, so the variables price1 
>and price2 have 500 observations.
>
>I came up with the program below, which works fine, but it chooses 
>random values of price1 and price2 variables. I would like like the 
>program go through each observation of price1 price2 one by one. How 
>would I do that?
>
>Any help will be appreciated!
>
>Thanks a lot!
>Derya
>
>
>gen k=0
>gen wmean=0
>gen wsum=0
>gen wsqdev=0
>gen wsd=0
>
>
>forv k=1/500 {
>
>gen r = uniform()
>sort r
>gen select =_n==1
>      scalar P1=price1
>      scalar P2=price2
>drop r select
>gen Y_`k'=P1*os1+P2*os2
>replace k=`k'
>replace wsum=wsum+Y_`k'
>replace wmean=wsum/`k'
>replace wsqdev=wsqdev+((wmean-Y_`k')^2)
>replace wsd=sqrt(wsqdev/(k-1))
>
>}

It is not clear what you are trying to do and why you are sorting on 
a random order 500 times.
(And even without the random ordering, there is usually no need to 
step through the observations.)
Just a few minor points to start with:
You indicated that variables price1 and price2 have 500 observations. 
It is more accurate to say that your dataset has 500 observations.
The commands
         scalar P1=price1
         scalar P2=price2
take their values from the first observation.
The variable select is unused.
----

Putting those matters aside, is all you want just the mean and 
standard deviation of price1*os1+price2*os2 ?
Then generate a variable containing that value, and summarize it:
gen newvariable = price1*os1+price2*os2
summ newvariable

HTH
--David

*
*   For searches and help try:
*  http://www.stata.com/help.cgi?searchhttp://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index