如何将fitdistrplus :: fitdist摘要转换为整洁的格式? [英] How to convert fitdistrplus::fitdist summary into tidy format?

查看:117
本文介绍了如何将fitdistrplus :: fitdist摘要转换为整洁的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

基本上,它需要一个向量并尝试使用< a href = https://cran.r-project.org/web/packages/fitdistrplus/index.html rel = noreferrer> fitdistrplus包。

Basically, it takes a vector and tried to fit the distribution using fitdistrplus package.

我尝试查看扫帚包,但是没有。

I tried looking at the broom package, but it doesn't have a function that covers that.

推荐答案

调用 broom :: tidy( fn),您会收到一条错误消息:

When you call broom::tidy(fn) you receive an error that says:


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

Error: No tidy method for objects of class fitdist

这是因为 broom 中的此函数仅包含有限个对象,这些对象是好使用,请参见方法(整洁)以获取完整列表。 (了解更多有关R中S3方法的信息。更多此处)。

This is because this function from broom only has a finite number objects that are "good to use", see methods(tidy) for the complete list. (Read more about S3 methods in R. More here).

因此该函数不会适用于对象 fitdist ,但适用于 MASS fitdistr 对象c>(更著名)。

So the function doesn't work for an object fitdist but works for a fitdistr object from MASS (more "famous").

然后我们可以将分配给 fn ,然后使用扫帚

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

请注意只能看到参数。如果您希望看到更多内容并将所有内容整理为整洁,则应向我们详细说明您的预期输出。

Note that you can only see the parameters. If you wish to see more and organize everything as "tidy", you should tell us more about your expected output.

broom :: tidy( )让您走得更远,如果您想要更多,我先定义自己的方法函数,该函数可用于 fitdist 对象用作引用 tidy.fitdistr 方法,并对其进行调整。

broom::tidy() gets you this far, if you want more I'd start by defining my own method function that works for a class fitdist object using as reference the tidy.fitdistr method, and adapting it.

如何使用S3方法为类 fitdist <修改原始 broom :: tidy()代码的示例/ code>。

Example of how I'd adapt from the original broom::tidy() code, using the S3 method for the class fitdist.

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

Define your own method (similar to how you define your own function):

# 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)
}

现在,您可以通过这种方式调用此新的方法

This is how you can call this new 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 是:

class(fn)
[1] "fitdist"

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

So now you don't actually need to assign the fitdistr (from MASS) class as before.

这篇关于如何将fitdistrplus :: fitdist摘要转换为整洁的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆