library(vcd)
library(vcdExtra)Use the data set WomenQueue to:
Produce a histogram plot for number of women in queues of 10.
data("WomenQueue")
barplot(WomenQueue, xlab="Number of Women in queues of 10", ylab="Frequency")Check for goodness-of-fit to the binomial distribution using the goodfit(). What do you think about these numbers?
fit <- goodfit(WomenQueue, type = "binomial", par = list(size = 10))
summary(fit)##
## Goodness-of-fit test for binomial distribution
##
## X^2 df P(> X^2)
## Likelihood Ratio 8.650999 8 0.3725869
plot(fit, type = "standing", main = "Binomial Model")The p-value is large enough for us to be unable to reject the null hypothesis that the distribution follows a binomial distribution. However, based on the plot, there do seem to be some deviations.
Now draw the binomial distribution, which is fitted on the hanging rootogram.
plot(fit, type = "hanging", shade = TRUE, main = "Rootogram of binomial model")Mosteller and Wallace (1963) give the frequencies, nk, of counts k = 0, 1, . . . of other selected marker words in 247 blocks of text known to have been written by Alexander Hamilton. The data below show the occurrences of the word upon, that Hamilton used much more than did James Madison.
count <- 0:5
Freq <- c(129, 83, 20, 9, 5, 1)Create a one-way table of frequencies of counts or a matrix or data frame with frequencies in the first column and the corresponding counts in the second column, suitable for use with goodfit().
xname <- "count"
yname <- "freq"
df <-data.frame(count,Freq)
names(df) <- c(xname,yname)
df.table <- xtabs(Freq ~ count, df)
head(df.table)## count
## 0 1 2 3 4 5
## 129 83 20 9 5 1
Fit and plot the Poisson model for these frequencies and plot the rootogram and fitted model.
Poisson.fit <- goodfit(df.table, type = "poisson")
plot(Poisson.fit, type = "standing", main = "Poisson Model")plot(Poisson.fit, type = "hanging", main = "Rootogram of Poisson Model")Fit and plot the negative binomial model for these frequencies and plot the rootogram and fitted model.
NegBinom.fit <- goodfit(df.table, type = "nbinomial")
plot(NegBinom.fit, type = "standing", main = "Negative Binomial Model")plot(NegBinom.fit, type = "hanging", main = "Rootogram of Negative Binomial Model")What do you conclude?
Based on the plots, the distribution fits both the Poisson Distribution and the Negative Binomial Distribution models pretty well, albeit with minor deviations.