1- #    Copyright (C) 2016 University of Southern California and
2- #  Chao Deng and Andrew D. Smith and Timothy Daley
1+ #  Copyright (C) 2016-2022  University of Southern California and
2+ #    Chao Deng and Andrew D. Smith and Timothy Daley
33# 
4- #    Authors: Chao Deng
4+ #  Authors: Chao Deng
55# 
6- #    This program is free software: you can redistribute it and/or modify
7- #    it under the terms of the GNU General Public License as published by
8- #    the Free Software Foundation, either version 3 of the License, or
9- #    (at your option) any later version.
6+ #  This program is free software: you can redistribute it and/or modify
7+ #  it under the terms of the GNU General Public License as published by
8+ #  the Free Software Foundation, either version 3 of the License, or
9+ #  (at your option) any later version.
1010# 
11- #  This program is distributed in the hope that it will be useful,
12- #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13- #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14- #  GNU General Public License for more details.
15- # 
16- #  You should have received a copy of the GNU General Public License
17- #  along with this program. If not, see <http://www.gnu.org/licenses/>.
11+ #  This program is distributed in the hope that it will be useful, but
12+ #  WITHOUT ANY WARRANTY; without even the implied warranty of
13+ #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ #  General Public License for more details.
1815# 
16+ #  You should have received a copy of the GNU General Public License
17+ #  along with this program. If not, see <http://www.gnu.org/licenses/>.
1918
2019# # Zero Truncated Poisson (ZTP) estimator
21- # # Cohen, A. Clifford. "Estimating the parameter in a conditional Poisson  
20+ # # Cohen, A. Clifford. "Estimating the parameter in a conditional Poisson
2221# # distribution." Biometrics 16, no. 2 (1960): 203-211.
23- ztp.rSAC  <-  function (n , r = 1 )  
22+ ztp.rSAC  <-  function (n , r = 1 )
2423{
2524 # # mle based on zero-truncated Poisson
2625 n [, 2 ] <-  as.numeric(n [, 2 ])
@@ -31,22 +30,22 @@ ztp.rSAC <- function(n, r=1)
3130 lambda  <-  result $ root 
3231 L  <-  sum(n [, 2 ]) /  (1  -  ppois(0 , lambda ))
3332
34-  # # estimator  
33+  # # estimator
3534 function (x ) {
3635 L  *  ppois(q = r  -  1 , lambda = lambda  *  x , lower.tail = FALSE )}
3736}
3837
3938
4039# # BBC estimator
4140# # Boneh, Shahar, Arnon Boneh, and Richard J. Caron. "Estimating the prediction
42- # # function and the number of unseen species in sampling with replacement."  
41+ # # function and the number of unseen species in sampling with replacement."
4342# # Journal of the American Statistical Association 93, no. 441 (1998): 372-379.
4443bbc.rSAC  <-  function (n , r = 1 ) {
4544 n [, 2 ] <-  as.numeric(n [, 2 ])
4645 S  <-  sum(n [, 2 ])
4746 # # BBC estimator without bias correction
4847 tmp  <-  function (t ) {sapply(t , function (x ) {
49-    n [, 2 ] %*%  (exp(- n [, 1 ]) -  ppois(r - 1 , n [, 1 ] *  x )) +  S })}
48+  n [, 2 ] %*%  (exp(- n [, 1 ]) -  ppois(r - 1 , n [, 1 ] *  x )) +  S })}
5049 # # bias correction
5150 index.f1  <-  which(n [, 1 ] ==  1 )
5251 f1  <-  n [index.f1 , 2 ]
@@ -66,8 +65,8 @@ bbc.rSAC <- function(n, r=1) {
6665
6766
6867# # CS estimator
69- # # Chao, Anne, and Tsung-Jen Shen. "Nonparametric prediction in species  
70- # # sampling." Journal of agricultural, biological, and environmental  
68+ # # Chao, Anne, and Tsung-Jen Shen. "Nonparametric prediction in species
69+ # # sampling." Journal of agricultural, biological, and environmental
7170# # statistics 9, no. 3 (2004): 253-269.
7271cs.rSAC  <-  function (n , r = 1 , k = 10 ) {
7372 n [, 2 ] <-  as.numeric(n [, 2 ])
@@ -82,9 +81,9 @@ cs.rSAC <- function(n, r=1, k=10) {
8281 S.rare  <-  sum(n [index.rare , 2 ])
8382 C.rare  <-  1  -  f1  /  (n [index.rare , 1 ] %*%  n [index.rare , 2 ])
8483 # # estimate parameters
85-  gamma.rare  <-  max(S.rare  /  C.rare  *   
84+  gamma.rare  <-  max(S.rare  /  C.rare  * 
8685 ((n [index.rare , 1 ] *  (n [index.rare , 1 ] -  1 )) %*%  n [index.rare , 2 ]) / 
87-  (n [index.rare , 1 ] %*%  n [index.rare , 2 ])^ 2  -  1 , 0 )
86+    (n [index.rare , 1 ] %*%  n [index.rare , 2 ])^ 2  -  1 , 0 )
8887 f0  <-  S.rare  /  C.rare  +  f1  /  C.rare  *  gamma.rare  -  S.rare 
8988 # # estimator
9089 #  consistent type for estimator arithmetic to avoid warnings
@@ -104,7 +103,7 @@ fisher.alpha <- function(n) {
104103 n [, 2 ] <-  as.numeric(n [, 2 ])
105104 N  <-  n [, 1 ] %*%  n [, 2 ]
106105 S  <-  sum(n [, 2 ])
107-  result  <-  uniroot(function (x ) (exp(x ) -  1 ) /  x  -  N  /  S , c(0.001 , 1e9 ),  
106+  result  <-  uniroot(function (x ) (exp(x ) -  1 ) /  x  -  N  /  S , c(0.001 , 1e9 ),
108107 tol = 0.0001 , extendInt = " upX"  )
109108 alpha  <-  S  /  result $ root 
110109 return (alpha )
@@ -115,20 +114,22 @@ fisher.rSAC <- function(n, r=1) {
115114 n [, 2 ] <-  as.numeric(n [, 2 ])
116115 alpha  <-  fisher.alpha(n )
117116 N  <-  n [, 1 ] %*%  n [, 2 ]
118-  f.rSAC  <-  function (t ) {sapply(t , function (x ) alpha  * 
119-  integrate(function (z ) (z ^ (r - 1 ) /  (1  -  z )), lower = 0 , 
120-  upper = N * x  /  (N * x  +  alpha ))$ value )}
117+  f.rSAC  <-  function (t ) {
118+  sapply(t , function (x ) alpha  * 
119+  integrate(function (z ) (z ^ (r - 1 ) /  (1  -  z )), lower = 0 ,
120+  upper = N * x  /  (N * x  +  alpha ))$ value )
121+  }
121122 return (f.rSAC )
122123}
123124
124125
125126# # fit a Poisson distribution
126127pois.rSAC  <-  function (n , L , r = 1 ) {
127-    lambda  <-  n [, 1 ] %*%  n [, 2 ] /  L 
128-    f.rSAC  <-  function (t ) {
129-    L  *  ppois(q = r  -  1 , lambda = lambda  *  t , lower.tail = FALSE )
130-    }
131-    return (f.rSAC )
128+  lambda  <-  n [, 1 ] %*%  n [, 2 ] /  L 
129+  f.rSAC  <-  function (t ) {
130+  L  *  ppois(q = r  -  1 , lambda = lambda  *  t , lower.tail = FALSE )
131+  }
132+  return (f.rSAC )
132133}
133134
134135# # estimate the parameter for negative binomial distribuiton
@@ -145,14 +146,14 @@ nb.fitting <- function(n, L, size=SIZE.INIT)
145146
146147 # # target function f
147148 f  <-  function (x ) {
148-    return ( - nb.loglikelihood(n , zero.counts , size  =  x , mu  =  m )/ L  )
149+  return ( - nb.loglikelihood(n , zero.counts , size  =  x , mu  =  m )/ L  )
149150 }
150151
151152 # # derivative of f
152153 gr  <-  function (x )
153154 {
154155 first.term  <-  ( digamma(x ) *  zero.counts  + 
155-  digamma(n [, 1 ] +  x ) %*%  n [, 2 ] )/ L 
156+    digamma(n [, 1 ] +  x ) %*%  n [, 2 ] )/ L 
156157 second.term  <-  digamma(x )
157158 third.term  <-  log(x ) -  log(x  +  m )
158159 result  <-  first.term  -  second.term  +  third.term 
@@ -163,10 +164,10 @@ nb.fitting <- function(n, L, size=SIZE.INIT)
163164 # # optimization
164165 if  (v  >  m ) {
165166 res  <-  optim(m ^ 2  /  (v  -  m ), f , gr , method  =  " L-BFGS-B"  ,
166-  lower  =  0.00001 , upper  =  100000 )
167+    lower  =  0.00001 , upper  =  100000 )
167168 } else  {
168169 res  <-  optim(size , f , gr , method  =  " L-BFGS-B"  ,
169-  lower  =  0.00001 , upper  =  100000 )
170+    lower  =  0.00001 , upper  =  100000 )
170171 }
171172
172173 loglikelihood  <-  nb.loglikelihood(n , zero.counts , size = res $ par , mu = m )
0 commit comments