Diff of /weighted.statsOLD.R [000000] .. [81de4e]

Switch to unified view

a b/weighted.statsOLD.R
1
weighted.stats =
2
  function (x, w, c) 
3
  {
4
    n = ncol(x) #number of samples
5
    p = nrow(x) #number of genes
6
    k = length(unique(c)) #number of class
7
    c = as.integer(c)
8
    WM = WS = matrix(0, p, k)
9
    rownames(WS) = rownames(x)
10
    colnames(WS) = unique(c)
11
    c0 <- as.integer(min(c, na.rm = TRUE) - 1)
12
    c <- as.integer(c) - c0
13
    mk = NULL
14
    
15
    w.mean00 =
16
      function (x, w) 
17
      {
18
        wm = NULL
19
        
20
        for (i in 1:p)
21
        {
22
          wm0 = sum(w[i,]*x[i,]) / sum(w[i,])
23
          wm = c(wm, wm0)
24
        }
25
        return(wm)
26
      }
27
    
28
    w.mean =
29
      function (x, w, c) 
30
      {
31
        for (j in 1:k)
32
        {
33
          WM[,j] = w.mean00(x[,c == j], w[,c == j])
34
        }
35
        return(WM)
36
      }
37
    
38
    w.sd =
39
      function (x, w, c) 
40
      {
41
        w.sd00 =
42
          function (x, w) 
43
          {
44
            ws = NULL
45
            
46
            w.sd0 =
47
              function (x, w)
48
              {
49
                sumw = sum(w)
50
                sumw.sq = sum(w)^2
51
                w.sq = sum(w^2)
52
                denom = sum(w * ((x - mean(x))^2))
53
                sqrt((sumw * denom) / (sumw.sq - w.sq))
54
              }
55
            
56
            for (i in 1:p)
57
            {
58
              ws0 = w.sd0(x[i,], w[i,])
59
              ws = c(ws, ws0)
60
            }
61
            
62
            return(ws)
63
          }
64
        
65
        for (j in 1:k)
66
        {
67
          WS[,j] = w.sd00(x[,c == j], w[,c == j])
68
        }
69
        return(WS)
70
      }
71
    
72
    WMEAN = w.mean00(x, w) #Overall weighted mean
73
    delta = WMEAN.G = w.mean(x, w, c) #Weighted means for each group 
74
    WSD.G = w.sd(x, w, c) #Weighted standard deviations for each group
75
    WSD.POOLED = WSD.G
76
    
77
    for (i in 1:k)
78
    {
79
      WSD.POOLED[,i] = (table(c)[i]-1) * (WSD.POOLED[,i]^2)
80
      mk[i] = sqrt((1 / table(c)[i]) + (1 / n))
81
    }
82
    
83
    WSD.POOLED = sqrt(rowSums(as.data.frame(WSD.POOLED)) / (n - k))
84
    s0 = median(WSD.POOLED)
85
    
86
    for (i in 1:k)
87
    {
88
      delta[,i] = (delta[,i] - WMEAN) / (mk[i]*(WSD.POOLED + s0))
89
    }
90
    
91
    rownames(WMEAN) = rownames(WMEAN.G) = rownames(delta) = rownames(WSD.G) = rownames(WSD.POOLED) = rownames(x)
92
    colnames(WMEAN.G) = colnames(delta) = colnames(WSD.G) = unique(c)
93
    
94
    stats = list(n = n, p = p, k = k, mk = mk, s0 = s0, delta = delta, WMEAN = WMEAN, WMEAN.G = WMEAN.G, WSD.G = WSD.G, WSD.POOLED = WSD.POOLED)
95
    
96
    return(stats)
97
  }