【问题标题】:Output of chi-square as vector卡方输出为向量
【发布时间】:2021-11-21 19:05:53
【问题描述】:

我对@9​​87654321@ 使用卡方拟合优度检验。有没有办法将此函数的输出保存为向量,例如: test <- c(X-squared, df, p-value)?

【问题讨论】:

    标签: statistics chi-squared hypothesis-test


    【解决方案1】:

    chisq.test 的输出是一个列表

    x <- c(A = 20, B = 15, C = 25)
    
    test_chi <- chisq.test(x)
    
    str(test_chi)
    
    List of 9
     $ statistic: Named num 2.5
      ..- attr(*, "names")= chr "X-squared"
     $ parameter: Named num 2
      ..- attr(*, "names")= chr "df"
     $ p.value  : num 0.287
     $ method   : chr "Chi-squared test for given probabilities"
     $ data.name: chr "x"
     $ observed : Named num [1:3] 20 15 25
      ..- attr(*, "names")= chr [1:3] "A" "B" "C"
     $ expected : Named num [1:3] 20 20 20
      ..- attr(*, "names")= chr [1:3] "A" "B" "C"
     $ residuals: Named num [1:3] 0 -1.12 1.12
      ..- attr(*, "names")= chr [1:3] "A" "B" "C"
     $ stdres   : Named num [1:3] 0 -1.37 1.37
      ..- attr(*, "names")= chr [1:3] "A" "B" "C"
     - attr(*, "class")= chr "htest"
    

    我们可以用列表中的一些元素创建一个向量

    out <- c(test_chi$statistic,test_chi$parameter,test_chi$p.value)
    out
    X-squared        df           
    2.5000000 2.0000000 0.2865048
    

    另一种选择是使用库broom

    broom::tidy(test_chi)
    
    # A tibble: 1 x 4
      statistic p.value parameter method                                  
          <dbl>   <dbl>     <dbl> <chr>                                   
    1       2.5   0.287         2 Chi-squared test for given probabilities
    

    【讨论】:

    • 非常感谢!这正是我想要的。
    • 很高兴它帮助了你!如果可能,请通过选择我的答案来结束此问题,这有助于搜索其他用户。
    • 完成!再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多