I use the chi-square goodness of fit test with chisq.test()
. Is there a way to save the output of this function as a vector, like:
test <- c(X-squared, df, p-value)
?
Asked
Active
Viewed 57 times
1

Vinícius Félix
- 8,448
- 6
- 16
- 32

Mikhail
- 37
- 5
1 Answers
0
The output of chisq.test
is a list
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"
We can create a vector with some elements of the list
out <- c(test_chi$statistic,test_chi$parameter,test_chi$p.value)
out
X-squared df
2.5000000 2.0000000 0.2865048
Another option is to use the library 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

Vinícius Félix
- 8,448
- 6
- 16
- 32
-
Thank you very much! That is exactly what I was looking for. – Mikhail Oct 01 '21 at 20:59
-
Glad it helped you! If possible, close this question by choosing my answer, this help the search of others users. – Vinícius Félix Oct 01 '21 at 21:03
-
Done! Thank you again. – Mikhail Oct 06 '21 at 03:55