Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.77 KB | None | 0 0
  1. library(BGGM)
  2. library(ggplot2)
  3. cors <- cor(BGGM::ptsd)
  4. pcors <- corpcor::cor2pcor(cors)
  5. pcors <- ifelse(abs(pcors) < 0.05, 0, pcors)
  6. adj <- ifelse(pcors == 0, 0, 1)
  7. cors <- corpcor::pcor2cor(pcors)
  8.  
  9.  
  10.  
  11. n <- c(100, 250, 500, 1000, 5000, 10000)
  12.  
  13. res_i <- list()
  14. res_j <- list()
  15.  
  16. for(j in 1:length(n)){
  17.  
  18.   for(i in 1:50){
  19.    
  20.     print(paste("iteration", i, "condition", j))
  21.    
  22.     Y <- MASS::mvrnorm(n = n[j], rep(0, 20), Sigma = cors)
  23.     bggm_fit <-  explore(Y, prior_sd = 0.40, iter = 5000)
  24.     garbage <- capture.output( bdgraph_fit <- BDgraph::bdgraph(Y) )
  25.     bdgraph_sel <- BDgraph::select(bdgraph_fit)
  26.     sel_50 <- select(bggm_fit, BF_cut = 1.00001)
  27.     sel_3 <- select(bggm_fit, BF_cut = 3)
  28.     bggm_50_res <- BDgraph::compare(adj, sel_50$Adj_10)[6:8,2]
  29.     bggm_3_res <- BDgraph::compare(adj, sel_3$Adj_10)[6:8,2]
  30.     bdgraph_res <- BDgraph::compare(adj, bdgraph_sel)[6:8,2]
  31.  
  32.  
  33. res <- data.frame(method = c("BGGM_50", "BGGM_50", "BGGM_50",
  34.                              "BGGM_3", "BGGM_3", "BGGM_3",
  35.                       "BDgraph", "BDgraph", "BDgraph"),
  36.            measure = c("SPC", "SN", "MCC",
  37.                        "SPC", "SN", "MCC",
  38.                        "SPC", "SN", "MCC"),
  39.            score = c(bggm_50_res,
  40.                      bggm_3_res,
  41.                      bdgraph_res),
  42.            N = n[j])
  43.  
  44. res_i[[i]] <- res
  45.  
  46. }
  47.  
  48.   res_j[[j]] <- do.call(rbind.data.frame, res_i)
  49.  
  50.  
  51.  
  52. }
  53.  
  54. # save(res_j, file = "res_j.Rdata")
  55.  
  56.  
  57. windowsFonts(times = windowsFont("Times New Roman"))
  58.  
  59. dat_res <- do.call(rbind.data.frame, res_j)
  60.  
  61.  
  62. # specifcity
  63. dat_res_spc <- reshape::melt(with(subset(dat_res, measure == "SPC"),
  64.      tapply(score, list(method, N), mean)))
  65.  
  66. dat_res_spc$sd <-as.numeric( with(subset(dat_res, measure == "SPC"),
  67.                        tapply(score, list(method, N), sd))) / sqrt(50)
  68.  
  69.  
  70. dat_res_spc$X1 <- factor(dat_res_spc$X1,
  71.                       levels = c("BDgraph", "BGGM_3", "BGGM_50"),
  72.                       labels = c("BDgraph",
  73.                                  "BGGM: BF = 3",
  74.                                  "BGGM: Post.prob = 0.50"))
  75.  
  76. plot_spc <- ggplot(dat_res_spc,  aes(x = as.factor(X2),
  77.                        y = value,
  78.                       group = X1)) +
  79.   geom_hline(yintercept = 0.95, linetype = "dotted") +
  80.   geom_ribbon(aes(ymax = value + sd,
  81.                   ymin = value - sd,
  82.                   fill = X1),
  83.               color = NA,
  84.               show.legend = F, alpha = 0.25) +
  85.   geom_line(size = 1.5, alpha = 0.85, aes(color = as.factor(X1))) +
  86.   ylab("Specificity") +
  87.   theme_bw(base_family = "times", base_size = 12) +
  88.   theme(legend.position = "top",
  89.       legend.title = element_blank(),
  90.       panel.grid = element_blank()) +
  91.   scale_x_discrete(expand = c(0.01,0.01), breaks = n, labels = n) +
  92.   guides(colour = guide_legend(override.aes = list(alpha = 1))) +
  93.   xlab(expression("Sample Size ("*italic(p)*" = 20)")) +
  94.   scale_color_manual(values = c("#999999", "#0072B2", "#CC79A7")) +
  95. scale_fill_manual(values = c("#999999", "#0072B2", "#CC79A7"))
  96.  
  97. plot_spc
  98.  
  99.  
  100. # sensitivity
  101. dat_res_sn <- reshape::melt(with(subset(dat_res, measure == "SN"),
  102.                                   tapply(score, list(method, N), mean)))
  103. dat_res_sn$sd <-as.numeric( with(subset(dat_res, measure == "SN"),
  104.                                   tapply(score, list(method, N), sd))) / sqrt(50)
  105.  
  106. dat_res_sn$X1 <- factor(dat_res_sn$X1,
  107.                          levels = c("BDgraph", "BGGM_3", "BGGM_50"),
  108.                          labels = c("BDgraph",
  109.                                     "BGGM: BF = 3",
  110.                                     "BGGM: Post.prob = 0.50"))
  111.  
  112. plot_sn <- ggplot(dat_res_sn,  aes(x = as.factor(X2),
  113.                                      y = value,
  114.                                      group = X1,
  115.                                      color = as.factor(X1))) +
  116.   geom_hline(yintercept = 0.95, linetype = "dotted") +
  117.   geom_line(size = 1.5, alpha = 0.85) +
  118.   ylab("Specificity") +
  119.   theme_bw(base_family = "times", base_size = 12) +
  120.   theme(legend.position = "top",
  121.         legend.title = element_blank(),
  122.         panel.grid = element_blank()) +
  123.   scale_x_discrete(expand = c(0.01,0.01), breaks = n, labels = n) +
  124.   guides(colour = guide_legend(override.aes = list(alpha = 1))) +
  125.   xlab(expression("Sample Size ("*italic(p)*" = 20)")) +
  126.   scale_color_manual(values = c("#999999", "#0072B2", "#CC79A7"))
  127.  
  128. plot_sn
  129.  
  130.  
  131.  
  132. # mcc
  133. dat_res_mcc <- reshape::melt(with(subset(dat_res, measure == "MCC"),
  134.                                  tapply(score, list(method, N), mean)))
  135. dat_res_mcc$sd <-as.numeric( with(subset(dat_res, measure == "MCC"),
  136.                                  tapply(score, list(method, N), sd))) / sqrt(50)
  137.  
  138. dat_res_mcc$X1 <- factor(dat_res_mcc$X1,
  139.                         levels = c("BDgraph", "BGGM_3", "BGGM_50"),
  140.                         labels = c("BDgraph",
  141.                                    "BGGM: BF = 3",
  142.                                    "BGGM: Post.prob = 0.50"))
  143.  
  144. plot_mcc <- ggplot(dat_res_mcc,  aes(x = as.factor(X2),
  145.                                    y = value,
  146.                                    group = X1,
  147.                                    color = as.factor(X1))) +
  148.   geom_hline(yintercept = 0.95, linetype = "dotted") +
  149.   geom_line(size = 1.5, alpha = 0.85) +
  150.   ylab("Specificity") +
  151.   theme_bw(base_family = "times", base_size = 12) +
  152.   theme(legend.position = "top",
  153.         legend.title = element_blank(),
  154.         panel.grid = element_blank()) +
  155.   scale_x_discrete(expand = c(0.01,0.01), breaks = n, labels = n) +
  156.   guides(colour = guide_legend(override.aes = list(alpha = 1))) +
  157.   xlab(expression("Sample Size ("*italic(p)*" = 20)")) +
  158.   scale_color_manual(values = c("#999999", "#0072B2", "#CC79A7"))
  159.  
  160. plot_mcc
  161.  
  162.  
  163.  
  164. qgraph::qgraph(pcors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement