Skip to content

Generic test function for distribution errors

2 messages · ASANTOS, microbiomics

#
Dear Members,

I try to create a generic test function for distribution error for 
binomial negative, Poisson and normal. But my function doesn't work 
(NA's produced and don't realize the test) and I don't know why, any 
member can help me? My function is:



require(MASS)

BD<-rpois(1000,10) ### Data set for test

Dist<-c("negative binomial","normal","Poisson")

for(typeD in 1:Dist){
k <- fitdistr(BD,typeD[Dist])
par <- k$estimate
size <- par[1]#Parameter
mu <- par[2]#Mean
SD<-sd(BD)
N <- length(BD)

if (typeD[Dist]=='negative binomial'){       ### For binomial negativa
est <-N*dnbinom(BD,size=size,mu=mu)  ## Estimates
fecdf <- ecdf(BD) ###ecdf- Empiric cumulative function
knotsX <- knots(fecdf)
emp <- fecdf(c(knotsX,Inf))  # Empiric
chisq.test(table(emp),table(est),correct=TRUE) ##Chi test
}
if (typeD[Dist]=='normal'){          ### For normal
est <-N*dnorm(BD,mean=mu, sd=SD)  ## Estimates
fecdf <- ecdf(BD) ###ecdf- Empiric cumulative function
knotsX <- knots(fecdf)
emp <- fecdf(c(knotsX,Inf))  # Empiric
chisq.test(table(emp),table(est),correct=TRUE) ##Chi test

}
  if (typeD[Dist]=='Poisson'){        ### For Poisson
est <-N*dpois(BD,lambda=mu)  ## Estimate
fecdf <- ecdf(BD) ###cdf- Empiric cumulative function
knotsX <- knots(fecdf)
emp <- fecdf(c(knotsX,Inf))  # Empiric
chisq.test(table(emp),table(est),correct=TRUE) ## Chi test
}
}
#


Thanks,
#
Dear Alexandre,
Your code should probably read "for(typeD in 1:length(Dist))"
or "for(typeD in seq_along(Dist)".

Then, instead of "typeD[Dist]", make it "Dist[typeD]".


Please see if that helps.


Best,
microbiomics