#----------------------------------------------------------------------------- # #----------------------------------------------------------------------------- SERE_fun <- function(observed, laneTotals, TH=1) { #calculate lambda and expected values total <- sum(laneTotals) fullObserved <- observed[rowSums(observed)>TH,]; fullLambda <- rowSums(fullObserved)/total; fullLhat <- fullLambda > 0; fullExpected<- outer(fullLambda, laneTotals); #keep values fullKeep <- which(rowSums(fullExpected) > 0); #calculate degrees of freedom (nrow*(ncol -1) >> number of parameters - number calculated parameter (lamda is calculated >> thus minus 1) #calculate pearson and deviance for all values oeFull <- rowSums((fullObserved[fullKeep,] - fullExpected[fullKeep,])^2/ fullExpected[fullKeep,]) # test for over dispersion dfFull <- ncol(observed)*length(fullKeep) - sum(fullLhat!=0); devFull <- rowSums( fullObserved[fullKeep,] * log(ifelse(fullObserved[fullKeep,]==0,1,fullObserved[fullKeep,])/fullExpected[fullKeep,])) return(sqrt(sum(oeFull)/dfFull)); } #----------------------------------------------------------------------------- #-----------------------------------------------------------------------------