【问题标题】:Aggregating rmse and r2 in r在 r 中聚合 rmse 和 r2
【发布时间】:2017-03-31 07:09:52
【问题描述】:

这是一个示例数据 数据2:

lvl x y 0 20.099 21.2 100 21.133 21.4 250 20.866 21.6 500 22.679 21.8 750 22.737 22.1 0 30.396 32.0 100 31.373 32.1 250 31.303 32.2 500 33.984 32.8 750 44.563 38.0 0 22.755 18.5 100 23.194 18.8 250 23.263 20.5 500 23.061 27.9 750 25.678 36.4

我尝试通过以下代码行获取每个级别 (lvl) 的 rmse 和 r2: 分别是data2 %>% group_by(lvl) %>% summarise_each(funs(rmse(data2$x~data2$y)))summary(lm(data2$x,data2$y))$r.squared,在计算rmse时得到如下错误信息:

错误:参数“obs”丢失,没有默认值

# A tibble: 5 x 3 lvl x y <int> <dbl> <dbl> 1 0 0.6639888 0.6639888 2 100 0.6639888 0.6639888 3 250 0.6639888 0.6639888 4 500 0.6639888 0.6639888 5 750 0.6639888 0.6639888

在计算 r2 时。

我想汇总每个级别的 rmse 和 r2。在这种情况下,我只有 5 个级别。所以答案看起来像 5 行 X 3 列,列名 `"lvl","rmse","r2" 提前谢谢你。

【问题讨论】:

    标签: r aggregate-functions summarize


    【解决方案1】:

    你不需要summarise_eachsummary 会做你想做的事。如果您更喜欢使用 dplyr,这里有一个解决方案

    data2 <-
    data.frame(
      lvl = c(  0, 100, 250, 500, 750, 0, 100, 250, 500, 750, 0, 100, 250, 500, 750)
      ,x = c(
        20.099, 21.133, 20.866, 22.679, 22.737, 30.396, 31.373, 31.303, 33.984, 44.563, 22.755, 23.194, 23.263, 23.061, 25.678
      )
      ,y = c(21.2, 21.4, 21.6, 21.8, 22.1, 32.0, 32.1, 32.2, 32.8, 38.0, 18.5, 18.8, 20.5, 27.9, 36.4)
    )
    
    #install.packages("ModelMetrics")
    library(ModelMetrics)
    
    data2 %>%
      group_by(lvl) %>%
      summarise(
        RMSE = rmse(x, y)
        ,R2 = cor(x, y)^2
      )
    
    ## A tibble: 5 × 3
    #    lvl     RMSE        R2
    #  <dbl>    <dbl>     <dbl>
    #1     0 2.701237 0.8176712
    #2   100 2.575982 0.8645350
    #3   250 1.729888 0.9091029
    #4   500 2.920640 0.7207692
    #5   750 7.267279 0.4542507
    

    【讨论】:

      【解决方案2】:
      ## split your data2 into a list by the levels of the factor and then use lapply
      list_of_rsquared <- lapply(split(data2, data2$lvl), function (z) {
        summary(lm(x ~ y, data = z))$r.squared
      }
      )
      
      ## you will get a list of r.squared for each level . Now you can simply rbind the list of r.squared.
      rsquared_vals <- do.call("rbind", list_of_rsquared)
      

      您可以对 RMSE 使用相同的方法。 (我假设您编写了一个名为 RMSE 的函数?因为我只是使用您上面的公式)

      list_of_rmse <- lapply(split(data2, data2$lvl), function (z) { sqrt(mean((z$x - z$y)^2)) } )
      
      rmse_vals <- do.call("rbind", list_of_rmse)
      

      你现在可以cbind你现在需要的所有三列:

      cbind(data2$lvl, rsquared_vals, rmse_vals)
      

      【讨论】:

      • 非常感谢。 rmse 没有工作。它给了我错误信息:Error in match(class(obs), c("integer", "numeric", "ts", "zoo")) : argument "obs" is missing, with no default。你能调整一下吗?
      • 你能分享你的rmse函数吗?
      • 我试过data2 %&gt;% group_by(lvl) %&gt;% summarise_each(funs(rmse)),但没用。
      • r 中没有预定义函数rmse。阅读:rforge.net/doc/packages/hydroGOF/rmse.html 学习如何计算 RMSE 并首先为其编写函数!
      • 是的,我看到了。它有预定义的rmse 和hydroGOF 包。这就是我尝试使用rmse 函数的原因。我也试过list_of_rmse &lt;- lapply(split(data2, data2$lvl), function (z) { z&lt;-sqrt(mean(data2$x - data2$y)^2) } ),但我得到了一个答案列表:$0` [1] 0.0144 $100 [1] 0.0144 $250 [1] 0.0144 $500 [1] 0.0144 $@987654337 @ [1] 0.0144`你能否调整一下以获得最终结果。谢谢
      猜你喜欢
      • 2018-10-02
      • 2021-08-28
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多