Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算R中平均MAPE和MSE的有效方法_R_Machine Learning_Statistics - Fatal编程技术网

计算R中平均MAPE和MSE的有效方法

计算R中平均MAPE和MSE的有效方法,r,machine-learning,statistics,R,Machine Learning,Statistics,我有一个真实数据和预测数据,我想计算整体MAPE和MSE。数据是时间序列,每列代表不同周的数据。我预测52周内每个项目的价值,如下所示。在R中,计算总误差的最佳方法是什么 real = matrix( c("item1", "item2", "item3", "item4", .5, .7, 0.40, 0.6, 0.3, 0.29, 0.7, 0.09, 0.42, 0.032, 0.3, 0.37), nrow=4, ncol=4) colnames(re

我有一个真实数据和预测数据,我想计算整体MAPE和MSE。数据是时间序列,每列代表不同周的数据。我预测52周内每个项目的价值,如下所示。在R中,计算总误差的最佳方法是什么

real = matrix( 
    c("item1", "item2", "item3", "item4", .5, .7, 0.40, 0.6, 0.3, 0.29, 0.7, 0.09, 0.42, 0.032, 0.3, 0.37), 
     nrow=4, 
     ncol=4) 
colnames(real) <- c("item", "week1", "week2", "week3")


predicted = matrix( 
  c("item1", "item2", "item3", "item4", .55, .67, 0.40, 0.69, 0.13, 0.9, 0.47, 0.19, 0.22, 0.033, 0.4, 0.37), 
  nrow=4, 
  ncol=4) 
colnames(predicted) <- c("item", "week1", "week2", "week3")
real=矩阵(
c(“第1项”、“第2项”、“第3项”、“第4项”、“第5项、.7项、第0.40项、第0.6项、第0.3项、第0.29项、第0.7项、第0.09项、第0.42项、第0.032项、第0.3项、第0.37项”),
nrow=4,
ncol=4)

colnames(real)首先如何获得预测值?用于获取预测值的模型可能基于最小化预测误差的某些函数(通常为MSE)。因此,如果您计算预测值,那么在拟合模型时,沿直线的某个位置计算了MSE和MAPE的残差和一些度量。您可能可以直接检索它们

如果预测值恰好被抛到你的膝上,而你与模型拟合无关,那么你可以根据以下公式计算MSE和MAPE:

每个项目每周只有一个记录。因此,对于每个项目,您每周只能计算一个预测误差。根据您的应用程序,您可以选择计算每个项目或每周的MSE和MAPE

这是您的数据的外观:

real <- matrix( 
  c(.5, .7, 0.40, 0.6, 0.3, 0.29, 0.7, 0.09, 0.42, 0.032, 0.3, 0.37), 
  nrow = 4, ncol = 3)
colnames(real) <- c("week1", "week2", "week3")

predicted <- matrix( 
  c(.55, .67, 0.40, 0.69, 0.13, 0.9, 0.47, 0.19, 0.22, 0.033, 0.4, 0.37), 
  nrow = 4, ncol = 3) 
colnames(predicted) <- c("week1", "week2", "week3")
pred_error    <- real - predicted 
pct_error     <- pred_error/real
squared_error <- pred_error^2
# For per-item prediction errors
apply(squared_error,  MARGIN = 1, mean)    # MSE
apply(abs(pct_error), MARGIN = 1, mean)    # MAPE

# For per-week prediction errors
apply(squared_error,  MARGIN = 0, mean)    # MSE
apply(abs(pct_error), MARGIN = 0, mean)    # MAPE