File: grad_ram.R

package info (click to toggle)
r-cran-regsem 1.6.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 496 kB
  • sloc: cpp: 263; ansic: 15; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

grad_ram = function(par,ImpCov,SampCov,Areg,Sreg,A,S,
                     F,lambda,type,pars_pen,diff_par){

  grad.out <- rep(0,length(par))

  B = solve(diag(nrow(A)) - Areg)
  C = diag(nrow(ImpCov)) - solve(ImpCov) %*% SampCov
  E = B %*% Sreg %*% t(B)



  # The S matrix gradients are exactly twice that of other methods

  if(type=="none"){

    for(i in 1:length(grad.out)){

      A2 <- A == i;
      A2[A2==T] <- 1
      S2 <- S == i;
      S2[S2==T] <- 1

      deriv15 <- F %*% B %*% A2 %*% E %*% t(F) + F %*% B %*% S2 %*% t(B) %*% t(F)
      # left out mean part
      grad.out[i]  <- trace(solve(ImpCov) %*% deriv15 %*% C)


    }

  }


  else if(type=="lasso"){
    for(i in 1:length(grad.out)){

      A2 <- A == i;
      A2[A2==T] <- 1
      S2 <- S == i;
      S2[S2==T] <- 1

      deriv15 <- F %*% B %*% A2 %*% E %*% t(F) + F %*% B %*% S2 %*% t(B) %*% t(F)
      # left out mean part
     # grad.out[i]  <- trace(solve(ImpCov) %*% deriv15 %*% C) + if(any(i==pars_pen)) lambda*sign(Areg[A==i]) else(0)# just penalize when A
      #add <- 0
      # soft threshold
    #  if(any(i==pars_pen)){
     #   if(Areg[A==i] >0 & abs(Areg[A==i]) < lambda){
     #     add <- Areg[A==i] - lambda
     #   }else if(Areg[A==i] < 0 & abs(Areg[A==i]) < lambda){
     #     add <- Areg[A==i] + lambda
      #  }else if(abs(Areg[A==i]) <= lambda){
      #    add <- 0
      #  }
     # }
     # if(any(i==pars_pen)){
     #   add = sign(Areg[A==i]) * max(abs(Areg[A==i])-lambda,0)
     # }else{
     #   add <- 0
     # }

      grad.out[i]  <- trace(solve(ImpCov) %*% deriv15 %*% C) #+ add

    }

  }

  else if(type=="ridge"){
    for(i in 1:length(grad.out)){

      A2 <- A == i;
      A2[A2==T] <- 1
      S2 <- S == i;
      S2[S2==T] <- 1

      deriv15 <- F %*% B %*% A2 %*% E %*% t(F) + F %*% B %*% S2 %*% t(B) %*% t(F)
      # left out mean part
      grad.out[i]  <- trace(solve(ImpCov) %*% deriv15 %*% C) +
                      if(any(i==pars_pen)) 2*lambda*Areg[A==i] else(0)


    }

  }

  else if(type=="diff_lasso"){
    count=0
    for(i in 1:length(grad.out)){

      A2 <- A == i;
      A2[A2==T] <- 1
      S2 <- S == i;
      S2[S2==T] <- 1

      deriv15 <- F %*% B %*% A2 %*% E %*% t(F) + F %*% B %*% S2 %*% t(B) %*% t(F)
      # left out mean part
      grad.out[i]  <- trace(solve(ImpCov) %*% deriv15 %*% C) +
        if(any(i==pars_pen)){
          count=count+1
          lambda*sign(Areg[A==i]-diff_par[count])
        }else(0)


    }

  }



  grad.out[(max(A)+1):max(S)] = grad.out[(max(A)+1):max(S)] *0.5
  #grad.out[min(S[S!=0],0):max(S)] = grad.out[min(S[S!=0],0):max(S)] *0.5
  grad.out
}