卡方输出作为向量

Output of chi-square as vector

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

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