Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

I have a data set with following structure:

a word | number of occurrence of a word in a document | a document id 

How can I perform a test for normal distribution in R? Probably it is an easy question but I am a R newbie.

share|improve this question
3  
@Skarab Maybe I'm totally off, but wouldn't you expect that the frequency of any word will be inversely proportional to its rank in the frequency table of words, according to Zipf's law (j.mp/9er2lv)? In this case, check out the zipfR package. –  chl Sep 28 '10 at 9:29
1  
I agree with @chl - it would be minor miracle if your data was normally distributed. Perhaps another question about what you want to do with the data would be worthwhile. Don't reinvent the wheel! –  csgillespie Sep 28 '10 at 9:32
2  
How could your data be distributed according to a model that gives non zero probability to negative occurrence? –  user603 Sep 28 '10 at 15:05
1  
What is the reason for doing this test? –  whuber Sep 28 '10 at 15:12
    
I want to estimate if the huge result of the Information Extraction is correct. I want to check if the distribution of the entities found in the text follows my expectations (I know the domain and the text corpus). –  Skarab Sep 29 '10 at 9:06

5 Answers 5

up vote 18 down vote accepted

If I understand your question correctly, then to test if word occurrences in a set of documents follows a Normal distribution you can just use a shapiro-Wilk test and some qqplots. For example,

## Generate two data sets
## First Normal, second from a t-distribution
words1 = rnorm(100); words2 = rt(100, df=3)

## Have a look at the densities
plot(density(words1));plot(density(words2))

## Perform the test
shapiro.test(words1); shapiro.test(words2)

## Plot using a qqplot
qqnorm(words1);qqline(words1, col = 2)
qqnorm(words2);qqline(words2, col = 2)

The qqplot commands give: alt text

You can see that the second data set is clearly not Normal.

share|improve this answer

Assuming your dataset is called words and has a counts column, you can plot the histogram to have a visualization of the distribution:

hist(words$counts, 100, col="black")

where 100 is the number of bins

You can also do a normal Q-Q plot using

qqnorm(words$counts)

Finally, you can also use the Shapiro-Wilk test for normality

shapiro.test(word$counts)

Although, look at this discussion: Normality Testing: 'Essentially Useless?'

share|improve this answer

No test will show you that your data has a normal distribution - it will only be able to show you when the data is sufficiently inconsistent with a normal that you would reject the null.

But counts are not normal in any case, they're positive integers - what's the probability that an observation from a normal distribution will take a value that isn't an integer?

Why would you test for normality in this case?

share|improve this answer

A more formal way of looking at the normality is by testing whether the kurtosis and skewness are significantly different from zero.

To do this, we need to get:

kurtosis.test <- function (x) {
m4 <- sum((x-mean(x))^4)/length(x)
s4 <- var(x)^2
kurt <- (m4/s4) - 3
sek <- sqrt(24/length(x))
totest <- kurt/sek
pvalue <- pt(totest,(length(x)-1))
pvalue 
}

for kurtosis, and:

skew.test <- function (x) {
m3 <- sum((x-mean(x))^3)/length(x)
s3 <- sqrt(var(x))^3
skew <- m3/s3
ses <- sqrt(6/length(x))
totest <- skew/ses
pt(totest,(length(x)-1))
pval <- pt(totest,(length(x)-1))
pval
}

for Skewness.

Both these tests are one-tailed, so you'll need to multiply the p-value by 2 to become two-tailed. If your p-value become larger than one you'll need to use 1-kurtosis.test() instead of kurtosis.test.

If you have any other questions you can email me at j.bredman@gmail.com

share|improve this answer
    
What are the differences, of the above two functions, regarding the kurtosis() and skewness() functions from the moments package? Results using rnorm() samples are different. –  Nikos Alexandris Sep 16 at 11:18

In addition to the Shapiro-Wilk test of the stats package, the nortest package (available on CRAN) provides other normality tests.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.