用R中的for循环计算平均绝对误差

时间:2016-02-23 16:55:16

标签: r for-loop

我有两个数据集Shuffle_WVolume_W。我想根据某些计算得到MAE错误。数据集Shuffle_W具有随机顺序,数据集Volume_W具有每个部分的卷详细信息。

Shuffle_W <- data.frame(C1 = c(Ar, Ba, Ca, Ba, Bu), 
                        C2 = c(Bu, Bu, Bu, Bu, Ar), 
                        C3 = c(Bl, Bl, Ba, Bl, Ca), 
                        C4 = c(Ca, Ar, Bl, Ar, Bl), 
                        C5 = c(Ba, Ca, Ar, Ca, Ba))

Volume <- data.frame(Ar = c(-5.1275, -2.2385, -5.3705, -6.4925, -5.068), 
                     Ba = c(1.3465, 1.5065, 1.1285, 1.5735, 0.9455), 
                     Bl = c(-1.544, 0.193, 1.966, 1.36, 0.947), 
                     Bu = c(-0.0877, 1.082, 1.183, -0.0761, -0.7775), 
                     Ca = c(3.2955, 3.074, -1.9305, 2.0875, 3.832))

在对两个数据集中的元素进行shuffle匹配之后,我想在shuffle的每一步找到MAE错误。我使用以下代码。

Total_Volume <- data.frame(matrix(NA, nrow = 5, ncol = 1))
print(Total_Volume)
Total_MAE <- data.frame(matrix(NA, nrow = 5, ncol = 5))
print(Total_MAE)
for (a in 1:5)
{
  Total_Volume = 0
  Volume_Temp = 0
  for (b in 1:5)
  {
    j <- match(Shuffle_W[a, b], names(Volume_W))
    Volume_Temp = Volume[j]
    Total_Volume = Volume_Temp + Total_Volume
    print(Total_Volume)
    #This is step by step process of calculating MAE
    #sample_abs = abs(Total_Volume)
    #print(sample_abs)
    #sample_mae = mean(as.numeric(sample_abs))
    #print(sample_mae)
    #Ends here
    MAE_Value = mean(abs(Total_Volume), na.rm = TRUE)
    print(MAE_Value)
    MAE_Value = MAE_Value + MAE_Initial
    Total_MAE[a, b] = MAE_Value
  }
  print(Total_MAE)
}

分别计算MAE(这很好)

sample_MAE_Ar = mean(abs(Volume_W$Ar), na.rm = TRUE)
print(sample_MAE_Ar)

我无法在循环中计算MAE。当我尝试直接使用公式计算MAE时会出现错误:

In mean.default(abs(Total_Volume), na.rm = TRUE) 
argument is not numeric or logical: returning NA

因此,当我尝试分解公式时,它似乎没有任何问题地计算绝对值,但在计算平均值时会卡住。我甚至尝试使用as.numeric的类型转换,但仍然无法正常工作。我只收到错误

Error in mean(as.numeric(sample_abs)) : 
  (list) object cannot be coerced to type 'double'

我不确定问题是什么,因为单独完成时同样的工作正常。我不确定是否是由于for循环。在这方面的任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

我认为您的问题在于您从数据框中提取列的方式。这两行

j <- match(Shuffle_W[a, b], names(Volume_W))
Volume_Temp = Volume[j]

导致数据帧,但mean函数需要一个向量。您应该将Volume_Temp转换为向量,或将sapplymean一起使用。看看以下是否有效:

Total_MAE <- data.frame(matrix(NA, nrow = 5, ncol = 5))
print(Total_MAE)
for (a in 1:5)
{
  Total_Volume = 0
  Volume_Temp = 0
  MAE_Initial = 0
  for (b in 1:5)
  {
    j <- match(Shuffle_W[a, b], names(Volume_W))
    Volume_Temp = Volume[j][[1]]  # Extract the first element of the list here
    Total_Volume = Volume_Temp + Total_Volume
    print(Total_Volume)
    #This is step by step process of calculating MAE
    #sample_abs = abs(Total_Volume)
    #print(sample_abs)
    #sample_mae = mean(as.numeric(sample_abs))
    #print(sample_mae)
    #Ends here
    MAE_Value = mean(abs(Total_Volume), na.rm = TRUE)
    print(MAE_Value)
    MAE_Value = MAE_Value + MAE_Initial
    Total_MAE[a, b] = MAE_Value
  }
  print(Total_MAE)
}

您没有提供可重现示例的所有信息(找不到MAE_InitialShuffle_WVolume_W没有明确定义的R代码)但我很漂亮确定就是这样。