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: Index


From   "Pavlos C. Symeou" <[email protected]>
To   [email protected]
Subject   Re: st: Index
Date   Thu, 12 Jan 2012 22:53:55 +0200

Dear Antonie and Nick,

thank you for your contributions. I have tried them both only to find that Nick's solution was the one that worked.

Antonie your code gives me an error that Stata cannot recognise the command - tuples -. As I have limited experience with programming, I can't suggest why this is happening. Probably you can.

Thank you again,

Pavlos

On 12/1/2012 1:25 μμ, Nick Cox wrote:
One solution has been suggested by Antonis Loumiotis. I am replying to
the original which keeps the text of the question.

First I would write a little Mata function

mata :

void symeou_index(real colvector m, real colvector s, string scalar iname) {
         real scalar s_index, n, i, j

         s_index = 0
         n = rows(m)
         for(i = 1; i<  n; i++) {
                 for(j = i + 1; j<= n; j++) {
                         s_index = s_index +
                                 (m[i] == m[j] ? s[i] * s[j] : 2 * s[i] * s[j])
                 }
         }

         st_numscalar(iname, s_index)
}

end

Then you need to load that, e.g. by putting it in a do-file and
running that do-file.

input firm   year      market    sales
1         1990    1              1
1         1991    1              1
1         1992    2              .3
1         1992    2             .1
1         1992    3              .15
1         1992    2             .15
1         1992    3             .15
1         1992    4             .15
1         1993    4              .40
1         1993    4             .20
1         1993    5             .40
2         1990    1              1
2         1991    1              1
2         1992    2              .3
2         1992    2             .1
2         1992    3              .15
2         1992    2             .15
2         1992    3             .15
2         1992    4             .15
2         1993    4              .40
2         1993    4             .20
2         1993    5             .40
end


egen group = group(firm year)
su group, meanonly
local ng = r(max)
gen thisuse = 0
gen index = .

quietly forval i = 1/`ng' {
	replace thisuse = group == `i'
	mata : work = st_data(., ("market", "sales"), "thisuse")
	mata : symeou_index(work[,1], work[,2], "result")
	replace index = scalar(result) if group == `i'
}

Nick

On Wed, Jan 11, 2012 at 3:16 PM, Pavlos C. Symeou<[email protected]>  wrote:

I have a number of companies for a number of years and each company sells
its products in a number of markets in a year. For each market, I have the
value of its sales (a proportion relative to the overall sales for the
company in a  year). I am interested in calculating an index for each
company-year as following:

1) for each company-year I must compare each market with each other. If the
two markets are the same I take their product and multiply it by 1. If the
two markets are not the same, I take their product and multiply them by 2.
The index should be the sum of all products.

 From the example data below, the Index for Firm 1 in Year 1993 should be:
(0.4 * 0.2 * 1) + (0.4 * 0.4 * 2) + (0.2 * 0.4 * 2) = 0.56

DATA

Firm   Year      Market    Market_sales    Index
1         1990    1              1
1         1991    1              1
1         1992    2              .3
1         1992    2             .1
1         1992    3              .15
1         1992    2             .15
1         1992    3             .15
1         1992    4             .15
1         1993    4              .40
1         1993    4             .20
1         1993    5             .40
2         1990    1              1
2         1991    1              1
2         1992    2              .3
2         1992    2             .1
2         1992    3              .15
2         1992    2             .15
2         1992    3             .15
2         1992    4             .15
2         1993    4              .40
2         1993    4             .20
2         1993    5             .40

*
*   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/


*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index