-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfit.param.fj.geom.R
34 lines (22 loc) · 963 Bytes
/
fit.param.fj.geom.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
.fit.param.fj.geom <- function(counting, j, kmax, cens.beg) {
# Estimation of the parameters of the distribution (No censoring case)
theta0 <- sum(counting$Njk[j, ]) / sum(1:kmax * counting$Njk[j, ])
if (cens.beg) {# Censoring at the beginning
logLik <- function(par) {
mask <- counting$Njk[j, ] != 0
kmask <- (0:(kmax - 1))[mask]
fk <- rep.int(x = 0, times = kmax)
fk[mask] <- dgeom(x = kmask, prob = par, log = TRUE)
mask <- counting$Nbjk[j, ] != 0
kmask <- (0:(kmax - 1))[mask]
Fk <- rep.int(x = 0, times = kmax)
Fk[mask] <- pgeom(q = kmask, prob = par, lower.tail = FALSE, log.p = TRUE)
return(-(sum(counting$Njk[j, ] * fk) + sum(counting$Nbjk[j, ] * Fk)))
}
mle <- optim(par = theta0, logLik, method = "Brent", lower = 0, upper = 1)
theta <- mle$par
} else {# No censoring
theta <- theta0
}
return(c(theta, NA))
}