Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • f115cd0
  • /
  • FAMD_iterative.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:31611a322f063f69216fbb0dd239177ef7dcbfe1
directory badge Iframe embedding
swh:1:dir:f115cd09f0791734573b74657def66c62323dff0

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
FAMD_iterative.R
#remove.packages("FactoMineR")
#install.packages('FactoMineR', dependencies = TRUE)
#install.packages("Rcpp", repos="https://rcppcore.github.io/drat")
library(FactoMineR)
library(factoextra)
library(jsonlite)

myArgs <- commandArgs(trailingOnly = TRUE)


data <- read.csv(myArgs)


########################################################################
# get contributing and not contributing variables from dataframe
get_contributing_variables <- function(data_frame_var_contrib) {

  col_names_var_contrib <- colnames(data_frame_var_contrib)


  contributing_variables <- c()
  not_contributing_variables <- c()

  index <- 1

  sum_contri <- 0

  for (val in data_frame_var_contrib[1,]){
    sum_contri <- sum_contri + abs(val)
    if (abs(val) > 100/length(col_names_var_contrib)) {
      contributing_variables <- append(contributing_variables, col_names_var_contrib[index])
    } else {
      not_contributing_variables <- append(not_contributing_variables, col_names_var_contrib[index])
    }
    index <- index +1
  }

  df_contributing_variables_from_wine <- as.data.frame(data[,contributing_variables])

  if (length(not_contributing_variables) > 0) {
    df_NOT_contributing_variables_from_wine <- as.data.frame(data[,not_contributing_variables])
    return (list(df_contributing_variables_from_wine, df_NOT_contributing_variables_from_wine))
  } else {
    return (list(df_contributing_variables_from_wine, NULL))
  }
}




########################################################################
#run FAMD
run_FAMD <- function(data_frame, index) {

  data_types <- sapply(data_frame, class)

  run_type <- 'MCA'
  if ((any(data_types=="integer") || any(data_types == "numeric")) && (any(data_types == "factor") || any(data_types == "character"))) {
    run_type <- 'famd'
  } else if ((any(data_types=="integer") || any(data_types == "numeric")) && (!any(data_types == "factor") || !any(data_types == "character"))) {
    run_type <- 'PCA'
  }


  if(run_type == 'famd' || run_type == 'MCA') {
    ################################
    # do FAMD in case of qualitative and quantitative data
    res.famd <- FAMD(data_frame, graph = FALSE)
    var <- get_famd_var(res.famd)

    # print(res.famd$eig)

    var_contrib <- res.famd[["var"]][["contrib"]]

    # Contribution to the first dimension
    # print(fviz_contrib(res.famd, "var", axes = 1))
    # print(fviz_contrib(res.famd, "var", axes = 2))

    data_frame_var_contribution <- as.data.frame(t(var_contrib))

    return(list(data_frame_var_contribution, res.famd$eig, res.famd$ind$coord, res.famd$var$coord, index))

  } else if (run_type == 'PCA') {
    ##########################
    # in case of only numerical values use PCA
    res.famd = PCA(data_frame, scale.unit=TRUE, ncp=5, graph=FALSE)

    var <- res.famd$var

    # print(res.famd$eig)


    # print(fviz_contrib(res.famd, "var", axes = 1))
    # print(fviz_contrib(res.famd, "var", axes = 2))

    var_contrib <- var$contrib

    data_frame_var_contribution <- as.data.frame(t(var_contrib))

    return(list(data_frame_var_contribution, res.famd$eig, res.famd$ind$coord, res.famd$var$coord, index))
  }
}

get_famds <- function(data, index) {

  if (length(colnames(data))<=1) {
    # not enough dimensions for a meaningful FAMD
    return(list(colnames(data)))
  }

  contribution_of_dimensions_and_eigen_value <- run_FAMD(data, index)

  eigenvalue <- contribution_of_dimensions_and_eigen_value[[2]]

  if (eigenvalue[1][1] > 1) {

    contribution_of_dimensions <- contribution_of_dimensions_and_eigen_value[[1]]

    contributing_and_not_contributing <- get_contributing_variables(contribution_of_dimensions)

    INITIAL_contributing <- contributing_and_not_contributing[[1]]
    INITIAL_not_contributing <- contributing_and_not_contributing[[2]]

    if (length(INITIAL_contributing) <2) {
      return(list(colnames(data)))
    }

    INITIAL_contributing_PC_FINAL <- run_FAMD(INITIAL_contributing, index)

    return(c(Recall(INITIAL_not_contributing, index + 1), list(INITIAL_contributing_PC_FINAL)))

  } else {
    return(list(colnames(data)))
  }
}

list_final_contributing <- get_famds(data, 1)

cat(jsonlite::toJSON(list_final_contributing, pretty=TRUE))

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API