Diff of /Fun_Auxiliary.R [000000] .. [6ff15d]

Switch to unified view

a b/Fun_Auxiliary.R
1
# -------------------------------------------------------------------------
2
get.penalityMatrix=function(adj,X1, y1){
3
  feature.num = dim(adj)[2] 
4
  M.c = diag(0,feature.num)
5
  M.lasso = diag(0,feature.num)
6
  M.elastic = diag(1,feature.num)  
7
  M.network = Non.NormalizedLaplacianMatrix(adj)
8
  M.AdaptNet = AdaptNet.Non.NormalizedLap(adj,X1, y1)
9
  return(list(M.c=M.c,M.lasso=M.lasso,M.elastic=M.elastic,M.network=M.network,M.AdaptNet=M.AdaptNet))
10
}
11
# -------------------------------------------------------------------------
12
# Normalized Laplacian Matrix from adjacency matrix
13
laplacianMatrix = function(adj){
14
  diag(adj) <- 0
15
  deg <- apply(abs(adj),1,sum)
16
  p <- ncol(adj)
17
  L <- matrix(0,p,p)
18
  nonzero <- which(deg!=0)
19
  for (i in nonzero){
20
    for (j in nonzero){
21
      L[i,j] <- -adj[i,j]/sqrt(deg[i]*deg[j])
22
    }
23
  }
24
  diag(L) <- 1
25
  return(L)
26
}
27
28
# Non-Normalized Laplacian Matrix from adjacency matrix
29
Non.NormalizedLaplacianMatrix = function(adj){
30
  diag(adj) <- 0
31
  deg <- apply(adj,1,sum)
32
  D = diag(deg)
33
  L = D - adj
34
  return(L)
35
}
36
# ------------------------------------------------------
37
AdaptNet.penality.matrix = function(adj, X, y){
38
  
39
  library(glmnet)
40
  glmnet.fit = glmnet(X, y, lambda=0, family='binomial')
41
  Beta = coef(glmnet.fit)
42
  
43
  p <- ncol(adj)
44
  coeff.sign = sign(Beta)[2:(p+1)]
45
  
46
  diag(adj) <- 0
47
  deg <- apply(abs(adj),1,sum)
48
  L <- matrix(0,p,p)
49
  nonzero <- which(deg!=0)
50
  for (i in nonzero){
51
    for (j in nonzero){
52
      temp.sign = coeff.sign[i]*coeff.sign[j]
53
      L[i,j] <- -temp.sign*adj[i,j]/sqrt(deg[i]*deg[j])
54
    }
55
  }
56
  diag(L) <- 1
57
  return(L_star=L)
58
}
59
AdaptNet.Non.NormalizedLap = function(adj,X, y){
60
  library(glmnet)
61
  glmnet.fit = glmnet(X, y, lambda=0, family='binomial')
62
  Beta = coef(glmnet.fit)
63
  
64
  p <- ncol(adj)
65
  coeff.sign = sign(Beta)[2:(p+1)]
66
  diag(adj) <- 0
67
  deg <- apply(abs(adj),1,sum)
68
  L <- matrix(0,p,p)
69
  nonzero <- which(deg!=0)
70
  for (i in nonzero){
71
    for (j in nonzero){
72
      temp.sign = coeff.sign[i]*coeff.sign[j]
73
      L[i,j] <- -temp.sign*adj[i,j]
74
    }
75
  }
76
  diag(L) <- deg
77
  return(L_star=L)
78
}
79
# -------------------------------------------------------------------------
80
sen.spe = function(pred, truth){
81
  #----------------------------------------
82
  # 1: True sample
83
  # 0: False sample
84
  # truth = abs(sign(sim.data$w))
85
  # pred = abs(sign(out1$w[-length(tmp)]))
86
  #----------------------------------------
87
  sen = length(which(pred[which(truth==1)]==1))/length(which(truth==1))
88
  spe = length(which(pred[which(truth==0)]==0))/length(which(truth==0))
89
  result = c(sen,spe);names(result) = c("sensitivity","specificity")
90
  return(result)
91
}
92
93
# Prior network regularization
94
Prior.network = function(adj){
95
  # adj is a p x p matrix
96
  # L   is a (p+1) x (p+1) matrix
97
  L = laplacianMatrix(adj)
98
  L = rbind(L,rep(0,p))
99
  L = cbind(L,rep(0,p+1))
100
  return(L)
101
}
102
# -------------------------------------------------------------------------
103
get_sim_prior_Net = function(n,t,p11,p12){
104
  A = matrix(0,nrow=n,ncol=n) 
105
  for(i in 1:n){
106
    for(j in 1:n){
107
      if(i>j){
108
        set.seed(10*i+8*j)
109
        if(i<t&j<t){
110
          if(runif(1)<p11) A[i,j] = 1}
111
        else{
112
          if(runif(1)<p12) A[i,j] = 1}  
113
      }
114
    }
115
  }
116
  A = A +t(A)
117
  diag(A)=0
118
  return(A)
119
}
120
# -------------------------------------------------------------------------
121
GET.SIM.DATA = function(smaple.num, feature.num, random.seed, snrlam=0.05){
122
  # smaple.num = 700; feature.num = 100; random.seed = 10; snrlam=0.05
123
  ii = random.seed
124
  set.seed(30)
125
  w  <-c(rnorm(40),rep(0,(feature.num-40)));b = 0
126
  mu <- rep(0,40)
127
  Sigma <- matrix(.6, nrow=40, ncol=40) + diag(40)*.4
128
  
129
  set.seed(ii*2)
130
  X1 <- mvrnorm(n=smaple.num, mu=mu, Sigma=Sigma)
131
  
132
  set.seed(ii*3)
133
  X2 <- matrix(rnorm(smaple.num*(feature.num-40), mean = 0, sd = 1), nrow = smaple.num, ncol = feature.num-40)
134
  X = cbind(X1,X2)
135
136
  Xw <- -X%*%w 
137
  pp <- 1/(1+exp(Xw))
138
  y  <- rep(1,smaple.num)
139
  y[pp<0.5] <- 0
140
141
  p = dim(X)[1];q = dim(X)[2]
142
  set.seed(ii*3)
143
  XX = X + snrlam*matrix(rnorm(p*q),ncol=q)
144
  
145
  return(list(X=XX,y=y,w=w))
146
}
147
# -------------------------------------------------------------------------
148
GET.SIM.DATA2 = function(smaple.num, feature.num, random.seed, snrlam=0.05){
149
  # smaple.num = 700; feature.num = 100; random.seed = 10; snrlam=0.05
150
  ii = 10
151
  set.seed(30)
152
  w  <-c(rnorm(40),rep(0,(feature.num-40)));b = 0
153
  mu <- rep(0,40)
154
  Sigma <- matrix(.6, nrow=40, ncol=40) + diag(40)*.4
155
  
156
  set.seed(ii*2)
157
  X1 <- mvrnorm(n=smaple.num, mu=mu, Sigma=Sigma)
158
  
159
  set.seed(ii*3)
160
  X2 <- matrix(rnorm(smaple.num*(feature.num-40), mean = 0, sd = 1), nrow = smaple.num, ncol = feature.num-40)
161
  X = cbind(X1,X2)
162
163
  Xw <- -X%*%w 
164
  pp <- 1/(1+exp(Xw))
165
  y  <- rep(1,smaple.num)
166
  y[pp<0.5] <- 0
167
  
168
  p = dim(X)[1];q = dim(X)[2]
169
  
170
  set.seed(random.seed)
171
  XX = X + snrlam*matrix(rnorm(p*q),ncol=q)
172
  
173
  return(list(X=XX,y=y,w=w))
174
}