Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chi-squared goodness of fit test in R

I have a vector of observed values and also a vector of values calculated with model:

actual <- c(1411,439,214,100,62,38,29,64)
expected <- c(1425.3,399.5,201.6,116.9,72.2,46.3,30.4,64.8)

Now I'm using the Chi-squared goodness of fit test to see how well my model performs. I wrote the following:

chisq.test(expected,actual) 

but it doesn't work. Can you help me with this?

like image 437
AliCivil Avatar asked Jul 10 '12 07:07

AliCivil


People also ask

What is chi-square goodness-of-fit test R?

What is chi-square goodness of fit test? The chi-square goodness of fit test is used to compare the observed distribution to an expected distribution, in a situation where we have two or more categories in a discrete data. In other words, it compares multiple observed proportions to expected probabilities.

Can you do a chi-square test in R?

In R, the function used for performing a chi-square test is chisq. test() . Parameters: data: data is a table containing count values of the variables in the table.


1 Answers

X^2 = 10.2 at 7 degrees of freedom will give you a p ~ 0.18 .

> 1-pchisq(10.2, df = 7)
[1] 0.1775201

You should pass on the expected values under argument p. Make sure you scale your values to sum to 1.

> chisq.test(actual, p = expected/sum(expected))

    Chi-squared test for given probabilities

data:  actual 
X-squared = 10.2581, df = 7, p-value = 0.1744

This about what X^2 test is doing. You give the function a model (expected) and ask - how likely it is that my observed data came from a population that "generated" expected?

like image 194
Roman Luštrik Avatar answered Nov 04 '22 07:11

Roman Luštrik