Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: scaling by average

 From Claudius Li To statalist@hsphsun2.harvard.edu Subject Re: st: scaling by average Date Wed, 12 Dec 2012 11:19:48 -0500

```Thanks. I ended up doing:

gen day_of_week = dow(date)
gen score_count = score * count
egen score_count_total = total(score_count), by(state day_of_week)
egen count_total = total(count), by(state day_of_week)
gen average_score = score_count_total / count_total
gen scaled_score = score - average_score
gen score_diff = (scaled_score[_n+1] - scaled_score[_n-1]) / scaled_score[_n-1]

That seemed to work.

On Wed, Dec 12, 2012 at 5:21 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> Alternatively, and again if these are weekly data,
>
> tsset state date, delta(7)
> gen total = score*count
> tssmooth ma s_total = total, w(1 1 1)
> tssmooth ma s_count = count, w(1 1 1)
> gen average = s_total/s_count
>
> Nick
>
> On Wed, Dec 12, 2012 at 9:51 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>
>> The -sum()- here looks wrong as the function returns cumulative sums
>> and your additions are explicit any way. If I understand you correctly
>> you want
>>
>> tsset state date
>>
>> gen average = (score* count + L7.score*L7.count + F7.score*F7.count) /
>> (count + L7.count + F7.count)
>>
>> but if this is weekly data, then
>>
>> tsset state date, delta(7)
>> gen average = (score* count + L1.score*L1.count + F1.score*F1.count) /
>> (count + L1.count + F1.count)
>>
>> is simpler.
>>
>> Nick
>>
>> On Tue, Dec 11, 2012 at 9:10 PM, Claudius Li
>> <claudiusli@janysanalytics.com> wrote:
>>
>>> I've got an other question on the panel data set from my previous post.
>>> I've reshaped it so that I now have
>>>
>>> date state score time <other stuff>
>>>
>>> I want to add a variable normalized_score = score - average_for_day
>>> Where average_for_day = sum(score * count + score[n-7] * count[n-7]
>>> +score[n+7] * count[n+7] ... ) / sum(count + count[n-7] + count[n+7]
>>> ... )
>>>
>>> It seems a bit complicated to do this with loops. Is there a simpler way?
> *
> *   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/
```