Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何拟合R中计数数据的负二项分布_R_Optimization - Fatal编程技术网

如何拟合R中计数数据的负二项分布

如何拟合R中计数数据的负二项分布,r,optimization,R,Optimization,我想在数据上拟合负二项函数 在不同体积(v)中测量的病原体计数(x) 水样。我写了一些代码,但它们不能正常工作。 谢谢你的帮助 x<-c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,

我想在数据上拟合负二项函数 在不同体积(v)中测量的病原体计数(x) 水样。我写了一些代码,但它们不能正常工作。 谢谢你的帮助

x<-c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L)

v<-c(48, 51, 52, 54.9, 55, 55, 55, 57, 59, 59, 85.2, 100, 100, 100, 
100, 100, 100, 100, 100.4, 100.4, 100.6, 100.7, 101.7, 102, 102, 
102.2, 102.2, 103.3, 185.4, 189.3, 189.3, 190, 191.4, 18.4, 74.1, 
99.9, 100, 100, 100, 100, 100, 101.1, 101.3, 183.5, 193, 95.8, 
223.7, 223.7, 227.1, 89.9, 98.4, 100)

#This is Negative binomial function:  
#PNB(x)= (gamma(x+k)/(gamma(k)*factorial(x)))*
#        ((mu*V/(k+mu*V))^x)*
#        ((k+mu*V)/k)^(-k)

#k and mu are the parameters to be optimized.

data<-data.frame(x,v)

NB<-function(par,data){
A<-gamma(x+par[1])/(gamma(par[1])*factorial(x))
B<-(par[2]*v/(par[1]+par[2]*v))^par[1]
C<-((par[1]+par[2]*v)/par[1])^(-par[1])
return(A*B*C)
}

best<-optim(par=c(1,1),data=data,NB,gr=NULL,method='BFGS',control=list(trace=12,REPORT=1))
我还使用了FitDisr,如下所示,但它只适用于x,而不考虑样本体积(v)


此mu独立于v,我认为不正确。

库(MASS)
具有用于此目的的
FitDisr
函数(使用最大似然法)。您可能希望使用MASS软件包中的glm.nb。您的优化函数似乎编写不正确。尝试
NB(c(1,1),data)
-它不返回标量。
Error in optim(par = c(1, 1), data = data, NB, gr = NULL, method = "BFGS",  : 
  objective function in optim evaluates to length 52 not 1
> fitdistr (x,"negative binomial")
     size         mu    
  1.3078442   0.5577050 
 (1.1107484) (0.1236888)