Dear All, Suppose that I have the following dataset
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(company year invest)
1 1935  317.6
1 1936  391.8
1 1937  410.6
1 1938  257.7
1 1939  330.8
1 1940  461.2
1 1941    512
1 1942    448
1 1943  499.6
1 1944  547.5
1 1945  561.2
1 1946  688.1
1 1947  568.9
1 1948  529.2
1 1949  555.1
1 1950  642.9
1 1951  755.9
1 1952  891.2
1 1953 1304.4
1 1954 1486.7
2 1935  209.9
2 1936  355.3
2 1937  469.9
2 1938  262.3
2 1939  230.4
2 1940  361.6
2 1941  472.8
2 1942  445.6
2 1943  361.6
2 1944  288.2
2 1945  258.7
2 1946  420.3
2 1947  420.5
2 1948  494.5
2 1949  405.1
2 1950  418.8
2 1951  588.2
2 1952  645.5
2 1953    641
2 1954  459.3
end
format %ty year
For each `company', and `each year', I need to use the lastest 5 observations (the current year is included) to regress `invest' on a constant and trend (has to be created by ourselves, always taking values from 1 to 5, starting from the earliest year), obtaining the residuals, and calculating the standard deviation of those 5 residuals. Any suggestions? Thanks.