如何将fitdistrplus :: fitdist摘要转换为整齐格式?

sca*_*der 5 r broom tidyverse

我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

基本上,它需要一个向量,并尝试使用fitdistrplus包来适应分布.

我试着看一下扫帚包,但它没有涵盖那个的功能.

RLa*_*ave 5

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

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

这是因为此函数 frombroom只有有限数量的“好用”对象,请参阅methods(tidy)完整列表。(阅读有关 R 中 S3 方法的更多信息。更多信息请点击此处)。

因此该函数不适用于对象,fitdist但适用于(更“著名”)的fitdistr对象MASS

然后我们可以分配给fn那个class,然后使用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
Run Code Online (Sandbox Code Playgroud)

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

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


我如何适应原始broom::tidy()代码的示例,使用类的 S3 方法fitdist

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

# 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)
}
Run Code Online (Sandbox Code Playgroud)

这就是你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
Run Code Online (Sandbox Code Playgroud)

请注意,它class是:

class(fn)
[1] "fitdist"
Run Code Online (Sandbox Code Playgroud)

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