计算R数据框中的加权平均值

Calculate weighted average in R dataframe

"f","index","values","lo.80","lo.95","hi.80","hi.95"

"auto.arima",2017-07-31 16:40:00,2.81613884762163,NA,NA,NA,NA

"auto.arima",2017-07-31 16:40:10,2.83441637197378,NA,NA,NA,NA

"auto.arima",2017-07-31 20:39:10,3.18497899649267,2.73259824384436,2.49312233904087,3.63735974914098,3.87683565394447

"auto.arima",2017-07-31 20:39:20,3.16981166809297,2.69309866988864,2.44074205235297,3.64652466629731,3.89888128383297

"ets",2017-07-31 16:40:00,2.93983529828936,NA,NA,NA,NA

"ets",2017-07-31 16:40:10,3.09739640066054,NA,NA,NA,NA

"ets",2017-07-31 20:39:10,3.1951571771414,2.80966705285567,2.60560090776504,3.58064730142714,3.78471344651776

"ets",2017-07-31 20:39:20,3.33876776870274,2.93593322313957,2.72268549604222,3.7416023142659,3.95485004136325

"bats",2017-07-31 16:40:00,2.82795253090081,NA,NA,NA,NA

"bats",2017-07-31 16:40:10,2.96389759682623,NA,NA,NA,NA

"bats",2017-07-31 20:39:10,3.1383560278272,2.76890864400062,2.573335012715,3.50780341165378,3.7033770429394

"bats",2017-07-31 20:39:20,3.3561357998535,2.98646195085452,2.79076843614824,3.72580964885248,3.92150316355876

我有一个像上面这样的数据框,它的列名是:"f"、"index"、"values"、"lo.80"、"lo.95"、"hi.80","hi.95".

我想做的是针对特定时间戳计算来自不同模型的预测结果的加权平均值。我的意思是

对于 auto.arima 中的每一行,在 ets 和 bats 中都有一个具有相同时间戳值的对应行,因此加权平均值应该这样计算:

value_arima*1/3 + values_ets*1/3 + values_bats*1/3 ;应该计算 lo.80 和其他列的相似值。

此结果应与所有加权平均值一起存储在新数据框中。

新数据框可能类似于:

index(timesamp from above dataframe),avg,avg_lo_80,avg_lo_95,avg_hi_80,avg_hi_95

我想我需要使用spread() 和mutate() 函数来实现。作为 R 的新手,我在形成此数据框后无法继续。

请帮忙

您提供的示例不是加权平均,而是简单平均。 你想要的是一个简单的聚合。 第一部分是 dput 提供的数据集(最好在此处共享)

d <- structure(list(f = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
2L, 2L, 2L, 2L), .Label = c("auto.arima", "bats", "ets"), class = "factor"), 
index = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), .Label = c("2017-07-31 16:40:00", "2017-07-31 16:40:10", 
"2017-07-31 20:39:10", "2017-07-31 20:39:20"), class = "factor"), 
values = c(2.81613884762163, 2.83441637197378, 3.18497899649267, 
3.16981166809297, 2.93983529828936, 3.09739640066054, 3.1951571771414, 
3.33876776870274, 2.82795253090081, 2.96389759682623, 3.1383560278272, 
3.3561357998535), lo.80 = c(NA, NA, 2.73259824384436, 2.69309866988864, 
NA, NA, 2.80966705285567, 2.93593322313957, NA, NA, 2.76890864400062, 
2.98646195085452), lo.95 = c(NA, NA, 2.49312233904087, 2.44074205235297, 
NA, NA, 2.60560090776504, 2.72268549604222, NA, NA, 2.573335012715, 
2.79076843614824), hi.80 = c(NA, NA, 3.63735974914098, 3.64652466629731, 
NA, NA, 3.58064730142714, 3.7416023142659, NA, NA, 3.50780341165378, 
3.72580964885248), hi.95 = c(NA, NA, 3.87683565394447, 3.89888128383297, 
NA, NA, 3.78471344651776, 3.95485004136325, NA, NA, 3.7033770429394, 
3.92150316355876)), .Names = c("f", "index", "values", "lo.80", 
"lo.95", "hi.80", "hi.95"), class = "data.frame", row.names = c(NA, 
-12L))

> aggregate(d[,3:7], by = d["index"], FUN = mean)
                index   values    lo.80    lo.95    hi.80    hi.95
1 2017-07-31 16:40:00 2.861309       NA       NA       NA       NA
2 2017-07-31 16:40:10 2.965237       NA       NA       NA       NA
3 2017-07-31 20:39:10 3.172831 2.770391 2.557353 3.575270 3.788309
4 2017-07-31 20:39:20 3.288238 2.871831 2.651399 3.704646 3.925078

您可以将此输出保存在对象中并根据需要更改列名。

如果你真的想要加权平均值,这是一种获得它的方法(这里蝙蝠的权重为 0.8,其他 2 个为 0.1):

> d$weight <- (d$f)
> levels(d$weight) # check the levels
[1] "auto.arima" "bats"       "ets"       
> levels(d$weight) <- c(0.1, 0.8, 0.1)
> # transform the factor into numbers
> # warning as.numeric(d$weight) is not correct !!
> d$weight <- as.numeric(as.character((d$weight))) 
> 
> # Here the result is saved in a data.frame called "result
> result <- aggregate(d[,3:7] * d$weight, by = d["index"], FUN = sum)
> result
                index   values    lo.80    lo.95    hi.80    hi.95
1 2017-07-31 16:40:00 2.837959       NA       NA       NA       NA
2 2017-07-31 16:40:10 2.964299       NA       NA       NA       NA
3 2017-07-31 20:39:10 3.148698 2.769353 2.568540 3.528043 3.728857
4 2017-07-31 20:39:20 3.335767 2.952073 2.748958 3.719460 3.922576