File: hess_ramParallel.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 (69 lines) | stat: -rw-r--r-- 2,110 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

hess_ramParallel = 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))



  # get rid of
  #Areg = A.est
  #Sreg= S.est
  #ImpCov = out$imp_cov
  #SampCov=S1



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


  vec = seq(1:length(par))
  li <- list()
  grid <- expand.grid(i=vec,j=vec)
  grid <- as.matrix(grid)
  for(i in 1:nrow(grid)) li[[i]] <- grid[i,]


  snowfall::sfExport("Areg","A","ImpCov","SampCov","B","C","E","S","li")

  hess_fun <- function(indexI,indexJ,Areg,A,ImpCov,SampCov,B,C,E,S){
    #indexI <- list[1]
    #indexJ <- list[2]
    Ai <- A == indexI
    Aj <- A == indexJ;
    Aij <- matrix(0,nrow(A),ncol(A))
    Aij[Ai==T | A==T] <- 1

    Si <- S == indexI;
    Sj <- S == indexJ;
    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)

    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
   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))
  }



  #lapply(li,hess_fun(indexI=li$i,indexJ=li$j,Areg,A,ImpCov,SampCov,B,C,E,S))

 rett = matrix(unlist(snowfall::sfLapply(li,function(x) hess_fun(indexI=x["i"],indexJ=x["j"],Areg,A,ImpCov,SampCov,B,C,E,S))),length(par),length(par))
 #as.numeric(rett)
 rett
}