File: epi.empbayes.R

package info (click to toggle)
r-cran-epir 2.0.80%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,332 kB
  • sloc: makefile: 5
file content (29 lines) | stat: -rw-r--r-- 837 bytes parent folder | download | duplicates (6)
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
"epi.empbayes" <- function(obs, pop){
	# gamma: mean of rate
  # phi: variance of rate
        
  gamma <- (sum(obs)) / (sum(pop))
  rate <- obs / pop
  sum.pop <- sum(pop)
        
  phi.left <- sum(pop * (rate - gamma)^2) / sum.pop
  phi.right <- gamma / mean(pop)
  phi <- phi.left - phi.right
        
  # The convention is that phi = 0 whenever the above expression is negative.
  phi <- ifelse(phi < 0, 0, phi)
  emp <- ((phi * (rate - gamma)) / (phi + (gamma / pop))) + gamma
        
  # gamma = nu / alpha
  # phi = nu / alpha^2
        
  alpha <- gamma / phi
  nu <- gamma^2 / phi
  inv.nu <- 1 / nu
                
  rval <- data.frame(gamma, phi, alpha, nu, inv.nu)
  names(rval) <- c("gamma (mean)", "phi (variance)", "alpha (shape)", "nu (scale)", "inv.nu (rate)")
  unlist(rval)
}