File: hess_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 (56 lines) | stat: -rw-r--r-- 1,618 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

hess_ram = function(par,ImpCov,SampCov,Areg,Sreg,A,S,F){

#?sfClusterApplyLB
#sfClusterApplyLB(index, simulationIteration, n, rho, g, h, pv, het=F)

hess.out <- matrix(0,length(par),length(par))
h <- 0.00001


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

#if(type=="none"){

# not symmetric

    for(i in 1:nrow(hess.out)){
          for(j in 1:ncol(hess.out)){

          Ai <- (A == i)*1
          Aj <- (A == j)*1
          Aij <- matrix(0,nrow(A),ncol(A))
          Aij[Ai==T & Aj ==T] <- 1

          Si <- (S == i)*1;
          Sj <- (S == j)*1;
          Sij <- matrix(0,nrow(S),ncol(S))
          Sij[Si==T & Sj==T] <- 1

  deriv15_I <- F %*% B %*% Ai %*% E %*% t(F) + F %*% B %*% Si %*% t(B) %*% t(F)
  deriv15_J <- F %*% B %*% Aj %*% E %*% t(F) + F %*% B %*% Sj %*% t(B) %*% t(F)

  # this is cause of asymmetry
  deriv15_IJ <- F %*% B %*% Ai %*% B %*% Aj %*% E %*% t(F) +
                F %*% B %*% Aj %*% B %*% Ai %*% E %*% t(F) +
                F %*% B %*% Ai %*% B %*% Sj %*% t(B) %*% t(F) +
                F %*% B %*% Ai %*% E %*% t(Aj) %*% t(B) %*% t(F) +
                F %*% B %*% Aj %*% B %*% Si %*% t(B) %*% t(F)

  # left out mean part
  hess.out[i,j]  <- trace(solve(ImpCov) %*% deriv15_IJ %*% C - solve(ImpCov) %*%
                          deriv15_I %*% solve(ImpCov) %*% deriv15_J %*% C +
                          solve(ImpCov) %*% deriv15_J %*% solve(ImpCov) %*% deriv15_I %*%
                          solve(ImpCov) %*% SampCov)

          }
      }


hess.out



}