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: egen rowmean, loops and if
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: egen rowmean, loops and if
Date
Wed, 6 Apr 2011 01:09:27 +0100
Here is example code for a -reshape- solution.
clear
set obs 10
forval j = 1/3 {
forval i = 1/8 {
gen occ_met`j'_`i' = runiform()
}
}
gen id = _n
reshape long occ_met, i(id) string
split _j, parse(_) destring
rename _j1 i
rename _j2 j
egen mean1 = mean(occ_met) if occ_met > 0.5 , by(j)
egen mean2 = mean(occ_met) if occ_met <= 0.5 , by(j)
On Wed, Apr 6, 2011 at 12:59 AM, Nick Cox <[email protected]> wrote:
> Here is example code for a long-winded solution:
>
> clear
> set obs 10
> forval j = 1/3 {
> forval i = 1/8 {
> gen occ_met`j'_`i' = runiform()
> }
> }
> ds
>
> forval i = 1/8 {
> gen mean1_`i' = 0
> gen mean2_`i' = 0
> gen n1_`i' = 0
> gen n2_`i' = 0
> qui forval j = 1/3 {
> replace mean1_`i' = mean1_`i' + occ_met`j'_`i' if occ_met`j'_`i' > 0.5
> replace n1_`i' = n1_`i' + (occ_met`j'_`i' > 0.5)
> replace mean2_`i' = mean2_`i' + occ_met`j'_`i' if occ_met`j'_`i' <= 0.5
> replace n2_`i' = n2_`i' + (occ_met`j'_`i' <= 0.5)
> }
> replace mean1_`i' = mean1_`i' / n1_`i'
> replace mean2_`i' = mean2_`i' / n2_`i'
> }
>
>
> On Wed, Apr 6, 2011 at 12:37 AM, Nick Cox <[email protected]> wrote:
>
>> This would be a lot easier if you -reshape-d, even temporarily.
>>
>> Otherwise, with this data structure: -egen, rowmean()- is a
>> non-starter here and I think you need to work at a lower level,
>> building up sums and counts and deriving means.
>>
>> A side-detail is that -foreach- is not needed here: use -forval- instead.
>>
>> Nick
>>
>> On Tue, Apr 5, 2011 at 10:09 PM, Thomas Speidel <[email protected]> wrote:
>>> I have a wide dataset:
>>> +-------------------------------------+
>>> | id occ_~1_1 occ_~2_1 occ_~3_1 |
>>> |-------------------------------------|
>>> | 1 4 7 . |
>>> | 2 1.5 . . |
>>> | 3 2.3 3.3 . |
>>> | 4 3.3 2.3 3.5 |
>>> | 5 1.5 . . |
>>> |-------------------------------------|
>>> | 6 1.5 . . |
>>> | 7 2.3 . . |
>>> | 8 1.5 . . |
>>> | 9 1.5 2.3 3.3 |
>>> | 10 1.5 2.3 3.3 |
>>> +-------------------------------------+
>>>
>>> where occ_~1_1 = occ_met1_1
>>> occ_~2_1 = occ_met2_1
>>> occ_~3_1 = occ_met3_1
>>>
>>> Of course, the data is much wider (and taller), with
>>> occ_metj_i j = 1 to <=3
>>> i = 1 to <=8
>>>
>>> I need to create summary measures for each i that takes the mean of the
>>> three j's. Two means are to be created: one that only evaluates values
>>>>1.5, and one that only evaluates values <=1.5:
>>>
>>> +-----------------------------------------------------+
>>> | id occ_~1_1 occ_~2_1 occ_~3_1 mean1_1 mean2_1 |
>>> |-----------------------------------------------------|
>>> | 1 4 7 . 5.50 . |
>>> | 2 1.5 . . . 1.50 |
>>> | 3 2.3 3.3 . 2.80 . |
>>> | 4 3.3 2.3 3.5 3.03 . |
>>> | 5 1.5 . . . 1.50 |
>>> |-----------------------------------------------------|
>>> | 6 1.5 . . . 1.50 |
>>> | 7 2.3 . . 2.30 . |
>>> | 8 1.5 . . . 1.50 |
>>> | 9 1.5 2.3 3.3 2.80 1.50 |
>>> | 10 1.5 2.3 3.3 2.80 1.50 |
>>> +-----------------------------------------------------+
>>>
>>> The main issue is that the two commands I thought of using, egen and cond,
>>> do not allow replace and egen, respectively.
>>> This loop is clearly wrong, but it was an attempt at producing what I need:
>>> foreach j of num 1/3 {
>>> foreach i of num 1/8 {
>>> egen mean1_`i' = rowmean(occ_met1_`i' occ_met2_`i'
>>> occ_met3_`i') if (occ_met`j'_`i'>1.5)
>>> egen mean2_`i' = rowmean(occ_met1_`i' occ_met2_`i'
>>> occ_met3_`i') if (occ_met`j'_`i'<=1.5)
>>> }
>>> }
>>>
>>
>
*
* 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/