Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205491

calculate the growth rate for panel data

$
0
0
data("Grunfeld",package = "AER")
library("plm") 
gr <- subset(Grunfeld, firm %in% c("General Electric","General Motors"))
pgr <- plm.data(gr,index = c("firm","year"))

row.names   firm    year    invest  value   capital
1   1   General Motors  1935    317.6   3078.5  2.8
2   2   General Motors  1936    391.8   4661.7  52.6
3   3   General Motors  1937    410.6   5387.1  156.9
4   4   General Motors  1938    257.7   2792.2  209.2
5   5   General Motors  1939    330.8   4313.2  203.4
6   6   General Motors  1940    461.2   4643.9  207.2
7   7   General Motors  1941    512.0   4551.2  255.2
8   8   General Motors  1942    448.0   3244.1  303.7
9   9   General Motors  1943    499.6   4053.7  264.1
10  10  General Motors  1944    547.5   4379.3  201.6
11  11  General Motors  1945    561.2   4840.9  265.0
12  12  General Motors  1946    688.1   4900.9  402.2
13  13  General Motors  1947    568.9   3526.5  761.5
14  14  General Motors  1948    529.2   3254.7  922.4
15  15  General Motors  1949    555.1   3700.2  1020.1
16  16  General Motors  1950    642.9   3755.6  1099.0
17  17  General Motors  1951    755.9   4833.0  1207.7
18  18  General Motors  1952    891.2   4924.9  1430.5
19  19  General Motors  1953    1304.4  6241.7  1777.3
20  20  General Motors  1954    1486.7  5593.6  2226.3
21  41  General Electric    1935    33.1    1170.6  97.8
22  42  General Electric    1936    45.0    2015.8  104.4
23  43  General Electric    1937    77.2    2803.3  118.0
24  44  General Electric    1938    44.6    2039.7  156.2
25  45  General Electric    1939    48.1    2256.2  172.6
26  46  General Electric    1940    74.4    2132.2  186.6
27  47  General Electric    1941    113.0   1834.1  220.9
28  48  General Electric    1942    91.9    1588.0  287.8
29  49  General Electric    1943    61.3    1749.4  319.9
30  50  General Electric    1944    56.8    1687.2  321.3
31  51  General Electric    1945    93.6    2007.7  319.6
32  52  General Electric    1946    159.9   2208.3  346.0
33  53  General Electric    1947    147.2   1656.7  456.4
34  54  General Electric    1948    146.3   1604.4  543.4
35  55  General Electric    1949    98.3    1431.8  618.3
36  56  General Electric    1950    93.5    1610.5  647.4
37  57  General Electric    1951    135.2   1819.4  671.3
38  58  General Electric    1952    157.3   2079.7  726.1
39  59  General Electric    1953    179.5   2371.6  800.3
40  60  General Electric    1954    189.6   2759.9  888.9

above is the data, then I want to calculate the growth rate of invest, so the first value of General Motors' growth rate will be NA, and the General Electric's first value will be the same,that means I want to calculate the growth rate by group.

if I use the following command:

pgr$invest_growth <- NA
pgr$invest_growth<- c(NA,diff(invest)/invest[-length(invest)])

I will get a result, but for the number 41, I get the growth rate between General Motors and General Electric, and for my question, I want to have the number 41 value be NA.

just like

id firm invest
1  A    2
2  A    1 
3  A    4
4  A    3
1  B    2
2  B    5
3  B    2
4  B    1

then

id firm invest growth rate
1  A    1         NA
2  A    2         1          
3  A    3        0.5
4  A    4        0.3333
1  B    5        NA
2  B    6        0.2
3  B    7        0.1666.
4  B    8        0.14

so what is the command for this situation? Many thanks.


Viewing all articles
Browse latest Browse all 205491

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>