【问题标题】:Batch distribution fitting using Tidyverse and fitdistrplus使用 Tidyverse 和 fitdistrplus 进行批量分布拟合
【发布时间】:2021-04-20 04:17:07
【问题描述】:

我有一个如下数据集(10,000+ 行):

P_ID SNUM RNUM X
ID_233 10 2 40.31
ID_233 10 3 23.21
ID_234 12 5 11.00
ID_234 12 6 0.31
ID_234 13 1 0.00
ID_235 10 2 66.23

从这个数据集中,我想将每个不同的 P_ID 拟合到 Gamma 分布(忽略对采样数据与分布的拟合程度的测试)

使用fitdistrplus 包,我可以通过将单个P_IDX 提取到向量中,然后通过fw <- fitdist(data,"gamma") 运行它,然后提取shaperate 描述性变量来实现此目的出来了,但这都是非常手动的。

我想找到一种方法,使用 tidyverse 从上面的数据框转到:

P_ID Distrib G_Shape G_Rate
ID_233 Gamma 1.21557116 0.09206639
ID_234 Gamma 3.23234542 0.34566432
ID_235 Gamma 2.34555553 0.92344521

我将如何使用 Tidyverse 和 Pipes 来实现这一点,而不是进行连续的 for 循环?

【问题讨论】:

  • 如何从fw 中提取shaperate 描述性变量?
  • fw$estimate['shape']fw$estmate['rate']

标签: r tidyverse fitdistrplus


【解决方案1】:

您可以使用group_by 为每个人应用fitdist,并从每个模型中提取shaperate 值。

library(dplyr)
library(purrr)
library(fitdistrplus)

data %>%
  group_by(P_ID) %>%
  summarise(model = list(fitdist(X, "gamma"))) %>%
  mutate(G_Shape = map_dbl(model, pluck, 'estimate', 'shape'),
         G_rate =  map_dbl(model, pluck, 'estimate', 'rate')) -> result

result

【讨论】:

    猜你喜欢
    • 2016-07-05
    • 1970-01-01
    • 2023-04-07
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2019-09-18
    • 2019-04-30
    相关资源
    最近更新 更多