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: Panel data - Foreach-Collapse commands


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Panel data - Foreach-Collapse commands
Date   Wed, 15 Jan 2014 12:20:46 +0000

I really don't think I can explain this more clearly, but I will
explain once more and once only.

Your -collapse- statement reduced your dataset to (mean) investment
-invest- and -company-. You dropped everything else, including your SD
results that you had just calculated. So, your code did not do what
you say you want. I showed that and showed how to avoid it.

You are posting questions as if you just want the answers as code that
does exactly what you want and you are going to keeping asking the
same questions until that happens. Sometimes for a simple problem
people will do that for you; for a complicated problem it is very
unlikely. Other times people will try to give you some suggestions and
especially try to explain what is going on so that you can help
yourself to solve the problems. If you are not interested in
explanations, but only in complete solutions, you need to hire a
consultant or employ someone to write the code for you.

Nick
[email protected]


On 15 January 2014 12:10, Sadia Khalid <[email protected]> wrote:
> Sir thanks for your reply.
>
> I am not threwing away the moving SDs. I will use them later in the
> regression analysis  . But Firstly I have to calculate the average of
> this 3 year moving average by country , to draw a scatter graph.
>
>
>
>
>
>
> On 15 January 2014 17:00, Nick Cox <[email protected]> wrote:
>> I havealready explained your immediate Stata problem in posts you
>> include here, otherwise found at
>>
>> http://www.stata.com/statalist/archive/2014-01/msg00494.html
>>
>> and
>>
>> http://www.stata.com/statalist/archive/2014-01/msg00500.html
>>
>> Let me try again. Consider the following code
>>
>> webuse grunfeld, clear
>> xtset company year
>>
>> foreach var in   invest kstock {
>>      mvsumm `var', stat(sd) win(3) gen(`var'3sd) end
>>      collapse `var' , by(company)
>>      describe
>> }
>>
>> I added the command -describe-. This is what it says first time around the loop.
>>
>> Contains data
>>   obs:            10
>>  vars:             2
>>  size:           120 (99.9% of memory free)
>> -------------------------------------------------------------------------------------
>>               storage  display     value
>> variable name   type   format      label      variable label
>> -------------------------------------------------------------------------------------
>> company         float  %9.0g
>> invest          float  %9.0g                  (mean) invest
>> -------------------------------------------------------------------------------------
>> Sorted by:  company
>>      Note:  dataset has changed since last saved
>>
>> In other words, there are only two variables in the dataset, -company-
>> and -invest-. This is because of your -collapse-. You got what you
>> asked for. -collapse- changes the existing dataset. Thus -kstock- has
>> disappeared and, second time round the loop, you inevitably get the
>> error reported.
>>
>> In addition, you generated moving SDs and then threw them away. This
>> seems pointless.
>>
>> I imagine you really wanted to do something like this. Note that the
>> repeated -collapse- is a bad idea, as it obliges you to read in the
>> dataset again. So, don't -collapse- within the loop, but do it later.
>>
>> webuse grunfeld, clear
>> xtset company year
>>
>> foreach var in invest kstock {
>>      mvsumm `var', stat(sd) win(3) gen(`var'3sd) end
>> }
>>
>> collapse *sd , by(company)
>> describe
>>
>> All that said, what are you doing? The mean (over its history) of a
>> moving SD of 3 values doesn't strike me as a well-defined property,
>> regardless of whether you can find it used in the literature.
>>
>> I've left your other questions as much more than I want to try to handle.
>>
>> Nick
>> [email protected]
>>
>>
>> On 15 January 2014 04:15, Sadia Khalid <[email protected]> wrote:
>>
>>> There is no issue of calculating the moving SD of Y and X.
>>>
>>> but when i am going to collapse (SD of Y ans X), it is only
>>> collpaseing the first varialbe, error for the second variable. how to
>>> aviod this problem . by collapising the varialbes i want to a single
>>> value for each country.
>>>
>>>
>>>
>>>
>>> On 15 January 2014 08:47, Sadia Khalid <[email protected]> wrote:
>>>> firstly I want to calculate the  the 3 Year Moving Sd of variables (Y
>>>> and X). like SD of (1,2,3)
>>>> first three obs and than SD of (2,3,4 )th obs ans so on.
>>>>
>>>>
>>>> than i have to take the average of these variables by country
>>>> variables, to collapse them to a single number for each country.
>>>>
>>>> Regards
>>>> Sadia Khalid
>>>>
>>>> On 15 January 2014 08:26, Nick Cox <[email protected]> wrote:
>>>>> Your -collapse- ignores your SD results.  Otherwise I stand by my
>>>>> earlier comments. I have difficulties writing code when the aim seems
>>>>> ill-judged.
>>>>> Nick
>>>>> [email protected]
>>>>>
>>>>>
>>>>> On 15 January 2014 03:14, Sadia Khalid <[email protected]> wrote:
>>>>>> Yes I want is
>>>>>>  collapsing on one variable , saving, reopening the original, collapsing...,
>>>>>>
>>>>>> I have got the program to do but i cannot modify this to my situtation.
>>>>>>
>>>>>> the link to the code is
>>>>>> http://www.urch.com/forums/phd-economics/125348-stata-repeated-collapse.html
>>>>>>
>>>>>> yes i want to calculate the 3 Year  Moving Sd. like SD of (1,2,3)
>>>>>> first three obs and than SD of (2,3,4 )th obs ans so on.
>>>>>>  Regards
>>>>>> Sadia Khalid
>>>>>>
>>>>>>
>>>>>> On 15 January 2014 01:39, Nick Cox <[email protected]> wrote:
>>>>>>> Too many questions (at least for me to want to answer)!  I'll peel off
>>>>>>> the first.
>>>>>>>
>>>>>>> Once you -collapse- the first time round your loop, -kstock- has
>>>>>>> disappeared from your dataset. That's what -collapse- does: reduce
>>>>>>> your dataset to a new one. But the -collapse- is (arguably) illogical.
>>>>>>> Moving standard deviations are unique to each observation. A mean
>>>>>>> moving SD could be calculated, but it seems unlikely to be what you
>>>>>>> want. If it is really what you want, you need to read in the original
>>>>>>> data again.  On the other hand, I want whether you really want a
>>>>>>> moving SD. The rest of your question seems to imply otherwise.
>>>>>>>
>>>>>>> General comment: Some Statalist members use MS Excel. Many don't in
>>>>>>> practice or won't on principle.  It's not a good idea to imply that
>>>>>>> members are inclined to try and work out how to read your data into
>>>>>>> Stata from Excel.
>>>>>>>
>>>>>>> Nick
>>>>>>> [email protected]
>>>>>>>
>>>>>>> On 14 January 2014 20:18, Sadia Khalid [edited]
>>>>>>>
>>>>>>> I am working on panel data. I have to create the Rolling SD of Y
>>>>>>> variable which I am able to create with the help of -mvsumm- (SSC).
>>>>>>>
>>>>>>> The command for creating the 3 year moving SD is
>>>>>>>
>>>>>>> mvsumm y, stat(mean) win(3) gen(`var'3avg) end
>>>>>>>
>>>>>>> As I have to do 3 year moving SD for a number of variables I am trying
>>>>>>> to use -foreach- and these are the commands in my do file
>>>>>>>
>>>>>>> webuse grunfeld,clear
>>>>>>> xtset company year
>>>>>>>  *** Code for calculating 3 Year moving SD**
>>>>>>>
>>>>>>> foreach var in   invest kstock {
>>>>>>>      mvsumm `var', stat(sd) win(3) gen(`var'3sd) end
>>>>>>>      collapse `var' , by(company)
>>>>>>> }
>>>>>>>
>>>>>>> I get the following error message.
>>>>>>>
>>>>>>>  “variable kstock not found”
>>>>>>>
>>>>>>> How can I overcome this?
>>>>>>>
>>>>>>> <big snip>
>>
>> *
>> *   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/
>
> *
> *   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index