vendredi 30 octobre 2015

Distribution generic test function

I try to create a generic test function for distribution error for binomial negative, Poisson and normal. But my function doesn't work 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,

ASANTOS

Aucun commentaire:

Enregistrer un commentaire