【问题标题】:How to convert fitdistrplus::fitdist summary into tidy format?如何将 fitdistrplus::fitdist 摘要转换为整洁的格式?
【发布时间】:2018-12-28 06:52:15
【问题描述】:

我有以下代码:

x <- c(
  0.367141764080875, 0.250037975705769, 0.167204185003365, 0.299794433447383,
  0.366885973041269, 0.300453205296379, 0.333686861081341, 0.33301168850398,
  0.400142004893329, 0.399433677388411, 0.366077304765104, 0.166402979455671,
  0.466624230750293, 0.433499934139897, 0.300017278751768, 0.333673696762895,
  0.29973685692478
)

fn <- fitdistrplus::fitdist(x,"norm")
summary(fn)
#> Fitting of the distribution ' norm ' by maximum likelihood 
#> Parameters : 
#>        estimate Std. Error
#> mean 0.32846024 0.01918923
#> sd   0.07911922 0.01355908
#> Loglikelihood:  19.00364   AIC:  -34.00727   BIC:  -32.34084 
#> Correlation matrix:
#>      mean sd
#> mean    1  0
#> sd      0  1

基本上,它需要一个向量并试图拟合分布 使用fitdistrplus package

我尝试查看broom package,但没有 涵盖该功能的功能。

【问题讨论】:

  • 你想要的输出到底是什么?我假设一个数据框,就像已经存在的 broom::tidy 函数一样,但是有哪些列?

标签: r tidyverse broom


【解决方案1】:

当您致电broom::tidy(fn) 时,您会收到一条错误消息:

错误:fitdist 类的对象没有整洁的方法

这是因为来自broom 的这个函数只有有限数量的“好用”的对象,完整列表请参见methods(tidy)。 (Read more 关于 R 中的 S3 方法。更多 here)。

因此,该函数不适用于 fitdist 对象,但适用于来自 MASSfitdistr 对象(更“著名”)。

然后我们可以将class分配给fn,然后使用broom

class(fn) <- ("fitdist", "fitdistr") 
# notice that I've kept the original class and added the other
# you shouldn't overwrite classes. ie: don't to this: class(fn) <- "fitdistr"

broom::tidy(fn)
# # A tibble: 2 x 3
# term  estimate std.error
# <chr>    <dbl>     <dbl>
# 1 mean    0.328     0.0192
# 2 sd      0.0791    0.0136

请注意,您只能看到parameters。如果您希望查看更多内容并将所有内容组织为“整洁”,您应该告诉我们更多关于您的预期输出的信息。

broom::tidy() 让你走到这一步,如果你想要更多,我会先定义我自己的方法函数,它适用于 class fitdist 对象,使用 reference tidy.fitdistr 方法,并调整它.


我将如何改编原始 broom::tidy() 代码的示例,对 fitdist 类使用 S3 方法。

定义自己的方法(类似于定义自己的函数):

# necessary libraries
library(dplyr)
library(broom)

# method definition:
tidy.fitdist <- function(x, ...) { # notice the use of .fitdist

  # you decide what you want to keep from summary(fn)
  # use fn$ecc... to see what you can harvest

  e1 <- tibble(
    term = names(x$estimate),
    estimate = unname(x$estimate),
    std.error = unname(x$sd)
  )

  e2 <- tibble(
    term = c("loglik", "aic", "bic"),
    value = c(unname(x$loglik), unname(x$aic), unname(x$bic))
  )

  e3 <- x$cor # I prefer this to: as_tibble(x$cor)

  list(e1, e2, e3) # you can name each element for a nicer result
  # example: list(params = e1, scores = e2, corrMatr = e3)
}

您现在可以这样称呼这个新的method

tidy(fn) # to be more clear this is calling your tidy.fitdist(fn) under the hood.
# [[1]]
# # A tibble: 2 x 3
# term  estimate std.error
# <chr>    <dbl>     <dbl>
# 1 mean    0.328     0.0192
# 2 sd      0.0791    0.0136
# 
# [[2]]
# # A tibble: 3 x 2
# term   value
# <chr>  <dbl>
# 1 loglik  19.0
# 2 aic    -34.0
# 3 bic    -32.3
# 
# [[3]]
#      mean sd
# mean    1  0
# sd      0  1

注意class 是:

class(fn)
[1] "fitdist"

所以现在您实际上不需要像以前那样分配fitdistr(来自MASS)类。

【讨论】:

    【解决方案2】:

    不确定你到底需要什么,但你可以试试:

    tidy_fn <- rbind(fn$estimate,fn$sd)
    

    https://stats.stackexchange.com/questions/23539/use-fitdist-parameters-in-variables

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2021-05-07
      相关资源
      最近更新 更多