This question is an extension of this question. Euclidean distant for distinct classes of factors iterated by groups
The same explanations from the previous question apply here as well. I want to calculate the Euclidean distance between consecutive years for each firm based on patent classes according to the following formula:
Where Xi represents the number of patents belonging to a specific class in year t, and Yi represents the number of patents belonging to a specific class in the previous year (t-1).
The difference here is that I want to add another assumption: If a year/some years is/are missing in between, I want to implement the assumption that the firm has been active in the same patent classes as the latest non-missing year. For example, in the following dataset:
> set.seed(123)
> df <- data.table(firm = rep(c("A","B"),each=5),
year = c(1979,1981,1981,1984,1984,1959,1960,1963,1963,1965),
patent = sample(3800000:4200000,10,replace = FALSE),
class = c("410","73","52","250","252","105","454","380","380","60")
)
> df
firm year patent class
1: A 1979 3988941 410
2: A 1981 3934057 73
3: A 1981 3924021 52
4: A 1984 3960996 250
5: A 1984 4026317 252
6: B 1959 4165208 105
7: B 1960 3924506 454
8: B 1963 3993626 380
9: B 1963 3845403 380
10: B 1965 3865160 60
Firm A is missing the years 1980, 1982, and 1983. For the year 1980, the assumption is that firm has been active in the same technological patent as the year 1979: a single patent in the class 410. For the year 1982 and 1983, the assumption is that firm has been active in the same technological patent as the year 1981: two patents in classes 73 and 52 each. Therefore, the assumption is manifested in this way:
> df
firm year patent class
1: A 1979 4108578 410
2: A 1980 4108578 410
3: A 1981 3859133 73
4: A 1981 3983203 52
5: A 1982 3859133 73
6: A 1982 3983203 52
7: A 1983 3859133 73
8: A 1983 3983203 52
9: A 1984 4158992 250
10: A 1984 3945254 252
11: B 1959 4077323 105
12: B 1960 3889708 454
13: B 1961 3889708 454
14: B 1962 3889708 454
15: B 1963 3830537 380
16: B 1963 4025588 380
17: B 1964 3830537 380
18: B 1964 4025588 380
19: B 1965 3944607 60
Here again, for firm A, since year 1979 is the beginning year , there is no Euclidean distance for that year (NAs should be produced). Moving forward to year 1980, the distance is zero. For year 1981, the distinct classes for this year (1981) and the previous year (1980) are 73, 52, and 410. Therefore, the above formula is summed over these three distinct classes (there is three distinct 'i's). So the formula's output will be:
To add more clarification, the computation for year 1984 is explained: In year 1984, firm A has a total of two patents in classes 250 and 252 (one in each). The immediate previous year is 1983, which was originally missing, but after applying the above assumption, now it has two patents in classes 73 and 52 each. Because the distance is only between two consecutive years, to calculate the distance for year 1984, only years 1984 and 1983 are considered. Therefore, we have a total of four classes (250, 252, 73, and 52), meaning the summation is done over four 'i's. Beginning with the first i (class 250), the total number of patents in this class in 1 for 1984 and 0 for 1983, so Xi is equal to 1 and Yi is equal to 0. The same logic applies to 252 (Xi=1, and Yi=0). Now for the third 'i', or class 73, the total number of patents is 0 in 1984 and 1 in 1983, so Xi is equal to 0 and Yi is equal to 1. The same logic applies to class 52. Therefore the distance is given by:
Following the same calculation and reiterating over firms, the final output should be:
> df
firm year patent class El_Dist
1: A 1979 4108578 410 NA
2: A 1980 4108578 410 0.000000
3: A 1981 3859133 73 1.224745
4: A 1981 3983203 52 1.224745
5: A 1982 3859133 73 0.000000
6: A 1982 3983203 52 0.000000
7: A 1983 3859133 73 0.000000
8: A 1983 3983203 52 0.000000
9: A 1984 4158992 250 1.000000
10: A 1984 3945254 252 1.000000
11: B 1959 4077323 105 NA
12: B 1960 3889708 454 1.414214
13: B 1961 3889708 454 0.000000
14: B 1962 3889708 454 0.000000
15: B 1963 3830537 380 1.118034
16: B 1963 4025588 380 1.118034
17: B 1964 3830537 380 0.000000
18: B 1964 4025588 380 0.000000
19: B 1965 3944607 60 1.118034
I'm preferably looking for a data.table solution for speed purposes (my raw data contains about 7 million rows).
Thank you very much in advance for any help.