数据帧中的总和绝对差异

时间:2015-11-09 17:43:26

标签: r forecasting

我有一个小数据帧,其值为10个周期。我想对每个值和预测值之间的绝对差值(绝对误差)求和。

列标签:P1,P2,P3,...... P10

值:3,4,3 ...... 7(见下面的数据)

预测值= 5(并不总是5)

“错误”公式= | 3-5 | + | 4-5 | + | 3-5 | + .... + | 7-5 |

> data
   cust P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 predict error
1     A  1  1  1  1  1  1  1  1  1   1       5     ?
2     B  3  3  3  3  3  3  3  3  3   3       5     ?
3     C  1  1  1  1  1  3  3  3  3   3       5     ?
4     D  1  0  1  0  1  0  1  0  1   0       5     ?
5     E  1  0  0  1  0  0  1  0  0   1       5     ?
6     F  1  3  1  3  1  3  1  3  1   3       5     ?
7     G  5  5  5  5  5  5  5  5  5   5       5     ?
8     H  8  8  8  8  8  8  8  8  8   8       5     ?
9     I  5  5  5  5  5  8  8  8  8   8       5     ?
10    J  5  0  5  0  5  0  5  0  5   0       5     ?
11    K  5  0  0  5  0  0  5  0  0   5       5     ?
12    L  5  8  5  8  5  8  5  8  5   8       5     ?

我可以用长格式进行计算,但我不想为不同大小的数据重做冗长的公式。最终的数据集将有更多的期间和客户,因此我需要一个适用于不同大小的数据框的公式/函数。我将不胜感激。

我知道这可以使用预测包完成,但我需要从底部构建它,以便我可以用结果做其他事情。

4 个答案:

答案 0 :(得分:3)

这应该可以解决问题

data$error <- rowSums(abs(data[,grepl("^P\\d+", names(data))] - data$predict))

它假设所有句号都以“P”开头,后跟一个或多个数字。

答案 1 :(得分:2)

我认为你厌恶涉及melt长期答案的很多原因是因为像这里出现的其他两个答案的代码。他们做的工作 - 但实际上是不可读的。

使用dplyr&amp; tidyr,生成一般代码和可读代码:

library(dplyr)
library(tidyr)
library(ggplot2)

# read data in
dfX = as_data_frame(read.table(textConnection("
                cust P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 predict error
1     A  1  1  1  1  1  1  1  1  1   1       5     ?
               2     B  3  3  3  3  3  3  3  3  3   3       5     ?
               3     C  1  1  1  1  1  3  3  3  3   3       5     ?
               4     D  1  0  1  0  1  0  1  0  1   0       5     ?
               5     E  1  0  0  1  0  0  1  0  0   1       5     ?
               6     F  1  3  1  3  1  3  1  3  1   3       5     ?
               7     G  5  5  5  5  5  5  5  5  5   5       5     ?
               8     H  8  8  8  8  8  8  8  8  8   8       5     ?
               9     I  5  5  5  5  5  8  8  8  8   8       5     ?
               10    J  5  0  5  0  5  0  5  0  5   0       5     ?
               11    K  5  0  0  5  0  0  5  0  0   5       5     ?
               12    L  5  8  5  8  5  8  5  8  5   8       5     ?"),
                 header = TRUE, stringsAsFactors = FALSE))

# melt & compute error
dfXErr = dfX %>%
  select(-error) %>%                    
  gather(period, actual, -cust, -predict) %>%
  group_by(cust) %>%
  summarize(mape = mean(abs(actual - predict)))

# join back to original data (if required)
inner_join(dfX, dfXErr, by = "cust") 

答案 2 :(得分:0)

data$error <- apply(apply(data[,-c(1,12)], 2, function(x) abs(x - data[,12])),1, sum)
data
   cust P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 predict error
1     A  1  1  1  1  1  1  1  1  1   1       5    40
2     B  3  3  3  3  3  3  3  3  3   3       5    20
3     C  1  1  1  1  1  3  3  3  3   3       5    30
4     D  1  0  1  0  1  0  1  0  1   0       5    45
5     E  1  0  0  1  0  0  1  0  0   1       5    46
6     F  1  3  1  3  1  3  1  3  1   3       5    30
7     G  5  5  5  5  5  5  5  5  5   5       5     0
8     H  8  8  8  8  8  8  8  8  8   8       5    30
9     I  5  5  5  5  5  8  8  8  8   8       5    15
10    J  5  0  5  0  5  0  5  0  5   0       5    25
11    K  5  0  0  5  0  0  5  0  0   5       5    30
12    L  5  8  5  8  5  8  5  8  5   8       5    15

答案 3 :(得分:0)

使用for-loop的解决方案(可能比其他解决方案慢):

df = data.frame(P1=c(1,2,3),P2=c(4,5,6),predict=c(5,5,6))
numLabels = 2
df$error = 0
for(i in 1:numLabels) {
  df$error = df$error + abs(df[,paste0("P",i)] - df$predict)
}
相关问题