0

I'm trying to write a piece of software in R that finds the most fitting distribution family to a set of data by performing the chi-squared test on the data (with regards to said family) and finding the best chi-squared value.

However, when using the goodfit function, seemingly the only way to retrieve the chi-squared statistic is by running the function and using the summary(gf) command. This only results in a human-readable output, and I need something that I can draw in the form of gf$chisqvalue so that I can compare it to the results of the other tests I'm running. Is there any way to retrieve this statistic as a variable?

Martin
  • 277
  • 2
  • 5
  • 17
  • 1
    Could you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? it seems that is just a matter of subsetting but without a piece of code nor functions you have used is very hard to read your mind, trying to guess, and suggest packages and/or ways of doing something that might be even useless for you. – SabDeM Aug 08 '15 at 21:17
  • Hi SabDem. Thanks for the suggestion; my code is a bit messy at the moment, hence me not posting my example straight off the bat. Luckily AntoniosK's solution works perfectly, and I'd recommend it for anyone in a similar situation! – Martin Aug 10 '15 at 10:37

1 Answers1

3

Try the broom package and the command "tidy" like this:

library(vcd)
library(broom)

x <- rnbinom(200, size = 3, prob = 0.2)
res <- goodfit(x, type = "nbinomial", method = "MinChisq")
summary(res)

dt_res = tidy(summary(res))

This will get the human-readable info and save it in a data.frame. You might prefer to change the column names after (or maybe not). The broom package is great if you want to create a data.frame from a statistical test or model output.

AntoniosK
  • 15,991
  • 2
  • 19
  • 32
  • Going to try this out when I get home and see if it works. Thanks so much! – Martin Aug 08 '15 at 20:46
  • You're welcome. You'll find that package very useful if you deal with this kind of situations all the time. Here's the link : https://cran.r-project.org/web/packages/broom/vignettes/broom.html – AntoniosK Aug 08 '15 at 20:50
  • After a small problem (I was running R 3.0.2, which doesn't support broom) I managed to get it installed and tried it out; it works brilliantly! One more question; is there a way to run tidy(summary(res)) without R actually giving output? In other words, have tidy run and store the results in a dataframe without actually showing the output of summary? And yes, this is definitely a package I see myself using further on in the project. Thanks again! – Martin Aug 10 '15 at 10:34