R-help to Exercise 10.2 i BSS

 

 

# The following function may be used to calculate the ?means, variances, 
# coefficients of dispersion, Pearson X2 with corresponding P-value from the three datasets. 
# The function also calculates (an extension of) tables like those on page 81 in BS.
 
# Copy the commands below and paste them into R:
poissontab<-function(counts,x=(0:(length(counts)-1)))
{
? n<-sum(counts)
? meanx<-sum(x*counts)/n
? varx<-sum((x-meanx)^2*counts)/n
? CD<-varx/meanx
? df<-length(counts)-2
? px<-c(dpois(0:df,meanx),1-ppois(df,meanx))
? EX<-n*px
? tab<-cbind(x,counts,round(EX,2),round(counts-EX,2),round((counts-EX)^2/EX,2))
? X2<-sum((counts-EX)^2/EX)
? pval<-1-pchisq(X2,df)
? print(paste("Mean=",meanx))
? print(paste("Var= ",varx))
? print(paste("CD=? ",CD)) 
??tab<-as.data.frame(tab)
? names(tab)<-c("x","Obs","Exp","O-E","(O-E)^2/E")
? print(tab)
? print(paste("Pearson X2 =",round(X2,2)))
? print(paste("p-value??? =",round(pval,4)))
}
 
 
# In order to apply the function to the first data set in the exercise, you give the commands:
yeast<-c(75,103,121,54,30,13,4)
poissontab(yeast)

 

 

#Try to understand how the function produces the output given.
# Make sure you understand the output, and use the output to answer the questions in the exercise.
 
 
# Repeat the analysis for the two other data sets in the exercise.