-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfit.param.f.pois.R
74 lines (50 loc) · 2.26 KB
/
fit.param.f.pois.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
.fit.param.f.pois <- function(counting, kmax, cens.beg, cens.end) {
# Estimation of the parameters of the distribution (No censoring case)
theta0 <- sum(0:(kmax - 1) * counting$Nk) / sum(counting$Nk)
if (!cens.beg & cens.end) {# Censoring at the end
logLik <- function(par) {
mask <- counting$Nk != 0
kmask <- (0:(kmax - 1))[mask]
fk <- rep.int(x = 0, times = kmax)
fk[mask] <- dpois(x = kmask, lambda = par, log = TRUE)
mask <- counting$Nek != 0
kmask <- (0:(kmax - 1))[mask]
Fk <- rep.int(x = 0, times = kmax)
Fk[mask] <- ppois(q = kmask, lambda = par, lower.tail = FALSE, log.p = TRUE)
return(-(sum(counting$Nk * fk) + sum(counting$Nek * Fk)))
}
mle <- optim(par = theta0, logLik, method = "Brent", lower = 0, upper = kmax - 1)
theta <- mle$par
} else if (cens.beg & !cens.end) {# Censoring at the beginning
logLik <- function(par) {
mask <- counting$Nk != 0
kmask <- (0:(kmax - 1))[mask]
fk <- rep.int(x = 0, times = kmax)
fk[mask] <- dpois(x = kmask, lambda = par, log = TRUE)
mask <- counting$Nbk != 0
kmask <- (0:(kmax - 1))[mask]
Fk <- rep.int(x = 0, times = kmax)
Fk[mask] <- ppois(q = kmask, lambda = par, lower.tail = FALSE, log.p = TRUE)
return(-(sum(counting$Nk * fk) + sum(counting$Nbk * Fk)))
}
mle <- optim(par = theta0, logLik, method = "Brent", lower = 0, upper = kmax - 1)
theta <- mle$par
} else if (cens.beg & cens.end) {# Censoring at the beginning and at the end
logLik <- function(par) {
mask <- counting$Nk != 0
kmask <- (0:(kmax - 1))[mask]
fk <- rep.int(x = 0, times = kmax)
fk[mask] <- dpois(x = kmask, lambda = par, log = TRUE)
mask <- counting$Nebk != 0
kmask <- (0:(kmax - 1))[mask]
Fk <- rep.int(x = 0, times = kmax)
Fk[mask] <- ppois(q = kmask, lambda = par, lower.tail = FALSE, log.p = TRUE)
return(-(sum(counting$Nk * fk) + sum(counting$Nebk * Fk)))
}
mle <- optim(par = theta0, logLik, method = "Brent", lower = 0, upper = kmax - 1)
theta <- mle$par
} else {# No censoring
theta <- theta0
}
return(c(theta, NA))
}