如何将 "weighted.mean" 应用于此数据集?
How to apply "weighted.mean" to this dataset?
我正在尝试对类似于以下内容的数据集进行加权平均:
#dataframe
df = structure(list(IR = c(-1.83661599818263,
-1.00531026686364, -0.174004535544658, 0.657301195774328, 1.18696961514297,
1.71663803451162, 2.24630645388026, 0.469082318187177, -1.30814181750591,
-3.08536595319899, -2.45791672108286, -1.83046748896673, -1.2030182568506,
-0.490163831424105, 0.22269059400239), GR = c(-5.10521472891203,
-4.44031900064011, -3.77542327236818, -3.11052754409626, -4.32261108351108,
-5.53469462292591, -6.74677816234073, -7.45090322145527, -8.15502828056982,
-8.85915333968436, -8.25254990457514, -7.64594646946592, -7.0393430343567,
-5.67987610726306, -4.32040918016943), SP = c(0.0487639780437379,
-0.0848146569926247, -0.218393292028987, -0.35197192706535, -0.339455362585609,
-0.326938798105868, -0.314422233626127, -0.349140526484154, -0.38385881934218,
-0.418577112200207, -0.396944000571169, -0.375310888942131, -0.353677777313093,
-0.587452031413525, -0.821226285513958), IT = c(-3.40052119237083,
-3.62536742705297, -3.85021366173512, -4.07505989641726, -3.83028608109095,
-3.58551226576463, -3.34073845043832, -2.82138206645535, -2.30202568247239,
-1.78266929848942, -2.31826063159191, -2.85385196469439, -3.38944329779688,
-2.83365598615358, -2.27786867451029), MT = c(-1.42337499223543,
-3.04369471308491, -4.6640144339344, -6.28433415478388, -6.13148752871668,
-5.97864090264949, -5.82579427658229, -6.54573166791302, -7.26566905924376,
-7.98560645057449, -7.24203266452186, -6.49845887846922, -5.75488509241659,
-6.29525341763074, -6.8356217428449), PT = c(-2.59973913868916,
-2.90784524953643, -3.2159513603837, -3.52405747123097, -4.65715616381749,
-5.79025485640401, -6.92335354899053, -4.72262155686285, -2.52188956473517,
-0.321157572607492, -2.00599729396485, -3.6908370153222, -5.37567673667956,
-6.05549902582433, -6.73532131496911)), row.names = c(NA, 15L
), class = "data.frame")
# weights
wt = c(0.093071463, 0.049875216, 0.325615985, 0.472220525, 0.003411733, 0.055805079)
所以让我们假设这是我的数据集:
IR GR SP IT MT PT
1 -1.8366160 -5.105215 0.04876398 -3.400521 -1.423375 -2.5997391
2 -1.0053103 -4.440319 -0.08481466 -3.625367 -3.043695 -2.9078452
3 -0.1740045 -3.775423 -0.21839329 -3.850214 -4.664014 -3.2159514
4 0.6573012 -3.110528 -0.35197193 -4.075060 -6.284334 -3.5240575
5 1.1869696 -4.322611 -0.33945536 -3.830286 -6.131488 -4.6571562
6 1.7166380 -5.534695 -0.32693880 -3.585512 -5.978641 -5.7902549
7 2.2463065 -6.746778 -0.31442223 -3.340738 -5.825794 -6.9233535
8 0.4690823 -7.450903 -0.34914053 -2.821382 -6.545732 -4.7226216
9 -1.3081418 -8.155028 -0.38385882 -2.302026 -7.265669 -2.5218896
10 -3.0853660 -8.859153 -0.41857711 -1.782669 -7.985606 -0.3211576
11 -2.4579167 -8.252550 -0.39694400 -2.318261 -7.242033 -2.0059973
12 -1.8304675 -7.645946 -0.37531089 -2.853852 -6.498459 -3.6908370
13 -1.2030183 -7.039343 -0.35367778 -3.389443 -5.754885 -5.3756767
14 -0.4901638 -5.679876 -0.58745203 -2.833656 -6.295253 -6.0554990
15 0.2226906 -4.320409 -0.82122629 -2.277869 -6.835622 -6.7353213
我想做的是对每一行取加权平均值。我面临的问题是我的权重 wt
(1X6) 需要对每行的每个元素进行加权。例如,计算第一行的加权平均值:
((-1.8366160*wt[1]) + (-5.105215*wt[2]) + (0.04876398*wt[3]) + (-3.400521*wt[4]) + (-1.423375*wt[5]) + (-2.5997391*[6]))
显然,我想对具有相同权重的每一行执行此操作 wt
。换句话说,列是国家,所以我在加权国家的份额。我尝试了很多东西,甚至对矩阵进行了转置,但我没能用 weighted.mean
.
做到这一点
谁能帮帮我?
您可以使用矩阵乘法来获得加权平均值。
matrix_of_weighted_averages <- as.matrix(df) %*% wt
我正在尝试对类似于以下内容的数据集进行加权平均:
#dataframe
df = structure(list(IR = c(-1.83661599818263,
-1.00531026686364, -0.174004535544658, 0.657301195774328, 1.18696961514297,
1.71663803451162, 2.24630645388026, 0.469082318187177, -1.30814181750591,
-3.08536595319899, -2.45791672108286, -1.83046748896673, -1.2030182568506,
-0.490163831424105, 0.22269059400239), GR = c(-5.10521472891203,
-4.44031900064011, -3.77542327236818, -3.11052754409626, -4.32261108351108,
-5.53469462292591, -6.74677816234073, -7.45090322145527, -8.15502828056982,
-8.85915333968436, -8.25254990457514, -7.64594646946592, -7.0393430343567,
-5.67987610726306, -4.32040918016943), SP = c(0.0487639780437379,
-0.0848146569926247, -0.218393292028987, -0.35197192706535, -0.339455362585609,
-0.326938798105868, -0.314422233626127, -0.349140526484154, -0.38385881934218,
-0.418577112200207, -0.396944000571169, -0.375310888942131, -0.353677777313093,
-0.587452031413525, -0.821226285513958), IT = c(-3.40052119237083,
-3.62536742705297, -3.85021366173512, -4.07505989641726, -3.83028608109095,
-3.58551226576463, -3.34073845043832, -2.82138206645535, -2.30202568247239,
-1.78266929848942, -2.31826063159191, -2.85385196469439, -3.38944329779688,
-2.83365598615358, -2.27786867451029), MT = c(-1.42337499223543,
-3.04369471308491, -4.6640144339344, -6.28433415478388, -6.13148752871668,
-5.97864090264949, -5.82579427658229, -6.54573166791302, -7.26566905924376,
-7.98560645057449, -7.24203266452186, -6.49845887846922, -5.75488509241659,
-6.29525341763074, -6.8356217428449), PT = c(-2.59973913868916,
-2.90784524953643, -3.2159513603837, -3.52405747123097, -4.65715616381749,
-5.79025485640401, -6.92335354899053, -4.72262155686285, -2.52188956473517,
-0.321157572607492, -2.00599729396485, -3.6908370153222, -5.37567673667956,
-6.05549902582433, -6.73532131496911)), row.names = c(NA, 15L
), class = "data.frame")
# weights
wt = c(0.093071463, 0.049875216, 0.325615985, 0.472220525, 0.003411733, 0.055805079)
所以让我们假设这是我的数据集:
IR GR SP IT MT PT
1 -1.8366160 -5.105215 0.04876398 -3.400521 -1.423375 -2.5997391
2 -1.0053103 -4.440319 -0.08481466 -3.625367 -3.043695 -2.9078452
3 -0.1740045 -3.775423 -0.21839329 -3.850214 -4.664014 -3.2159514
4 0.6573012 -3.110528 -0.35197193 -4.075060 -6.284334 -3.5240575
5 1.1869696 -4.322611 -0.33945536 -3.830286 -6.131488 -4.6571562
6 1.7166380 -5.534695 -0.32693880 -3.585512 -5.978641 -5.7902549
7 2.2463065 -6.746778 -0.31442223 -3.340738 -5.825794 -6.9233535
8 0.4690823 -7.450903 -0.34914053 -2.821382 -6.545732 -4.7226216
9 -1.3081418 -8.155028 -0.38385882 -2.302026 -7.265669 -2.5218896
10 -3.0853660 -8.859153 -0.41857711 -1.782669 -7.985606 -0.3211576
11 -2.4579167 -8.252550 -0.39694400 -2.318261 -7.242033 -2.0059973
12 -1.8304675 -7.645946 -0.37531089 -2.853852 -6.498459 -3.6908370
13 -1.2030183 -7.039343 -0.35367778 -3.389443 -5.754885 -5.3756767
14 -0.4901638 -5.679876 -0.58745203 -2.833656 -6.295253 -6.0554990
15 0.2226906 -4.320409 -0.82122629 -2.277869 -6.835622 -6.7353213
我想做的是对每一行取加权平均值。我面临的问题是我的权重 wt
(1X6) 需要对每行的每个元素进行加权。例如,计算第一行的加权平均值:
((-1.8366160*wt[1]) + (-5.105215*wt[2]) + (0.04876398*wt[3]) + (-3.400521*wt[4]) + (-1.423375*wt[5]) + (-2.5997391*[6]))
显然,我想对具有相同权重的每一行执行此操作 wt
。换句话说,列是国家,所以我在加权国家的份额。我尝试了很多东西,甚至对矩阵进行了转置,但我没能用 weighted.mean
.
谁能帮帮我?
您可以使用矩阵乘法来获得加权平均值。
matrix_of_weighted_averages <- as.matrix(df) %*% wt