File: deff.s

package info (click to toggle)
hmisc 5.2-4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,044 kB
  • sloc: asm: 28,905; f90: 590; ansic: 415; xml: 160; fortran: 75; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 628 bytes parent folder | download | duplicates (8)
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
deff <- function(y, cluster)
{
  ss <- function(x)
  {
    n <- length(x)
    xbar <- sum(x) / n
    sum((x - xbar)^2)
  }

  if(!is.factor(cluster)) cluster <- as.factor(cluster)
  
  cluster <- unclass(cluster)
  s <- !is.na(cluster + y)
  y <- y[s]
  cluster <- as.integer(cluster[s])
  n <- length(y)
  sst <- ss(y)
  sses <- tapply(y,cluster,ss)
  k  <- length(sses)
  R2 <- 1 - sum(sses) / sst
  Fstat  <- R2 * (n - k) / (1 - R2) / k
  g  <- (Fstat - 1.) * k / n
  rho <- if(R2 == 1.) 1. else g / (1. + g)
  ng <- table(cluster)
  B  <- sum(ng^2) / n
  deff <- 1 + (B - 1) * rho
  c(n=n, clusters=k, rho=rho, deff=deff)
}