pacotes <- c("dplyr","openxlsx","readxl","topsis") if(sum(as.numeric(!pacotes %in% installed.packages())) != 0){ instalador <- pacotes[!pacotes %in% installed.packages()] for(i in 1:length(instalador)) { install.packages(instalador, dependencies = T) break()} sapply(pacotes, require, character = T) } else { sapply(pacotes, require, character = T) } # df <- read.xlsx("Modelo_entrada_dados.xlsx", sheet = "Exemplo_1") # Tratamento da base de dados ************************************************** # alternatives <- df[3:nrow(df), 1] # alternatives <- as.data.frame(alternatives) # colnames(alternatives) <- "Alternatives" # weights_decisor <- as.numeric(df[2, 2:ncol(df)]) # criteria_direction <- df[1, 2:ncol(df)] # criterias <- colnames(df)[2:ncol(df)] # matrix_decis <- as.data.frame(df[3:nrow(df), 2:ncol(df)]) # colnames(matrix_decis) <- criterias # matrix_decis <- matrix_decis %>% mutate_all(as.numeric) # Calcular pesos usando PSI **************************************************** weights_psi <- function(df) { alternatives <- df[3:nrow(df), 1] alternatives <- as.data.frame(alternatives) colnames(alternatives) <- "Alternatives" weights_decisor <- as.numeric(df[2, 2:ncol(df)]) criteria_direction <- df[1, 2:ncol(df)] criterias <- colnames(df)[2:ncol(df)] matrix_decis <- as.data.frame(df[3:nrow(df), 2:ncol(df)]) colnames(matrix_decis) <- criterias matrix_decis <- matrix_decis %>% mutate_all(as.numeric) matrix_decis_norm <- matrix_decis # Inicializar matrix_decis_norm # Normalização da Matriz de Decisão for(i in 1:ncol(matrix_decis_norm)) { if(criteria_direction[i] == "MAX") { matrix_decis_norm[,i] <- matrix_decis[,i] / max(matrix_decis[,i]) } else { matrix_decis_norm[,i] <- min(matrix_decis[,i]) / matrix_decis[,i] } } # Cálculo da variação quadrática pv <- rep(0, ncol(matrix_decis_norm)) for(j in 1:ncol(matrix_decis_norm)) { for(i in 1:nrow(matrix_decis_norm)) { pv[j] <- pv[j] + ((matrix_decis_norm[i,j]) - mean(matrix_decis_norm[,j]))^2 } } desv_pref <- 1 - pv weights_PSI <- round(desv_pref / sum(desv_pref), 20) weights_PSI <- as.data.frame(t(weights_PSI)) colnames(weights_PSI) <- criterias return(weights_PSI) # End of PSI weights calculation ************************ } pesos_psi <- weights_psi(df) # Ranking using PSI ************************************************************ rank_psi <- function(df, weights_PSI) { alternatives <- df[3:nrow(df), 1] alternatives <- as.data.frame(alternatives) colnames(alternatives) <- "Alternatives" weights_decisor <- as.numeric(df[2, 2:ncol(df)]) criteria_direction <- df[1, 2:ncol(df)] criterias <- colnames(df)[2:ncol(df)] matrix_decis <- as.data.frame(df[3:nrow(df), 2:ncol(df)]) colnames(matrix_decis) <- criterias matrix_decis <- matrix_decis %>% mutate_all(as.numeric) matrix_decis_norm <- matrix_decis # Normalização da Matriz de Decisão for(i in 1:ncol(matrix_decis_norm)) { if(criteria_direction[i] == "MAX") { matrix_decis_norm[,i] <- matrix_decis[,i] / max(matrix_decis[,i]) } else { matrix_decis_norm[,i] <- min(matrix_decis[,i]) / matrix_decis[,i] } } ind_pref <- numeric(nrow(matrix_decis_norm)) for(i in 1:nrow(matrix_decis_norm)) { for(j in 1:ncol(matrix_decis_norm)) { ind_pref[i] <- ind_pref[i] + matrix_decis_norm[i,j] * weights_PSI[j] } } rank_PSI <- matrix_decis rank_PSI$PSI <- ind_pref rank_PSI <- rank_PSI %>% mutate(Alternatives = alternatives) %>% select(Alternatives, everything()) %>% arrange(desc(PSI)) %>% mutate(Position = row_number()) %>% select(Alternatives, Position, everything()) colnames(rank_PSI[,1]) <- "Alternatives" return(rank_PSI) } # End of PSI ranking *********************************************************