Page 1 of 1

Estimating a trimmed mean

Posted: Mon Aug 31, 2015 12:47 am
by rahildevgan
Hi,

I would like to estimate a trimmed mean inflation measure and thought whether it was easily doable in EViews rather than R. Essentially what I have to do is this:

DATA:
1) Time series of CPI weights
2) Time series of monthly changes of components

For each i, I need to:
a) sort a row of data (the row containing all the monthly changes of components)
b) order that row by magnitude
c) delete the top x and bottom x observations
d) use the remaining data to multiply with the row of weights (sorted similarly/simultaneously) and calculate the monthly change

Can this be done in a relatively easy way? Would be grateful for any help.

Example: I have 50 components and 10 months of data.
- weights[j] where i=1 to 50, j = 1 to 10
- monthly changes mom[j] where i =1 to 50, j = 1 to 10

(for i = 1 to 10) #i need to cycle through the months
{
for (j= 1 to 50) #loop through the components
{
sort(mom[j]) (order the monthly changes)
then AT THE SAME TIME sort the weights[j]
}
}
then cut off the top x and bottom x values
then multiply remaining mom values by weights

Is this too complicated to be done in eviews? Thanks a lot!

Re: Estimating a trimmed mean

Posted: Mon Aug 31, 2015 8:23 am
by EViews Gareth
It is possible if you convert the series into matrices, but not particularly pleasant.

If you're used to R, it might be easier for you to use xopen/xput/xrun to quickly send the series over to R, do the calculations there, then bring them back.

Re: Estimating a trimmed mean

Posted: Mon Aug 31, 2015 8:53 am
by EViews Glenn
I think there's a way to do this in EViews. Let me give it some thought when I get into the office.

[edit: can you describe in a bit more detail how your workfile is set up?]

Re: Estimating a trimmed mean

Posted: Wed Sep 02, 2015 1:57 am
by rahildevgan
Thanks for the reply! Sure I will try my best because I ran into a bit of trouble in R.

The main trouble i'm facing is "sorting one row on the basis of another". My workfile essentially would have:

1) matrix (X) of CPI component weights (dimensions [7][299] because I have 7 months of data and 299 components)
2) matrix (Y) of CPI component m/m% changes (dimensions same as above)

what i need to do is:
1) loop through the months (1 to n) (in this case n=7)
2) for each month, i need to sort Y from smallest to largest values but simultaneously, the corresponding row/month in X needs to be reordered/sorted as well.
3) then i need to 'trim' the re-ordered row (remove the top and bottom x%) and sum up the cross-product (basically multiply the m/m% changes (Y) by weights (X) and then sum them to get the monthly contribution)

Step 2 is the crucial issue which i'm having trouble with. It seems to be impossible in eviews. Thanks in advance for any help!

Re: Estimating a trimmed mean

Posted: Wed Sep 02, 2015 8:46 am
by EViews Glenn
Should be easy to do without the sorting using a couple of by-statistics functions.

Can you post your workfile here? It's easier to illustrate if I have your data. Best if your data is panel structured, but we'll address that when I see your workfile.

Re: Estimating a trimmed mean

Posted: Sun Sep 06, 2015 11:38 pm
by rahildevgan
Thanks a lot! Sorry for the delayed response.

group_weights is matrix X, group_mom is matrix Y. do let me know if other information, is there any way i can loop and name?

Re: Estimating a trimmed mean

Posted: Mon Sep 07, 2015 4:31 pm
by speruzzo
Thanks rahildevgan and Glenn,

I am actually trying to address exactly the same estimation and very keen to read your followups.

All the best.

Re: Estimating a trimmed mean

Posted: Tue Sep 08, 2015 10:16 pm
by EViews Glenn
Open your workfile, and run the following program snippet:

Code: Select all

delete group_weights rename _*_weight weight* group group_weights weight?? pagestack series? @ *? * series high = @quantilesby(series, @date, .95) series low = @quantilesby(series, @date, .05) series wsums = @sumsby(series*weight, @date, "if series>low and series<high") series wcounts=@sumsby(1, @date, "if series>low and series<high")
The first block reshapes the workfile page into a panel structure by stacking the weight and the data series. The naming convention for the weight series differed from the convention for the data (which is a problem when stacking) so I've renamed all of those series. I think this matches how the data are structured; my apologies if I misunderstood the structure.

The second block is the actual computation. As I said, it's only a couple of lines. First we computed quantiles for the data for each date (month) in the stacked workfile. Note that the quantiles use the Cleveland definition as described in the manual. You may have to adjust the quantile values slightly to get the trimming percentages to match exatly what you want. Here, we're computing quantiles for the approximate highest 5% and lowest 5%.

We then compute the weighted sums for each period over a restricted sample using the quantile values computed above. I've also computed a counts equivalent so you can see the number of observations used in each quantile calculation. Note that the @sumsby will match merge the weighted sums to each period, repeating the values for a given period across all of the cross-section units.

I think this is what was requested. Let me know if anything is unclear.

Re: Estimating a trimmed mean

Posted: Mon Nov 02, 2020 11:16 am
by akd
This looks like the trimming is done without reference to the weights, and then a weighted average is taken of the remaining items.

How would this be done if we wanted to account for weights in the trimming process? For example, say we want to take the inflation rate of the middle 50% of the consumption basket, the weight series are the consumption shares, and the series "series" are the item inflation rates. I don't see how to do that in eviews.