diff --git a/R/mafa.R b/R/mafa.R index aaf61c5c7414ed7120c8b10ab373595900889015..02b4598a6f069ccdf4eabf1e7ddd5122c07620bb 100644 --- a/R/mafa.R +++ b/R/mafa.R @@ -20,8 +20,8 @@ #' @export #' @seealso \url{https://doi.org/10.1051/alr/2009020} #' @examples -#' df <- data.frame(variable1 = 1:20, variable2= rnorm(20, 100, 30), variable3 = 1:20 + rnorm(20)) -#' mafa(x = df, time = 1981:2000) +#' df <- data.frame(variable1variabl = 1:20, variable2= rnorm(20, 100, 30), variable3 = 1:20 + rnorm(20)) +#' mafa(x = df, time = 1981:2000, nreal = 10) #' df <- matrix(rnorm(100), ncol = 5) #' colnames(df) <- letters[1:5] #' mafa(df) @@ -29,7 +29,7 @@ mafa <- function(x, time = NULL, nreal = 1000, nmafs = 3, seed = NULL, contr_var = NULL){ form <- stats::as.formula(paste("~", paste(colnames(x), collapse = "+"), sep = "")) - vars <- names(x) # Ind + vars <- colnames(x) # Ind nv <- length(vars) # jInd zn <- 1:nv # Ind.n @@ -99,7 +99,7 @@ mafa <- function(x, time = NULL, nreal = 1000, nmafs = 3, seed = NULL, contr_var rot <- matrix(rep(med.rot,nij[1]),nij[1],byrow=T) xsum <- apply(rot * xsc,1,sum) resmaf$rotation[,j] <- resmaf$rotation[,j]/stats::sd(xsum) - resmaf$x[,j] <- xsum/sd(xsum) + resmaf$x[,j] <- xsum/stats::sd(xsum) } # Return 3 plots for each MAF @@ -127,20 +127,26 @@ mafa <- function(x, time = NULL, nreal = 1000, nmafs = 3, seed = NULL, contr_var # sort ord <- order(maf_rot) maf_rot <- maf_rot[ord] - vars <- names(x)[ord] + vars <- vars[ord] nvc <- length(vars) mr_pos <- which(maf_rot > 0) mr_neg <- which(maf_rot < 0) - x_min <- abs(max(maf_rot))*-5 - x_max <- abs(max(maf_rot))*5 + fac <- 2 + x_min <- max(abs(maf_rot))*-fac + x_max <- max(abs(maf_rot))*fac plot(0, 0, xlim = c(x_min, x_max), ylim = c(0, nvc), type = "n", xlab = "", ylab = "", yaxt = "n", main = "Loadings") segments(x0 = rep(0, nvc), x1 = maf_rot, y0 = seq(0.5, nvc-.5, 1), y1 = seq(0.5, nvc-.5, 1)) abline(v = 0) Col <- rep(1, nvc) Col[seq(1, nvc, 1)[abs(maf_rot) == max(abs(maf_rot))]] <- 3 - text(maf_rot[mr_pos], seq(0.5, nvc-.5, 1)[mr_pos], vars[mr_pos], cex = .9, col = Col[mr_pos], pos = 4) - text(maf_rot[mr_neg], seq(0.5, nvc-.5, 1)[mr_neg], vars[mr_neg], cex = .9, col = Col[mr_neg], pos = 2) + if (length(mr_pos) > 0) { + text(maf_rot[mr_pos], seq(0.5, nvc-.5, 1)[mr_pos], vars[mr_pos], cex = .9, col = Col[mr_pos], pos = 4) + } + if (length(mr_neg) > 0) { + text(maf_rot[mr_neg], seq(0.5, nvc-.5, 1)[mr_neg], vars[mr_neg], cex = .9, col = Col[mr_neg], pos = 2) + } + } par(op)