计算预期值的汇总变化(不是标准偏差)
Calculate aggregated variations from expected values (not std deviations)
我正在尝试用它来模拟网络中一组卖家的定价准确性。
我的数据集(定价)如下所示:
transactionID sellerID expectedprice actualprice pricediff
1 1001 251 200 210 10
2 1002 101 200 300 100
3 1003 251 400 190 -210
4 1004 251 300 300 0
5 1005 101 250 250 0
6 1006 350 200 210 10
7 1007 401 400 400 0
注意:我不是要计算标准偏差,因为我不是要计算与均值的方差,而是与预期值列的方差,这将因交易而异。
我很乐意table插入新列以获取与预期值的绝对差异到table中使用:
pricing$diffabs <- abs(pricing$pricediff)
结果如下:
transactionID sellerID expectedprice actualprice pricediff diffabs
1001 251 200 210 10 10
1002 101 200 300 100 100
1003 251 400 190 -210 210
1004 251 300 300 0 0
1005 101 250 250 0 0
1006 350 200 210 10 10
1007 401 400 400 0 0
然后如何计算每个卖家的方差分数:
在 "sellerID" 处分组的 abs(pricing$diff)
的总和除以数据中 "sellerID" 的观测数(计数)。
我期望的输出如下:
SellerID Count Sumofdiffabs Variation
251 3 220 73.33333333
101 2 100 50
350 1 10 10
401 1 0 0
在聚合级别处理 R 中方差的其他帮助主题似乎只处理标准差或均值方差,例如:
当我使用像标准偏差这样的简单函数时,聚合函数对我来说效果很好,但在我必须弄清楚如何将计数插入函数的地方却不行。让我失望的是,我的方差不是偏离平均值,而是偏离我的 table.
中的列结果
m = matrix(c(1001,251,200,210,10,1002,101,200,300,100,1003,251,400,190,-210,1004,251,300,300,0,1005,101,250,250,0,1006,350,200,210,10,1007,401,400,400,0),ncol = 5,nrow=7,byrow=TRUE)
colnames(m) = c("transactionID","sellerID","expectedprice","actualprice","pricediff")
pricing = as.data.frame(m)
pricing$diffabs <- abs(pricing$pricediff)
pricing
transactionID sellerID expectedprice actualprice pricediff diffabs
1001 251 200 210 10 10
1002 101 200 300 100 100
1003 251 400 190 -210 210
1004 251 300 300 0 0
1005 101 250 250 0 0
1006 350 200 210 10 10
1007 401 400 400 0 0
结果如下:
library(data.table)
pricing = as.data.table(pricing)
f <- function(x) {list( Count=length(x))}
result <- pricing[ , c(f(diffabs), Sumofdiffabs=sum(diffabs),Variation=mean(diffabs)),by=sellerID]
result
sellerID Count Sumofdiffabs Variation
1: 251 3 220 73.33333
2: 101 2 100 50.00000
3: 350 1 10 10.00000
4: 401 1 0 0.00000
我正在尝试用它来模拟网络中一组卖家的定价准确性。
我的数据集(定价)如下所示:
transactionID sellerID expectedprice actualprice pricediff
1 1001 251 200 210 10
2 1002 101 200 300 100
3 1003 251 400 190 -210
4 1004 251 300 300 0
5 1005 101 250 250 0
6 1006 350 200 210 10
7 1007 401 400 400 0
注意:我不是要计算标准偏差,因为我不是要计算与均值的方差,而是与预期值列的方差,这将因交易而异。
我很乐意table插入新列以获取与预期值的绝对差异到table中使用:
pricing$diffabs <- abs(pricing$pricediff)
结果如下:
transactionID sellerID expectedprice actualprice pricediff diffabs
1001 251 200 210 10 10
1002 101 200 300 100 100
1003 251 400 190 -210 210
1004 251 300 300 0 0
1005 101 250 250 0 0
1006 350 200 210 10 10
1007 401 400 400 0 0
然后如何计算每个卖家的方差分数:
在 "sellerID" 处分组的 abs(pricing$diff)
的总和除以数据中 "sellerID" 的观测数(计数)。
我期望的输出如下:
SellerID Count Sumofdiffabs Variation
251 3 220 73.33333333
101 2 100 50
350 1 10 10
401 1 0 0
在聚合级别处理 R 中方差的其他帮助主题似乎只处理标准差或均值方差,例如:
当我使用像标准偏差这样的简单函数时,聚合函数对我来说效果很好,但在我必须弄清楚如何将计数插入函数的地方却不行。让我失望的是,我的方差不是偏离平均值,而是偏离我的 table.
中的列结果m = matrix(c(1001,251,200,210,10,1002,101,200,300,100,1003,251,400,190,-210,1004,251,300,300,0,1005,101,250,250,0,1006,350,200,210,10,1007,401,400,400,0),ncol = 5,nrow=7,byrow=TRUE)
colnames(m) = c("transactionID","sellerID","expectedprice","actualprice","pricediff")
pricing = as.data.frame(m)
pricing$diffabs <- abs(pricing$pricediff)
pricing
transactionID sellerID expectedprice actualprice pricediff diffabs
1001 251 200 210 10 10
1002 101 200 300 100 100
1003 251 400 190 -210 210
1004 251 300 300 0 0
1005 101 250 250 0 0
1006 350 200 210 10 10
1007 401 400 400 0 0
结果如下:
library(data.table)
pricing = as.data.table(pricing)
f <- function(x) {list( Count=length(x))}
result <- pricing[ , c(f(diffabs), Sumofdiffabs=sum(diffabs),Variation=mean(diffabs)),by=sellerID]
result
sellerID Count Sumofdiffabs Variation
1: 251 3 220 73.33333
2: 101 2 100 50.00000
3: 350 1 10 10.00000
4: 401 1 0 0.00000