Hi, I have been trying my best to figure out this problem, but had no luck. Any suggestion would be greatly appreciated!
I have a panel dataset of multiple individuals (i), multiple groups, (g) and multiple years (t). Let's say my key variable is score.
I am trying to operationalize the external influence of peers on an individual in a same group at t. Thus, I want to generate the weighted-average score of peers in group (g) in year (t).
It has been not easy for two reasons:
(1) I want to derived "similarity" weight from the dataset. Even within a same group, individuals are quite heterogeneous. Therefore, I generated the score rank within a group as follows:
If the "gap" between two ranks is small, two individuals are likely to be very similar. Thus, I want to apply 1/(the absolute difference between two ranks) as the weight that is multiplied by score.
For an individual, there would be multiple peers in group g, in year t - some are more similar while others are less similar to the individual. In other words, more-similar peer A's score is likely to be more impactful on individual i than the score of B. The different between A's rank and i's rank is smaller than the gap between B's rank and i's rank. Thus, what I eventually want to derive is as follows:

(2) As what I want to operationalize is the impact of peers, I want to exclude the own firm's score from the sum.
My code is as follows:
Ideally, the code should include the condition that the individual's own score is not included in the sum, and ignore any missing values. Please share your wisdom with me. Thank you!
I have a panel dataset of multiple individuals (i), multiple groups, (g) and multiple years (t). Let's say my key variable is score.
I am trying to operationalize the external influence of peers on an individual in a same group at t. Thus, I want to generate the weighted-average score of peers in group (g) in year (t).
It has been not easy for two reasons:
(1) I want to derived "similarity" weight from the dataset. Even within a same group, individuals are quite heterogeneous. Therefore, I generated the score rank within a group as follows:
Code:
bysort group year: egen rank_p=rank(-score), unique
For an individual, there would be multiple peers in group g, in year t - some are more similar while others are less similar to the individual. In other words, more-similar peer A's score is likely to be more impactful on individual i than the score of B. The different between A's rank and i's rank is smaller than the gap between B's rank and i's rank. Thus, what I eventually want to derive is as follows:
(2) As what I want to operationalize is the impact of peers, I want to exclude the own firm's score from the sum.
My code is as follows:
Code:
gen numerator=. gen denominator=. foreach x in year{ foreach y in group{ foreach i in individual{ by group year: replace numerator = sum(1/(abs(rank[`i']-rank))*score) by group year: replace denominator = sum(1/(abs(rank[`i']-rank))) } } } gen peerinfluence = numerator/denominator
Comment