[R] How can I save the result for goodness of fit test

Michael Friendly friendly at yorku.ca
Wed Mar 17 14:22:45 CET 2010


pinusan wrote:
> Dear All,
> I run the goodness of fit test using goodfit() in vcd package.
> The result is as follow: 
> 
>         Goodness-of-fit test for poisson distribution
> 
>              X^2 df  P(> X^2)
> Pearson 1.053348  2 0.5905661
> Warning message:
> In summary.goodfit(gf) : Chi-squared approximation may be incorrect
> 
> I want to save the the test statistics(X^2), df, and p-value. How can I save
> the result. Actually, I want to make a table.

The usual general answer to this question is to read the documentation, 
and look for the Value section.  ?goodfit gives

Value
A list of class "goodfit" with elements:

observed observed frequencies.
count    corresponding counts.
fitted   expected frequencies (fitted by ML).
type     a character string indicating the distribution fitted.
method   a character string indicating the fitting method (can be either 
  "ML", "MinChisq" or "fixed" if the parameters were specified).
df       degrees of freedom.
par      a named list of the (estimated) distribution parameters.

So, the quantities you want are not returned in the resulting object. 
But, they *are* returned from the summary method that you used to print 
the result. [That isn't documented, so perhaps that could be added.]

So, the 2nd general answer to your question is to use str() on the
result of the summary() function.

 > ## Simulated data examples:
 > dummy <- rnbinom(200, size = 1.5, prob = 0.8)
 > gf <- goodfit(dummy, type = "nbinomial", method = "MinChisq")
 > summary(gf)

          Goodness-of-fit test for nbinomial distribution

               X^2 df  P(> X^2)
Pearson 0.5865029  1 0.4437746
Warning message:
In summary.goodfit(gf) : Chi-squared approximation may be incorrect
 > result <- summary(gf)

          Goodness-of-fit test for nbinomial distribution

               X^2 df  P(> X^2)
Pearson 0.5865029  1 0.4437746
Warning message:
In summary.goodfit(gf) : Chi-squared approximation may be incorrect
 > str(result)
  num [1, 1:3] 0.587 1 0.444
  - attr(*, "dimnames")=List of 2
   ..$ : chr "Pearson"
   ..$ : chr [1:3] "X^2" "df" "P(> X^2)"
 >
 > result
               X^2 df  P(> X^2)
Pearson 0.5865029  1 0.4437746
 >
So, there you go!


> In addition, there is warning message "In summary.goodfit(gf) : Chi-squared
> approximation may be incorrect".
> How can I interpret this result. 
> 
Read the Details section
> Have a nice day.
> 


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list