我想创建一个测试二项式负概率分布的循环,但是不起作用。在我的示例中:
创建包含2个变量的人工数据集-Feature和Resp(响应变量)
#Package
library(MASS)
Features<-sort(rep(1:3,30))
Resp1<-rnbinom(60,0.75,0.10) ##60 Negative binomial values
Resp2<-rpois(30,10)## 30 Poisson values
Resp<-c(Resp1,Resp2)
d<-as.data.frame(cbind(Features,Resp))
应用基于卡方的二项式阴性检验
uniq <- unique(unlist(d$Features))
res<-NULL
for (i in 1:length(uniq)){
data_1 <- subset(d, Features == uniq[i])
k <- fitdistr(data_1$Resp,"negative binomial") #Extraction of probability parameters (size and mu)
par <- k$estimate
size <- par[1]#k parameter
mu <- par[2]#Mean
N <- length(data_1$Resp)
est <-N*dnbinom(data_1$Resp,size=size,mu=mu) ## Estimation of values
fecdf <- ecdf(data_1$Resp) ###ecdf- Cumulative empirical distribution
knotsX <- knots(fecdf)
emp <- fecdf(c(knotsX,Inf)) # Empirical distribution
testChi<-chisq.test(table(emp),table(est),correct=TRUE) #Chi square test empirical vs estimated values
result<-ifelse(testChi$p.value>0.05,"Binomial Negative","Other distribution")
res<-rbind(res,c(uniq[i],result))
colnames(res)<-c("Features","Distribution")
}
res
Features Distribution
[1,] "1" "Binomial Negative"
[2,] "2" "Other distribution"
[3,] "3" "Binomial Negative"
不起作用,因为我的前60个数字具有二项式负数(特征1和2),而后30个数字(特征3)具有泊松分布(我在创建人工数据集时对此值进行了模拟)。我的股票在哪里出问题? 预先感谢!