Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.82 KB | None | 0 0
  1.  
  2. unreg_fit <- function(x, n_cl){
  3. # x: data
  4. # n_cl: number of clusters
  5.  
  6. # clusters  
  7. cl <- makeCluster(n_cl)
  8. registerDoSNOW(cl)
  9.  
  10. # matrices
  11. mat_p <- mat_aic <- mat_bic <- matrix(0, ncol = ncol(x), nrow = ncol(x))
  12.  
  13. # scale data
  14. x <- as.data.frame(scale(x))
  15. # number of columns
  16. p <- ncol(x)
  17. # column names
  18. colnames(x) <- letters[1:p]
  19.  
  20. # name matrix columns
  21. colnames(mat_p) <- colnames(mat_aic) <- colnames(mat_bic)  <- letters[1:p]
  22. temp <-   noquote(paste(letters[1:p], "~ .", sep = " "))
  23. # parallel loop
  24. fit <- foreach(i = 1:p, .packages =  c("SignifReg", "dplyr"), rbind) %dopar%{
  25.                
  26.                  scope <- as.formula(temp[i])
  27.                  
  28.                  # forward selection
  29.                  #  1) p-value with no correction
  30.                  coef_p <- coefficients(SignifReg(scope = scope, data = x, alpha = 0.05,
  31.                                         direction = "forward", correction = "None"))[-1]
  32.                  names_p <- names(coef_p)
  33.                  
  34.                  #  2) aic
  35.                  coef_aic <- coefficients(SignifReg(scope = scope, data = x, criterion = "AIC",
  36.                                           direction = "forward", alpha = 0.99, correction = "None"))[-1]
  37.                  names_aic <- names(coef_aic)
  38.                  
  39.                  #  3) bic
  40.                  coef_bic <- coefficients(SignifReg(scope = scope, data = x, criterion = "BIC",
  41.                                           alpha = 0.99, direction = "forward", correction = "None"))[-1]
  42.                  names_bic <- names(coef_bic)
  43.                  
  44.                  # returned values
  45.                  p <-   data.frame(coef_p, names_p)
  46.                  aic <- data.frame(coef_aic, names_aic)
  47.                  bic <- data.frame(coef_bic, names_bic)
  48.                  list(p = p, aic = aic, bic = bic)
  49.                  }
  50.  
  51. # storage lists
  52. p_l <- aic_l <- bic_l <- list()
  53.  
  54. for(i in 1:length(fit)){
  55.   p_l[[i]] <- fit[[i]]$p
  56.   aic_l[[i]] <- fit[[i]]$aic
  57.   bic_l[[i]] <- fit[[i]]$bic
  58. }
  59.  
  60. # betas into data frame
  61. p_beta <- suppressMessages(reshape2::melt(p_l))
  62. aic_beta <- suppressMessages(reshape2::melt(aic_l))
  63. bic_beta <- suppressMessages(reshape2::melt(bic_l))
  64.  
  65. # place betas in matrix
  66. for(i in 1:ncol(x)){
  67.   temp_p <-   p_beta %>% filter(L1 == i)
  68.   temp_aic <- aic_beta %>% filter(L1 == i)
  69.   temp_bic <- bic_beta %>% filter(L1 == i)
  70.  
  71.   mat_p[i, as.character(temp_p$names_p)] <- temp_p$value
  72.   mat_aic[i, as.character(temp_aic$names_aic)] <- temp_aic$value
  73.   mat_bic[i, as.character(temp_bic$names_bic)] <- temp_bic$value
  74. }
  75. stopCluster(cl)
  76.  
  77. # compute partial correlations
  78. p_pcor <- parcor::Beta2parcor(mat_p)  
  79. aic_pcor <- parcor::Beta2parcor(mat_aic)  
  80. bic_pcor <- parcor::Beta2parcor(mat_bic)  
  81.  
  82. # return these
  83. list(p_pcor = p_pcor, aic_pcor = aic_pcor, bic_pcor = bic_pcor)  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement