Re: [R] calculating goodness-of-fit statistics

From: Pontarelli, Brett <bpontar_at_amazon.com>
Date: Wed 01 Feb 2006 - 13:11:26 EST


The trouble is log(0/anything) = log(0) = NaN. If you want them to evaulate to zero you might try zeroing out the values you know will be NaN:

X2 = 2*sum(observed*log(observed/expected)); X2[observed==0] = 0;

--Brett  

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Taka Matzmoto Sent: Tuesday, January 31, 2006 6:03 PM
To: r-help@stat.math.ethz.ch
Subject: [R] calculating goodness-of-fit statistics

Hi R users

I have a simple data for calculating goodness-of-fit statistics (e.g., X2 by Pearson, G2 by Wilks)

#################################################
observed<-c(424,174,0,402)
expected<-c(282.7174, 314.2972, 142.3142, 260.6712) 2*sum(observed*log(observed/expected)) # for X2
sum((observed-expected)^2/expected)  # for G2 #################################################

(note. expected ones were calculating by a model I used, not by marginal of observed ones.)

The third element of the observed vector is zero.

For third element, 0 * log(0/142.3142) is NaN. That is why I got NaN for G2.

I think 0 multiplied by anything should be zero. Am I wrong ?

Is there any R functions to correct zero cells for calculating G2? If there is, I like to know some

references justifying the correction.

Thank you in advance

TM



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Feb 01 13:21:32 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:16 EST