Stata Tips: 计算加权平均值
(2015-11-01 15:18:51)
标签:
旅游 |
分类: 科研 |
clear
set obs 20
gen x=_n
gen y=x
gen id=.
replace id=1 in 1/5
replace id=2 in 6/10
replace id=3 in 11/15
replace id=4 in 16/20
bysort id : egen mean_x=mean(x)
*bysort id : egen xx2=sum(x)
*bysort id : egen xx3=total(x)
gen z=x*y
bysort id: egen total_y=total(y)
gen weight=y/total_y
bysort id: egen weight_mean_x=total(x*weight)
//此处是关键应该用total 函数 而不是mean函数,否则容易出错
bysort id : egen yy=mean(x*y)