Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.72 KB | None | 0 0
  1. if (!require("BDgraph")) install.packages("BDgraph")
  2. if (!require("mvtnorm")) install.packages("mvtnorm")
  3.  
  4. if (!require("dplyr")) install.packages("dplyr")
  5.  
  6.  
  7. par_cor_CI <- function(X, alpha){
  8.  
  9.   # X: data frame
  10.   if (!require("BDgraph")) install.packages("BDgraph")
  11.   if (!require("qgraph")) install.packages("qgraph")
  12.   if (!require("glasso")) install.packages("glasso")
  13.   if (!require("mvtnorm")) install.packages("mvtnorm")
  14.  
  15.  
  16.   n <- nrow(X)
  17.   p <- ncol(X)
  18.   I = diag(1,p)
  19.   ## compute maximum likelihood estimator for covariance matrix
  20.   mle_cov <- cov(X, use = "pairwise.complete")#crossprod(scale(X, scale = F)) / n
  21.  
  22.   ## compute maximum likelihood estimator of precision matrix
  23.   ## (inverse covariance matrix)
  24.   mle_inv <- solve(mle_cov)
  25.  
  26.   ## standardize and revese sign = partial correaltions
  27.   pc  <-  as.matrix(wi2net(mle_inv))
  28.  
  29.  
  30.  
  31.   mle_parcors <- ci_par_cor(alpha = alpha, par_cors = pc, n = n, s = p - 1)
  32.  
  33.   mle_inv <- mle_parcors$sig_mat * mle_inv
  34.   #if(det(mle_inv <= 0)) mle_inv <- mle_inv + (abs(min(eigen(mle_inv)$values)) + 10)*I
  35.   list(mle_parcors = mle_parcors, mle_inv = mle_inv )
  36.  
  37. }
  38.  
  39.  
  40. ci_par_cor <- function(alpha, par_cors, s, n) {
  41.  
  42.   # n: sample size
  43.   # s: p - 1 (controlled for)
  44.   # alpha: confidence level
  45.   # par_cors: partial correlations
  46.   mat <- matrix(0,nrow =  s + 1, ncol = s + 1)
  47.   CI_ls <- list()
  48.   par_cor <- par_cors[upper.tri(par_cors)]
  49.   cov <- list()
  50.   for(i in 1:length(par_cor)){
  51.     # crtiical value
  52.     z_crit <- qnorm(1 - alpha/2)
  53.     # standard error
  54.     se <- sqrt(1/((n - s - 3)))
  55.     # z transformation
  56.     z <- log((1 + par_cor[i])/(1 - par_cor[i]))/2
  57.     # z lower bound
  58.     Z_L <- z - z_crit * se
  59.     # Z upper bound
  60.    
  61.     Z_U <- z + z_crit * se
  62.     rho_L <- (exp(2*Z_L) - 1)/(exp(2*Z_L) + 1)
  63.     rho_U <- (exp(2*Z_U) - 1)/(exp(2*Z_U) + 1)
  64.     CI <- c(rho_L, rho_U)
  65.     CI_ls[[i]] <- CI
  66.     cov[[i]] <- ifelse(CI[1] < 0 & CI[2] > 0, 0, 1)
  67.   }
  68.  
  69.   c_dat <- do.call(rbind.data.frame, CI_ls)
  70.   colnames(c_dat) <- c("low", "up")
  71.   c_dat$pcar <- unlist(par_cor)
  72.   diag(mat) <- 1
  73.   mat[upper.tri(mat)] <- unlist(cov)
  74.   mat <- as.matrix(forceSymmetric(mat) )
  75.   list(sig_mat = mat, par_cors = par_cors, par_sig = mat * par_cors,
  76.        cis = c_dat, cov_prob = unlist(cov))
  77. }
  78.  
  79. sim_coverage <- function(n, alpha){
  80.   main <- bdgraph.sim(p = 20, n = n, prob = 0, b = 3)
  81.   dat <-  rmvnorm(n, mean = rep(0, 20), sigma = main$sigma)
  82.   test_run <-  par_cor_CI(dat,  alpha = alpha)
  83.   spec <- compare(main, test_run$mle_parcors$sig_mat)[6,2]
  84.   cov <- sum(ifelse(test_run$mle_parcors$cis$low < 0 & test_run$mle_parcors$cis$up > 0, 1, 0)) / 190
  85.   cis <- test_run$mle_parcors$cis
  86.   list(cis = cis, spec = spec, cov = cov)
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement