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

2024-04-19

我有以下代码:

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 包 https://cran.r-project.org/web/packages/fitdistrplus/index.html.

我试着看看扫帚包 https://cran.r-project.org/web/packages/broom/,但它没有 涵盖该功能的函数。


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

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

这是因为这个函数来自broom只有有限数量的“好用”的对象,请参阅methods(tidy)获取完整列表。 (阅读更多 http://adv-r.had.co.nz/S3.html关于 R 中的 S3 方法。 更多here https://adv-r.hadley.nz/s3.html).

所以该函数不适用于对象fitdist但适用于fitdistr对象来自MASS(更有名”)。

然后我们可以分配给fn that 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

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

broom::tidy()到目前为止,如果您想要更多,我会从定义我自己的方法函数开始,该函数适用于class fitdist对象使用 as参考 https://github.com/tidymodels/broom/blob/master/R/mass-fitdistr-tidiers.R the 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)
}

这就是你如何称呼这个新的method now:

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 is:

class(fn)
[1] "fitdist"

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 fitdistrplus::fitdist 摘要转换为整齐的格式? 的相关文章

随机推荐