首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用`broom`提取模型样本大小

在R中使用`broom`提取模型样本大小
EN

Stack Overflow用户
提问于 2020-04-25 04:47:51
回答 1查看 286关注 0票数 0

对于从模型中提取所需的几乎所有内容,broom包非常方便。但是,有没有办法也能得到样本大小(观察次数)呢?下面是一个例子:

代码语言:javascript
复制
library(tidyverse)
library(broom)

data(iris)

iris %>% 
  as_tibble() %>% 
  nest(data = c(-Species)) %>% 
  mutate(
    model = map(data, ~ lm(Petal.Width ~ Petal.Length, data = .x)),
    tidied = map(model, tidy),
    fit = map(model, glance)
  ) %>% 
  select(Species, tidied, fit) %>% 
  unnest(tidied) %>% 
  rename(t.statistic = statistic,
         t.p = p.value) %>% 
  unnest(fit) %>% 
  rename(f.statistic = statistic,
         f.p = p.value)
# A tibble: 6 x 17
  Species term  estimate std.error t.statistic      t.p r.squared adj.r.squared sigma
  <fct>   <chr>    <dbl>     <dbl>       <dbl>    <dbl>     <dbl>         <dbl> <dbl>
1 setosa  (Int~  -0.0482    0.122       -0.396 6.94e- 1     0.110        0.0914 0.100
2 setosa  Peta~   0.201     0.0826       2.44  1.86e- 2     0.110        0.0914 0.100
3 versic~ (Int~  -0.0843    0.161       -0.525 6.02e- 1     0.619        0.611  0.123
4 versic~ Peta~   0.331     0.0375       8.83  1.27e-11     0.619        0.611  0.123
5 virgin~ (Int~   1.14      0.379        2.99  4.34e- 3     0.104        0.0851 0.263
6 virgin~ Peta~   0.160     0.0680       2.36  2.25e- 2     0.104        0.0851 0.263
# ... with 8 more variables: f.statistic <dbl>, f.p <dbl>, df <int>, logLik <dbl>,
#   AIC <dbl>, BIC <dbl>, deviance <dbl>, df.residual <int>

代码字符串(1)按物种嵌套数据,(2)使用map为每个物种运行模型,(3)解嵌得到的数据。

这里有没有一种简单的方法来获得每个模型的观测值?我不想依赖于使用自由度进行计算。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-25 05:11:26

可以从broom::glance()创建的对象中获得自由度和剩余自由度。

代码语言:javascript
复制
library(broom)

aModel <- lm(Petal.Width ~ Petal.Length, data = iris)
aGlance <- glance(aModel)
aGlance$df
aGlance$df.residual

对输出执行...and操作:

代码语言:javascript
复制
> aGlance$df
[1] 2
> aGlance$df.residual
[1] 148
> 

我们可以通过将这些数字与标准模型摘要输出进行比较来验证准确性。

代码语言:javascript
复制
> summary(aModel)

Call:
lm(formula = Petal.Width ~ Petal.Length, data = iris)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.56515 -0.12358 -0.01898  0.13288  0.64272 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -0.363076   0.039762  -9.131  4.7e-16 ***
Petal.Length  0.415755   0.009582  43.387  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2065 on 148 degrees of freedom
Multiple R-squared:  0.9271,    Adjusted R-squared:  0.9266 
F-statistic:  1882 on 1 and 148 DF,  p-value: < 2.2e-16

> 

由于intercept和Petal.Length,模型消耗了2个自由度,在以150个观测值开始的数据帧中留下了148个自由度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61417205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档