如何为Tobit模型使用broom::tidy?

92vpleto  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(92)

亲爱的Stackoverflow社区,
我正在努力使用broom包中的整洁功能。我需要在多重插补的上下文中使用此函数。
你可以在这里看到一个使用ggplot 2数据集的reprex示例。

library(ggplot2, quietly = T)
library(AER, quietly = T)
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
library(broom, quietly = T)
library(tidyverse, quietly = T)

data <- ggplot2::diamonds

data
#> # A tibble: 53,940 × 10
#>    carat cut       color clarity depth table price     x     y     z
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#>  1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
#>  2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
#>  3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
#>  4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
#>  5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
#>  6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
#>  7  0.24 Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
#>  8  0.26 Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
#>  9  0.22 Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
#> 10  0.23 Very Good H     VS1      59.4    61   338  4     4.05  2.39
#> # ℹ 53,930 more rows

fit <- AER::tobit(
  formula = price ~ z,
  left = 500,
  right = 1500,
  data = data
)

fit %>%  summary(tidy)
#> Error in if (correlation) cov2cor(vcov.) else NULL: argument is not interpretable as logical
Created on 2023-07-26 with reprex v2.0.2

字符串
我检查了我的R版本和包版本:
R版本4.3.1(2023-06-16 ucrt)平台:x86_64-w 64-mingw 32/x64(64位)运行于:Windows 10 x64(内部版本19044)
其他附加包:[1] reprex_2.0.2扫帚_1.0.5 AER_1.2-10
我在GitHub上发现了以下问题和解决方案,但我无法将其转移到我正在工作的项目中:https://github.com/tidymodels/broom/issues/749https://github.com/tidymodels/broom/commit/56437bce30841211bfa64074677fe2d9124d99cc
任何帮助将非常感谢!
提前感谢!

zf9nrax1

zf9nrax11#

我想出了一个解决方法,你应该谨慎使用:class(fit)c("tobit", "survreg")survreg对象有一个整洁的方法(参见methods("tidy")),但tidy(fit)返回
错误:没有tobit类对象的整理方法
所以:

tidy.tobit <- function(x, ...) {
   class(x) <- "survreg"
   tidy(x, ...)
}
 
tidy(fit)
# A tibble: 3 × 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept) -3109.    19.7         -158.       0
2 z            1420.     6.75         210.       0
3 Log(scale)      5.63   0.00551     1021.       0

字符串
这似乎与summary()的结果相匹配:

summary(fit)

Call:
AER::tobit(formula = price ~ z, left = 500, right = 1500, data = data)

Observations:
         Total  Left-censored     Uncensored Right-censored 
         53940           1749          18261          33930 

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -3.109e+03  1.970e+01  -157.8   <2e-16 ***
z            1.420e+03  6.751e+00   210.3   <2e-16 ***
Log(scale)   5.626e+00  5.511e-03  1020.9   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Scale: 277.6 

Gaussian distribution
Number of Newton-Raphson Iterations: 8 
Log-likelihood: -1.35e+05 on 3 Df
Wald-statistic: 4.423e+04 on 1 Df, p-value: < 2.22e-16

vzgqcmou

vzgqcmou2#

fit %>% summary(tidy)不起作用,因为正确的语法是fit %>% summary %>% tidy。然而,不幸的是,没有适用于tobit摘要的整理方法。
总之,{broom}为您提供了一个glance在您的模型:

> glance(fit) ## or: fit |> glance()
# A tibble: 1 x 9
   iter    df statistic   logLik     AIC     BIC df.residual  nobs p.value
  <int> <int>     <dbl>    <dbl>   <dbl>   <dbl>       <int> <int>   <dbl>
1     8     3    72669. -135047. 270099. 270126.       53937 53940       0

...或augment艾德(带有模型预测) Dataframe :

fit |> augment(data)
# A tibble: 53,940 x 12
   carat cut    color clarity depth table price     x     y     z .fitted .resid
   <dbl> <ord>  <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>   <dbl>  <dbl>
 1  0.23 Ideal  E     SI2      61.5    55   326  3.95  3.98  2.43    341.  159. 
 2  0.21 Premi~ E     SI1      59.8    61   326  3.89  3.84  2.31    170.  330.

相关问题