library(vcd)
## Warning: package 'vcd' was built under R version 3.4.4
## Loading required package: grid
data("WomenQueue",package = "vcd")
str(WomenQueue)
##  table [1:11(1d)] 1 3 4 23 25 19 18 5 1 1 ...
##  - attr(*, "dimnames")=List of 1
##   ..$ nWomen: chr [1:11] "0" "1" "2" "3" ...

Use the data set WomenQueue to:

Produce plots analogous to those shown below

barplot(WomenQueue, main="",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?

good_fit <- goodfit(WomenQueue, type="binomial")
## Warning in goodfit(WomenQueue, type = "binomial"): size was not given,
## taken as maximum count
summary(good_fit)
## 
##   Goodness-of-fit test for binomial distribution
## 
##                       X^2 df  P(> X^2)
## Likelihood Ratio 8.650999  8 0.3725869
plot(good_fit)

Now draw the binomial distribution, which is fitted on the hanging rootogram

plot(good_fit, xlab="Counts",main = 'Rootogram')

Exercise 3.5

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().

count <- 0:5
Freq <- c(129, 83, 20, 9, 5, 1)
x<-"count"
y<-"Freq"
df <-data.frame(count,Freq)
names(df) <- c(x,y)
df.tab <- xtabs(Freq ~ count, df)
head(df.tab)
## 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.

PoisModel<- goodfit(df.tab, type = "poisson")
plot(PoisModel, type = "standing", xlab="count", main = "Poisson Model")

plot(PoisModel, xlab="count", main = "Rootogram")

Fit and plot the negative binomial model for these frequencies and plot the rootogram and fitted model.

PoisModel <- goodfit(df.tab, type = "nbinomial")
plot(PoisModel, type = "standing", xlab="count", main = "Negative Binomial Model")

plot(PoisModel, xlab="count", main = "Rootogram of Negative Binomial Model")

What do you conclude?

There are lot of deviations from the actual value