├── .Rbuildignore ├── .github ├── .gitignore └── workflows │ ├── lint.yaml │ ├── pkgdown.yaml │ ├── r.yml │ └── test-coverage.yaml ├── .gitignore ├── .lintr ├── .travis.yml ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R ├── autoencoder.R ├── dataSets.R ├── diffmap.R ├── dimRed.R ├── dimRedData-class.R ├── dimRedMethod-class.R ├── dimRedResult-class.R ├── drr.R ├── embed.R ├── fastica.R ├── get_info.R ├── graph_embed.R ├── hlle.R ├── isomap.R ├── kpca.R ├── l1pca.R ├── leim.R ├── lle.R ├── loe.R ├── mds.R ├── misc.R ├── mixColorSpaces.R ├── nmds.R ├── nnmf.R ├── pca.R ├── plot.R ├── quality.R ├── rotate.R ├── soe.R ├── tsne.R └── umap.R ├── README.md ├── _pkgdown.yml ├── builddoc.sh ├── check_rhub.sh ├── checkpackage.sh ├── inst └── CITATION ├── install.sh ├── man-roxygen ├── dimRedMethodGeneralUsage.R └── dimRedMethodSlots.R ├── man ├── AUC_lnK_R_NX-dimRedResult-method.Rd ├── DRR-class.Rd ├── DiffusionMaps-class.Rd ├── DrL-class.Rd ├── FastICA-class.Rd ├── FruchtermanReingold-class.Rd ├── HLLE-class.Rd ├── Isomap-class.Rd ├── KamadaKawai-class.Rd ├── LCMC-dimRedResult-method.Rd ├── MDS-class.Rd ├── NNMF-class.Rd ├── PCA-class.Rd ├── PCA_L1-class.Rd ├── Q_NX-dimRedResult-method.Rd ├── Q_global-dimRedResult-method.Rd ├── Q_local-dimRedResult-method.Rd ├── R_NX-dimRedResult-method.Rd ├── UMAP-class.Rd ├── as.data.frame.Rd ├── as.dimRedData.Rd ├── cophenetic_correlation-dimRedResult-method.Rd ├── dataSets.Rd ├── dimRed-package.Rd ├── dimRedData-class.Rd ├── dimRedMethod-class.Rd ├── dimRedMethodList.Rd ├── dimRedResult-class.Rd ├── distance_correlation-dimRedResult-method.Rd ├── embed.Rd ├── getData.Rd ├── getDimRedData.Rd ├── getMeta.Rd ├── getNDim.Rd ├── getOrgData.Rd ├── getOtherData.Rd ├── getPars.Rd ├── getRotationMatrix.Rd ├── installSuggests.Rd ├── kPCA-class.Rd ├── makeKNNgraph.Rd ├── maximize_correlation-dimRedResult-method.Rd ├── mean_R_NX-dimRedResult-method.Rd ├── mixColorRamps.Rd ├── nMDS-class.Rd ├── ndims.Rd ├── plot.Rd ├── plot_R_NX.Rd ├── print.Rd ├── quality.Rd ├── reconstruction_error-dimRedResult-method.Rd ├── reconstruction_rmse-dimRedResult-method.Rd ├── tSNE-class.Rd └── total_correlation-dimRedResult-method.Rd ├── nonascii.sh ├── tests ├── testthat.R └── testthat │ ├── test_HLLE.R │ ├── test_NNMF.R │ ├── test_PCA.R │ ├── test_PCA_L1.R │ ├── test_UMAP.R │ ├── test_autoencoder.R │ ├── test_dataSets.R │ ├── test_diffusion_maps.R │ ├── test_dimRedData_class.R │ ├── test_dimRedMethod-class.R │ ├── test_dimRedResult_class.R │ ├── test_drr.R │ ├── test_embed.R │ ├── test_fastICA.R │ ├── test_high_level_functions.R │ ├── test_isomap.R │ ├── test_kPCA.R │ ├── test_misc_functions.R │ └── test_quality.R └── vignettes ├── Makefile ├── bibliography.bib ├── classification_tree.tex ├── dimensionality-reduction.Rnw └── dimensionality-reduction.pdf /.Rbuildignore: -------------------------------------------------------------------------------- 1 | 2 | dimRed_.*\.tar.gz$ 3 | 4 | README.md 5 | 6 | DRR\.check 7 | 8 | scratch 9 | \.gitignore 10 | \.projectile 11 | dir-locals\.el 12 | man-roxygen 13 | 14 | server\.R 15 | ui\.R 16 | src 17 | 18 | .*\.sh 19 | 20 | ^\..*$ 21 | notes\.org 22 | 23 | TAGS 24 | ^.*\.Rproj$ 25 | ^\.Rproj\.user$ 26 | 27 | ^\.travis\.yml$ 28 | 29 | revdep 30 | 31 | .*\.log 32 | 33 | ^\.github$ 34 | ^_pkgdown\.yml$ 35 | ^docs$ 36 | ^pkgdown$ 37 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/lint.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: lint 10 | 11 | jobs: 12 | lint: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | steps: 17 | - uses: actions/checkout@v2 18 | 19 | - uses: r-lib/actions/setup-r@v2 20 | with: 21 | use-public-rspm: true 22 | 23 | - uses: r-lib/actions/setup-r-dependencies@v2 24 | with: 25 | extra-packages: any::lintr, local::. 26 | needs: lint 27 | 28 | - name: Lint 29 | run: lintr::lint_package() 30 | shell: Rscript {0} 31 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: [main, master] 4 | pull_request: 5 | branches: [main, master] 6 | release: 7 | types: [published] 8 | workflow_dispatch: 9 | 10 | name: pkgdown 11 | 12 | jobs: 13 | pkgdown: 14 | runs-on: ubuntu-latest 15 | # Only restrict concurrency for non-PR jobs 16 | concurrency: 17 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 18 | env: 19 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 20 | steps: 21 | - uses: actions/checkout@v2 22 | 23 | - uses: r-lib/actions/setup-pandoc@v2 24 | 25 | - uses: r-lib/actions/setup-r@v2 26 | with: 27 | use-public-rspm: true 28 | 29 | - uses: r-lib/actions/setup-r-dependencies@v2 30 | with: 31 | extra-packages: any::pkgdown, local::. 32 | needs: website 33 | 34 | - name: Build site 35 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 36 | shell: Rscript {0} 37 | 38 | - name: Deploy to GitHub pages 🚀 39 | if: github.event_name != 'pull_request' 40 | uses: JamesIves/github-pages-deploy-action@4.1.4 41 | with: 42 | clean: false 43 | branch: gh-pages 44 | folder: docs 45 | -------------------------------------------------------------------------------- /.github/workflows/r.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | # 6 | # See https://github.com/r-lib/actions/tree/master/examples#readme for 7 | # additional example workflows available for the R community. 8 | 9 | name: R 10 | 11 | on: 12 | push: 13 | branches: [ master ] 14 | pull_request: 15 | branches: [ master ] 16 | 17 | env: 18 | BNET_FORCE_NNMF_TESTS: 0 19 | BNET_FORCE_AUTOENCODER_TESTS: 0 20 | BNET_FORCE_UMAP_TESTS: 0 21 | # _R_CHECK_FORCE_SUGGESTS_: false 22 | # _R_CHECK_INSTALL_DEPENDS_: true 23 | MAKEFLAGS: "-j 2" 24 | 25 | jobs: 26 | build: 27 | runs-on: ubuntu-latest 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | r-version: ["oldrel", "release", "devel"] 32 | depends-only: [true, false] 33 | suggests-only: [true, false] 34 | env: 35 | _R_CHECK_DEPENDS_ONLY_: ${{ matrix.depends-only }} 36 | _R_CHECK_SUGGESTS_ONLY_: ${{ matrix.suggests-only }} 37 | 38 | steps: 39 | - uses: actions/checkout@v2 40 | - uses: r-lib/actions/setup-r@v2 41 | with: 42 | r-version: ${{ matrix.r-version }} 43 | - name: Install System libraries 44 | if: runner.os == 'Linux' 45 | run: | 46 | sudo apt-get update -y 47 | sudo apt-get install -y coinor-libclp-dev mesa-common-dev libglu1-mesa-dev python3 python3-pip python3-setuptools texlive texlive-latex-extra python3-pip ghostscript 48 | shell: sh {0} 49 | - uses: r-lib/actions/setup-r-dependencies@v2 50 | with: 51 | cache-version: 1 52 | extra-packages: | 53 | any::rcmdcheck 54 | # - name: Install dependencies 55 | # run: | 56 | # # reticulate::install_miniconda() 57 | # # reticulate::py_install("umap-learn==0.4") 58 | # # tensorflow::install_tensorflow() 59 | # # keras::install_keras() 60 | # shell: Rscript {0} 61 | - uses: r-lib/actions/check-r-package@v2 62 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage 10 | 11 | jobs: 12 | test-coverage: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: any::covr 27 | needs: coverage 28 | 29 | - name: Test coverage 30 | run: covr::codecov(quiet = FALSE) 31 | shell: Rscript {0} 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | projectile* 3 | dir-locals.el* 4 | .#* 5 | \#*# 6 | *~ 7 | dimRed_*.tar.gz 8 | TAGS 9 | dimRed.Rcheck 10 | 11 | *.Rhistory 12 | *.RData 13 | /.Rprofile 14 | /.projectile 15 | /dimRed.Rproj 16 | /.Rprofile.bak 17 | *.juliahistory 18 | /src/test.r 19 | 20 | /scratch 21 | 22 | notes.org 23 | 24 | server.R 25 | ui.R 26 | *.jl 27 | /.dir-locals.el 28 | .DS_Store 29 | vignettes/auto* 30 | docs 31 | -------------------------------------------------------------------------------- /.lintr: -------------------------------------------------------------------------------- 1 | linters: linters_with_defaults(commented_code_linter = NULL, 2 | # camel_case_linter = NULL, 3 | # snake_case_linter, 4 | spaces_left_parentheses_linter = NULL, 5 | indentation_linter(hanging_indent_style = "tidy")) 6 | exclusions: list("vignettes/dimensionality-reduction.Rnw") 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | sudo: false 3 | 4 | env: 5 | global: 6 | - BNET_FORCE_NNMF_TESTS=1 BNET_FORCE_AUTOENCODER_TESTS=1 BNET_FORCE_UMAP_TESTS=1 _R_CHECK_INSTALL_DEPENDS_=true MAKEFLAGS="-j 2" 7 | 8 | # for pcaL1 and rgl: 9 | addons: 10 | apt: 11 | packages: 12 | - coinor-libclp-dev 13 | - mesa-common-dev 14 | - libglu1-mesa-dev 15 | - python3 16 | - python3-pip 17 | - python3-setuptools 18 | 19 | r: 20 | - oldrel 21 | - release 22 | - devel 23 | 24 | cache: 25 | packages: true 26 | directories: 27 | - $HOME/.keras 28 | - $HOME/.cache/pip 29 | 30 | # r_check_args: 31 | # "--run-donttest --run-dontrun --timings" 32 | 33 | before_install: 34 | # Keras and TensorFlow stuff 35 | # pip was guilty of the failing travis build!!! 36 | # - pip2.7 install --upgrade --ignore-installed --user pip 37 | # - pip2.7 install --upgrade --ignore-installed --user travis 38 | # - pip2.7 install --upgrade --ignore-installed --user setuptools 39 | # - pip2.7 install --upgrade --ignore-installed --user wheel 40 | # - pip2.7 install --upgrade --ignore-installed --user virtualenv 41 | # - pip2.7 install --upgrade --ignore-installed --user keras 42 | # - pip2.7 install --upgrade --ignore-installed --user h5py 43 | # - pip2.7 install --upgrade --ignore-installed --user pyyaml 44 | # - pip2.7 install --upgrade --ignore-installed --user requests 45 | # - pip2.7 install --upgrade --ignore-installed --user Pillow 46 | # - pip2.7 install --upgrade --ignore-installed --user scipy 47 | # - pip2.7 install --upgrade --ignore-installed --user umap-learn 48 | # - pip3 install --upgrade --ignore-installed --user pip 49 | - python3 -m pip install --upgrade pip 50 | # - pip3 install --upgrade --ignore-installed --user travis setuptools wheel virtualenv h5py pyyaml requests Pillow scipy umap-learn tensorflow keras 51 | - pip3 install --upgrade --user virtualenv 52 | # Install R packages 53 | - Rscript -e "update.packages(ask = FALSE)" 54 | - Rscript -e "if (!'covr' %in% rownames(installed.packages())) install.packages('covr')" 55 | - Rscript -e "if (!'rgl' %in% rownames(installed.packages())) install.packages('rgl')" 56 | - Rscript -e "if (!'roxygen2' %in% rownames(installed.packages())) install.packages('roxygen2')" 57 | - Rscript -e "if (!'shiny' %in% rownames(installed.packages())) install.packages('shiny')" 58 | # - Rscript -e "if (!'tensorflow' %in% rownames(installed.packages())) install.packages('tensorflow')" 59 | # Set CXX1X for R-devel, as R-devel does not detect CXX1X support for gcc 4.6.3, 60 | # - if [[ "$TRAVIS_R_VERSION_STRING" = 'devel' ]]; then mkdir ~/.R && echo 'CXX1X=g++ -std=c++0x -g -O2 -fPIC' > ~/.R/Makevars; fi 61 | 62 | before_script: 63 | # - Rscript -e "devtools::document()" 64 | - Rscript -e 'reticulate::py_install("umap-learn")' 65 | - Rscript -e "tensorflow::install_tensorflow()" 66 | - Rscript -e "keras::install_keras()" 67 | - Rscript -e 'library(tensorflow); print(tf$Session()$run(tf$constant("hello")))' 68 | 69 | # Only report coverage for the release version 70 | after_success: 71 | - test $TRAVIS_R_VERSION_STRING = 'release' && Rscript -e 'covr::codecov()' 72 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: dimRed 2 | Title: A Framework for Dimensionality Reduction 3 | Version: 0.2.7 4 | Authors@R: c( 5 | person("Guido", "Kraemer", email = "guido.kraemer@uni-leipzig.de", role = c("aut","cre"), 6 | comment = c(ORCID = "0000-0003-4865-5041") 7 | ) 8 | ) 9 | Description: A collection of dimensionality reduction 10 | techniques from R packages and a common 11 | interface for calling the methods. 12 | Depends: 13 | R (>= 3.0.0), 14 | DRR 15 | Imports: 16 | magrittr, 17 | methods 18 | Suggests: 19 | NMF, 20 | MASS, 21 | Matrix, 22 | RANN, 23 | RSpectra, 24 | Rtsne, 25 | cccd, 26 | coRanking, 27 | diffusionMap, 28 | energy, 29 | fastICA, 30 | ggplot2, 31 | graphics, 32 | igraph, 33 | keras, 34 | kernlab, 35 | knitr, 36 | optimx, 37 | pcaL1, 38 | pcaPP, 39 | reticulate, 40 | rgl, 41 | scales, 42 | scatterplot3d, 43 | stats, 44 | tensorflow, 45 | testthat, 46 | tidyr, 47 | tinytex, 48 | umap, 49 | vegan 50 | VignetteBuilder: 51 | knitr 52 | License: GPL-3 | file LICENSE 53 | BugReports: https://github.com/gdkrmr/dimRed/issues 54 | URL: https://www.guido-kraemer.com/software/dimred/ 55 | Encoding: UTF-8 56 | Collate: 57 | 'autoencoder.R' 58 | 'misc.R' 59 | 'dimRedData-class.R' 60 | 'dataSets.R' 61 | 'dimRedMethod-class.R' 62 | 'dimRedResult-class.R' 63 | 'diffmap.R' 64 | 'dimRed.R' 65 | 'drr.R' 66 | 'embed.R' 67 | 'fastica.R' 68 | 'get_info.R' 69 | 'graph_embed.R' 70 | 'hlle.R' 71 | 'isomap.R' 72 | 'kpca.R' 73 | 'l1pca.R' 74 | 'leim.R' 75 | 'lle.R' 76 | 'loe.R' 77 | 'mds.R' 78 | 'mixColorSpaces.R' 79 | 'nmds.R' 80 | 'nnmf.R' 81 | 'pca.R' 82 | 'plot.R' 83 | 'quality.R' 84 | 'rotate.R' 85 | 'soe.R' 86 | 'tsne.R' 87 | 'umap.R' 88 | RoxygenNote: 7.3.2 89 | Config/testthat/edition: 3 90 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(AUC_lnK_R_NX) 4 | export(DRR) 5 | export(DiffusionMaps) 6 | export(DrL) 7 | export(FastICA) 8 | export(FruchtermanReingold) 9 | export(HLLE) 10 | export(Isomap) 11 | export(KamadaKawai) 12 | export(LCMC) 13 | export(MDS) 14 | export(NNMF) 15 | export(PCA) 16 | export(PCA_L1) 17 | export(Q_NX) 18 | export(Q_global) 19 | export(Q_local) 20 | export(R_NX) 21 | export(UMAP) 22 | export(dataSetList) 23 | export(dimRedData) 24 | export(dimRedMethodList) 25 | export(dimRedQualityList) 26 | export(dimRedResult) 27 | export(distance_correlation) 28 | export(embed) 29 | export(getRotationMatrix) 30 | export(installSuggests) 31 | export(inverse) 32 | export(kPCA) 33 | export(loadDataSet) 34 | export(mean_R_NX) 35 | export(mixColor1Ramps) 36 | export(mixColor2Ramps) 37 | export(mixColor3Ramps) 38 | export(mixColorRamps) 39 | export(nMDS) 40 | export(plot) 41 | export(plot_R_NX) 42 | export(predict) 43 | export(quality) 44 | export(reconstruction_error) 45 | export(reconstruction_rmse) 46 | export(tSNE) 47 | export(total_correlation) 48 | exportClasses(DRR) 49 | exportClasses(DiffusionMaps) 50 | exportClasses(DrL) 51 | exportClasses(FastICA) 52 | exportClasses(FruchtermanReingold) 53 | exportClasses(HLLE) 54 | exportClasses(Isomap) 55 | exportClasses(KamadaKawai) 56 | exportClasses(MDS) 57 | exportClasses(NNMF) 58 | exportClasses(PCA) 59 | exportClasses(PCA_L1) 60 | exportClasses(UMAP) 61 | exportClasses(dimRedData) 62 | exportClasses(dimRedMethod) 63 | exportClasses(dimRedResult) 64 | exportClasses(kPCA) 65 | exportClasses(nMDS) 66 | exportClasses(tSNE) 67 | exportMethods("[") 68 | exportMethods(AUC_lnK_R_NX) 69 | exportMethods(LCMC) 70 | exportMethods(Q_NX) 71 | exportMethods(Q_global) 72 | exportMethods(Q_local) 73 | exportMethods(R_NX) 74 | exportMethods(as.data.frame) 75 | exportMethods(as.dimRedData) 76 | exportMethods(cophenetic_correlation) 77 | exportMethods(distance_correlation) 78 | exportMethods(embed) 79 | exportMethods(getData) 80 | exportMethods(getDimRedData) 81 | exportMethods(getMeta) 82 | exportMethods(getNDim) 83 | exportMethods(getOrgData) 84 | exportMethods(getOtherData) 85 | exportMethods(getPars) 86 | exportMethods(inverse) 87 | exportMethods(maximize_correlation) 88 | exportMethods(mean_R_NX) 89 | exportMethods(ndims) 90 | exportMethods(nrow) 91 | exportMethods(plot) 92 | exportMethods(predict) 93 | exportMethods(print) 94 | exportMethods(quality) 95 | exportMethods(reconstruction_error) 96 | exportMethods(reconstruction_rmse) 97 | exportMethods(total_correlation) 98 | import(DRR) 99 | import(methods) 100 | import(utils) 101 | importFrom(grDevices,colorRamp) 102 | importFrom(grDevices,rgb) 103 | importFrom(graphics,plot) 104 | importFrom(magrittr,"%>%") 105 | importFrom(stats,predict) 106 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # dimRed 0.2.1 and 0.2.2 2 | 3 | * Bugfix releases to pass CRAN tests 4 | 5 | # dimRed 0.2.0 6 | 7 | * Added the R-Journal [paper](https://journal.r-project.org/archive/2018/RJ-2018-039/index.html "dimRed and coRanking") as Vignette 8 | 9 | * Added UMAP 10 | 11 | * Added NMF (thanks @topepo) 12 | 13 | * Added the possibility to return other data such as distance 14 | matrices/eigenvalues 15 | 16 | * Added Autoencoder 17 | 18 | * Added l1 PCA 19 | 20 | * Added `getNDim` 21 | 22 | * Added an `ndim` parameter to many quality functions. 23 | 24 | * fixed bug in kPCA if inverse was not computable. 25 | 26 | * added autoencoder 27 | 28 | # dimRed 0.1.0 29 | 30 | * Fixed kPCA predict function and documentation typos (@topepo #2) 31 | 32 | * Added predict and inverse functions 33 | 34 | * Added a function to extract rotation matrices from PCA and FastICA 35 | 36 | # dimRed 0.0.3 37 | 38 | * First version on CRAN 39 | -------------------------------------------------------------------------------- /R/diffmap.R: -------------------------------------------------------------------------------- 1 | #' Diffusion Maps 2 | #' 3 | #' An S4 Class implementing Diffusion Maps 4 | #' 5 | #' Diffusion Maps uses a diffusion probability matrix to robustly 6 | #' approximate a manifold. 7 | #' 8 | #' 9 | #' @template dimRedMethodSlots 10 | #' 11 | #' @template dimRedMethodGeneralUsage 12 | #' 13 | #' @section Parameters: 14 | #' Diffusion Maps can take the following parameters: 15 | #' \describe{ 16 | #' \item{d}{a function transforming a matrix row wise into a 17 | #' distance matrix or \code{dist} object, 18 | #' e.g. \code{\link[stats]{dist}}.} 19 | #' \item{ndim}{The number of dimensions} 20 | #' \item{eps}{The epsilon parameter that determines the 21 | #' diffusion weight matrix from a distance matrix \code{d}, 22 | #' \eqn{exp(-d^2/eps)}, if set to \code{"auto"} it will 23 | #' be set to the median distance to the 0.01*n nearest 24 | #' neighbor.} 25 | #' \item{t}{Time-scale parameter. The recommended value, 0, 26 | #' uses multiscale geometry.} 27 | #' \item{delta}{Sparsity cut-off for the symmetric graph Laplacian, 28 | #' a higher value results in more sparsity and faster calculation. 29 | #' The predefined value is 10^-5.} 30 | #' } 31 | #' 32 | #' @section Implementation: 33 | #' Wraps around \code{\link[diffusionMap]{diffuse}}, see there for 34 | #' details. It uses the notation of Richards et al. (2009) which is 35 | #' slightly different from the one in the original paper (Coifman and 36 | #' Lafon, 2006) and there is no \eqn{\alpha} parameter. 37 | #' There is also an out-of-sample extension, see examples. 38 | #' 39 | #' 40 | #' @references 41 | #' Richards, J.W., Freeman, P.E., Lee, A.B., Schafer, 42 | #' C.M., 2009. Exploiting Low-Dimensional Structure in 43 | #' Astronomical Spectra. ApJ 691, 44 | #' 32. doi:10.1088/0004-637X/691/1/32 45 | #' 46 | #' Coifman, R.R., Lafon, S., 2006. Diffusion maps. Applied and 47 | #' Computational Harmonic Analysis 21, 48 | #' 5-30. doi:10.1016/j.acha.2006.04.006 49 | #' 50 | #' @examples 51 | #' if(requireNamespace("diffusionMap", quietly = TRUE)) { 52 | 53 | #' dat <- loadDataSet("3D S Curve", n = 300) 54 | #' emb <- embed(dat, "DiffusionMaps") 55 | #' 56 | #' plot(emb, type = "2vars") 57 | #' 58 | #' # predicting is possible: 59 | #' samp <- sample(floor(nrow(dat) / 10)) 60 | #' emb2 <- embed(dat[samp]) 61 | #' emb3 <- predict(emb2, dat[-samp]) 62 | #' 63 | #' plot(emb2, type = "2vars") 64 | #' points(getData(emb3)) 65 | 66 | #' } 67 | #' @include dimRedResult-class.R 68 | #' @include dimRedMethod-class.R 69 | #' @family dimensionality reduction methods 70 | #' @export DiffusionMaps 71 | #' @exportClass DiffusionMaps 72 | DiffusionMaps <- setClass( 73 | "DiffusionMaps", 74 | contains = "dimRedMethod", 75 | prototype = list( 76 | stdpars = list(d = stats::dist, 77 | ndim = 2, 78 | eps = "auto", 79 | t = 0, 80 | delta = 1e-5), 81 | fun = function (data, pars, 82 | keep.org.data = TRUE) { 83 | chckpkg("diffusionMap") 84 | 85 | meta <- data@meta 86 | orgdata <- if (keep.org.data) data@data else NULL 87 | indata <- data@data 88 | 89 | distmat <- pars$d(indata) 90 | if (pars$eps == "auto") 91 | pars$eps <- diffusionMap::epsilonCompute(distmat) 92 | diffres <- diffusionMap::diffuse( 93 | D = distmat, 94 | t = pars$t, 95 | eps.val = pars$eps, 96 | neigen = pars$ndim, 97 | maxdim = pars$ndim, 98 | delta = pars$delta 99 | ) 100 | outdata <- as.matrix(diffres$X) 101 | 102 | 103 | appl <- function(x) { 104 | appl.meta <- if (inherits(x, "dimRedData")) x@meta else data.frame() 105 | proj <- if (inherits(x, "dimRedData")) x@data else x 106 | 107 | if (ncol(proj) != ncol(data@data)) 108 | stop("x must have the same number of dimensions ", 109 | "as the original data") 110 | 111 | dd <- sqrt(pdist2(proj, indata)) 112 | 113 | appl.res <- 114 | diffusionMap::nystrom(diffres, dd, sigma = diffres$epsilon) 115 | dimnames(appl.res) <- list( 116 | rownames(x), paste0("diffMap", seq_len(ncol(outdata))) 117 | ) 118 | 119 | new("dimRedData", data = appl.res, meta = appl.meta) 120 | } 121 | 122 | colnames(outdata) <- paste0("diffMap", seq_len(ncol(outdata))) 123 | 124 | return(new( 125 | "dimRedResult", 126 | data = new("dimRedData", 127 | data = outdata, 128 | meta = meta), 129 | org.data = orgdata, 130 | apply = appl, 131 | has.apply = TRUE, 132 | has.org.data = keep.org.data, 133 | method = "diffmap", 134 | pars = pars 135 | )) 136 | }, 137 | requires = c("diffusionMap")) 138 | ) 139 | -------------------------------------------------------------------------------- /R/dimRed.R: -------------------------------------------------------------------------------- 1 | #' @title The dimRed package 2 | #' 3 | #' @description This package simplifies dimensionality reduction in R by 4 | #' providing a framework of S4 classes and methods. dimRed collects 5 | #' dimensionality reduction methods that are implemented in R and implements 6 | #' others. It gives them a common interface and provides plotting 7 | #' functions for visualization and functions for quality assessment. 8 | #' 9 | #' Funding provided by the Department for Biogeochemical Integration, 10 | #' Empirical Inference of the Earth System Group, at the Max Plack 11 | #' Institute for Biogeochemistry, Jena. 12 | #' 13 | #' @references 14 | #' 15 | #' Lee, J.A., Renard, E., Bernard, G., Dupont, P., Verleysen, M., 16 | #' 2013. Type 1 and 2 mixtures of Kullback-Leibler divergences as cost 17 | #' functions in dimensionality reduction based on similarity 18 | #' preservation. Neurocomputing. 112, 19 | #' 92-107. doi:10.1016/j.neucom.2012.12.036 20 | #' 21 | #' Lee, J.A., Lee, J.A., Verleysen, M., 2008. Rank-based quality 22 | #' assessment of nonlinear dimensionality reduction. Proceedings of 23 | #' ESANN 2008 49-54. 24 | #' 25 | #' Chen, L., Buja, A., 2006. Local Multidimensional Scaling for 26 | #' Nonlinear Dimension Reduction, Graph Layout and Proximity Analysis. 27 | #' 28 | #' 29 | #' @import methods 30 | #' @importFrom magrittr %>% 31 | #' 32 | "_PACKAGE" 33 | -------------------------------------------------------------------------------- /R/dimRedMethod-class.R: -------------------------------------------------------------------------------- 1 | #' Class "dimRedMethod" 2 | #' 3 | #' A virtual class "dimRedMethod" to serve as a template to implement 4 | #' methods for dimensionality reduction. 5 | #' 6 | #' Implementations of dimensionality reductions should inherit from 7 | #' this class. 8 | #' 9 | #' The \code{fun} slot should be a function that takes three arguments 10 | #' \describe{ 11 | #' \item{data}{An object of class \code{\link{dimRedData}}.} 12 | #' \item{pars}{A list with the standard parameters.} 13 | #' \item{keep.org.data}{Logical. If the original data should be kept in the output.} 14 | #' } 15 | #' and returns an object of class \code{\link{dimRedResult}}. 16 | #' 17 | #' The \code{stdpars} slot should take a list that contains standard 18 | #' parameters for the implemented methods. 19 | #' 20 | #' This way the method can be called by \code{embed(data, "method-name", 21 | #' ...)}, where \code{...} can be used to to change single parameters. 22 | #' 23 | #' 24 | #' @slot fun A function that does the embedding. 25 | #' @slot stdpars A list with the default parameters for the \code{fun} slot. 26 | #' @slot requires A vector with all packages R packages that need to be 27 | #' installed to run the method. In some occasions a method may work without 28 | #' one of the packages. Does not include Python dependencies such as 29 | #' Tensorflow. Used to auto skip tests 30 | #' 31 | #' @family dimensionality reduction methods 32 | #' @export 33 | setClass("dimRedMethod", 34 | contains = "VIRTUAL", 35 | slots = c(fun = "function", 36 | stdpars = "list", 37 | requires = "character")) 38 | 39 | 40 | #' dimRedMethodList 41 | #' 42 | #' Get the names of all methods for dimensionality reduction. 43 | #' 44 | #' Returns the name of all classes that inherit from 45 | #' \code{\link{dimRedMethod-class}} to use with \code{\link{embed}}. 46 | #' @param filter filter methods by methods that have their dependencies installed 47 | #' @return a character vector with the names of classes that inherit 48 | #' from \code{dimRedMethod}. 49 | #' 50 | #' @examples 51 | #' dimRedMethodList() 52 | #' 53 | #' @family dimensionality reduction methods 54 | #' @export 55 | dimRedMethodList <- function (filter = FALSE) { 56 | all_methods <- names(completeClassDefinition("dimRedMethod", doExtends = FALSE)@subclasses) 57 | if(!filter) 58 | return(all_methods) 59 | all_deps <- lapply(all_methods, getMethodDependencies) 60 | is_possible <- sapply(all_deps, function(x) { 61 | if(length(x) > 0) 62 | requireNamespace(x, quietly = TRUE) 63 | else 64 | TRUE 65 | }) 66 | all_methods[is_possible] 67 | } 68 | 69 | 70 | # to put standard values for omitted arguments 71 | 72 | setGeneric("matchPars", function(object, pars) standardGeneric("matchPars"), 73 | valueClass = c("list")) 74 | 75 | 76 | setMethod("matchPars", 77 | signature(object = "dimRedMethod", 78 | pars = "list"), 79 | definition = function(object, pars) { 80 | nsp <- names(object@stdpars) 81 | ncp <- names(pars) 82 | nap <- union(nsp, ncp) 83 | 84 | res <- list() 85 | 86 | ## exists can deal with elements being NULL 87 | ## to assign list@el <- NULL do: 88 | ## list["el"] <- list(NULL) 89 | for (np in nap) { 90 | miss.std <- !exists(np, where = object@stdpars) 91 | miss.par <- !exists(np, where = pars) 92 | if (miss.std) { 93 | warning("Parameter matching: ", np, 94 | " is not a standard parameter, ignoring.") 95 | } else if (miss.par) { 96 | res[np] <- object@stdpars[np] 97 | } else { 98 | res[np] <- pars[np] 99 | } 100 | } 101 | 102 | ## if the method does not accept parameters we have to return 103 | ## null, so in embed there is no args$par created. and passed by 104 | ## do.call in the embed() function. if (length(res) != 0) 105 | ## return(res) else return(NULL) 106 | 107 | ## first try without the above, all methods should have a pars 108 | ## argument. 109 | return(res) 110 | }) 111 | 112 | getMethodDependencies <- function(method) { 113 | getMethodObject(method)@requires 114 | } 115 | 116 | method_can_run <- function(method) { 117 | all(getMethodDependencies(method) %in% row.names(installed.packages())) 118 | } 119 | -------------------------------------------------------------------------------- /R/fastica.R: -------------------------------------------------------------------------------- 1 | #' Independent Component Analysis 2 | #' 3 | #' An S4 Class implementing the FastICA algorithm for Indepentend 4 | #' Component Analysis. 5 | #' 6 | #' ICA is used for blind signal separation of different sources. It is 7 | #' a linear Projection. 8 | #' 9 | #' @template dimRedMethodSlots 10 | #' 11 | #' @template dimRedMethodGeneralUsage 12 | #' 13 | #' @section Parameters: 14 | #' FastICA can take the following parameters: 15 | #' \describe{ 16 | #' \item{ndim}{The number of output dimensions. Defaults to \code{2}} 17 | #' } 18 | #' 19 | #' @section Implementation: 20 | #' Wraps around \code{\link[fastICA]{fastICA}}. FastICA uses a very 21 | #' fast approximation for negentropy to estimate statistical 22 | #' independences between signals. Because it is a simple 23 | #' rotation/projection, forward and backward functions can be given. 24 | #' 25 | #' @references 26 | #' 27 | #' Hyvarinen, A., 1999. Fast and robust fixed-point algorithms for independent 28 | #' component analysis. IEEE Transactions on Neural Networks 10, 626-634. 29 | #' https://doi.org/10.1109/72.761722 30 | #' 31 | #' @examples 32 | #' if(requireNamespace("fastICA", quietly = TRUE)) { 33 | #' 34 | #' dat <- loadDataSet("3D S Curve") 35 | #' emb <- embed(dat, "FastICA", ndim = 2) 36 | #' plot(getData(getDimRedData(emb))) 37 | #' 38 | #' } 39 | #' @include dimRedResult-class.R 40 | #' @include dimRedMethod-class.R 41 | #' @family dimensionality reduction methods 42 | #' @export FastICA 43 | #' @exportClass FastICA 44 | FastICA <- setClass( 45 | "FastICA", 46 | contains = "dimRedMethod", 47 | prototype = list( 48 | stdpars = list(ndim = 2), 49 | fun = function (data, 50 | pars, 51 | keep.org.data = TRUE) { 52 | chckpkg("fastICA") 53 | 54 | meta <- data@meta 55 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 56 | orgdata.colmeans <- colMeans(orgdata) 57 | indata <- data@data 58 | 59 | res <- fastICA::fastICA(indata, n.comp = pars$ndim, method = "C") 60 | 61 | outdata <- res$S 62 | colnames(outdata) <- paste0("ICA", 1:ncol(outdata)) 63 | 64 | appl <- function(x){ 65 | appl.meta <- if (inherits(x, "dimRedData")) 66 | x@meta 67 | else 68 | matrix(numeric(0), 0, 0) 69 | 70 | proj <- if (inherits(x, "dimRedData")) 71 | x@data 72 | else 73 | x 74 | 75 | out <- scale(proj, center = orgdata.colmeans, scale = FALSE) %*% 76 | res$K %*% 77 | res$W 78 | colnames(out) <- paste0("ICA", 1:ncol(out)) 79 | return(new("dimRedData", data = out, meta = appl.meta)) 80 | } 81 | 82 | inv <- function(x){ 83 | appl.meta <- if (inherits(x, "dimRedData")) 84 | x@meta 85 | else 86 | matrix(numeric(0), 0, 0) 87 | 88 | proj <- if (inherits(x, "dimRedData")) 89 | x@data 90 | else 91 | x 92 | 93 | out <- scale(proj %*% res$A[1:ncol(proj), ], 94 | center = -orgdata.colmeans, 95 | scale = FALSE) 96 | reproj <- new("dimRedData", data = out, meta = appl.meta) 97 | return(reproj) 98 | } 99 | 100 | 101 | return(new( 102 | "dimRedResult", 103 | data = new("dimRedData", 104 | data = outdata, 105 | meta = meta), 106 | org.data = orgdata, 107 | has.org.data = keep.org.data, 108 | apply = appl, 109 | inverse = inv, 110 | has.apply = TRUE, 111 | has.inverse = TRUE, 112 | method = "FastICA", 113 | pars = pars 114 | )) 115 | }, 116 | requires = c("fastICA")) 117 | ) 118 | -------------------------------------------------------------------------------- /R/get_info.R: -------------------------------------------------------------------------------- 1 | #' getRotationMatrix 2 | #' 3 | #' Extract the rotation matrix from \code{\link{dimRedResult}} objects derived from PCA and FastICA 4 | #' 5 | #' The data has to be pre-processed the same way as the method does, e.g. 6 | #' centering and/or scaling. 7 | #' 8 | #' @param x of type \code{\link{dimRedResult}} 9 | #' @return a matrix 10 | #' 11 | #' @examples 12 | #' dat <- loadDataSet("Iris") 13 | #' 14 | #' pca <- embed(dat, "PCA") 15 | #' rot_pca <- getRotationMatrix(pca) 16 | #' scale(getData(dat), TRUE, FALSE) %*% rot_pca - getData(getDimRedData(pca)) 17 | #' 18 | #' 19 | #' if(requireNamespace("fastICA", quietly = TRUE)) { 20 | #' ica <- embed(dat, "FastICA") 21 | #' rot_ica <- getRotationMatrix(ica) 22 | #' scale(getData(dat), TRUE, FALSE) %*% rot_ica - getData(getDimRedData(ica)) 23 | #' } 24 | #' 25 | #' 26 | #' @family convenience functions 27 | #' @export 28 | getRotationMatrix <- function(x) { 29 | if(!inherits(x, "dimRedResult")) stop("x must be of type 'dimRedResult'") 30 | if(x@method == "PCA") return(environment(x@apply)$rot) 31 | if(x@method == "PCA_L1") return(environment(x@apply)$rot) 32 | if(x@method == "FastICA") return(environment(x@apply)$res$K %*% environment(x@apply)$res$W) 33 | stop(paste("Not implemented for", x@method)) 34 | } 35 | -------------------------------------------------------------------------------- /R/hlle.R: -------------------------------------------------------------------------------- 1 | #' Hessian Locally Linear Embedding 2 | #' 3 | #' An S4 Class implementing Hessian Locally Linear Embedding (HLLE) 4 | #' 5 | #' HLLE uses local hessians to approximate the curvines and is an 6 | #' extension to non-convex subsets in lowdimensional space. 7 | #' 8 | #' @template dimRedMethodSlots 9 | #' 10 | #' @template dimRedMethodGeneralUsage 11 | #' 12 | #' @section Parameters: 13 | #' HLLE can take the following parameters: 14 | #' \describe{ 15 | #' \item{knn}{neighborhood size} 16 | #' \item{ndim}{number of output dimensions} 17 | #' } 18 | #' 19 | #' @section Implementation: 20 | #' Own implementation, sticks to the algorithm in Donoho and Grimes 21 | #' (2003). Makes use of sparsity to speed up final embedding. 22 | #' 23 | #' @references 24 | #' Donoho, D.L., Grimes, C., 2003. Hessian eigenmaps: Locally linear 25 | #' embedding techniques for high-dimensional data. PNAS 100, 26 | #' 5591-5596. doi:10.1073/pnas.1031596100 27 | #' 28 | #' @examples 29 | #' if(requireNamespace(c("RSpectra", "Matrix", "RANN"), quietly = TRUE)) { 30 | #' 31 | #' dat <- loadDataSet("3D S Curve", n = 300) 32 | #' emb <- embed(dat, "HLLE", knn = 15) 33 | #' plot(emb, type = "2vars") 34 | #' 35 | #' } 36 | #' 37 | #' @include dimRedResult-class.R 38 | #' @include dimRedMethod-class.R 39 | #' @family dimensionality reduction methods 40 | #' @export HLLE 41 | #' @exportClass HLLE 42 | HLLE <- setClass( 43 | "HLLE", 44 | contains = "dimRedMethod", 45 | prototype = list( 46 | stdpars = list(knn = 50, ndim = 2), 47 | fun = function(data, pars, 48 | keep.org.data = TRUE) { 49 | chckpkg("RSpectra") 50 | chckpkg("Matrix") 51 | chckpkg("RANN") 52 | 53 | if (pars$ndim < 2) stop("ndim must be 2 or larger.") 54 | 55 | if (is.null(pars$knn)) pars$knn <- 50 56 | if (is.null(pars$ndim)) pars$ndim <- 2 57 | 58 | indata <- data@data 59 | n <- nrow(indata) 60 | hs <- pars$ndim * (pars$ndim + 1) / 2 61 | W <- Matrix::sparseMatrix(i = numeric(0), 62 | j = numeric(0), 63 | x = numeric(0), 64 | dims = c(n, hs * n)) 65 | ii <- jj <- ww <- list() 66 | ## Identify neighbors: 67 | message(Sys.time(), ": Finding nearest neighbors", sep = "") 68 | nnidx <- RANN::nn2(data = indata, query = indata, k = pars$knn + 1, 69 | treetype = "kd", "standard", eps = 0)$nn.idx#[, -1] 70 | message(Sys.time(), ": Calculating Hessian", sep = "") 71 | for (i in seq_len(n)) { 72 | cat(i, "/", n, "\r", sep = "") 73 | ## get neighborhood 74 | Nui <- indata[nnidx[i, ], , drop = FALSE] 75 | 76 | ## Form tangent coordinates: 77 | Nui <- sweep(Nui, 2, colMeans(Nui), "-") 78 | tc <- svd(Nui, nu = pars$ndim, nv = 0)$u 79 | 80 | ## Develop Hessian Estimator 81 | Xi <- cbind( 82 | 1, tc, tc ^ 2, 83 | apply(combn(seq_len(pars$ndim), 2), 2, 84 | function(x) tc[, x[1]] * tc[, x[2]]) 85 | ) 86 | tHi <- qr.Q(qr(Xi))[, -(1:(pars$ndim + 1)), 87 | drop = FALSE] 88 | 89 | ## Add quadratic form to hessian 90 | ii[[i]] <- rep(nnidx[i, ], hs) 91 | jj[[i]] <- rep((i - 1) * hs + (1:hs), each = ncol(nnidx)) 92 | ww[[i]] <- as.vector(tHi) 93 | } 94 | H <- as(Matrix::tcrossprod(Matrix::spMatrix( 95 | i = unlist(ii, FALSE, FALSE), 96 | j = unlist(jj, FALSE, FALSE), 97 | x = unlist(ww, FALSE, FALSE), 98 | nrow = n, ncol = n * hs) 99 | ), "dgCMatrix") 100 | 101 | ## Find null space: 102 | message(Sys.time(), ": Embedding", sep = "") 103 | ## eigs and eigs_sym converges much more reliably and faster 104 | ## with sigma = -eps than with which = "L*" 105 | outdata <- RSpectra::eigs_sym(H, k = pars$ndim + 1, sigma = -1e-5) 106 | 107 | message(paste(c("Eigenvalues:", format(outdata$values)), 108 | collapse = " ")) 109 | outdata <- outdata$vectors[, order(outdata$values)[-1], drop = FALSE] 110 | 111 | colnames(outdata) <- paste0("HLLE", seq_len(ncol(outdata))) 112 | 113 | message(Sys.time(), ": DONE", sep = "") 114 | return(new( 115 | "dimRedResult", 116 | data = new("dimRedData", 117 | data = outdata, 118 | meta = data@meta), 119 | org.data = if (keep.org.data) data@data else matrix(0, 0, 0), 120 | has.org.data = keep.org.data, 121 | method = "HLLE", 122 | pars = pars 123 | )) 124 | }, 125 | requires = c("RSpectra", "Matrix", "RANN")) 126 | ) 127 | -------------------------------------------------------------------------------- /R/kpca.R: -------------------------------------------------------------------------------- 1 | #' Kernel PCA 2 | #' 3 | #' An S4 Class implementing Kernel PCA 4 | #' 5 | #' Kernel PCA is a nonlinear extension of PCA using kernel methods. 6 | #' 7 | #' 8 | #' @template dimRedMethodSlots 9 | #' 10 | #' @template dimRedMethodGeneralUsage 11 | #' 12 | #' @section Parameters: 13 | #' Kernel PCA can take the following parameters: 14 | #' \describe{ 15 | #' \item{ndim}{the number of output dimensions, defaults to 2} 16 | #' \item{kernel}{The kernel function, either as a function or a 17 | #' character vector with the name of the kernel. Defaults to 18 | #' \code{"rbfdot"}} 19 | #' \item{kpar}{A list with the parameters for the kernel function, 20 | #' defaults to \code{list(sigma = 0.1)}} 21 | #' } 22 | #' 23 | #' The most comprehensive collection of kernel functions can be found in 24 | #' \code{\link[kernlab]{kpca}}. In case the function does not take any 25 | #' parameters \code{kpar} has to be an empty list. 26 | #' 27 | #' @section Implementation: 28 | #' 29 | #' Wraps around \code{\link[kernlab]{kpca}}, but provides additionally 30 | #' forward and backward projections. 31 | #' 32 | #' @references 33 | #' 34 | #' Sch\"olkopf, B., Smola, A., M\"uller, K.-R., 1998. Nonlinear Component Analysis 35 | #' as a Kernel Eigenvalue Problem. Neural Computation 10, 1299-1319. 36 | #' https://doi.org/10.1162/089976698300017467 37 | #' 38 | #' @examples 39 | #' \dontrun{ 40 | #' if(requireNamespace("kernlab", quietly = TRUE)) { 41 | #' 42 | #' dat <- loadDataSet("3D S Curve") 43 | #' emb <- embed(dat, "kPCA") 44 | #' plot(emb, type = "2vars") 45 | #' } 46 | #' 47 | #' } 48 | #' @include dimRedResult-class.R 49 | #' @include dimRedMethod-class.R 50 | #' @family dimensionality reduction methods 51 | #' @export kPCA 52 | #' @exportClass kPCA 53 | kPCA <- setClass( 54 | "kPCA", 55 | contains = "dimRedMethod", 56 | prototype = list( 57 | stdpars = list(kernel = "rbfdot", 58 | kpar = list(sigma = 0.1), 59 | ndim = 2), 60 | fun = function (data, pars, 61 | keep.org.data = TRUE) { 62 | 63 | chckpkg("kernlab") 64 | 65 | if (is.null(pars$ndim)) pars$ndim <- 2 66 | 67 | meta <- data@meta 68 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 69 | indata <- data@data 70 | 71 | message(Sys.time(), ": Calculating kernel PCA") 72 | res <- do.call(kernlab::kpca, c(list(x = indata), pars)) 73 | 74 | kernel <- get_kernel_fun(pars$kernel, pars$kpar) 75 | 76 | message(Sys.time(), ": Trying to calculate reverse") 77 | K_rev <- kernlab::kernelMatrix(kernel, res@rotated) 78 | diag(K_rev) <- 0.1 + diag(K_rev) 79 | dual_coef <- try(solve(K_rev, indata), silent = TRUE) 80 | 81 | appl <- function (x) { 82 | appl.meta <- if (inherits(x, "dimRedData")) x@meta else data.frame() 83 | proj <- if (inherits(x, "dimRedData")) x@data else x 84 | 85 | proj <- kernlab::predict(res, proj)[, 1:pars$ndim, drop = FALSE] 86 | colnames(proj) <- paste0("kPCA", 1:ncol(proj)) 87 | 88 | new("dimRedData", data = proj, meta = appl.meta) 89 | } 90 | 91 | inv <- 92 | if (inherits(dual_coef, "try-error")) { 93 | message("No inverse function.") 94 | function(x) NA 95 | } else { 96 | function (x) { 97 | appl.meta <- 98 | if (inherits(x, "dimRedData")) x@meta else data.frame() 99 | proj <- if (inherits(x, "dimRedData")) x@data else x 100 | 101 | resrot <- res@rotated[, 1:ncol(proj)] 102 | rot <- kernlab::kernelMatrix(kernel, proj, resrot) 103 | proj <- rot %*% dual_coef 104 | 105 | new("dimRedData", data = proj, meta = appl.meta) 106 | } 107 | } 108 | 109 | 110 | outdata <- res@rotated[, 1:pars$ndim, drop = FALSE] 111 | colnames(outdata) <- paste0("kPCA", 1:ncol(outdata)) 112 | 113 | message(Sys.time(), ": DONE") 114 | return( 115 | new( 116 | "dimRedResult", 117 | data = new("dimRedData", 118 | data = outdata, 119 | meta = meta), 120 | org.data = orgdata, 121 | apply = appl, 122 | inverse = inv, 123 | has.org.data = keep.org.data, 124 | has.apply = TRUE, 125 | has.inverse = TRUE, 126 | method = "kpca", 127 | pars = pars 128 | ) 129 | ) 130 | }, 131 | requires = c("kernlab")) 132 | ) 133 | 134 | 135 | ## get the kernel function out of the kernlab namespace: 136 | get_kernel_fun <- function (kernel, pars) { 137 | if (!is(kernel, "kernel")) { 138 | if (is(kernel, "function")) { 139 | kernel <- deparse(substitute(kernel)) 140 | } else { 141 | kernel <- get(kernel, asNamespace("kernlab")) 142 | } 143 | kernel <- do.call(kernel, pars) 144 | } 145 | return(kernel) 146 | } 147 | -------------------------------------------------------------------------------- /R/lle.R: -------------------------------------------------------------------------------- 1 | ## #' Locally Linear Embedding 2 | ## #' 3 | ## #' An S4 Class implementing Locally Linear Embedding (LLE) 4 | ## #' 5 | ## #' LLE approximates the points in the manifold by linear combination 6 | ## #' of its neighbors. These linear combinations are the same inside the 7 | ## #' manifold and in highdimensional space. 8 | ## #' 9 | ## #' @template dimRedMethodSlots 10 | ## #' 11 | ## #' @template dimRedMethodGeneralUsage 12 | ## #' 13 | ## #' @section Parameters: 14 | ## #' LLE can take the following parameters: 15 | ## #' \describe{ 16 | ## #' \item{knn}{the number of neighbors for the knn graph., defaults to 50.} 17 | ## #' \item{ndim}{the number of embedding dimensions, defaults to 2.} 18 | ## #' } 19 | ## #' 20 | ## #' @section Implementation: 21 | ## #' Wraps around \code{\link[lle]{lle}}, only 22 | ## #' exposes the parameters \code{k} and \code{m}. 23 | ## #' 24 | ## #' @references 25 | ## #' 26 | ## #' Roweis, S.T., Saul, L.K., 2000. Nonlinear Dimensionality Reduction 27 | ## #' by Locally Linear Embedding. Science 290, 28 | ## #' 2323-2326. doi:10.1126/science.290.5500.2323 29 | ## #' 30 | ## #' @examples 31 | ## #' \dontrun{ 32 | ## #' if(requireNamespace("lle")) { 33 | ## #' 34 | ## #' dat <- loadDataSet("3D S Curve", n = 500) 35 | ## #' emb <- embed(dat, "LLE", knn = 45) 36 | ## #' plot(emb, type = "2vars") 37 | ## #' 38 | ## #' } 39 | ## #' } 40 | ## #' @include dimRedResult-class.R 41 | ## #' @include dimRedMethod-class.R 42 | ## #' @family dimensionality reduction methods 43 | ## #' @export LLE 44 | ## #' @exportClass LLE 45 | ## LLE <- setClass( 46 | ## "LLE", 47 | ## contains = "dimRedMethod", 48 | ## prototype = list( 49 | ## stdpars = list(knn = 50, ndim = 2), 50 | ## fun = function (data, pars, 51 | ## keep.org.data = TRUE) { 52 | ## chckpkg("lle") 53 | ## meta <- data@meta 54 | ## orgdata <- if (keep.org.data) data@data else NULL 55 | ## indata <- data@data 56 | 57 | ## outdata <- lle::lle(indata, 58 | ## k = pars$knn, 59 | ## m = pars$ndim)$Y 60 | ## if (is.null(dim(outdata))) { 61 | ## dim(outdata) <- c(length(outdata), 1) 62 | ## } 63 | ## colnames(outdata) <- paste0("LLE", 1:ncol(outdata)) 64 | 65 | ## return(new( 66 | ## "dimRedResult", 67 | ## data = new("dimRedData", 68 | ## data = outdata, 69 | ## meta = meta), 70 | ## org.data = orgdata, 71 | ## has.org.data = keep.org.data, 72 | ## method = "lle", 73 | ## pars = pars 74 | ## )) 75 | ## }, 76 | ## requires = c("lle") 77 | ## ) 78 | ## ) 79 | -------------------------------------------------------------------------------- /R/loe.R: -------------------------------------------------------------------------------- 1 | 2 | ## this function produces segfaults and is super slow 3 | 4 | ## #' Local Ordinal Embedding 5 | ## #' 6 | ## #' Instance of \code{\link{dimRedMethod}} for Local Ordinal Embedding. 7 | ## #' 8 | ## #' For details see \code{\link[loe]{LOE}} 9 | ## #' 10 | ## #' @examples 11 | ## #' # for whatever reason the loe package has problems if I run this 12 | ## #' # with R CMD check, running it in the REPL works just fine 13 | ## #' if(requireNamespace("loe")) { 14 | ## #' dat <- loadDataSet("Iris")[sample(20)] 15 | ## #' loe <- LOE() 16 | ## #' emb <- loe@fun(dat, loe@stdpars) 17 | ## #' 18 | ## #' 19 | ## #' plot(emb@data@data) 20 | ## #' } 21 | ## #' @include dimRedResult-class.R 22 | ## #' @include dimRedMethod-class.R 23 | ## #' @export 24 | ## LOE <- setClass( 25 | ## "LOE", 26 | ## contains = "dimRedMethod", 27 | ## prototype = list( 28 | ## stdpars = list(d = stats::dist, knn = 50, ndim = 2), 29 | ## fun = function (data, pars, 30 | ## keep.org.data = TRUE) { 31 | ## chckpkg("loe") 32 | 33 | ## meta <- data@meta 34 | ## orgdata <- if (keep.org.data) data@data else NULL 35 | ## indata <- data@data 36 | 37 | ## data.adj <- loe:::make.kNNG(as.matrix(pars$d(indata)), k = pars$knn) 38 | ## outdata <- loe::LOE(data.adj, p = pars$ndim, method = "MM")$X 39 | 40 | ## colnames(outdata) <- paste0("LOE", 1:ncol(outdata)) 41 | 42 | ## return(new( 43 | ## "dimRedResult", 44 | ## data = new("dimRedData", 45 | ## data = outdata, 46 | ## meta = meta), 47 | ## org.data = orgdata, 48 | ## has.org.data = keep.org.data, 49 | ## method = "loe", 50 | ## pars = pars 51 | ## )) 52 | ## }) 53 | ## ) 54 | -------------------------------------------------------------------------------- /R/mixColorSpaces.R: -------------------------------------------------------------------------------- 1 | #' Mixing color ramps 2 | #' 3 | #' mix different color ramps 4 | #' 5 | #' automatically create colors to represent a varying number of 6 | #' dimensions. 7 | #' 8 | #' @param vars a list of variables 9 | #' @param ramps a list of color ramps, one for each variable. 10 | #' 11 | #' @examples 12 | #' cols <- expand.grid(x = seq(0, 1, length.out = 10), 13 | #' y = seq(0, 1, length.out = 10), 14 | #' z = seq(0, 1, length.out = 10)) 15 | #' mixed <- mixColor3Ramps(cols) 16 | #' 17 | #' \dontrun{ 18 | #' if(requireNamespace("rgl", quietly = TRUE)) { 19 | #' rgl::plot3d(cols$x, cols$y, cols$z, col = mixed, pch = 15) 20 | #' } 21 | #' 22 | #' cols <- expand.grid(x = seq(0, 1, length.out = 10), 23 | #' y = seq(0, 1, length.out = 10)) 24 | #' mixed <- mixColor2Ramps(cols) 25 | #' 26 | #' if(requireNamespace("graphics", quietly = TRUE)) { 27 | #' plot(cols$x, cols$y, col = mixed, pch = 15) 28 | #' } 29 | #' } 30 | #' @importFrom grDevices colorRamp 31 | #' @importFrom grDevices rgb 32 | #' @export 33 | mixColorRamps <- function (vars, ramps) { 34 | if (length(vars) > length(ramps)) stop("need more or equal ramps than vars") 35 | 36 | nvars <- length(vars) 37 | 38 | rgbs <- list() 39 | for (i in 1:nvars){ 40 | rgbs[[i]] <- ramps[[i]](scale01(as.numeric(vars[[i]]))) 41 | } 42 | 43 | retrgb <- Reduce(`+`, rgbs) 44 | 45 | res <- apply(retrgb, 2, function(x) (x - min(x)) / (max(x) - min(x))) 46 | res[is.nan(res)] <- 0 47 | 48 | return(rgb(res)) 49 | } 50 | 51 | #' @rdname mixColorRamps 52 | #' @export 53 | mixColor1Ramps <- function (vars, 54 | ramps = colorRamp(c("blue", "black", "red"))) { 55 | mixColorRamps(vars, list(ramps)) 56 | } 57 | 58 | #' @rdname mixColorRamps 59 | #' @export 60 | mixColor2Ramps <- function (vars, 61 | ramps = list(colorRamp(c("blue", "green")), 62 | colorRamp(c("blue", "red")))) { 63 | mixColorRamps(vars, ramps) 64 | } 65 | 66 | #' @rdname mixColorRamps 67 | #' @export 68 | mixColor3Ramps <- function (vars, 69 | ramps = list(colorRamp(c("#001A00", "#00E600")), 70 | colorRamp(c("#00001A", "#0000E6")), 71 | colorRamp(c("#1A0000", "#E60000")))) { 72 | mixColorRamps(vars, ramps) 73 | } 74 | 75 | 76 | colorize <- function (vars) { 77 | l <- length(vars) 78 | if (l == 1) return(mixColor1Ramps(vars)) 79 | if (l == 2) return(mixColor2Ramps(vars)) 80 | if (l == 3) return(mixColor3Ramps(vars)) 81 | return("#000000") 82 | } 83 | 84 | scale01 <- function(x, 85 | low = min(x, na.rm = TRUE), 86 | high = max(x, na.rm = FALSE)) { 87 | x <- (x - low) / (high - low) 88 | x 89 | } 90 | -------------------------------------------------------------------------------- /R/nmds.R: -------------------------------------------------------------------------------- 1 | #' Non-Metric Dimensional Scaling 2 | #' 3 | #' An S4 Class implementing Non-Metric Dimensional Scaling. 4 | #' 5 | #' A non-linear extension of MDS using monotonic regression 6 | #' 7 | #' @template dimRedMethodSlots 8 | #' 9 | #' @template dimRedMethodGeneralUsage 10 | #' 11 | #' @section Parameters: 12 | #' nMDS can take the following parameters: 13 | #' \describe{ 14 | #' \item{d}{A distance function.} 15 | #' \item{ndim}{The number of embedding dimensions.} 16 | #' } 17 | #' 18 | #' @section Implementation: 19 | #' Wraps around the 20 | #' \code{\link[vegan]{monoMDS}}. For parameters that are not 21 | #' available here, the standard configuration is used. 22 | #' 23 | #' @references 24 | #' 25 | #' Kruskal, J.B., 1964. Nonmetric multidimensional scaling: A numerical method. 26 | #' Psychometrika 29, 115-129. https://doi.org/10.1007/BF02289694 27 | #' 28 | #' @examples 29 | #' if(requireNamespace("vegan", quietly = TRUE)) { 30 | #' 31 | #' dat <- loadDataSet("3D S Curve", n = 300) 32 | #' emb <- embed(dat, "nMDS") 33 | #' plot(emb, type = "2vars") 34 | #' 35 | #' } 36 | #' @include dimRedResult-class.R 37 | #' @include dimRedMethod-class.R 38 | #' @family dimensionality reduction methods 39 | #' @export nMDS 40 | #' @exportClass nMDS 41 | nMDS <- setClass( 42 | "nMDS", 43 | contains = "dimRedMethod", 44 | prototype = list( 45 | stdpars = list(d = stats::dist, ndim = 2), 46 | fun = function (data, pars, 47 | keep.org.data = TRUE) { 48 | chckpkg("vegan") 49 | 50 | meta <- data@meta 51 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 52 | indata <- data@data 53 | 54 | outdata <- vegan::monoMDS(pars$d(indata), k = pars$ndim)$points 55 | 56 | colnames(outdata) <- paste0("NMDS", 1:ncol(outdata)) 57 | 58 | return(new( 59 | "dimRedResult", 60 | data = new("dimRedData", 61 | data = outdata, 62 | meta = meta), 63 | org.data = orgdata, 64 | has.org.data = keep.org.data, 65 | method = "nmds", 66 | pars = pars 67 | )) 68 | }, 69 | requires = c("vegan")) 70 | ) 71 | -------------------------------------------------------------------------------- /R/pca.R: -------------------------------------------------------------------------------- 1 | #' Principal Component Analysis 2 | #' 3 | #' S4 Class implementing PCA. 4 | #' 5 | #' PCA transforms the data in orthogonal components so that the first 6 | #' axis accounts for the larges variance in the data, all the 7 | #' following axes account for the highest variance under the 8 | #' constraint that they are orthogonal to the preceding axes. PCA is 9 | #' sensitive to the scaling of the variables. PCA is by far the 10 | #' fastest and simples method of dimensionality reduction and should 11 | #' probably always be applied as a baseline if other methods are tested. 12 | #' 13 | #' @template dimRedMethodSlots 14 | #' 15 | #' @template dimRedMethodGeneralUsage 16 | #' 17 | #' @section Parameters: 18 | #' PCA can take the following parameters: 19 | #' \describe{ 20 | #' \item{ndim}{The number of output dimensions.} 21 | #' \item{center}{logical, should the data be centered, defaults to \code{TRUE}.} 22 | #' \item{scale.}{logical, should the data be scaled, defaults to \code{FALSE}.} 23 | #' } 24 | #' 25 | #' @section Implementation: 26 | #' 27 | #' Wraps around \code{\link{prcomp}}. Because PCA can be reduced to a 28 | #' simple rotation, forward and backward projection functions are 29 | #' supplied. 30 | #' 31 | #' @references 32 | #' 33 | #' Pearson, K., 1901. On lines and planes of closest fit to systems of points in 34 | #' space. Philosophical Magazine 2, 559-572. 35 | #' 36 | #' @examples 37 | #' dat <- loadDataSet("Iris") 38 | #' emb <- embed(dat, "PCA") 39 | #' 40 | #' plot(emb, type = "2vars") 41 | #' if(requireNamespace("scatterplot3d", quietly = TRUE)) 42 | #' plot(inverse(emb, getDimRedData(emb)), type = "3vars") 43 | #' 44 | #' @include dimRedResult-class.R 45 | #' @include dimRedMethod-class.R 46 | #' @family dimensionality reduction methods 47 | #' @export PCA 48 | #' @exportClass PCA 49 | PCA <- setClass( 50 | "PCA", 51 | contains = "dimRedMethod", 52 | prototype = list( 53 | stdpars = list(ndim = 2, 54 | center = TRUE, 55 | scale. = FALSE), 56 | fun = function (data, pars, 57 | keep.org.data = TRUE) { 58 | ndim <- pars$ndim 59 | pars$ndim <- NULL 60 | 61 | meta <- data@meta 62 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 63 | data <- data@data 64 | res <- do.call( 65 | prcomp, 66 | c(list(x = data), pars) 67 | ) 68 | 69 | # evaluate results here for functions 70 | data <- res$x[, seq_len(ndim), drop = FALSE] 71 | ce <- res$center 72 | sc <- res$scale 73 | rot <- res$rotation[, seq_len(ndim)] 74 | rerot <- t(rot) 75 | 76 | 77 | appl <- function(x) { 78 | appl.meta <- if (inherits(x, "dimRedData")) x@meta else data.frame() 79 | proj <- if (inherits(x, "dimRedData")) x@data else x 80 | 81 | if (ncol(proj) != ncol(orgdata)) 82 | stop("x must have the same number of dimensions ", 83 | "as the original data") 84 | 85 | 86 | if (ce[1] != FALSE) proj <- t(apply(proj, 1, function(x) x - ce)) 87 | if (sc[1] != FALSE) proj <- t(apply(proj, 1, function(x) x / sc)) 88 | proj <- proj %*% rot 89 | 90 | proj <- new("dimRedData", data = proj, meta = appl.meta) 91 | return(proj) 92 | } 93 | inv <- function(x) { 94 | appl.meta <- if (inherits(x, "dimRedData")) x@meta else data.frame() 95 | proj <- if (inherits(x, "dimRedData")) x@data else x 96 | if (ncol(proj) > ncol(data)) 97 | stop("x must have less or equal number of dimensions ", 98 | "as the original data") 99 | 100 | 101 | d <- ncol(proj) 102 | reproj <- proj %*% rerot[seq_len(d), ] 103 | 104 | if (sc[1] != FALSE) 105 | reproj <- t(apply(reproj, 1, function(x) x * sc)) 106 | if (ce[1] != FALSE) 107 | reproj <- t(apply(reproj, 1, function(x) x + ce)) 108 | 109 | reproj <- new("dimRedData", data = reproj, meta = appl.meta) 110 | 111 | return(reproj) 112 | } 113 | 114 | res <- new( 115 | "dimRedResult", 116 | data = new("dimRedData", 117 | data = data, 118 | meta = meta), 119 | org.data = orgdata, 120 | apply = appl, 121 | inverse = inv, 122 | has.org.data = keep.org.data, 123 | has.apply = TRUE, 124 | has.inverse = TRUE, 125 | method = "PCA", 126 | pars = pars 127 | ) 128 | 129 | return(res) 130 | }) 131 | ) 132 | -------------------------------------------------------------------------------- /R/soe.R: -------------------------------------------------------------------------------- 1 | ## #' Soft Ordinal Embedding 2 | ## #' 3 | ## #' Instance of \code{\link{dimRedMethod}} for Soft Ordinal Embedding. 4 | ## #' 5 | ## #' For details see \code{\link[loe]{SOE}}. 6 | ## #' 7 | ## #' 8 | ## #' @examples 9 | ## #' dat <- loadDataSet("3D S Curve", n = 50) 10 | ## #' soe <- SOE() 11 | ## #' emb <- soe@fun(dat, soe@stdpars) 12 | ## #' 13 | ## #' 14 | ## #' plot(emb@data@data) 15 | ## #' 16 | ## #' 17 | ## #' @include dimRedResult-class.R 18 | ## #' @include dimRedMethod-class.R 19 | ## #' @export 20 | ## SOE <- setClass( 21 | ## "SOE", 22 | ## contains = "dimRedMethod", 23 | ## prototype = list( 24 | ## stdpars = list(d = stats::dist, knn = 50, ndim = 2), 25 | ## fun = function (data, 26 | ## pars, 27 | ## keep.org.data = TRUE) { 28 | ## chckpkg("loe") 29 | 30 | ## meta <- data@meta 31 | ## orgdata <- if (keep.org.data) data@data else NULL 32 | ## indata <- data@data 33 | 34 | ## outdata <- loe::SOE(loe::get.order(as.matrix(pars$d(indata))), 35 | ## N = nrow(indata), p = pars$ndim)$X 36 | 37 | ## colnames(outdata) <- paste0("SOE", 1:ncol(outdata)) 38 | 39 | ## return(new( 40 | ## "dimRedResult", 41 | ## data = new("dimRedData", 42 | ## data = outdata, 43 | ## meta = meta), 44 | ## org.data = orgdata, 45 | ## has.org.data = keep.org.data, 46 | ## method = "soe", 47 | ## pars = pars 48 | ## )) 49 | ## }) 50 | ## ) 51 | -------------------------------------------------------------------------------- /R/tsne.R: -------------------------------------------------------------------------------- 1 | #' t-Distributed Stochastic Neighborhood Embedding 2 | #' 3 | #' An S4 Class for t-SNE. 4 | #' 5 | #' t-SNE is a method that uses Kullback-Leibler divergence between the 6 | #' distance matrices in high and low-dimensional space to embed the 7 | #' data. The method is very well suited to visualize complex 8 | #' structures in low dimensions. 9 | #' 10 | #' @template dimRedMethodSlots 11 | #' 12 | #' @template dimRedMethodGeneralUsage 13 | #' 14 | #' @section Parameters: 15 | #' t-SNE can take the following parameters: 16 | #' \describe{ 17 | #' \item{d}{A distance function, defaults to euclidean distances} 18 | #' \item{perplexity}{The perplexity parameter, roughly equivalent to neighborhood size.} 19 | #' \item{theta}{Approximation for the nearest neighbour search, large values are more inaccurate.} 20 | #' \item{ndim}{The number of embedding dimensions.} 21 | #' } 22 | #' 23 | #' @section Implementation: 24 | #' 25 | #' Wraps around \code{\link[Rtsne]{Rtsne}}, which is very well 26 | #' documented. Setting \code{theta = 0} does a normal t-SNE, larger 27 | #' values for \code{theta < 1} use the Barnes-Hut algorithm which 28 | #' scales much nicer with data size. Larger values for perplexity take 29 | #' larger neighborhoods into account. 30 | #' 31 | #' @references 32 | #' Maaten, L. van der, 2014. Accelerating t-SNE using Tree-Based 33 | #' Algorithms. Journal of Machine Learning Research 15, 3221-3245. 34 | #' 35 | #' van der Maaten, L., Hinton, G., 2008. Visualizing Data using 36 | #' t-SNE. J. Mach. Learn. Res. 9, 2579-2605. 37 | #' 38 | #' @examples 39 | #' \dontrun{ 40 | #' dat <- loadDataSet("3D S Curve", n = 300) 41 | #' emb <- embed(dat, "tSNE", perplexity = 80) 42 | #' plot(emb, type = "2vars") 43 | #' } 44 | #' @include dimRedResult-class.R 45 | #' @include dimRedMethod-class.R 46 | #' @family dimensionality reduction methods 47 | #' @export tSNE 48 | #' @exportClass tSNE 49 | tSNE <- setClass( 50 | "tSNE", 51 | contains = "dimRedMethod", 52 | prototype = list( 53 | stdpars = list(d = stats::dist, 54 | perplexity = 30, 55 | theta = 0.5, 56 | ndim = 2), 57 | fun = function (data, pars, 58 | keep.org.data = TRUE) { 59 | chckpkg("Rtsne") 60 | 61 | meta <- data@meta 62 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 63 | indata <- data@data 64 | 65 | outdata <- Rtsne::Rtsne(pars$d(indata), 66 | perplexity = pars$perplexity, 67 | theta = pars$theta, 68 | dims = pars$ndim)$Y 69 | 70 | colnames(outdata) <- paste0("tSNE", 1:ncol(outdata)) 71 | 72 | return(new( 73 | "dimRedResult", 74 | data = new("dimRedData", 75 | data = outdata, 76 | meta = meta), 77 | org.data = orgdata, 78 | has.org.data = keep.org.data, 79 | method = "tsne", 80 | pars = pars 81 | )) 82 | }, 83 | requires = c("Rtsne")) 84 | ) 85 | -------------------------------------------------------------------------------- /R/umap.R: -------------------------------------------------------------------------------- 1 | #' Umap embedding 2 | #' 3 | #' An S4 Class implementing the UMAP algorithm 4 | #' 5 | #' Uniform Manifold Approximation is a gradient descend based algorithm that 6 | #' gives results similar to t-SNE, but scales better with the number of points. 7 | #' 8 | #' @template dimRedMethodSlots 9 | #' 10 | #' @template dimRedMethodGeneralUsage 11 | #' 12 | #' @section Parameters: 13 | #' 14 | #' UMAP can take the follwing parameters: 15 | #' \describe{ 16 | #' \item{ndim}{The number of embedding dimensions.} 17 | #' \item{knn}{The number of neighbors to be used.} 18 | #' \item{d}{The distance metric to use.} 19 | #' \item{method}{\code{"naive"} for an R implementation, \code{"python"} 20 | #' for the reference implementation.} 21 | #' } 22 | #' 23 | #' Other method parameters can also be passed, see 24 | #' \code{\link[umap]{umap.defaults}} for details. The ones above have been 25 | #' standardized for the use with \code{dimRed} and will get automatically 26 | #' translated for \code{\link[umap]{umap}}. 27 | #' 28 | #' @section Implementation: 29 | #' 30 | #' The dimRed package wraps the \code{\link[umap]{umap}} packages which provides 31 | #' an implementation in pure R and also a wrapper around the original python 32 | #' package \code{umap-learn} (https://github.com/lmcinnes/umap/). This requires 33 | #' \code{umap-learn} version 0.4 installed, at the time of writing, there is 34 | #' already \code{umap-learn} 0.5 but it is not supported by the R package 35 | #' \code{\link[umap]{umap}}. 36 | #' 37 | #' The \code{"naive"} implementation is a pure R implementation and considered 38 | #' experimental at the point of writing this, it is also much slower than the 39 | #' python implementation. 40 | #' 41 | #' The \code{"python"} implementation is the reference implementation used by 42 | #' McInees et. al. (2018). It requires the \code{\link[reticulate]{reticulate}} 43 | #' package for the interaction with python and the python package 44 | #' \code{umap-learn} installed (use \code{pip install umap-learn}). 45 | #' 46 | #' @references 47 | #' 48 | #' McInnes, Leland, and John Healy. 49 | #' "UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction." 50 | #' https://arxiv.org/abs/1802.03426 51 | #' 52 | #' @examples 53 | #' \dontrun{ 54 | #' dat <- loadDataSet("3D S Curve", n = 300) 55 | #' emb <- embed(dat, "UMAP", .mute = NULL, knn = 10) 56 | #' plot(emb, type = "2vars") 57 | #' } 58 | #' 59 | #' @include dimRedResult-class.R 60 | #' @include dimRedMethod-class.R 61 | #' @family dimensionality reduction methods 62 | #' @export UMAP 63 | #' @exportClass UMAP 64 | UMAP <- setClass( 65 | "UMAP", 66 | contains = "dimRedMethod", 67 | prototype = list( 68 | stdpars = list( 69 | knn = 15, 70 | ndim = 2, 71 | d = "euclidean", 72 | method = "umap-learn" 73 | ), 74 | fun = function (data, pars, 75 | keep.org.data = TRUE) { 76 | chckpkg("umap") 77 | if (pars$method == "python") { 78 | chckpkg("reticulate") 79 | if (!reticulate::py_module_available("umap")) 80 | stop("cannot find python umap, install with `pip install umap-learn`") 81 | } 82 | 83 | meta <- data@meta 84 | orgdata <- if (keep.org.data) data@data else matrix(0, 0, 0) 85 | indata <- data@data 86 | 87 | ## Create config 88 | umap_call_pars <- umap::umap.defaults 89 | umap_call_pars$n_neighbors <- pars$knn 90 | umap_call_pars$n_components <- pars$ndim 91 | umap_call_pars$metric <- pars$d 92 | umap_call_pars$method <- pars$method 93 | umap_call_pars$d <- indata 94 | 95 | pars_2 <- pars 96 | pars_2$knn <- NULL 97 | pars_2$ndim <- NULL 98 | pars_2$d <- NULL 99 | pars_2$method <- NULL 100 | 101 | for (n in names(pars_2)) 102 | umap_call_pars[[n]] <- pars_2[[n]] 103 | 104 | ## Do the embedding 105 | outdata <- do.call(umap::umap, umap_call_pars) 106 | 107 | ## Post processing 108 | colnames(outdata$layout) <- paste0("UMAP", 1:ncol(outdata$layout)) 109 | 110 | appl <- function(x) { 111 | appl.meta <- if (inherits(x, "dimRedData")) x@meta else data.frame() 112 | proj <- if (inherits(x, "dimRedData")) x@data else x 113 | 114 | if (ncol(proj) != ncol(orgdata)) 115 | stop("x must have the same number of dimensions ", 116 | "as the original data") 117 | 118 | new_proj <- umap:::predict.umap(outdata, as.matrix(proj)) 119 | 120 | colnames(new_proj) <- paste0("UMAP", 1:ncol(new_proj)) 121 | rownames(new_proj) <- NULL 122 | 123 | out_data <- new("dimRedData", data = new_proj, meta = appl.meta) 124 | return(out_data) 125 | } 126 | 127 | return(new( 128 | "dimRedResult", 129 | data = new("dimRedData", 130 | data = outdata$layout, 131 | meta = meta), 132 | org.data = orgdata, 133 | apply = appl, 134 | has.org.data = keep.org.data, 135 | has.apply = TRUE, 136 | method = "UMAP", 137 | pars = pars 138 | )) 139 | }, 140 | requires = c("umap", "reticulate") 141 | ) 142 | ) 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dimRed 2 | 3 | [![docs](https://img.shields.io/badge/docs-dev-blue.svg)](https://dimred.guido-kraemer.com) 4 | [![R](https://github.com/gdkrmr/dimRed/actions/workflows/r.yml/badge.svg)](https://github.com/gdkrmr/dimRed/actions/workflows/r.yml) 5 | [![codecov](https://codecov.io/gh/gdkrmr/dimRed/branch/master/graph/badge.svg?token=voiiWocTst)](https://codecov.io/gh/gdkrmr/dimRed) 6 | [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/dimRed)](https://cran.r-project.org/package=dimRed) 7 | [![DOI](https://zenodo.org/badge/70895041.svg)](https://zenodo.org/badge/latestdoi/70895041) 8 | 9 | A Framework for Dimensionality Reduction for the R language. 10 | 11 | A collection of dimensionality reduction 12 | techniques from R packages and provides a common 13 | interface for calling the methods. 14 | 15 | ## Installing 16 | Install the latest development version from Github: 17 | ```R 18 | ## install.packages("devtools") 19 | devtools::install_github("gdkrmr/dimRed") 20 | ``` 21 | 22 | Install the latest stable version from CRAN: 23 | ```R 24 | install.packages("dimRed") 25 | ``` 26 | 27 | Load it: 28 | ```R 29 | library(dimRed) 30 | ``` 31 | 32 | Install dependencies: 33 | ```R 34 | ## To install all dependencies: 35 | dimRed::installSuggests() 36 | ``` 37 | 38 | ## Citing 39 | The corresponding publication can be found 40 | [here](https://journal.r-project.org/archive/2018/RJ-2018-039/index.html "dimRed 41 | and coRanking - Unifying Dimensionality Reduction in R"), please cite if you use 42 | `dimRed`: 43 | 44 | ```bibtex 45 | @article{RJ-2018-039, 46 | author = {Guido Kraemer and Markus Reichstein and Miguel D. Mahecha}, 47 | title = {{dimRed and coRanking---Unifying Dimensionality Reduction in R}}, 48 | year = {2018}, 49 | journal = {{The R Journal}}, 50 | url = {https://journal.r-project.org/archive/2018/RJ-2018-039/index.html}, 51 | pages = {342--358}, 52 | volume = {10}, 53 | number = {1} 54 | } 55 | ``` 56 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: ~ 2 | template: 3 | bootstrap: 5 4 | 5 | 6 | authors: 7 | Guido Kraemer: 8 | href: https://www.guido-kraemer.com 9 | 10 | footer: 11 | structure: 12 | left: developed_by 13 | right: [built_with, impressum] 14 | components: 15 | impressum: Impressum 16 | -------------------------------------------------------------------------------- /builddoc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | Rscript -e 'devtools::document()' 5 | -------------------------------------------------------------------------------- /check_rhub.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | R --slave << END_FILE 4 | 5 | library(rhub) 6 | archs <- rhub::platforms()[["docker-image"]] 7 | archs <- na.exclude(archs) 8 | sapply( 9 | archs, 10 | function(x) try(rhub::check(platform = x, show_status = FALSE)) 11 | ) 12 | 13 | END_FILE 14 | -------------------------------------------------------------------------------- /checkpackage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -i 2 | 3 | echo "== START =============================================" 4 | 5 | R_HOME= 6 | # R_FOLDER=/usr/bin 7 | R_FOLDER=$HOME/progs/R/R-4.4.2/bin 8 | # R_FOLDER=$HOME/progs/R/R-devel/bin 9 | 10 | $R_FOLDER/R --version 11 | 12 | export RETICULATE_PYTHON=$HOME/progs/py/miniconda3/envs/r-reticulate/bin/python 13 | 14 | echo "== BUILDING DOCUMENTATION ============================" 15 | Rscript --vanilla --default-packages=methods,utils \ 16 | -e 'devtools::document()' 17 | 18 | echo "== REMOVING emacs lockfiles ==========================" 19 | find . -type l -exec rm {} \; 20 | 21 | echo "== BUILDING ==========================================" 22 | # Setting this forces the testing of the autoencoders, even if there is no 23 | # tensorflow or keras installed. 24 | export BNET_FORCE_AUTOENCODER_TESTS=1 25 | export BNET_FORCE_UMAP_TESTS=1 26 | export BNET_FORCE_NNMF_TESTS=1 27 | 28 | # this is to make the import of the nrow fail if not correctly specified 29 | # This did not actually work, but I keep it in here for good measure. 30 | # export _R_CHECK_INSTALL_DEPENDS_=true 31 | # export _R_CHECK_DEPENDS_ONLY_=true 32 | # export _R_CHECK_SUGGESTS_ONLY_=true 33 | # export _R_CHECK_FORCE_SUGGESTS_=false 34 | 35 | BNET_BUILD_VIGNETTE=1 $R_FOLDER/R CMD build --compact-vignettes . 36 | 37 | pkgversion=$(cat DESCRIPTION | grep Version | sed 's|Version: \(.*\)|\1|') 38 | echo "== INSTALLING version $pkgversion ====================" 39 | $R_FOLDER/R CMD INSTALL dimRed_$pkgversion.tar.gz 40 | 41 | echo "== LINTING ===========================================" 42 | $R_FOLDER/Rscript -e 'lintr::lint_package()' 43 | 44 | echo "" 45 | echo "== CHECKING AS CRAN ==================================" 46 | $R_FOLDER/R CMD check dimRed_$pkgversion.tar.gz --as-cran --timings 47 | 48 | echo "== CHECK everything ==================================" 49 | $R_FOLDER/R CMD check dimRed_$pkgversion.tar.gz --run-donttest --run-dontrun --timings 50 | 51 | echo "== CHECK without suggests ============================" 52 | export _R_CHECK_DEPENDS_ONLY_=true 53 | unset BNET_FORCE_NNMF_TESTS 54 | $R_FOLDER/R CMD check dimRed_$pkgversion.tar.gz --as-cran --timings 55 | $R_FOLDER/R CMD check dimRed_$pkgversion.tar.gz --run-donttest --run-dontrun --timings 56 | unset _R_CHECK_DEPENDS_ONLY_ 57 | 58 | echo "== DONE ==============================================" 59 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | bibentry(bibtype = "Article", 2 | author = c(person("Guido", "Kraemer"), 3 | person("Markus", "Reichstein"), 4 | person(c("Miguel", "D."), "Mahecha")), 5 | title = "{dimRed} and {coRanking}---Unifying Dimensionality Reduction in R", 6 | year = "2018", 7 | journal = "The R Journal", 8 | url = "https://journal.r-project.org/archive/2018/RJ-2018-039/index.html", 9 | pages = "342--358", 10 | volume = "10", 11 | number = "1", 12 | note = sprintf("coRanking version %s", meta$Version)) 13 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This file is supposed to install dimRed and all dependencies and development 4 | # tools for the R version chosen below. 5 | 6 | # R_FOLDER=/usr/bin 7 | # R_FOLDER=$HOME/progs/R/R-3.5.1/bin 8 | # R_FOLDER=$HOME/progs/R/R-3.5.2/bin 9 | # R_FOLDER=$HOME/progs/R/R-3.5.3/bin 10 | R_FOLDER=$HOME/progs/R/R-3.6.0/bin 11 | # R_FOLDER=$HOME/progs/R/R-devel/bin 12 | 13 | $R_FOLDER/R --version 14 | 15 | pkgversion=$(cat DESCRIPTION | grep Version | sed 's|Version: \(.*\)|\1|') 16 | echo "INSTALLING version $pkgversion" 17 | $R_FOLDER/R CMD INSTALL dimRed_$pkgversion.tar.gz 18 | 19 | $R_FOLDER/R -e 'install.packages(c("NMF", "magrittr", "DRR", "lintr", "knitr"), Ncpus = 4, ask = FALSE, repos = "https://cloud.r-project.org")' 20 | $R_FOLDER/R -e 'options(Ncpus = 4, repos = "https://cloud.r-project.org"); dimRed::installSuggests()' 21 | # $R_FOLDER/R -e 'tensorflow::install_tensorflow()' 22 | # $R_FOLDER/R -e 'keras::install_keras()' 23 | 24 | pip install --user --upgrade tensorflow keras umap-learn 25 | 26 | $R_FOLDER/R -e 'options(Ncpus = 4, repos = "https://cloud.r-project.org"); install.packages("devtools")' 27 | -------------------------------------------------------------------------------- /man-roxygen/dimRedMethodGeneralUsage.R: -------------------------------------------------------------------------------- 1 | #' @section General usage: 2 | #' Dimensionality reduction methods are S4 Classes that either be used 3 | #' directly, in which case they have to be initialized and a full 4 | #' list with parameters has to be handed to the \code{@@fun()} 5 | #' slot, or the method name be passed to the embed function and 6 | #' parameters can be given to the \code{...}, in which case 7 | #' missing parameters will be replaced by the ones in the 8 | #' \code{@@stdpars}. 9 | #' 10 | -------------------------------------------------------------------------------- /man-roxygen/dimRedMethodSlots.R: -------------------------------------------------------------------------------- 1 | #' @slot fun A function that does the embedding and returns a 2 | #' dimRedResult object. 3 | #' @slot stdpars The standard parameters for the function. 4 | -------------------------------------------------------------------------------- /man/AUC_lnK_R_NX-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{AUC_lnK_R_NX,dimRedResult-method} 4 | \alias{AUC_lnK_R_NX,dimRedResult-method} 5 | \alias{AUC_lnK_R_NX} 6 | \title{Method AUC_lnK_R_NX} 7 | \usage{ 8 | \S4method{AUC_lnK_R_NX}{dimRedResult}(object, weight = "inv") 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | 13 | \item{weight}{the weight function used, one of \code{c("inv", "log", "log10")}} 14 | } 15 | \description{ 16 | Calculate the Area under the R_NX(ln K), used in Lee et. al. (2015). Note 17 | that despite the name, this does not weight the mean by the logarithm, but by 18 | 1/K. If explicit weighting by the logarithm is desired use \code{weight = 19 | "log"} or \code{weight = "log10"} 20 | } 21 | \details{ 22 | The naming confusion originated from equation 17 in Lee et al (2015) and the 23 | name of this method may change in the future to avoid confusion. 24 | } 25 | \references{ 26 | Lee, J.A., Peluffo-Ordonez, D.H., Verleysen, M., 2015. 27 | Multi-scale similarities in stochastic neighbour embedding: Reducing 28 | dimensionality while preserving both local and global structure. 29 | Neurocomputing 169, 246-261. https://doi.org/10.1016/j.neucom.2014.12.095 30 | } 31 | \seealso{ 32 | Other Quality scores for dimensionality reduction: 33 | \code{\link{LCMC,dimRedResult-method}}, 34 | \code{\link{Q_NX,dimRedResult-method}}, 35 | \code{\link{Q_global,dimRedResult-method}}, 36 | \code{\link{Q_local,dimRedResult-method}}, 37 | \code{\link{R_NX,dimRedResult-method}}, 38 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 39 | \code{\link{distance_correlation,dimRedResult-method}}, 40 | \code{\link{mean_R_NX,dimRedResult-method}}, 41 | \code{\link{plot_R_NX}()}, 42 | \code{\link{quality,dimRedResult-method}}, 43 | \code{\link{reconstruction_error,dimRedResult-method}}, 44 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 45 | \code{\link{total_correlation,dimRedResult-method}} 46 | } 47 | \concept{Quality scores for dimensionality reduction} 48 | -------------------------------------------------------------------------------- /man/DRR-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/drr.R 3 | \docType{class} 4 | \name{DRR-class} 5 | \alias{DRR-class} 6 | \alias{DRR} 7 | \title{Dimensionality Reduction via Regression} 8 | \description{ 9 | An S4 Class implementing Dimensionality Reduction via Regression (DRR). 10 | } 11 | \details{ 12 | DRR is a non-linear extension of PCA that uses Kernel Ridge regression. 13 | } 14 | \section{Slots}{ 15 | 16 | \describe{ 17 | \item{\code{fun}}{A function that does the embedding and returns a 18 | dimRedResult object.} 19 | 20 | \item{\code{stdpars}}{The standard parameters for the function.} 21 | }} 22 | 23 | \section{General usage}{ 24 | 25 | Dimensionality reduction methods are S4 Classes that either be used 26 | directly, in which case they have to be initialized and a full 27 | list with parameters has to be handed to the \code{@fun()} 28 | slot, or the method name be passed to the embed function and 29 | parameters can be given to the \code{...}, in which case 30 | missing parameters will be replaced by the ones in the 31 | \code{@stdpars}. 32 | } 33 | 34 | \section{Parameters}{ 35 | 36 | DRR can take the following parameters: 37 | \describe{ 38 | \item{ndim}{The number of dimensions} 39 | \item{lambda}{The regularization parameter for the ridge 40 | regression.} 41 | \item{kernel}{The kernel to use for KRR, defaults to 42 | \code{"rbfdot"}.} 43 | \item{kernel.pars}{A list with kernel parameters, elements depend 44 | on the kernel used, \code{"rbfdot"} uses \code{"sigma"}.} 45 | \item{pca}{logical, should an initial pca step be performed, 46 | defaults to \code{TRUE}.} 47 | \item{pca.center}{logical, should the data be centered before the 48 | pca step. Defaults to \code{TRUE}.} 49 | \item{pca.scale}{logical, should the data be scaled before the 50 | pca ste. Defaults to \code{FALSE}.} 51 | \item{fastcv}{logical, should \code{\link[CVST]{fastCV}} from the 52 | CVST package be used instead of normal cross-validation.} 53 | \item{fastcv.test}{If \code{fastcv = TRUE}, separate test data set for fastcv.} 54 | \item{cv.folds}{if \code{fastcv = FALSE}, specifies the number of 55 | folds for crossvalidation.} 56 | \item{fastkrr.nblocks}{integer, higher values sacrifice numerical 57 | accuracy for speed and less memory, see below for details.} 58 | \item{verbose}{logical, should the cross-validation results be 59 | printed out.} 60 | } 61 | } 62 | 63 | \section{Implementation}{ 64 | 65 | Wraps around \code{\link[DRR]{drr}}, see there for details. DRR is 66 | a non-linear extension of principal components analysis using Kernel 67 | Ridge Regression (KRR, details see \code{\link[CVST]{constructKRRLearner}} 68 | and \code{\link[DRR]{constructFastKRRLearner}}). Non-linear 69 | regression is used to explain more variance than PCA. DRR provides 70 | an out-of-sample extension and a backward projection. 71 | 72 | The most expensive computations are matrix inversions therefore the 73 | implementation profits a lot from a multithreaded BLAS library. 74 | The best parameters for each KRR are determined by cross-validaton 75 | over all parameter combinations of \code{lambda} and 76 | \code{kernel.pars}, using less parameter values will speed up 77 | computation time. Calculation of KRR can be accelerated by 78 | increasing \code{fastkrr.nblocks}, it should be smaller than 79 | \eqn{n^{1/3}} up to sacrificing some accuracy, for details see 80 | \code{\link[DRR]{constructFastKRRLearner}}. Another way to speed up 81 | is to use \code{pars$fastcv = TRUE} which might provide a more 82 | efficient way to search the parameter space but may also miss the 83 | global maximum, I have not ran tests on the accuracy of this method. 84 | } 85 | 86 | \examples{ 87 | \dontrun{ 88 | if(requireNamespace(c("kernlab", "DRR"), quietly = TRUE)) { 89 | 90 | dat <- loadDataSet("variable Noise Helix", n = 200)[sample(200)] 91 | 92 | emb <- embed(dat, "DRR", ndim = 3) 93 | 94 | plot(dat, type = "3vars") 95 | plot(emb, type = "3vars") 96 | 97 | # We even have function to reconstruct, also working for only the first few dimensions 98 | rec <- inverse(emb, getData(getDimRedData(emb))[, 1, drop = FALSE]) 99 | plot(rec, type = "3vars") 100 | } 101 | 102 | } 103 | 104 | } 105 | \references{ 106 | Laparra, V., Malo, J., Camps-Valls, G., 107 | 2015. Dimensionality Reduction via Regression in Hyperspectral 108 | Imagery. IEEE Journal of Selected Topics in Signal Processing 109 | 9, 1026-1036. doi:10.1109/JSTSP.2015.2417833 110 | } 111 | \seealso{ 112 | Other dimensionality reduction methods: 113 | \code{\link{DiffusionMaps-class}}, 114 | \code{\link{DrL-class}}, 115 | \code{\link{FastICA-class}}, 116 | \code{\link{FruchtermanReingold-class}}, 117 | \code{\link{HLLE-class}}, 118 | \code{\link{Isomap-class}}, 119 | \code{\link{KamadaKawai-class}}, 120 | \code{\link{MDS-class}}, 121 | \code{\link{NNMF-class}}, 122 | \code{\link{PCA-class}}, 123 | \code{\link{PCA_L1-class}}, 124 | \code{\link{UMAP-class}}, 125 | \code{\link{dimRedMethod-class}}, 126 | \code{\link{dimRedMethodList}()}, 127 | \code{\link{kPCA-class}}, 128 | \code{\link{nMDS-class}}, 129 | \code{\link{tSNE-class}} 130 | } 131 | \concept{dimensionality reduction methods} 132 | -------------------------------------------------------------------------------- /man/DiffusionMaps-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diffmap.R 3 | \docType{class} 4 | \name{DiffusionMaps-class} 5 | \alias{DiffusionMaps-class} 6 | \alias{DiffusionMaps} 7 | \title{Diffusion Maps} 8 | \description{ 9 | An S4 Class implementing Diffusion Maps 10 | } 11 | \details{ 12 | Diffusion Maps uses a diffusion probability matrix to robustly 13 | approximate a manifold. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{fun}}{A function that does the embedding and returns a 19 | dimRedResult object.} 20 | 21 | \item{\code{stdpars}}{The standard parameters for the function.} 22 | }} 23 | 24 | \section{General usage}{ 25 | 26 | Dimensionality reduction methods are S4 Classes that either be used 27 | directly, in which case they have to be initialized and a full 28 | list with parameters has to be handed to the \code{@fun()} 29 | slot, or the method name be passed to the embed function and 30 | parameters can be given to the \code{...}, in which case 31 | missing parameters will be replaced by the ones in the 32 | \code{@stdpars}. 33 | } 34 | 35 | \section{Parameters}{ 36 | 37 | Diffusion Maps can take the following parameters: 38 | \describe{ 39 | \item{d}{a function transforming a matrix row wise into a 40 | distance matrix or \code{dist} object, 41 | e.g. \code{\link[stats]{dist}}.} 42 | \item{ndim}{The number of dimensions} 43 | \item{eps}{The epsilon parameter that determines the 44 | diffusion weight matrix from a distance matrix \code{d}, 45 | \eqn{exp(-d^2/eps)}, if set to \code{"auto"} it will 46 | be set to the median distance to the 0.01*n nearest 47 | neighbor.} 48 | \item{t}{Time-scale parameter. The recommended value, 0, 49 | uses multiscale geometry.} 50 | \item{delta}{Sparsity cut-off for the symmetric graph Laplacian, 51 | a higher value results in more sparsity and faster calculation. 52 | The predefined value is 10^-5.} 53 | } 54 | } 55 | 56 | \section{Implementation}{ 57 | 58 | Wraps around \code{\link[diffusionMap]{diffuse}}, see there for 59 | details. It uses the notation of Richards et al. (2009) which is 60 | slightly different from the one in the original paper (Coifman and 61 | Lafon, 2006) and there is no \eqn{\alpha} parameter. 62 | There is also an out-of-sample extension, see examples. 63 | } 64 | 65 | \examples{ 66 | if(requireNamespace("diffusionMap", quietly = TRUE)) { 67 | dat <- loadDataSet("3D S Curve", n = 300) 68 | emb <- embed(dat, "DiffusionMaps") 69 | 70 | plot(emb, type = "2vars") 71 | 72 | # predicting is possible: 73 | samp <- sample(floor(nrow(dat) / 10)) 74 | emb2 <- embed(dat[samp]) 75 | emb3 <- predict(emb2, dat[-samp]) 76 | 77 | plot(emb2, type = "2vars") 78 | points(getData(emb3)) 79 | } 80 | } 81 | \references{ 82 | Richards, J.W., Freeman, P.E., Lee, A.B., Schafer, 83 | C.M., 2009. Exploiting Low-Dimensional Structure in 84 | Astronomical Spectra. ApJ 691, 85 | 32. doi:10.1088/0004-637X/691/1/32 86 | 87 | Coifman, R.R., Lafon, S., 2006. Diffusion maps. Applied and 88 | Computational Harmonic Analysis 21, 89 | 5-30. doi:10.1016/j.acha.2006.04.006 90 | } 91 | \seealso{ 92 | Other dimensionality reduction methods: 93 | \code{\link{DRR-class}}, 94 | \code{\link{DrL-class}}, 95 | \code{\link{FastICA-class}}, 96 | \code{\link{FruchtermanReingold-class}}, 97 | \code{\link{HLLE-class}}, 98 | \code{\link{Isomap-class}}, 99 | \code{\link{KamadaKawai-class}}, 100 | \code{\link{MDS-class}}, 101 | \code{\link{NNMF-class}}, 102 | \code{\link{PCA-class}}, 103 | \code{\link{PCA_L1-class}}, 104 | \code{\link{UMAP-class}}, 105 | \code{\link{dimRedMethod-class}}, 106 | \code{\link{dimRedMethodList}()}, 107 | \code{\link{kPCA-class}}, 108 | \code{\link{nMDS-class}}, 109 | \code{\link{tSNE-class}} 110 | } 111 | \concept{dimensionality reduction methods} 112 | -------------------------------------------------------------------------------- /man/DrL-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph_embed.R 3 | \docType{class} 4 | \name{DrL-class} 5 | \alias{DrL-class} 6 | \alias{DrL} 7 | \title{Distributed Recursive Graph Layout} 8 | \description{ 9 | An S4 Class implementing Distributed recursive Graph Layout. 10 | } 11 | \details{ 12 | DrL uses a complex algorithm to avoid local minima in the graph 13 | embedding which uses several steps. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{fun}}{A function that does the embedding and returns a 19 | dimRedResult object.} 20 | 21 | \item{\code{stdpars}}{The standard parameters for the function.} 22 | }} 23 | 24 | \section{General usage}{ 25 | 26 | Dimensionality reduction methods are S4 Classes that either be used 27 | directly, in which case they have to be initialized and a full 28 | list with parameters has to be handed to the \code{@fun()} 29 | slot, or the method name be passed to the embed function and 30 | parameters can be given to the \code{...}, in which case 31 | missing parameters will be replaced by the ones in the 32 | \code{@stdpars}. 33 | } 34 | 35 | \section{Parameters}{ 36 | 37 | DrL can take the following parameters: 38 | \describe{ 39 | \item{ndim}{The number of dimensions, defaults to 2. Can only be 2 or 3} 40 | \item{knn}{Reduce the graph to keep only the neares neighbors. Defaults to 100.} 41 | \item{d}{The distance function to determine the weights of the graph edges. Defaults to euclidean distances.} 42 | } 43 | } 44 | 45 | \section{Implementation}{ 46 | 47 | Wraps around \code{\link[igraph]{layout_with_drl}}. The parameters 48 | maxiter, epsilon and kkconst are set to the default values and 49 | cannot be set, this may change in a future release. The DimRed 50 | Package adds an extra sparsity parameter by constructing a knn 51 | graph which also may improve visualization quality. 52 | } 53 | 54 | \examples{ 55 | \dontrun{ 56 | if(requireNamespace(c("igraph", "coRanking"), quietly = TRUE)) { 57 | 58 | dat <- loadDataSet("Swiss Roll", n = 200) 59 | emb <- embed(dat, "DrL") 60 | plot(emb, type = "2vars") 61 | } 62 | 63 | } 64 | 65 | } 66 | \references{ 67 | Martin, S., Brown, W.M., Wylie, B.N., 2007. Dr.l: Distributed Recursive 68 | (graph) Layout (No. dRl; 002182MLTPL00). Sandia National Laboratories. 69 | } 70 | \seealso{ 71 | Other dimensionality reduction methods: 72 | \code{\link{DRR-class}}, 73 | \code{\link{DiffusionMaps-class}}, 74 | \code{\link{FastICA-class}}, 75 | \code{\link{FruchtermanReingold-class}}, 76 | \code{\link{HLLE-class}}, 77 | \code{\link{Isomap-class}}, 78 | \code{\link{KamadaKawai-class}}, 79 | \code{\link{MDS-class}}, 80 | \code{\link{NNMF-class}}, 81 | \code{\link{PCA-class}}, 82 | \code{\link{PCA_L1-class}}, 83 | \code{\link{UMAP-class}}, 84 | \code{\link{dimRedMethod-class}}, 85 | \code{\link{dimRedMethodList}()}, 86 | \code{\link{kPCA-class}}, 87 | \code{\link{nMDS-class}}, 88 | \code{\link{tSNE-class}} 89 | } 90 | \concept{dimensionality reduction methods} 91 | -------------------------------------------------------------------------------- /man/FastICA-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fastica.R 3 | \docType{class} 4 | \name{FastICA-class} 5 | \alias{FastICA-class} 6 | \alias{FastICA} 7 | \title{Independent Component Analysis} 8 | \description{ 9 | An S4 Class implementing the FastICA algorithm for Indepentend 10 | Component Analysis. 11 | } 12 | \details{ 13 | ICA is used for blind signal separation of different sources. It is 14 | a linear Projection. 15 | } 16 | \section{Slots}{ 17 | 18 | \describe{ 19 | \item{\code{fun}}{A function that does the embedding and returns a 20 | dimRedResult object.} 21 | 22 | \item{\code{stdpars}}{The standard parameters for the function.} 23 | }} 24 | 25 | \section{General usage}{ 26 | 27 | Dimensionality reduction methods are S4 Classes that either be used 28 | directly, in which case they have to be initialized and a full 29 | list with parameters has to be handed to the \code{@fun()} 30 | slot, or the method name be passed to the embed function and 31 | parameters can be given to the \code{...}, in which case 32 | missing parameters will be replaced by the ones in the 33 | \code{@stdpars}. 34 | } 35 | 36 | \section{Parameters}{ 37 | 38 | FastICA can take the following parameters: 39 | \describe{ 40 | \item{ndim}{The number of output dimensions. Defaults to \code{2}} 41 | } 42 | } 43 | 44 | \section{Implementation}{ 45 | 46 | Wraps around \code{\link[fastICA]{fastICA}}. FastICA uses a very 47 | fast approximation for negentropy to estimate statistical 48 | independences between signals. Because it is a simple 49 | rotation/projection, forward and backward functions can be given. 50 | } 51 | 52 | \examples{ 53 | if(requireNamespace("fastICA", quietly = TRUE)) { 54 | 55 | dat <- loadDataSet("3D S Curve") 56 | emb <- embed(dat, "FastICA", ndim = 2) 57 | plot(getData(getDimRedData(emb))) 58 | 59 | } 60 | } 61 | \references{ 62 | Hyvarinen, A., 1999. Fast and robust fixed-point algorithms for independent 63 | component analysis. IEEE Transactions on Neural Networks 10, 626-634. 64 | https://doi.org/10.1109/72.761722 65 | } 66 | \seealso{ 67 | Other dimensionality reduction methods: 68 | \code{\link{DRR-class}}, 69 | \code{\link{DiffusionMaps-class}}, 70 | \code{\link{DrL-class}}, 71 | \code{\link{FruchtermanReingold-class}}, 72 | \code{\link{HLLE-class}}, 73 | \code{\link{Isomap-class}}, 74 | \code{\link{KamadaKawai-class}}, 75 | \code{\link{MDS-class}}, 76 | \code{\link{NNMF-class}}, 77 | \code{\link{PCA-class}}, 78 | \code{\link{PCA_L1-class}}, 79 | \code{\link{UMAP-class}}, 80 | \code{\link{dimRedMethod-class}}, 81 | \code{\link{dimRedMethodList}()}, 82 | \code{\link{kPCA-class}}, 83 | \code{\link{nMDS-class}}, 84 | \code{\link{tSNE-class}} 85 | } 86 | \concept{dimensionality reduction methods} 87 | -------------------------------------------------------------------------------- /man/FruchtermanReingold-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph_embed.R 3 | \docType{class} 4 | \name{FruchtermanReingold-class} 5 | \alias{FruchtermanReingold-class} 6 | \alias{FruchtermanReingold} 7 | \title{Fruchterman Reingold Graph Layout} 8 | \description{ 9 | An S4 Class implementing the Fruchterman Reingold Graph Layout 10 | algorithm. 11 | } 12 | \section{Slots}{ 13 | 14 | \describe{ 15 | \item{\code{fun}}{A function that does the embedding and returns a 16 | dimRedResult object.} 17 | 18 | \item{\code{stdpars}}{The standard parameters for the function.} 19 | }} 20 | 21 | \section{General usage}{ 22 | 23 | Dimensionality reduction methods are S4 Classes that either be used 24 | directly, in which case they have to be initialized and a full 25 | list with parameters has to be handed to the \code{@fun()} 26 | slot, or the method name be passed to the embed function and 27 | parameters can be given to the \code{...}, in which case 28 | missing parameters will be replaced by the ones in the 29 | \code{@stdpars}. 30 | } 31 | 32 | \section{Parameters}{ 33 | 34 | \describe{ 35 | \item{ndim}{The number of dimensions, defaults to 2. Can only be 2 or 3} 36 | \item{knn}{Reduce the graph to keep only the neares neighbors. Defaults to 100.} 37 | \item{d}{The distance function to determine the weights of the graph edges. Defaults to euclidean distances.} 38 | } 39 | } 40 | 41 | \section{Implementation}{ 42 | 43 | Wraps around \code{\link[igraph]{layout_with_fr}}, see there for 44 | details. The Fruchterman Reingold algorithm puts the data into 45 | a circle and puts connected points close to each other. 46 | } 47 | 48 | \examples{ 49 | if(requireNamespace(c("igraph", "coRanking"), quietly = TRUE)) { 50 | 51 | dat <- loadDataSet("Swiss Roll", n = 100) 52 | emb <- embed(dat, "FruchtermanReingold") 53 | plot(emb, type = "2vars") 54 | 55 | } 56 | 57 | } 58 | \references{ 59 | Fruchterman, T.M.J., Reingold, E.M., 1991. Graph drawing by force-directed 60 | placement. Softw: Pract. Exper. 21, 1129-1164. 61 | https://doi.org/10.1002/spe.4380211102 62 | } 63 | \seealso{ 64 | Other dimensionality reduction methods: 65 | \code{\link{DRR-class}}, 66 | \code{\link{DiffusionMaps-class}}, 67 | \code{\link{DrL-class}}, 68 | \code{\link{FastICA-class}}, 69 | \code{\link{HLLE-class}}, 70 | \code{\link{Isomap-class}}, 71 | \code{\link{KamadaKawai-class}}, 72 | \code{\link{MDS-class}}, 73 | \code{\link{NNMF-class}}, 74 | \code{\link{PCA-class}}, 75 | \code{\link{PCA_L1-class}}, 76 | \code{\link{UMAP-class}}, 77 | \code{\link{dimRedMethod-class}}, 78 | \code{\link{dimRedMethodList}()}, 79 | \code{\link{kPCA-class}}, 80 | \code{\link{nMDS-class}}, 81 | \code{\link{tSNE-class}} 82 | } 83 | \concept{dimensionality reduction methods} 84 | -------------------------------------------------------------------------------- /man/HLLE-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hlle.R 3 | \docType{class} 4 | \name{HLLE-class} 5 | \alias{HLLE-class} 6 | \alias{HLLE} 7 | \title{Hessian Locally Linear Embedding} 8 | \description{ 9 | An S4 Class implementing Hessian Locally Linear Embedding (HLLE) 10 | } 11 | \details{ 12 | HLLE uses local hessians to approximate the curvines and is an 13 | extension to non-convex subsets in lowdimensional space. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{fun}}{A function that does the embedding and returns a 19 | dimRedResult object.} 20 | 21 | \item{\code{stdpars}}{The standard parameters for the function.} 22 | }} 23 | 24 | \section{General usage}{ 25 | 26 | Dimensionality reduction methods are S4 Classes that either be used 27 | directly, in which case they have to be initialized and a full 28 | list with parameters has to be handed to the \code{@fun()} 29 | slot, or the method name be passed to the embed function and 30 | parameters can be given to the \code{...}, in which case 31 | missing parameters will be replaced by the ones in the 32 | \code{@stdpars}. 33 | } 34 | 35 | \section{Parameters}{ 36 | 37 | HLLE can take the following parameters: 38 | \describe{ 39 | \item{knn}{neighborhood size} 40 | \item{ndim}{number of output dimensions} 41 | } 42 | } 43 | 44 | \section{Implementation}{ 45 | 46 | Own implementation, sticks to the algorithm in Donoho and Grimes 47 | (2003). Makes use of sparsity to speed up final embedding. 48 | } 49 | 50 | \examples{ 51 | if(requireNamespace(c("RSpectra", "Matrix", "RANN"), quietly = TRUE)) { 52 | 53 | dat <- loadDataSet("3D S Curve", n = 300) 54 | emb <- embed(dat, "HLLE", knn = 15) 55 | plot(emb, type = "2vars") 56 | 57 | } 58 | 59 | } 60 | \references{ 61 | Donoho, D.L., Grimes, C., 2003. Hessian eigenmaps: Locally linear 62 | embedding techniques for high-dimensional data. PNAS 100, 63 | 5591-5596. doi:10.1073/pnas.1031596100 64 | } 65 | \seealso{ 66 | Other dimensionality reduction methods: 67 | \code{\link{DRR-class}}, 68 | \code{\link{DiffusionMaps-class}}, 69 | \code{\link{DrL-class}}, 70 | \code{\link{FastICA-class}}, 71 | \code{\link{FruchtermanReingold-class}}, 72 | \code{\link{Isomap-class}}, 73 | \code{\link{KamadaKawai-class}}, 74 | \code{\link{MDS-class}}, 75 | \code{\link{NNMF-class}}, 76 | \code{\link{PCA-class}}, 77 | \code{\link{PCA_L1-class}}, 78 | \code{\link{UMAP-class}}, 79 | \code{\link{dimRedMethod-class}}, 80 | \code{\link{dimRedMethodList}()}, 81 | \code{\link{kPCA-class}}, 82 | \code{\link{nMDS-class}}, 83 | \code{\link{tSNE-class}} 84 | } 85 | \concept{dimensionality reduction methods} 86 | -------------------------------------------------------------------------------- /man/Isomap-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/isomap.R 3 | \docType{class} 4 | \name{Isomap-class} 5 | \alias{Isomap-class} 6 | \alias{Isomap} 7 | \title{Isomap embedding} 8 | \description{ 9 | An S4 Class implementing the Isomap Algorithm 10 | } 11 | \details{ 12 | The Isomap algorithm approximates a manifold using geodesic 13 | distances on a k nearest neighbor graph. Then classical scaling is 14 | performed on the resulting distance matrix. 15 | } 16 | \section{Slots}{ 17 | 18 | \describe{ 19 | \item{\code{fun}}{A function that does the embedding and returns a 20 | dimRedResult object.} 21 | 22 | \item{\code{stdpars}}{The standard parameters for the function.} 23 | }} 24 | 25 | \section{General usage}{ 26 | 27 | Dimensionality reduction methods are S4 Classes that either be used 28 | directly, in which case they have to be initialized and a full 29 | list with parameters has to be handed to the \code{@fun()} 30 | slot, or the method name be passed to the embed function and 31 | parameters can be given to the \code{...}, in which case 32 | missing parameters will be replaced by the ones in the 33 | \code{@stdpars}. 34 | } 35 | 36 | \section{Parameters}{ 37 | 38 | Isomap can take the following parameters: 39 | \describe{ 40 | \item{knn}{The number of nearest neighbors in the graph. Defaults to 50.} 41 | \item{ndim}{The number of embedding dimensions, defaults to 2.} 42 | \item{get_geod}{Should the geodesic distance matrix be kept, 43 | if \code{TRUE}, access it as \code{getOtherData(x)$geod}} 44 | } 45 | } 46 | 47 | \section{Implementation}{ 48 | 49 | 50 | The dimRed package uses its own implementation of Isomap which also 51 | comes with an out of sample extension (known as landmark 52 | Isomap). The default Isomap algorithm scales computationally not 53 | very well, the implementation here uses \code{\link[RANN]{nn2}} for 54 | a faster search of the nearest neighbors. If data are too large it 55 | may be useful to fit a subsample of the data and use the 56 | out-of-sample extension for the other points. 57 | } 58 | 59 | \examples{ 60 | if(requireNamespace(c("RSpectra", "igraph", "RANN"), quietly = TRUE)) { 61 | 62 | dat <- loadDataSet("3D S Curve", n = 500) 63 | emb <- embed(dat, "Isomap", knn = 10) 64 | plot(emb) 65 | 66 | ## or simpler, use embed(): 67 | samp <- sample(nrow(dat), size = 200) 68 | emb2 <- embed(dat[samp], "Isomap", .mute = NULL, knn = 10) 69 | emb3 <- predict(emb2, dat[-samp]) 70 | 71 | plot(emb2, type = "2vars") 72 | plot(emb3, type = "2vars") 73 | 74 | } 75 | } 76 | \references{ 77 | Tenenbaum, J.B., Silva, V. de, Langford, J.C., 2000. A Global Geometric 78 | Framework for Nonlinear Dimensionality Reduction. Science 290, 2319-2323. 79 | https://doi.org/10.1126/science.290.5500.2319 80 | } 81 | \seealso{ 82 | Other dimensionality reduction methods: 83 | \code{\link{DRR-class}}, 84 | \code{\link{DiffusionMaps-class}}, 85 | \code{\link{DrL-class}}, 86 | \code{\link{FastICA-class}}, 87 | \code{\link{FruchtermanReingold-class}}, 88 | \code{\link{HLLE-class}}, 89 | \code{\link{KamadaKawai-class}}, 90 | \code{\link{MDS-class}}, 91 | \code{\link{NNMF-class}}, 92 | \code{\link{PCA-class}}, 93 | \code{\link{PCA_L1-class}}, 94 | \code{\link{UMAP-class}}, 95 | \code{\link{dimRedMethod-class}}, 96 | \code{\link{dimRedMethodList}()}, 97 | \code{\link{kPCA-class}}, 98 | \code{\link{nMDS-class}}, 99 | \code{\link{tSNE-class}} 100 | } 101 | \concept{dimensionality reduction methods} 102 | -------------------------------------------------------------------------------- /man/KamadaKawai-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/graph_embed.R 3 | \docType{class} 4 | \name{KamadaKawai-class} 5 | \alias{KamadaKawai-class} 6 | \alias{KamadaKawai} 7 | \title{Graph Embedding via the Kamada Kawai Algorithm} 8 | \description{ 9 | An S4 Class implementing the Kamada Kawai Algorithm for graph embedding. 10 | } 11 | \details{ 12 | Graph embedding algorithms se the data as a graph. Between the 13 | nodes of the graph exist attracting and repelling forces which can 14 | be modeled as electrical fields or springs connecting the 15 | nodes. The graph is then forced into a lower dimensional 16 | representation that tries to represent the forces betweent he nodes 17 | accurately by minimizing the total energy of the attracting and 18 | repelling forces. 19 | } 20 | \section{Slots}{ 21 | 22 | \describe{ 23 | \item{\code{fun}}{A function that does the embedding and returns a 24 | dimRedResult object.} 25 | 26 | \item{\code{stdpars}}{The standard parameters for the function.} 27 | }} 28 | 29 | \section{General usage}{ 30 | 31 | Dimensionality reduction methods are S4 Classes that either be used 32 | directly, in which case they have to be initialized and a full 33 | list with parameters has to be handed to the \code{@fun()} 34 | slot, or the method name be passed to the embed function and 35 | parameters can be given to the \code{...}, in which case 36 | missing parameters will be replaced by the ones in the 37 | \code{@stdpars}. 38 | } 39 | 40 | \section{Parameters}{ 41 | 42 | KamadaKawai can take the following parameters: 43 | \describe{ 44 | \item{ndim}{The number of dimensions, defaults to 2. Can only be 2 or 3} 45 | \item{knn}{Reduce the graph to keep only the neares neighbors. Defaults to 100.} 46 | \item{d}{The distance function to determine the weights of the graph edges. Defaults to euclidean distances.} 47 | } 48 | } 49 | 50 | \section{Implementation}{ 51 | 52 | Wraps around \code{\link[igraph]{layout_with_kk}}. The parameters 53 | maxiter, epsilon and kkconst are set to the default values and 54 | cannot be set, this may change in a future release. The DimRed 55 | Package adds an extra sparsity parameter by constructing a knn 56 | graph which also may improve visualization quality. 57 | } 58 | 59 | \examples{ 60 | if(requireNamespace(c("igraph", "coRanking"), quietly = TRUE)) { 61 | 62 | dat <- loadDataSet("Swiss Roll", n = 200) 63 | emb <- embed(dat, "KamadaKawai") 64 | plot(emb, type = "2vars") 65 | 66 | } 67 | } 68 | \references{ 69 | Kamada, T., Kawai, S., 1989. An algorithm for drawing general undirected 70 | graphs. Information Processing Letters 31, 7-15. 71 | https://doi.org/10.1016/0020-0190(89)90102-6 72 | } 73 | \seealso{ 74 | Other dimensionality reduction methods: 75 | \code{\link{DRR-class}}, 76 | \code{\link{DiffusionMaps-class}}, 77 | \code{\link{DrL-class}}, 78 | \code{\link{FastICA-class}}, 79 | \code{\link{FruchtermanReingold-class}}, 80 | \code{\link{HLLE-class}}, 81 | \code{\link{Isomap-class}}, 82 | \code{\link{MDS-class}}, 83 | \code{\link{NNMF-class}}, 84 | \code{\link{PCA-class}}, 85 | \code{\link{PCA_L1-class}}, 86 | \code{\link{UMAP-class}}, 87 | \code{\link{dimRedMethod-class}}, 88 | \code{\link{dimRedMethodList}()}, 89 | \code{\link{kPCA-class}}, 90 | \code{\link{nMDS-class}}, 91 | \code{\link{tSNE-class}} 92 | } 93 | \concept{dimensionality reduction methods} 94 | -------------------------------------------------------------------------------- /man/LCMC-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{LCMC,dimRedResult-method} 4 | \alias{LCMC,dimRedResult-method} 5 | \alias{LCMC} 6 | \title{Method LCMC} 7 | \usage{ 8 | \S4method{LCMC}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculates the Local Continuity Meta Criterion, which is 15 | \code{\link{Q_NX}} adjusted for random overlap inside the K-ary 16 | neighborhood. 17 | } 18 | \seealso{ 19 | Other Quality scores for dimensionality reduction: 20 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 21 | \code{\link{Q_NX,dimRedResult-method}}, 22 | \code{\link{Q_global,dimRedResult-method}}, 23 | \code{\link{Q_local,dimRedResult-method}}, 24 | \code{\link{R_NX,dimRedResult-method}}, 25 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 26 | \code{\link{distance_correlation,dimRedResult-method}}, 27 | \code{\link{mean_R_NX,dimRedResult-method}}, 28 | \code{\link{plot_R_NX}()}, 29 | \code{\link{quality,dimRedResult-method}}, 30 | \code{\link{reconstruction_error,dimRedResult-method}}, 31 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 32 | \code{\link{total_correlation,dimRedResult-method}} 33 | } 34 | \concept{Quality scores for dimensionality reduction} 35 | -------------------------------------------------------------------------------- /man/MDS-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mds.R 3 | \docType{class} 4 | \name{MDS-class} 5 | \alias{MDS-class} 6 | \alias{MDS} 7 | \title{Metric Dimensional Scaling} 8 | \description{ 9 | An S4 Class implementing classical scaling (MDS). 10 | } 11 | \details{ 12 | MDS tries to maintain distances in high- and low-dimensional space, 13 | it has the advantage over PCA that arbitrary distance functions can 14 | be used, but it is computationally more demanding. 15 | } 16 | \section{Slots}{ 17 | 18 | \describe{ 19 | \item{\code{fun}}{A function that does the embedding and returns a 20 | dimRedResult object.} 21 | 22 | \item{\code{stdpars}}{The standard parameters for the function.} 23 | }} 24 | 25 | \section{General usage}{ 26 | 27 | Dimensionality reduction methods are S4 Classes that either be used 28 | directly, in which case they have to be initialized and a full 29 | list with parameters has to be handed to the \code{@fun()} 30 | slot, or the method name be passed to the embed function and 31 | parameters can be given to the \code{...}, in which case 32 | missing parameters will be replaced by the ones in the 33 | \code{@stdpars}. 34 | } 35 | 36 | \section{Parameters}{ 37 | 38 | MDS can take the following parameters: 39 | \describe{ 40 | \item{ndim}{The number of dimensions.} 41 | \item{d}{The function to calculate the distance matrix from the input coordinates, defaults to euclidean distances.} 42 | } 43 | } 44 | 45 | \section{Implementation}{ 46 | 47 | 48 | Wraps around \code{\link[stats]{cmdscale}}. The implementation also 49 | provides an out-of-sample extension which is not completely 50 | optimized yet. 51 | } 52 | 53 | \examples{ 54 | \dontrun{ 55 | dat <- loadDataSet("3D S Curve") 56 | emb <- embed(dat, "MDS") 57 | plot(emb, type = "2vars") 58 | 59 | # a "manual" kPCA: 60 | emb2 <- embed(dat, "MDS", d = function(x) exp(stats::dist(x))) 61 | plot(emb2, type = "2vars") 62 | 63 | # a "manual", more customizable, and slower Isomap: 64 | emb3 <- embed(dat, "MDS", d = function(x) vegan::isomapdist(vegan::vegdist(x, "manhattan"), k = 20)) 65 | plot(emb3) 66 | 67 | } 68 | } 69 | \references{ 70 | Torgerson, W.S., 1952. Multidimensional scaling: I. Theory and method. 71 | Psychometrika 17, 401-419. https://doi.org/10.1007/BF02288916 72 | } 73 | \seealso{ 74 | Other dimensionality reduction methods: 75 | \code{\link{DRR-class}}, 76 | \code{\link{DiffusionMaps-class}}, 77 | \code{\link{DrL-class}}, 78 | \code{\link{FastICA-class}}, 79 | \code{\link{FruchtermanReingold-class}}, 80 | \code{\link{HLLE-class}}, 81 | \code{\link{Isomap-class}}, 82 | \code{\link{KamadaKawai-class}}, 83 | \code{\link{NNMF-class}}, 84 | \code{\link{PCA-class}}, 85 | \code{\link{PCA_L1-class}}, 86 | \code{\link{UMAP-class}}, 87 | \code{\link{dimRedMethod-class}}, 88 | \code{\link{dimRedMethodList}()}, 89 | \code{\link{kPCA-class}}, 90 | \code{\link{nMDS-class}}, 91 | \code{\link{tSNE-class}} 92 | } 93 | \concept{dimensionality reduction methods} 94 | -------------------------------------------------------------------------------- /man/NNMF-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/nnmf.R 3 | \docType{class} 4 | \name{NNMF-class} 5 | \alias{NNMF-class} 6 | \alias{NNMF} 7 | \title{Non-Negative Matrix Factorization} 8 | \description{ 9 | S4 Class implementing NNMF. 10 | } 11 | \details{ 12 | NNMF is a method for decomposing a matrix into a smaller 13 | dimension such that the constraint that the data (and the 14 | projection) are not negative is taken into account. 15 | } 16 | \section{Slots}{ 17 | 18 | \describe{ 19 | \item{\code{fun}}{A function that does the embedding and returns a 20 | dimRedResult object.} 21 | 22 | \item{\code{stdpars}}{The standard parameters for the function.} 23 | }} 24 | 25 | \section{General usage}{ 26 | 27 | Dimensionality reduction methods are S4 Classes that either be used 28 | directly, in which case they have to be initialized and a full 29 | list with parameters has to be handed to the \code{@fun()} 30 | slot, or the method name be passed to the embed function and 31 | parameters can be given to the \code{...}, in which case 32 | missing parameters will be replaced by the ones in the 33 | \code{@stdpars}. 34 | } 35 | 36 | \section{Parameters}{ 37 | 38 | The method can take the following parameters: 39 | \describe{ 40 | \item{ndim}{The number of output dimensions.} 41 | \item{method}{character, which algorithm should be used. See 42 | \code{\link[NMF]{nmf}} for possible values. Defaults to 43 | "brunet"} 44 | \item{nrun}{integer, the number of times the computations are 45 | conducted. See \code{\link[NMF]{nmf}}} 46 | \item{seed}{integer, a value to control the random numbers used.} 47 | \item{options}{named list, other options to pass to \code{\link[NMF]{nmf}}} 48 | } 49 | } 50 | 51 | \section{Implementation}{ 52 | 53 | 54 | Wraps around \code{\link[NMF]{nmf}}. Note that the estimation uses random 55 | numbers. To create reproducible results, set the random number seed in the 56 | function call. Also, in many cases, the computations will be conducted 57 | in parallel using multiple cores. To disable this, use the option 58 | \code{.pbackend = NULL}. 59 | } 60 | 61 | \examples{ 62 | if(requireNamespace(c("NNMF", "MASS"), quietly = TRUE)) { 63 | 64 | set.seed(4646) 65 | dat <- loadDataSet("Iris") 66 | emb <- embed(dat, "NNMF") 67 | 68 | plot(emb) 69 | 70 | # project new values: 71 | nn_proj <- predict(emb, dat[1:7]) 72 | plot(nn_proj) 73 | 74 | } 75 | } 76 | \references{ 77 | Lee, D.D., Seung, H.S., 1999. Learning the parts of objects by non-negative 78 | matrix factorization. Nature 401, 788-791. https://doi.org/10.1038/44565 79 | } 80 | \seealso{ 81 | Other dimensionality reduction methods: 82 | \code{\link{DRR-class}}, 83 | \code{\link{DiffusionMaps-class}}, 84 | \code{\link{DrL-class}}, 85 | \code{\link{FastICA-class}}, 86 | \code{\link{FruchtermanReingold-class}}, 87 | \code{\link{HLLE-class}}, 88 | \code{\link{Isomap-class}}, 89 | \code{\link{KamadaKawai-class}}, 90 | \code{\link{MDS-class}}, 91 | \code{\link{PCA-class}}, 92 | \code{\link{PCA_L1-class}}, 93 | \code{\link{UMAP-class}}, 94 | \code{\link{dimRedMethod-class}}, 95 | \code{\link{dimRedMethodList}()}, 96 | \code{\link{kPCA-class}}, 97 | \code{\link{nMDS-class}}, 98 | \code{\link{tSNE-class}} 99 | } 100 | \concept{dimensionality reduction methods} 101 | -------------------------------------------------------------------------------- /man/PCA-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pca.R 3 | \docType{class} 4 | \name{PCA-class} 5 | \alias{PCA-class} 6 | \alias{PCA} 7 | \title{Principal Component Analysis} 8 | \description{ 9 | S4 Class implementing PCA. 10 | } 11 | \details{ 12 | PCA transforms the data in orthogonal components so that the first 13 | axis accounts for the larges variance in the data, all the 14 | following axes account for the highest variance under the 15 | constraint that they are orthogonal to the preceding axes. PCA is 16 | sensitive to the scaling of the variables. PCA is by far the 17 | fastest and simples method of dimensionality reduction and should 18 | probably always be applied as a baseline if other methods are tested. 19 | } 20 | \section{Slots}{ 21 | 22 | \describe{ 23 | \item{\code{fun}}{A function that does the embedding and returns a 24 | dimRedResult object.} 25 | 26 | \item{\code{stdpars}}{The standard parameters for the function.} 27 | }} 28 | 29 | \section{General usage}{ 30 | 31 | Dimensionality reduction methods are S4 Classes that either be used 32 | directly, in which case they have to be initialized and a full 33 | list with parameters has to be handed to the \code{@fun()} 34 | slot, or the method name be passed to the embed function and 35 | parameters can be given to the \code{...}, in which case 36 | missing parameters will be replaced by the ones in the 37 | \code{@stdpars}. 38 | } 39 | 40 | \section{Parameters}{ 41 | 42 | PCA can take the following parameters: 43 | \describe{ 44 | \item{ndim}{The number of output dimensions.} 45 | \item{center}{logical, should the data be centered, defaults to \code{TRUE}.} 46 | \item{scale.}{logical, should the data be scaled, defaults to \code{FALSE}.} 47 | } 48 | } 49 | 50 | \section{Implementation}{ 51 | 52 | 53 | Wraps around \code{\link{prcomp}}. Because PCA can be reduced to a 54 | simple rotation, forward and backward projection functions are 55 | supplied. 56 | } 57 | 58 | \examples{ 59 | dat <- loadDataSet("Iris") 60 | emb <- embed(dat, "PCA") 61 | 62 | plot(emb, type = "2vars") 63 | if(requireNamespace("scatterplot3d", quietly = TRUE)) 64 | plot(inverse(emb, getDimRedData(emb)), type = "3vars") 65 | 66 | } 67 | \references{ 68 | Pearson, K., 1901. On lines and planes of closest fit to systems of points in 69 | space. Philosophical Magazine 2, 559-572. 70 | } 71 | \seealso{ 72 | Other dimensionality reduction methods: 73 | \code{\link{DRR-class}}, 74 | \code{\link{DiffusionMaps-class}}, 75 | \code{\link{DrL-class}}, 76 | \code{\link{FastICA-class}}, 77 | \code{\link{FruchtermanReingold-class}}, 78 | \code{\link{HLLE-class}}, 79 | \code{\link{Isomap-class}}, 80 | \code{\link{KamadaKawai-class}}, 81 | \code{\link{MDS-class}}, 82 | \code{\link{NNMF-class}}, 83 | \code{\link{PCA_L1-class}}, 84 | \code{\link{UMAP-class}}, 85 | \code{\link{dimRedMethod-class}}, 86 | \code{\link{dimRedMethodList}()}, 87 | \code{\link{kPCA-class}}, 88 | \code{\link{nMDS-class}}, 89 | \code{\link{tSNE-class}} 90 | } 91 | \concept{dimensionality reduction methods} 92 | -------------------------------------------------------------------------------- /man/PCA_L1-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/l1pca.R 3 | \docType{class} 4 | \name{PCA_L1-class} 5 | \alias{PCA_L1-class} 6 | \alias{PCA_L1} 7 | \title{Principal Component Analysis with L1 error.} 8 | \description{ 9 | S4 Class implementing PCA with L1 error. 10 | } 11 | \details{ 12 | PCA transforms the data so that the L2 reconstruction error is minimized or 13 | the variance of the projected data is maximized. This is sensitive to 14 | outliers, L1 PCA minimizes the L1 reconstruction error or maximizes the sum 15 | of the L1 norm of the projected observations. 16 | } 17 | \section{Slots}{ 18 | 19 | \describe{ 20 | \item{\code{fun}}{A function that does the embedding and returns a 21 | dimRedResult object.} 22 | 23 | \item{\code{stdpars}}{The standard parameters for the function.} 24 | }} 25 | 26 | \section{General usage}{ 27 | 28 | Dimensionality reduction methods are S4 Classes that either be used 29 | directly, in which case they have to be initialized and a full 30 | list with parameters has to be handed to the \code{@fun()} 31 | slot, or the method name be passed to the embed function and 32 | parameters can be given to the \code{...}, in which case 33 | missing parameters will be replaced by the ones in the 34 | \code{@stdpars}. 35 | } 36 | 37 | \section{Parameters}{ 38 | 39 | PCA can take the following parameters: 40 | \describe{ 41 | \item{ndim}{The number of output dimensions.} 42 | \item{center}{logical, should the data be centered, defaults to \code{TRUE}.} 43 | \item{scale.}{logical, should the data be scaled, defaults to \code{FALSE}.} 44 | \item{fun}{character or function, the method to apply, see the \code{pcaL1} package} 45 | \item{\ldots}{other parameters for \code{fun}} 46 | } 47 | } 48 | 49 | \section{Implementation}{ 50 | 51 | 52 | Wraps around the different methods is the \code{pcaL1} package. Because PCA 53 | can be reduced to a simple rotation, forward and backward projection 54 | functions are supplied. 55 | } 56 | 57 | \examples{ 58 | if(requireNamespace("pcaL1", quietly = TRUE)) { 59 | 60 | dat <- loadDataSet("Iris") 61 | emb <- embed(dat, "PCA_L1") 62 | 63 | plot(emb, type = "2vars") 64 | plot(inverse(emb, getData(getDimRedData((emb)))), type = "3vars") 65 | 66 | } 67 | 68 | } 69 | \references{ 70 | Park, Y.W., Klabjan, D., 2016. Iteratively Reweighted Least Squares 71 | Algorithms for L1-Norm Principal Component Analysis, in: Data Mining (ICDM), 72 | 2016 IEEE 16th International Conference On. IEEE, pp. 430-438. 73 | } 74 | \seealso{ 75 | Other dimensionality reduction methods: 76 | \code{\link{DRR-class}}, 77 | \code{\link{DiffusionMaps-class}}, 78 | \code{\link{DrL-class}}, 79 | \code{\link{FastICA-class}}, 80 | \code{\link{FruchtermanReingold-class}}, 81 | \code{\link{HLLE-class}}, 82 | \code{\link{Isomap-class}}, 83 | \code{\link{KamadaKawai-class}}, 84 | \code{\link{MDS-class}}, 85 | \code{\link{NNMF-class}}, 86 | \code{\link{PCA-class}}, 87 | \code{\link{UMAP-class}}, 88 | \code{\link{dimRedMethod-class}}, 89 | \code{\link{dimRedMethodList}()}, 90 | \code{\link{kPCA-class}}, 91 | \code{\link{nMDS-class}}, 92 | \code{\link{tSNE-class}} 93 | } 94 | \concept{dimensionality reduction methods} 95 | -------------------------------------------------------------------------------- /man/Q_NX-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{Q_NX,dimRedResult-method} 4 | \alias{Q_NX,dimRedResult-method} 5 | \alias{Q_NX} 6 | \title{Method Q_NX} 7 | \usage{ 8 | \S4method{Q_NX}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculate the Q_NX score (Chen & Buja 2006, the notation in the 15 | publication is M_k). Which is the fraction of points that remain inside 16 | the same K-ary neighborhood in high and low dimensional space. 17 | } 18 | \seealso{ 19 | Other Quality scores for dimensionality reduction: 20 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 21 | \code{\link{LCMC,dimRedResult-method}}, 22 | \code{\link{Q_global,dimRedResult-method}}, 23 | \code{\link{Q_local,dimRedResult-method}}, 24 | \code{\link{R_NX,dimRedResult-method}}, 25 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 26 | \code{\link{distance_correlation,dimRedResult-method}}, 27 | \code{\link{mean_R_NX,dimRedResult-method}}, 28 | \code{\link{plot_R_NX}()}, 29 | \code{\link{quality,dimRedResult-method}}, 30 | \code{\link{reconstruction_error,dimRedResult-method}}, 31 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 32 | \code{\link{total_correlation,dimRedResult-method}} 33 | } 34 | \concept{Quality scores for dimensionality reduction} 35 | -------------------------------------------------------------------------------- /man/Q_global-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{Q_global,dimRedResult-method} 4 | \alias{Q_global,dimRedResult-method} 5 | \alias{Q_global} 6 | \title{Method Q_global} 7 | \usage{ 8 | \S4method{Q_global}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculate the Q_global score to assess the quality of a dimensionality reduction. 15 | } 16 | \seealso{ 17 | Other Quality scores for dimensionality reduction: 18 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 19 | \code{\link{LCMC,dimRedResult-method}}, 20 | \code{\link{Q_NX,dimRedResult-method}}, 21 | \code{\link{Q_local,dimRedResult-method}}, 22 | \code{\link{R_NX,dimRedResult-method}}, 23 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 24 | \code{\link{distance_correlation,dimRedResult-method}}, 25 | \code{\link{mean_R_NX,dimRedResult-method}}, 26 | \code{\link{plot_R_NX}()}, 27 | \code{\link{quality,dimRedResult-method}}, 28 | \code{\link{reconstruction_error,dimRedResult-method}}, 29 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 30 | \code{\link{total_correlation,dimRedResult-method}} 31 | } 32 | \concept{Quality scores for dimensionality reduction} 33 | -------------------------------------------------------------------------------- /man/Q_local-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{Q_local,dimRedResult-method} 4 | \alias{Q_local,dimRedResult-method} 5 | \alias{Q_local} 6 | \title{Method Q_local} 7 | \usage{ 8 | \S4method{Q_local}{dimRedResult}(object, ndim = getNDim(object)) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult.} 12 | 13 | \item{ndim}{use the first ndim columns of the embedded data for calculation.} 14 | } 15 | \description{ 16 | Calculate the Q_local score to assess the quality of a dimensionality reduction. 17 | } 18 | \seealso{ 19 | Other Quality scores for dimensionality reduction: 20 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 21 | \code{\link{LCMC,dimRedResult-method}}, 22 | \code{\link{Q_NX,dimRedResult-method}}, 23 | \code{\link{Q_global,dimRedResult-method}}, 24 | \code{\link{R_NX,dimRedResult-method}}, 25 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 26 | \code{\link{distance_correlation,dimRedResult-method}}, 27 | \code{\link{mean_R_NX,dimRedResult-method}}, 28 | \code{\link{plot_R_NX}()}, 29 | \code{\link{quality,dimRedResult-method}}, 30 | \code{\link{reconstruction_error,dimRedResult-method}}, 31 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 32 | \code{\link{total_correlation,dimRedResult-method}} 33 | } 34 | \concept{Quality scores for dimensionality reduction} 35 | -------------------------------------------------------------------------------- /man/R_NX-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{R_NX,dimRedResult-method} 4 | \alias{R_NX,dimRedResult-method} 5 | \alias{R_NX} 6 | \title{Method R_NX} 7 | \usage{ 8 | \S4method{R_NX}{dimRedResult}(object, ndim = getNDim(object)) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | 13 | \item{ndim}{the number of dimensions to take from the embedded data.} 14 | } 15 | \description{ 16 | Calculate the R_NX score from Lee et. al. (2013) which shows the neighborhood 17 | preservation for the Kth nearest neighbors, corrected for random point 18 | distributions and scaled to range [0, 1]. 19 | } 20 | \seealso{ 21 | Other Quality scores for dimensionality reduction: 22 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 23 | \code{\link{LCMC,dimRedResult-method}}, 24 | \code{\link{Q_NX,dimRedResult-method}}, 25 | \code{\link{Q_global,dimRedResult-method}}, 26 | \code{\link{Q_local,dimRedResult-method}}, 27 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 28 | \code{\link{distance_correlation,dimRedResult-method}}, 29 | \code{\link{mean_R_NX,dimRedResult-method}}, 30 | \code{\link{plot_R_NX}()}, 31 | \code{\link{quality,dimRedResult-method}}, 32 | \code{\link{reconstruction_error,dimRedResult-method}}, 33 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 34 | \code{\link{total_correlation,dimRedResult-method}} 35 | } 36 | \concept{Quality scores for dimensionality reduction} 37 | -------------------------------------------------------------------------------- /man/UMAP-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/umap.R 3 | \docType{class} 4 | \name{UMAP-class} 5 | \alias{UMAP-class} 6 | \alias{UMAP} 7 | \title{Umap embedding} 8 | \description{ 9 | An S4 Class implementing the UMAP algorithm 10 | } 11 | \details{ 12 | Uniform Manifold Approximation is a gradient descend based algorithm that 13 | gives results similar to t-SNE, but scales better with the number of points. 14 | } 15 | \section{Slots}{ 16 | 17 | \describe{ 18 | \item{\code{fun}}{A function that does the embedding and returns a 19 | dimRedResult object.} 20 | 21 | \item{\code{stdpars}}{The standard parameters for the function.} 22 | }} 23 | 24 | \section{General usage}{ 25 | 26 | Dimensionality reduction methods are S4 Classes that either be used 27 | directly, in which case they have to be initialized and a full 28 | list with parameters has to be handed to the \code{@fun()} 29 | slot, or the method name be passed to the embed function and 30 | parameters can be given to the \code{...}, in which case 31 | missing parameters will be replaced by the ones in the 32 | \code{@stdpars}. 33 | } 34 | 35 | \section{Parameters}{ 36 | 37 | 38 | UMAP can take the follwing parameters: 39 | \describe{ 40 | \item{ndim}{The number of embedding dimensions.} 41 | \item{knn}{The number of neighbors to be used.} 42 | \item{d}{The distance metric to use.} 43 | \item{method}{\code{"naive"} for an R implementation, \code{"python"} 44 | for the reference implementation.} 45 | } 46 | 47 | Other method parameters can also be passed, see 48 | \code{\link[umap]{umap.defaults}} for details. The ones above have been 49 | standardized for the use with \code{dimRed} and will get automatically 50 | translated for \code{\link[umap]{umap}}. 51 | } 52 | 53 | \section{Implementation}{ 54 | 55 | 56 | The dimRed package wraps the \code{\link[umap]{umap}} packages which provides 57 | an implementation in pure R and also a wrapper around the original python 58 | package \code{umap-learn} (https://github.com/lmcinnes/umap/). This requires 59 | \code{umap-learn} version 0.4 installed, at the time of writing, there is 60 | already \code{umap-learn} 0.5 but it is not supported by the R package 61 | \code{\link[umap]{umap}}. 62 | 63 | The \code{"naive"} implementation is a pure R implementation and considered 64 | experimental at the point of writing this, it is also much slower than the 65 | python implementation. 66 | 67 | The \code{"python"} implementation is the reference implementation used by 68 | McInees et. al. (2018). It requires the \code{\link[reticulate]{reticulate}} 69 | package for the interaction with python and the python package 70 | \code{umap-learn} installed (use \code{pip install umap-learn}). 71 | } 72 | 73 | \examples{ 74 | \dontrun{ 75 | dat <- loadDataSet("3D S Curve", n = 300) 76 | emb <- embed(dat, "UMAP", .mute = NULL, knn = 10) 77 | plot(emb, type = "2vars") 78 | } 79 | 80 | } 81 | \references{ 82 | McInnes, Leland, and John Healy. 83 | "UMAP: Uniform Manifold Approximation and Projection for Dimension Reduction." 84 | https://arxiv.org/abs/1802.03426 85 | } 86 | \seealso{ 87 | Other dimensionality reduction methods: 88 | \code{\link{DRR-class}}, 89 | \code{\link{DiffusionMaps-class}}, 90 | \code{\link{DrL-class}}, 91 | \code{\link{FastICA-class}}, 92 | \code{\link{FruchtermanReingold-class}}, 93 | \code{\link{HLLE-class}}, 94 | \code{\link{Isomap-class}}, 95 | \code{\link{KamadaKawai-class}}, 96 | \code{\link{MDS-class}}, 97 | \code{\link{NNMF-class}}, 98 | \code{\link{PCA-class}}, 99 | \code{\link{PCA_L1-class}}, 100 | \code{\link{dimRedMethod-class}}, 101 | \code{\link{dimRedMethodList}()}, 102 | \code{\link{kPCA-class}}, 103 | \code{\link{nMDS-class}}, 104 | \code{\link{tSNE-class}} 105 | } 106 | \concept{dimensionality reduction methods} 107 | -------------------------------------------------------------------------------- /man/as.data.frame.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{as.data.frame} 4 | \alias{as.data.frame} 5 | \title{Converts to data.frame} 6 | \usage{ 7 | as.data.frame(x, row.names, optional, ...) 8 | } 9 | \arguments{ 10 | \item{x}{The object to be converted} 11 | 12 | \item{row.names}{unused in \code{dimRed}} 13 | 14 | \item{optional}{unused in \code{dimRed}} 15 | 16 | \item{...}{other arguments.} 17 | } 18 | \description{ 19 | General conversions of objects created by \code{dimRed} to \code{data.frame}. 20 | See class documentations for details (\code{\link{dimRedData}}, 21 | \code{\link{dimRedResult}}). For the documentation of this function in base 22 | package, see here: \code{\link[base]{as.data.frame.default}}. 23 | } 24 | -------------------------------------------------------------------------------- /man/as.dimRedData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R, R/dimRedData-class.R 3 | \name{as.dimRedData} 4 | \alias{as.dimRedData} 5 | \alias{as.dimRedData,formula-method} 6 | \title{Converts to dimRedData} 7 | \usage{ 8 | as.dimRedData(formula, ...) 9 | 10 | \S4method{as.dimRedData}{formula}(formula, data) 11 | } 12 | \arguments{ 13 | \item{formula}{The formula, left hand side is assigned to the meta slot right 14 | hand side is assigned to the data slot.} 15 | 16 | \item{...}{other arguments.} 17 | 18 | \item{data}{Will be coerced into a \code{\link{data.frame}} with 19 | \code{\link{as.data.frame}}} 20 | } 21 | \description{ 22 | Conversion functions to dimRedData. 23 | } 24 | \section{Methods (by class)}{ 25 | \itemize{ 26 | \item \code{as.dimRedData(formula)}: Convert a \code{data.frame} to a dimRedData 27 | object using a formula 28 | 29 | }} 30 | \examples{ 31 | ## create a dimRedData object using a formula 32 | as.dimRedData(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 33 | iris)[1:5] 34 | 35 | } 36 | \seealso{ 37 | Other dimRedData: 38 | \code{\link{dimRedData-class}} 39 | } 40 | \concept{dimRedData} 41 | -------------------------------------------------------------------------------- /man/cophenetic_correlation-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{cophenetic_correlation,dimRedResult-method} 4 | \alias{cophenetic_correlation,dimRedResult-method} 5 | \alias{cophenetic_correlation} 6 | \title{Method cophenetic_correlation} 7 | \usage{ 8 | \S4method{cophenetic_correlation}{dimRedResult}(object, d = stats::dist, cor_method = "pearson") 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | 13 | \item{d}{the distance function to use.} 14 | 15 | \item{cor_method}{The correlation method.} 16 | } 17 | \description{ 18 | Calculate the correlation between the distance matrices in high and 19 | low dimensioal space. 20 | } 21 | \seealso{ 22 | Other Quality scores for dimensionality reduction: 23 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 24 | \code{\link{LCMC,dimRedResult-method}}, 25 | \code{\link{Q_NX,dimRedResult-method}}, 26 | \code{\link{Q_global,dimRedResult-method}}, 27 | \code{\link{Q_local,dimRedResult-method}}, 28 | \code{\link{R_NX,dimRedResult-method}}, 29 | \code{\link{distance_correlation,dimRedResult-method}}, 30 | \code{\link{mean_R_NX,dimRedResult-method}}, 31 | \code{\link{plot_R_NX}()}, 32 | \code{\link{quality,dimRedResult-method}}, 33 | \code{\link{reconstruction_error,dimRedResult-method}}, 34 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 35 | \code{\link{total_correlation,dimRedResult-method}} 36 | } 37 | \concept{Quality scores for dimensionality reduction} 38 | -------------------------------------------------------------------------------- /man/dataSets.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dataSets.R 3 | \name{dataSets} 4 | \alias{dataSets} 5 | \alias{loadDataSet} 6 | \alias{dataSetList} 7 | \title{Example Data Sets for dimensionality reduction} 8 | \usage{ 9 | loadDataSet(name = dataSetList(), n = 2000, sigma = 0.05) 10 | 11 | dataSetList() 12 | } 13 | \arguments{ 14 | \item{name}{A character vector that specifies the name of the data 15 | set.} 16 | 17 | \item{n}{In generated data sets the number of points to be 18 | generated, else ignored.} 19 | 20 | \item{sigma}{In generated data sets the standard deviation of the 21 | noise added, else ignored.} 22 | } 23 | \value{ 24 | \code{loadDataSet} an object of class 25 | \code{\link{dimRedData}}. \code{dataSetList()} return a 26 | character string with the implemented data sets 27 | } 28 | \description{ 29 | A compilation of standard data sets that are often being used to 30 | showcase dimensionality reduction techniques. 31 | } 32 | \details{ 33 | The argument \code{name} should be one of 34 | \code{dataSetList()}. Partial matching is possible, see 35 | \code{\link{match.arg}}. Generated data sets contain the internal 36 | coordinates of the manifold in the \code{meta} slot. Call 37 | \code{dataSetList()} to see what data sets are available. 38 | } 39 | \examples{ 40 | ## a list of available data sets: 41 | dataSetList() 42 | 43 | ## Load a data set: 44 | swissRoll <- loadDataSet("Swiss Roll") 45 | \donttest{ 46 | if(requireNamespace("scatterplot3d", quietly = TRUE)) 47 | plot(swissRoll, type = "3vars") 48 | } 49 | 50 | ## Load Iris data set, partial matching: 51 | loadDataSet("I") 52 | 53 | } 54 | -------------------------------------------------------------------------------- /man/dimRed-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dimRed.R 3 | \docType{package} 4 | \name{dimRed-package} 5 | \alias{dimRed} 6 | \alias{dimRed-package} 7 | \title{The dimRed package} 8 | \description{ 9 | This package simplifies dimensionality reduction in R by 10 | providing a framework of S4 classes and methods. dimRed collects 11 | dimensionality reduction methods that are implemented in R and implements 12 | others. It gives them a common interface and provides plotting 13 | functions for visualization and functions for quality assessment. 14 | 15 | Funding provided by the Department for Biogeochemical Integration, 16 | Empirical Inference of the Earth System Group, at the Max Plack 17 | Institute for Biogeochemistry, Jena. 18 | } 19 | \references{ 20 | Lee, J.A., Renard, E., Bernard, G., Dupont, P., Verleysen, M., 21 | 2013. Type 1 and 2 mixtures of Kullback-Leibler divergences as cost 22 | functions in dimensionality reduction based on similarity 23 | preservation. Neurocomputing. 112, 24 | 92-107. doi:10.1016/j.neucom.2012.12.036 25 | 26 | Lee, J.A., Lee, J.A., Verleysen, M., 2008. Rank-based quality 27 | assessment of nonlinear dimensionality reduction. Proceedings of 28 | ESANN 2008 49-54. 29 | 30 | Chen, L., Buja, A., 2006. Local Multidimensional Scaling for 31 | Nonlinear Dimension Reduction, Graph Layout and Proximity Analysis. 32 | } 33 | \seealso{ 34 | Useful links: 35 | \itemize{ 36 | \item \url{https://www.guido-kraemer.com/software/dimred/} 37 | \item Report bugs at \url{https://github.com/gdkrmr/dimRed/issues} 38 | } 39 | 40 | } 41 | \author{ 42 | \strong{Maintainer}: Guido Kraemer \email{guido.kraemer@uni-leipzig.de} (\href{https://orcid.org/0000-0003-4865-5041}{ORCID}) 43 | 44 | } 45 | -------------------------------------------------------------------------------- /man/dimRedData-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dimRedData-class.R 3 | \docType{class} 4 | \name{dimRedData-class} 5 | \alias{dimRedData-class} 6 | \alias{dimRedData} 7 | \alias{as.data.frame,dimRedData-method} 8 | \alias{getData,dimRedData-method} 9 | \alias{getMeta,dimRedData-method} 10 | \alias{nrow,dimRedData-method} 11 | \alias{[,dimRedData,ANY,ANY,ANY-method} 12 | \alias{ndims,dimRedData-method} 13 | \title{Class "dimRedData"} 14 | \usage{ 15 | \S4method{as.data.frame}{dimRedData}(x, meta.prefix = "meta.", data.prefix = "") 16 | 17 | \S4method{getData}{dimRedData}(object) 18 | 19 | \S4method{getMeta}{dimRedData}(object) 20 | 21 | \S4method{nrow}{dimRedData}(x) 22 | 23 | \S4method{[}{dimRedData,ANY,ANY,ANY}(x, i) 24 | 25 | \S4method{ndims}{dimRedData}(object) 26 | } 27 | \arguments{ 28 | \item{x}{Of class dimRedData} 29 | 30 | \item{meta.prefix}{Prefix for the columns of the meta data names.} 31 | 32 | \item{data.prefix}{Prefix for the columns of the variable names.} 33 | 34 | \item{object}{Of class dimRedData.} 35 | 36 | \item{i}{a valid index for subsetting rows.} 37 | } 38 | \description{ 39 | A class to hold data for dimensionality reduction and methods. 40 | } 41 | \details{ 42 | The class hast two slots, \code{data} and \code{meta}. The 43 | \code{data} slot contains a \code{numeric matrix} with variables in 44 | columns and observations in rows. The \code{meta} slot may contain 45 | a \code{data.frame} with additional information. Both slots need to 46 | have the same number of rows or the \code{meta} slot needs to 47 | contain an empty \code{data.frame}. 48 | 49 | See examples for easy conversion from and to \code{data.frame}. 50 | 51 | For plotting functions see \code{\link{plot.dimRedData}}. 52 | } 53 | \section{Methods (by generic)}{ 54 | \itemize{ 55 | \item \code{as.data.frame(dimRedData)}: convert to data.frame 56 | 57 | \item \code{getData(dimRedData)}: Get the data slot. 58 | 59 | \item \code{getMeta(dimRedData)}: Get the meta slot. 60 | 61 | \item \code{nrow(dimRedData)}: Get the number of observations. 62 | 63 | \item \code{x[i}: Subset rows. 64 | 65 | \item \code{ndims(dimRedData)}: Extract the number of Variables from the data. 66 | 67 | }} 68 | \section{Slots}{ 69 | 70 | \describe{ 71 | \item{\code{data}}{of class \code{matrix}, holds the data, observations in 72 | rows, variables in columns} 73 | 74 | \item{\code{meta}}{of class \code{data.frame}, holds meta data such as 75 | classes, internal manifold coordinates, or simply additional 76 | data of the data set. Must have the same number of rows as the 77 | \code{data} slot or be an empty data frame.} 78 | }} 79 | 80 | \examples{ 81 | ## Load an example data set: 82 | s3d <- loadDataSet("3D S Curve") 83 | 84 | ## Create using a constructor: 85 | 86 | ### without meta information: 87 | dimRedData(iris[, 1:4]) 88 | 89 | ### with meta information: 90 | dimRedData(iris[, 1:4], iris[, 5]) 91 | 92 | ### using slot names: 93 | dimRedData(data = iris[, 1:4], meta = iris[, 5]) 94 | 95 | ## Convert to a dimRedData objects: 96 | Iris <- as(iris[, 1:4], "dimRedData") 97 | 98 | ## Convert to data.frame: 99 | head(as(s3d, "data.frame")) 100 | head(as.data.frame(s3d)) 101 | head(as.data.frame(as(iris[, 1:4], "dimRedData"))) 102 | 103 | ## Extract slots: 104 | head(getData(s3d)) 105 | head(getMeta(s3d)) 106 | 107 | ## Get the number of observations: 108 | nrow(s3d) 109 | 110 | ## Subset: 111 | s3d[1:5, ] 112 | 113 | ## Shuffle data: 114 | s3 <- s3d[nrow(s3d)] 115 | 116 | ## Get the number of variables: 117 | ndims(s3d) 118 | 119 | } 120 | \seealso{ 121 | Other dimRedData: 122 | \code{\link{as.dimRedData}()} 123 | 124 | Other dimRedData: 125 | \code{\link{as.dimRedData}()} 126 | } 127 | \concept{dimRedData} 128 | -------------------------------------------------------------------------------- /man/dimRedMethod-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dimRedMethod-class.R 3 | \docType{class} 4 | \name{dimRedMethod-class} 5 | \alias{dimRedMethod-class} 6 | \title{Class "dimRedMethod"} 7 | \description{ 8 | A virtual class "dimRedMethod" to serve as a template to implement 9 | methods for dimensionality reduction. 10 | } 11 | \details{ 12 | Implementations of dimensionality reductions should inherit from 13 | this class. 14 | 15 | The \code{fun} slot should be a function that takes three arguments 16 | \describe{ 17 | \item{data}{An object of class \code{\link{dimRedData}}.} 18 | \item{pars}{A list with the standard parameters.} 19 | \item{keep.org.data}{Logical. If the original data should be kept in the output.} 20 | } 21 | and returns an object of class \code{\link{dimRedResult}}. 22 | 23 | The \code{stdpars} slot should take a list that contains standard 24 | parameters for the implemented methods. 25 | 26 | This way the method can be called by \code{embed(data, "method-name", 27 | ...)}, where \code{...} can be used to to change single parameters. 28 | } 29 | \section{Slots}{ 30 | 31 | \describe{ 32 | \item{\code{fun}}{A function that does the embedding.} 33 | 34 | \item{\code{stdpars}}{A list with the default parameters for the \code{fun} slot.} 35 | 36 | \item{\code{requires}}{A vector with all packages R packages that need to be 37 | installed to run the method. In some occasions a method may work without 38 | one of the packages. Does not include Python dependencies such as 39 | Tensorflow. Used to auto skip tests} 40 | }} 41 | 42 | \seealso{ 43 | Other dimensionality reduction methods: 44 | \code{\link{DRR-class}}, 45 | \code{\link{DiffusionMaps-class}}, 46 | \code{\link{DrL-class}}, 47 | \code{\link{FastICA-class}}, 48 | \code{\link{FruchtermanReingold-class}}, 49 | \code{\link{HLLE-class}}, 50 | \code{\link{Isomap-class}}, 51 | \code{\link{KamadaKawai-class}}, 52 | \code{\link{MDS-class}}, 53 | \code{\link{NNMF-class}}, 54 | \code{\link{PCA-class}}, 55 | \code{\link{PCA_L1-class}}, 56 | \code{\link{UMAP-class}}, 57 | \code{\link{dimRedMethodList}()}, 58 | \code{\link{kPCA-class}}, 59 | \code{\link{nMDS-class}}, 60 | \code{\link{tSNE-class}} 61 | } 62 | \concept{dimensionality reduction methods} 63 | -------------------------------------------------------------------------------- /man/dimRedMethodList.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dimRedMethod-class.R 3 | \name{dimRedMethodList} 4 | \alias{dimRedMethodList} 5 | \title{dimRedMethodList} 6 | \usage{ 7 | dimRedMethodList(filter = FALSE) 8 | } 9 | \arguments{ 10 | \item{filter}{filter methods by methods that have their dependencies installed} 11 | } 12 | \value{ 13 | a character vector with the names of classes that inherit 14 | from \code{dimRedMethod}. 15 | } 16 | \description{ 17 | Get the names of all methods for dimensionality reduction. 18 | } 19 | \details{ 20 | Returns the name of all classes that inherit from 21 | \code{\link{dimRedMethod-class}} to use with \code{\link{embed}}. 22 | } 23 | \examples{ 24 | dimRedMethodList() 25 | 26 | } 27 | \seealso{ 28 | Other dimensionality reduction methods: 29 | \code{\link{DRR-class}}, 30 | \code{\link{DiffusionMaps-class}}, 31 | \code{\link{DrL-class}}, 32 | \code{\link{FastICA-class}}, 33 | \code{\link{FruchtermanReingold-class}}, 34 | \code{\link{HLLE-class}}, 35 | \code{\link{Isomap-class}}, 36 | \code{\link{KamadaKawai-class}}, 37 | \code{\link{MDS-class}}, 38 | \code{\link{NNMF-class}}, 39 | \code{\link{PCA-class}}, 40 | \code{\link{PCA_L1-class}}, 41 | \code{\link{UMAP-class}}, 42 | \code{\link{dimRedMethod-class}}, 43 | \code{\link{kPCA-class}}, 44 | \code{\link{nMDS-class}}, 45 | \code{\link{tSNE-class}} 46 | } 47 | \concept{dimensionality reduction methods} 48 | -------------------------------------------------------------------------------- /man/dimRedResult-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dimRedResult-class.R 3 | \docType{class} 4 | \name{dimRedResult-class} 5 | \alias{dimRedResult-class} 6 | \alias{dimRedResult} 7 | \alias{predict,dimRedResult-method} 8 | \alias{inverse,dimRedResult-method} 9 | \alias{inverse} 10 | \alias{as.data.frame,dimRedResult-method} 11 | \alias{getPars,dimRedResult-method} 12 | \alias{getNDim,dimRedResult-method} 13 | \alias{print,dimRedResult-method} 14 | \alias{getOrgData,dimRedResult-method} 15 | \alias{getDimRedData,dimRedResult-method} 16 | \alias{ndims,dimRedResult-method} 17 | \alias{getOtherData,dimRedResult-method} 18 | \title{Class "dimRedResult"} 19 | \usage{ 20 | \S4method{predict}{dimRedResult}(object, xnew) 21 | 22 | \S4method{inverse}{dimRedResult}(object, ynew) 23 | 24 | \S4method{as.data.frame}{dimRedResult}( 25 | x, 26 | org.data.prefix = "org.", 27 | meta.prefix = "meta.", 28 | data.prefix = "" 29 | ) 30 | 31 | \S4method{getPars}{dimRedResult}(object) 32 | 33 | \S4method{getNDim}{dimRedResult}(object) 34 | 35 | \S4method{print}{dimRedResult}(x) 36 | 37 | \S4method{getOrgData}{dimRedResult}(object) 38 | 39 | \S4method{getDimRedData}{dimRedResult}(object) 40 | 41 | \S4method{ndims}{dimRedResult}(object) 42 | 43 | \S4method{getOtherData}{dimRedResult}(object) 44 | } 45 | \arguments{ 46 | \item{object}{Of class \code{dimRedResult}} 47 | 48 | \item{xnew}{new data, of type \code{\link{dimRedData}}} 49 | 50 | \item{ynew}{embedded data, of type \code{\link{dimRedData}}} 51 | 52 | \item{x}{Of class \code{dimRedResult}} 53 | 54 | \item{org.data.prefix}{Prefix for the columns of the org.data slot.} 55 | 56 | \item{meta.prefix}{Prefix for the columns of \code{x@data@meta}.} 57 | 58 | \item{data.prefix}{Prefix for the columns of \code{x@data@data}.} 59 | } 60 | \description{ 61 | A class to hold the results of of a dimensionality reduction. 62 | } 63 | \section{Methods (by generic)}{ 64 | \itemize{ 65 | \item \code{predict(dimRedResult)}: apply a trained method to new data, does not work 66 | with all methods, will give an error if there is no \code{apply}. 67 | In some cases the apply function may only be an approximation. 68 | 69 | \item \code{inverse(dimRedResult)}: inverse transformation of embedded data, does not 70 | work with all methods, will give an error if there is no \code{inverse}. 71 | In some cases the apply function may only be an approximation. 72 | 73 | \item \code{as.data.frame(dimRedResult)}: convert to \code{data.frame} 74 | 75 | \item \code{getPars(dimRedResult)}: Get the parameters with which the method 76 | was called. 77 | 78 | \item \code{getNDim(dimRedResult)}: Get the number of embedding dimensions. 79 | 80 | \item \code{print(dimRedResult)}: Method for printing. 81 | 82 | \item \code{getOrgData(dimRedResult)}: Get the original data and meta.data 83 | 84 | \item \code{getDimRedData(dimRedResult)}: Get the embedded data 85 | 86 | \item \code{ndims(dimRedResult)}: Extract the number of embedding dimensions. 87 | 88 | \item \code{getOtherData(dimRedResult)}: Get other data produced by the method 89 | 90 | }} 91 | \section{Slots}{ 92 | 93 | \describe{ 94 | \item{\code{data}}{Output data of class dimRedData.} 95 | 96 | \item{\code{org.data}}{original data, a matrix.} 97 | 98 | \item{\code{apply}}{a function to apply the method to out-of-sampledata, 99 | may not exist.} 100 | 101 | \item{\code{inverse}}{a function to calculate the original coordinates from 102 | reduced space, may not exist.} 103 | 104 | \item{\code{has.org.data}}{logical, if the original data is included in the object.} 105 | 106 | \item{\code{has.apply}}{logical, if a forward method is exists.} 107 | 108 | \item{\code{has.inverse}}{logical if an inverse method exists.} 109 | 110 | \item{\code{method}}{saves the method used.} 111 | 112 | \item{\code{pars}}{saves the parameters used.} 113 | 114 | \item{\code{other.data}}{other data produced by the method, e.g. a distance matrix.} 115 | }} 116 | 117 | \examples{ 118 | ## Create object by embedding data 119 | iris.pca <- embed(loadDataSet("Iris"), "PCA") 120 | 121 | ## Convert the result to a data.frame 122 | head(as(iris.pca, "data.frame")) 123 | head(as.data.frame(iris.pca)) 124 | 125 | ## There are no nameclashes to avoid here: 126 | head(as.data.frame(iris.pca, 127 | org.data.prefix = "", 128 | meta.prefix = "", 129 | data.prefix = "")) 130 | 131 | ## Print it more or less nicely: 132 | print(iris.pca) 133 | 134 | ## Get the embedded data as a dimRedData object: 135 | getDimRedData(iris.pca) 136 | 137 | ## Get the original data including meta information: 138 | getOrgData(iris.pca) 139 | 140 | ## Get the number of variables: 141 | ndims(iris.pca) 142 | 143 | } 144 | \concept{dimRedResult} 145 | -------------------------------------------------------------------------------- /man/distance_correlation-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{distance_correlation,dimRedResult-method} 4 | \alias{distance_correlation,dimRedResult-method} 5 | \alias{distance_correlation} 6 | \title{Method distance_correlation} 7 | \usage{ 8 | \S4method{distance_correlation}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculate the distance correlation between the distance matrices in 15 | high and low dimensioal space. 16 | } 17 | \seealso{ 18 | Other Quality scores for dimensionality reduction: 19 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 20 | \code{\link{LCMC,dimRedResult-method}}, 21 | \code{\link{Q_NX,dimRedResult-method}}, 22 | \code{\link{Q_global,dimRedResult-method}}, 23 | \code{\link{Q_local,dimRedResult-method}}, 24 | \code{\link{R_NX,dimRedResult-method}}, 25 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 26 | \code{\link{mean_R_NX,dimRedResult-method}}, 27 | \code{\link{plot_R_NX}()}, 28 | \code{\link{quality,dimRedResult-method}}, 29 | \code{\link{reconstruction_error,dimRedResult-method}}, 30 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 31 | \code{\link{total_correlation,dimRedResult-method}} 32 | } 33 | \concept{Quality scores for dimensionality reduction} 34 | -------------------------------------------------------------------------------- /man/embed.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/embed.R 3 | \name{embed} 4 | \alias{embed} 5 | \alias{embed,formula-method} 6 | \alias{embed,ANY-method} 7 | \alias{embed,dimRedData-method} 8 | \title{dispatches the different methods for dimensionality reduction} 9 | \usage{ 10 | embed(.data, ...) 11 | 12 | \S4method{embed}{formula}( 13 | .formula, 14 | .data, 15 | .method = dimRedMethodList(), 16 | .mute = character(0), 17 | .keep.org.data = TRUE, 18 | ... 19 | ) 20 | 21 | \S4method{embed}{ANY}( 22 | .data, 23 | .method = dimRedMethodList(), 24 | .mute = character(0), 25 | .keep.org.data = TRUE, 26 | ... 27 | ) 28 | 29 | \S4method{embed}{dimRedData}( 30 | .data, 31 | .method = dimRedMethodList(), 32 | .mute = character(0), 33 | .keep.org.data = TRUE, 34 | ... 35 | ) 36 | } 37 | \arguments{ 38 | \item{.data}{object of class \code{\link{dimRedData}}, will be converted to 39 | be of class \code{\link{dimRedData}} if necessary; see examples for 40 | details.} 41 | 42 | \item{...}{the parameters, internally passed as a list to the dimensionality 43 | reduction method as \code{pars = list(...)}} 44 | 45 | \item{.formula}{a formula, see \code{\link{as.dimRedData}}.} 46 | 47 | \item{.method}{character vector naming one of the dimensionality reduction 48 | techniques.} 49 | 50 | \item{.mute}{a character vector containing the elements you want to mute 51 | (\code{c("message", "output")}), defaults to \code{character(0)}.} 52 | 53 | \item{.keep.org.data}{\code{TRUE}/\code{FALSE} keep the original data.} 54 | } 55 | \value{ 56 | an object of class \code{\link{dimRedResult}} 57 | } 58 | \description{ 59 | wraps around all dimensionality reduction functions. 60 | } 61 | \details{ 62 | Method must be one of \code{\link{dimRedMethodList}()}, partial matching 63 | is performed. All parameters start with a dot, to avoid clashes 64 | with partial argument matching (see the R manual section 4.3.2), if 65 | there should ever occur any clashes in the arguments, call the 66 | function with all arguments named, e.g. \code{embed(.data = dat, 67 | .method = "mymethod", .d = "some parameter")}. 68 | } 69 | \section{Methods (by class)}{ 70 | \itemize{ 71 | \item \code{embed(formula)}: embed a data.frame using a formula. 72 | 73 | \item \code{embed(ANY)}: Embed anything as long as it can be coerced to 74 | \code{\link{dimRedData}}. 75 | 76 | \item \code{embed(dimRedData)}: Embed a dimRedData object 77 | 78 | }} 79 | \examples{ 80 | ## embed a data.frame using a formula: 81 | as.data.frame( 82 | embed(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 83 | iris, "PCA") 84 | ) 85 | 86 | ## embed a data.frame and return a data.frame 87 | as.data.frame(embed(iris[, 1:4], "PCA")) 88 | 89 | ## embed a matrix and return a data.frame 90 | as.data.frame(embed(as.matrix(iris[, 1:4]), "PCA")) 91 | 92 | \dontrun{ 93 | ## embed dimRedData objects 94 | embed_methods <- dimRedMethodList() 95 | quality_methods <- dimRedQualityList() 96 | dataset <- loadDataSet("Iris") 97 | 98 | quality_results <- matrix(NA, length(embed_methods), length(quality_methods), 99 | dimnames = list(embed_methods, quality_methods)) 100 | embedded_data <- list() 101 | 102 | for (e in embed_methods) { 103 | message("embedding: ", e) 104 | embedded_data[[e]] <- embed(dataset, e, .mute = c("message", "output")) 105 | for (q in quality_methods) { 106 | message(" quality: ", q) 107 | quality_results[e, q] <- tryCatch( 108 | quality(embedded_data[[e]], q), 109 | error = function(e) NA 110 | ) 111 | } 112 | } 113 | 114 | print(quality_results) 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /man/getData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getData} 4 | \alias{getData} 5 | \title{Method getData} 6 | \usage{ 7 | getData(object) 8 | } 9 | \arguments{ 10 | \item{object}{The object to be converted.} 11 | } 12 | \description{ 13 | Extracts the data slot. 14 | } 15 | -------------------------------------------------------------------------------- /man/getDimRedData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getDimRedData} 4 | \alias{getDimRedData} 5 | \title{Method getDimRedData} 6 | \usage{ 7 | getDimRedData(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to extract data from.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extract dimRedData. 16 | } 17 | -------------------------------------------------------------------------------- /man/getMeta.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getMeta} 4 | \alias{getMeta} 5 | \title{Method getMeta} 6 | \usage{ 7 | getMeta(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to be converted.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extracts the meta slot. 16 | } 17 | -------------------------------------------------------------------------------- /man/getNDim.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getNDim} 4 | \alias{getNDim} 5 | \title{Method getNDim} 6 | \usage{ 7 | getNDim(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to get the dimensions from.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extract the number of embedding dimensions. 16 | } 17 | -------------------------------------------------------------------------------- /man/getOrgData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getOrgData} 4 | \alias{getOrgData} 5 | \title{Method getOrgData} 6 | \usage{ 7 | getOrgData(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to extract data from.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extract the Original data. 16 | } 17 | -------------------------------------------------------------------------------- /man/getOtherData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getOtherData} 4 | \alias{getOtherData} 5 | \title{Method getOtherData} 6 | \usage{ 7 | getOtherData(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to extract data from.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extract other data produced by a dimRedMethod 16 | } 17 | -------------------------------------------------------------------------------- /man/getPars.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{getPars} 4 | \alias{getPars} 5 | \title{Method getPars} 6 | \usage{ 7 | getPars(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The object to be converted.} 11 | 12 | \item{...}{other arguments.} 13 | } 14 | \description{ 15 | Extracts the pars slot. 16 | } 17 | -------------------------------------------------------------------------------- /man/getRotationMatrix.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/get_info.R 3 | \name{getRotationMatrix} 4 | \alias{getRotationMatrix} 5 | \title{getRotationMatrix} 6 | \usage{ 7 | getRotationMatrix(x) 8 | } 9 | \arguments{ 10 | \item{x}{of type \code{\link{dimRedResult}}} 11 | } 12 | \value{ 13 | a matrix 14 | } 15 | \description{ 16 | Extract the rotation matrix from \code{\link{dimRedResult}} objects derived from PCA and FastICA 17 | } 18 | \details{ 19 | The data has to be pre-processed the same way as the method does, e.g. 20 | centering and/or scaling. 21 | } 22 | \examples{ 23 | dat <- loadDataSet("Iris") 24 | 25 | pca <- embed(dat, "PCA") 26 | rot_pca <- getRotationMatrix(pca) 27 | scale(getData(dat), TRUE, FALSE) \%*\% rot_pca - getData(getDimRedData(pca)) 28 | 29 | 30 | if(requireNamespace("fastICA", quietly = TRUE)) { 31 | ica <- embed(dat, "FastICA") 32 | rot_ica <- getRotationMatrix(ica) 33 | scale(getData(dat), TRUE, FALSE) \%*\% rot_ica - getData(getDimRedData(ica)) 34 | } 35 | 36 | 37 | } 38 | \concept{convenience functions} 39 | -------------------------------------------------------------------------------- /man/installSuggests.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{installSuggests} 4 | \alias{installSuggests} 5 | \title{getSuggests} 6 | \usage{ 7 | installSuggests(...) 8 | } 9 | \arguments{ 10 | \item{...}{additional options passed to install.packages.} 11 | } 12 | \description{ 13 | Install packages wich are suggested by dimRed. 14 | } 15 | \details{ 16 | By default dimRed will not install all the dependencies, because 17 | there are quite a lot and in case some of them are not available 18 | for your platform you will not be able to install dimRed without 19 | problems. 20 | 21 | To solve this I provide a function which automatically installes 22 | all the suggested packages. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | installSuggests() 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /man/kPCA-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kpca.R 3 | \docType{class} 4 | \name{kPCA-class} 5 | \alias{kPCA-class} 6 | \alias{kPCA} 7 | \title{Kernel PCA} 8 | \description{ 9 | An S4 Class implementing Kernel PCA 10 | } 11 | \details{ 12 | Kernel PCA is a nonlinear extension of PCA using kernel methods. 13 | } 14 | \section{Slots}{ 15 | 16 | \describe{ 17 | \item{\code{fun}}{A function that does the embedding and returns a 18 | dimRedResult object.} 19 | 20 | \item{\code{stdpars}}{The standard parameters for the function.} 21 | }} 22 | 23 | \section{General usage}{ 24 | 25 | Dimensionality reduction methods are S4 Classes that either be used 26 | directly, in which case they have to be initialized and a full 27 | list with parameters has to be handed to the \code{@fun()} 28 | slot, or the method name be passed to the embed function and 29 | parameters can be given to the \code{...}, in which case 30 | missing parameters will be replaced by the ones in the 31 | \code{@stdpars}. 32 | } 33 | 34 | \section{Parameters}{ 35 | 36 | Kernel PCA can take the following parameters: 37 | \describe{ 38 | \item{ndim}{the number of output dimensions, defaults to 2} 39 | \item{kernel}{The kernel function, either as a function or a 40 | character vector with the name of the kernel. Defaults to 41 | \code{"rbfdot"}} 42 | \item{kpar}{A list with the parameters for the kernel function, 43 | defaults to \code{list(sigma = 0.1)}} 44 | } 45 | 46 | The most comprehensive collection of kernel functions can be found in 47 | \code{\link[kernlab]{kpca}}. In case the function does not take any 48 | parameters \code{kpar} has to be an empty list. 49 | } 50 | 51 | \section{Implementation}{ 52 | 53 | 54 | Wraps around \code{\link[kernlab]{kpca}}, but provides additionally 55 | forward and backward projections. 56 | } 57 | 58 | \examples{ 59 | \dontrun{ 60 | if(requireNamespace("kernlab", quietly = TRUE)) { 61 | 62 | dat <- loadDataSet("3D S Curve") 63 | emb <- embed(dat, "kPCA") 64 | plot(emb, type = "2vars") 65 | } 66 | 67 | } 68 | } 69 | \references{ 70 | Sch\"olkopf, B., Smola, A., M\"uller, K.-R., 1998. Nonlinear Component Analysis 71 | as a Kernel Eigenvalue Problem. Neural Computation 10, 1299-1319. 72 | https://doi.org/10.1162/089976698300017467 73 | } 74 | \seealso{ 75 | Other dimensionality reduction methods: 76 | \code{\link{DRR-class}}, 77 | \code{\link{DiffusionMaps-class}}, 78 | \code{\link{DrL-class}}, 79 | \code{\link{FastICA-class}}, 80 | \code{\link{FruchtermanReingold-class}}, 81 | \code{\link{HLLE-class}}, 82 | \code{\link{Isomap-class}}, 83 | \code{\link{KamadaKawai-class}}, 84 | \code{\link{MDS-class}}, 85 | \code{\link{NNMF-class}}, 86 | \code{\link{PCA-class}}, 87 | \code{\link{PCA_L1-class}}, 88 | \code{\link{UMAP-class}}, 89 | \code{\link{dimRedMethod-class}}, 90 | \code{\link{dimRedMethodList}()}, 91 | \code{\link{nMDS-class}}, 92 | \code{\link{tSNE-class}} 93 | } 94 | \concept{dimensionality reduction methods} 95 | -------------------------------------------------------------------------------- /man/makeKNNgraph.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{makeKNNgraph} 4 | \alias{makeKNNgraph} 5 | \title{makeKNNgraph} 6 | \usage{ 7 | makeKNNgraph(x, k, eps = 0, diag = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{data, a matrix, observations in rows, dimensions in 11 | columns} 12 | 13 | \item{k}{the number of nearest neighbors.} 14 | 15 | \item{eps}{number, if \code{eps > 0} the KNN search is approximate, 16 | see \code{\link[RANN]{nn2}}} 17 | 18 | \item{diag}{logical, if \code{TRUE} every edge of the returned 19 | graph will have an edge with weight \code{0} to itself.} 20 | } 21 | \value{ 22 | an object of type \code{\link[igraph]{igraph}} with edge 23 | weight being the distances. 24 | } 25 | \description{ 26 | Create a K-nearest neighbor graph from data x. Uses 27 | \code{\link[RANN]{nn2}} as a fast way to find the neares neighbors. 28 | } 29 | -------------------------------------------------------------------------------- /man/maximize_correlation-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rotate.R 3 | \name{maximize_correlation,dimRedResult-method} 4 | \alias{maximize_correlation,dimRedResult-method} 5 | \alias{maximize_correlation} 6 | \title{Maximize Correlation with the Axes} 7 | \usage{ 8 | \S4method{maximize_correlation}{dimRedResult}( 9 | object, 10 | naxes = ncol(object@data@data), 11 | cor_method = "pearson" 12 | ) 13 | } 14 | \arguments{ 15 | \item{object}{A dimRedResult object} 16 | 17 | \item{naxes}{the number of axes to optimize for.} 18 | 19 | \item{cor_method}{which correlation method to use} 20 | } 21 | \description{ 22 | Rotates the data in such a way that the correlation with the first 23 | \code{naxes} axes is maximized. 24 | } 25 | \details{ 26 | Methods that do not use eigenvector decomposition, like t-SNE often 27 | do not align the data with axes according to the correlation of 28 | variables with the data. \code{maximize_correlation} uses the 29 | \code{\link[optimx]{optimx}} package to rotate the data in such a 30 | way that the original variables have maximum correlation with the 31 | embedding axes. 32 | } 33 | -------------------------------------------------------------------------------- /man/mean_R_NX-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{mean_R_NX,dimRedResult-method} 4 | \alias{mean_R_NX,dimRedResult-method} 5 | \alias{mean_R_NX} 6 | \title{Method mean_R_NX} 7 | \usage{ 8 | \S4method{mean_R_NX}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculate the mean_R_NX score to assess the quality of a dimensionality reduction. 15 | } 16 | \seealso{ 17 | Other Quality scores for dimensionality reduction: 18 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 19 | \code{\link{LCMC,dimRedResult-method}}, 20 | \code{\link{Q_NX,dimRedResult-method}}, 21 | \code{\link{Q_global,dimRedResult-method}}, 22 | \code{\link{Q_local,dimRedResult-method}}, 23 | \code{\link{R_NX,dimRedResult-method}}, 24 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 25 | \code{\link{distance_correlation,dimRedResult-method}}, 26 | \code{\link{plot_R_NX}()}, 27 | \code{\link{quality,dimRedResult-method}}, 28 | \code{\link{reconstruction_error,dimRedResult-method}}, 29 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 30 | \code{\link{total_correlation,dimRedResult-method}} 31 | } 32 | \concept{Quality scores for dimensionality reduction} 33 | -------------------------------------------------------------------------------- /man/mixColorRamps.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mixColorSpaces.R 3 | \name{mixColorRamps} 4 | \alias{mixColorRamps} 5 | \alias{mixColor1Ramps} 6 | \alias{mixColor2Ramps} 7 | \alias{mixColor3Ramps} 8 | \title{Mixing color ramps} 9 | \usage{ 10 | mixColorRamps(vars, ramps) 11 | 12 | mixColor1Ramps(vars, ramps = colorRamp(c("blue", "black", "red"))) 13 | 14 | mixColor2Ramps( 15 | vars, 16 | ramps = list(colorRamp(c("blue", "green")), colorRamp(c("blue", "red"))) 17 | ) 18 | 19 | mixColor3Ramps( 20 | vars, 21 | ramps = list(colorRamp(c("#001A00", "#00E600")), colorRamp(c("#00001A", "#0000E6")), 22 | colorRamp(c("#1A0000", "#E60000"))) 23 | ) 24 | } 25 | \arguments{ 26 | \item{vars}{a list of variables} 27 | 28 | \item{ramps}{a list of color ramps, one for each variable.} 29 | } 30 | \description{ 31 | mix different color ramps 32 | } 33 | \details{ 34 | automatically create colors to represent a varying number of 35 | dimensions. 36 | } 37 | \examples{ 38 | cols <- expand.grid(x = seq(0, 1, length.out = 10), 39 | y = seq(0, 1, length.out = 10), 40 | z = seq(0, 1, length.out = 10)) 41 | mixed <- mixColor3Ramps(cols) 42 | 43 | \dontrun{ 44 | if(requireNamespace("rgl", quietly = TRUE)) { 45 | rgl::plot3d(cols$x, cols$y, cols$z, col = mixed, pch = 15) 46 | } 47 | 48 | cols <- expand.grid(x = seq(0, 1, length.out = 10), 49 | y = seq(0, 1, length.out = 10)) 50 | mixed <- mixColor2Ramps(cols) 51 | 52 | if(requireNamespace("graphics", quietly = TRUE)) { 53 | plot(cols$x, cols$y, col = mixed, pch = 15) 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /man/nMDS-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/nmds.R 3 | \docType{class} 4 | \name{nMDS-class} 5 | \alias{nMDS-class} 6 | \alias{nMDS} 7 | \title{Non-Metric Dimensional Scaling} 8 | \description{ 9 | An S4 Class implementing Non-Metric Dimensional Scaling. 10 | } 11 | \details{ 12 | A non-linear extension of MDS using monotonic regression 13 | } 14 | \section{Slots}{ 15 | 16 | \describe{ 17 | \item{\code{fun}}{A function that does the embedding and returns a 18 | dimRedResult object.} 19 | 20 | \item{\code{stdpars}}{The standard parameters for the function.} 21 | }} 22 | 23 | \section{General usage}{ 24 | 25 | Dimensionality reduction methods are S4 Classes that either be used 26 | directly, in which case they have to be initialized and a full 27 | list with parameters has to be handed to the \code{@fun()} 28 | slot, or the method name be passed to the embed function and 29 | parameters can be given to the \code{...}, in which case 30 | missing parameters will be replaced by the ones in the 31 | \code{@stdpars}. 32 | } 33 | 34 | \section{Parameters}{ 35 | 36 | nMDS can take the following parameters: 37 | \describe{ 38 | \item{d}{A distance function.} 39 | \item{ndim}{The number of embedding dimensions.} 40 | } 41 | } 42 | 43 | \section{Implementation}{ 44 | 45 | Wraps around the 46 | \code{\link[vegan]{monoMDS}}. For parameters that are not 47 | available here, the standard configuration is used. 48 | } 49 | 50 | \examples{ 51 | if(requireNamespace("vegan", quietly = TRUE)) { 52 | 53 | dat <- loadDataSet("3D S Curve", n = 300) 54 | emb <- embed(dat, "nMDS") 55 | plot(emb, type = "2vars") 56 | 57 | } 58 | } 59 | \references{ 60 | Kruskal, J.B., 1964. Nonmetric multidimensional scaling: A numerical method. 61 | Psychometrika 29, 115-129. https://doi.org/10.1007/BF02289694 62 | } 63 | \seealso{ 64 | Other dimensionality reduction methods: 65 | \code{\link{DRR-class}}, 66 | \code{\link{DiffusionMaps-class}}, 67 | \code{\link{DrL-class}}, 68 | \code{\link{FastICA-class}}, 69 | \code{\link{FruchtermanReingold-class}}, 70 | \code{\link{HLLE-class}}, 71 | \code{\link{Isomap-class}}, 72 | \code{\link{KamadaKawai-class}}, 73 | \code{\link{MDS-class}}, 74 | \code{\link{NNMF-class}}, 75 | \code{\link{PCA-class}}, 76 | \code{\link{PCA_L1-class}}, 77 | \code{\link{UMAP-class}}, 78 | \code{\link{dimRedMethod-class}}, 79 | \code{\link{dimRedMethodList}()}, 80 | \code{\link{kPCA-class}}, 81 | \code{\link{tSNE-class}} 82 | } 83 | \concept{dimensionality reduction methods} 84 | -------------------------------------------------------------------------------- /man/ndims.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{ndims} 4 | \alias{ndims} 5 | \title{Method ndims} 6 | \usage{ 7 | ndims(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{To extract the number of dimensions from.} 11 | 12 | \item{...}{Arguments for further methods} 13 | } 14 | \description{ 15 | Extract the number of dimensions. 16 | } 17 | -------------------------------------------------------------------------------- /man/plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot.R 3 | \name{plot} 4 | \alias{plot} 5 | \alias{plot.dimRed} 6 | \alias{plot,dimRedData,ANY-method} 7 | \alias{plot.dimRedData} 8 | \alias{plot,dimRedResult,ANY-method} 9 | \alias{plot.dimRedResult} 10 | \title{Plotting of dimRed* objects} 11 | \usage{ 12 | plot(x, y, ...) 13 | 14 | \S4method{plot}{dimRedData,ANY}( 15 | x, 16 | type = "pairs", 17 | vars = seq_len(ncol(x@data)), 18 | col = seq_len(min(3, ncol(x@meta))), 19 | ... 20 | ) 21 | 22 | \S4method{plot}{dimRedResult,ANY}( 23 | x, 24 | type = "pairs", 25 | vars = seq_len(ncol(x@data@data)), 26 | col = seq_len(min(3, ncol(x@data@meta))), 27 | ... 28 | ) 29 | } 30 | \arguments{ 31 | \item{x}{dimRedResult/dimRedData class, e.g. output of 32 | embedded/loadDataSet} 33 | 34 | \item{y}{Ignored} 35 | 36 | \item{...}{handed over to the underlying plotting function.} 37 | 38 | \item{type}{plot type, one of \code{c("pairs", "parpl", "2vars", 39 | "3vars", "3varsrgl")}} 40 | 41 | \item{vars}{the axes of the embedding to use for plotting} 42 | 43 | \item{col}{the columns of the meta slot to use for coloring, can be 44 | referenced as the column names or number of x@data} 45 | } 46 | \description{ 47 | Plots a object of class dimRedResult and dimRedData. For the 48 | documentation of the plotting function in base see here: 49 | \code{\link{plot.default}}. 50 | } 51 | \details{ 52 | Plotting functions for the classes usind in \code{dimRed}. they are 53 | intended to give a quick overview over the results, so they are 54 | somewhat inflexible, e.g. it is hard to modify color scales or 55 | plotting parameters. 56 | 57 | If you require more control over plotting, it is better to convert 58 | the object to a \code{data.frame} first and use the standard 59 | functions for plotting. 60 | } 61 | \section{Methods (by class)}{ 62 | \itemize{ 63 | \item \code{plot(x = dimRedData, y = ANY)}: Ploting of dimRedData objects 64 | 65 | \item \code{plot(x = dimRedResult, y = ANY)}: Ploting of dimRedResult objects. 66 | 67 | }} 68 | \examples{ 69 | scurve = loadDataSet("3D S Curve") 70 | if(requireNamespace("graphics", quietly = TRUE)) 71 | plot(scurve, type = "pairs", main = "pairs plot of S curve") 72 | if(requireNamespace("MASS", quietly = TRUE)) 73 | plot(scurve, type = "parpl") 74 | if(requireNamespace("graphics", quietly = TRUE)) 75 | plot(scurve, type = "2vars", vars = c("y", "z")) 76 | if(requireNamespace("scatterplot3d", quietly = TRUE)) 77 | plot(scurve, type = "3vars") 78 | if(requireNamespace("rgl", quietly = TRUE)) 79 | plot(scurve, type = "3varsrgl") 80 | 81 | } 82 | -------------------------------------------------------------------------------- /man/plot_R_NX.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot.R 3 | \name{plot_R_NX} 4 | \alias{plot_R_NX} 5 | \title{plot_R_NX} 6 | \usage{ 7 | plot_R_NX(x, ndim = NA, weight = "inv") 8 | } 9 | \arguments{ 10 | \item{x}{a list of \code{\link{dimRedResult}} objects. The names of the list 11 | will appear in the legend with the AUC_lnK value.} 12 | 13 | \item{ndim}{the number of dimensions, if \code{NA} the original number of 14 | embedding dimensions is used, can be a vector giving the embedding 15 | dimensionality for each single list element of \code{x}.} 16 | 17 | \item{weight}{the weight function used for K when calculating the AUC, one of 18 | \code{c("inv", "log", "log10")}} 19 | } 20 | \value{ 21 | A ggplot object, the design can be changed by appending 22 | \code{theme(...)} 23 | } 24 | \description{ 25 | Plot the R_NX curve for different embeddings. Takes a list of 26 | \code{\link{dimRedResult}} objects as input. 27 | Also the Area under the curve values are computed for a weighted K 28 | (see \link{AUC_lnK_R_NX} for details) and appear in the legend. 29 | } 30 | \examples{ 31 | if(requireNamespace(c("RSpectra", "igraph", "RANN", "ggplot", "tidyr", "scales"), quietly = TRUE)) { 32 | ## define which methods to apply 33 | embed_methods <- c("Isomap", "PCA") 34 | ## load test data set 35 | data_set <- loadDataSet("3D S Curve", n = 200) 36 | ## apply dimensionality reduction 37 | data_emb <- lapply(embed_methods, function(x) embed(data_set, x)) 38 | names(data_emb) <- embed_methods 39 | ## plot the R_NX curves: 40 | plot_R_NX(data_emb) + 41 | ggplot2::theme(legend.title = ggplot2::element_blank(), 42 | legend.position = c(0.5, 0.1), 43 | legend.justification = c(0.5, 0.1)) 44 | } 45 | } 46 | \seealso{ 47 | Other Quality scores for dimensionality reduction: 48 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 49 | \code{\link{LCMC,dimRedResult-method}}, 50 | \code{\link{Q_NX,dimRedResult-method}}, 51 | \code{\link{Q_global,dimRedResult-method}}, 52 | \code{\link{Q_local,dimRedResult-method}}, 53 | \code{\link{R_NX,dimRedResult-method}}, 54 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 55 | \code{\link{distance_correlation,dimRedResult-method}}, 56 | \code{\link{mean_R_NX,dimRedResult-method}}, 57 | \code{\link{quality,dimRedResult-method}}, 58 | \code{\link{reconstruction_error,dimRedResult-method}}, 59 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 60 | \code{\link{total_correlation,dimRedResult-method}} 61 | } 62 | \concept{Quality scores for dimensionality reduction} 63 | -------------------------------------------------------------------------------- /man/print.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misc.R 3 | \name{print} 4 | \alias{print} 5 | \title{Method print} 6 | \usage{ 7 | print(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{The object to be printed.} 11 | 12 | \item{...}{Other arguments for printing.} 13 | } 14 | \description{ 15 | Imports the print method into the package namespace. 16 | } 17 | -------------------------------------------------------------------------------- /man/quality.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{quality,dimRedResult-method} 4 | \alias{quality,dimRedResult-method} 5 | \alias{quality} 6 | \alias{quality.dimRedResult} 7 | \alias{dimRedQualityList} 8 | \title{Quality Criteria for dimensionality reduction.} 9 | \usage{ 10 | \S4method{quality}{dimRedResult}(.data, .method = dimRedQualityList(), .mute = character(0), ...) 11 | 12 | dimRedQualityList(filter = FALSE) 13 | } 14 | \arguments{ 15 | \item{.data}{object of class \code{dimRedResult}} 16 | 17 | \item{.method}{character vector naming one of the methods} 18 | 19 | \item{.mute}{what output from the embedding method should be muted.} 20 | 21 | \item{...}{the pameters, internally passed as a list to the 22 | quality method as \code{pars = list(...)}} 23 | 24 | \item{filter}{filter methods by installed packages} 25 | } 26 | \value{ 27 | a number 28 | } 29 | \description{ 30 | A collection of functions to compute quality measures on 31 | \code{\link{dimRedResult}} objects. 32 | } 33 | \section{Methods (by class)}{ 34 | \itemize{ 35 | \item \code{quality(dimRedResult)}: Calculate a quality index from a dimRedResult object. 36 | 37 | }} 38 | \section{Implemented methods}{ 39 | 40 | 41 | Method must be one of \code{"\link{Q_local}", "\link{Q_global}", 42 | "\link{mean_R_NX}", "\link{total_correlation}", 43 | "\link{cophenetic_correlation}", "\link{distance_correlation}", 44 | "\link{reconstruction_rmse}"} 45 | } 46 | 47 | \section{Rank based criteria}{ 48 | 49 | 50 | \code{Q_local}, \code{Q_global}, and \code{mean_R_NX} are 51 | quality criteria based on the Co-ranking matrix. \code{Q_local} 52 | and \code{Q_global} determine the local/global quality of the 53 | embedding, while \code{mean_R_NX} determines the quality of the 54 | overall embedding. They are parameter free and return a single 55 | number. The object must include the original data. The number 56 | returns is in the range [0, 1], higher values mean a better 57 | local/global embedding. 58 | } 59 | 60 | \section{Correlation based criteria}{ 61 | 62 | 63 | \code{total_correlation} calculates the sum of the mean squared 64 | correlations of the original axes with the axes in reduced 65 | dimensions, because some methods do not care about correlations 66 | with axes, there is an option to rotate data in reduced space to 67 | maximize this criterium. The number may be greater than one if more 68 | dimensions are summed up. 69 | 70 | \code{cophenetic_correlation} calculate the correlation between the 71 | lower triangles of distance matrices, the correlation and distance 72 | methods may be specified. The result is in range [-1, 1]. 73 | 74 | \code{distance_correlation} measures the independes of samples by 75 | calculating the correlation of distances. For details see 76 | \code{\link[energy]{dcor}}. 77 | } 78 | 79 | \section{Reconstruction error}{ 80 | 81 | 82 | \code{reconstruction_rmse} calculates the root mean squared error 83 | of the reconstrucion. \code{object} requires an inverse function. 84 | } 85 | 86 | \examples{ 87 | \dontrun{ 88 | embed_methods <- dimRedMethodList() 89 | quality_methods <- dimRedQualityList() 90 | scurve <- loadDataSet("Iris") 91 | 92 | quality_results <- matrix(NA, length(embed_methods), length(quality_methods), 93 | dimnames = list(embed_methods, quality_methods)) 94 | embedded_data <- list() 95 | 96 | for (e in embed_methods) { 97 | message("embedding: ", e) 98 | embedded_data[[e]] <- embed(scurve, e, .mute = c("message", "output")) 99 | for (q in quality_methods) { 100 | message(" quality: ", q) 101 | quality_results[e, q] <- tryCatch( 102 | quality(embedded_data[[e]], q), 103 | error = function (e) NA 104 | ) 105 | } 106 | } 107 | 108 | print(quality_results) 109 | } 110 | } 111 | \references{ 112 | Lueks, W., Mokbel, B., Biehl, M., Hammer, B., 2011. How 113 | to Evaluate Dimensionality Reduction? - Improving the 114 | Co-ranking Matrix. arXiv:1110.3917 [cs]. 115 | 116 | Szekely, G.J., Rizzo, M.L., Bakirov, N.K., 2007. Measuring and 117 | testing dependence by correlation of distances. Ann. Statist. 35, 118 | 2769-2794. doi:10.1214/009053607000000505 119 | 120 | Lee, J.A., Peluffo-Ordonez, D.H., Verleysen, M., 2015. Multi-scale 121 | similarities in stochastic neighbour embedding: Reducing 122 | dimensionality while preserving both local and global 123 | structure. Neurocomputing, 169, 124 | 246-261. doi:10.1016/j.neucom.2014.12.095 125 | } 126 | \seealso{ 127 | Other Quality scores for dimensionality reduction: 128 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 129 | \code{\link{LCMC,dimRedResult-method}}, 130 | \code{\link{Q_NX,dimRedResult-method}}, 131 | \code{\link{Q_global,dimRedResult-method}}, 132 | \code{\link{Q_local,dimRedResult-method}}, 133 | \code{\link{R_NX,dimRedResult-method}}, 134 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 135 | \code{\link{distance_correlation,dimRedResult-method}}, 136 | \code{\link{mean_R_NX,dimRedResult-method}}, 137 | \code{\link{plot_R_NX}()}, 138 | \code{\link{reconstruction_error,dimRedResult-method}}, 139 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 140 | \code{\link{total_correlation,dimRedResult-method}} 141 | } 142 | \author{ 143 | Guido Kraemer 144 | } 145 | \concept{Quality scores for dimensionality reduction} 146 | -------------------------------------------------------------------------------- /man/reconstruction_error-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{reconstruction_error,dimRedResult-method} 4 | \alias{reconstruction_error,dimRedResult-method} 5 | \alias{reconstruction_error} 6 | \title{Method reconstruction_error} 7 | \usage{ 8 | \S4method{reconstruction_error}{dimRedResult}(object, n = seq_len(ndims(object)), error_fun = "rmse") 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | 13 | \item{n}{a positive integer or vector of integers \code{<= ndims(object)}} 14 | 15 | \item{error_fun}{a function or string indicating an error function, if 16 | indication a function it must take to matrices of the same size and return 17 | a scalar.} 18 | } 19 | \value{ 20 | a vector of number with the same length as \code{n} with the 21 | } 22 | \description{ 23 | Calculate the error using only the first \code{n} dimensions of the embedded 24 | data. \code{error_fun} can either be one of \code{c("rmse", "mae")} to 25 | calculate the root mean square error or the mean absolute error respectively, 26 | or a function that takes to equally sized vectors as input and returns a 27 | single number as output. 28 | } 29 | \examples{ 30 | \dontrun{ 31 | ir <- loadDataSet("Iris") 32 | ir.drr <- embed(ir, "DRR", ndim = ndims(ir)) 33 | ir.pca <- embed(ir, "PCA", ndim = ndims(ir)) 34 | 35 | rmse <- data.frame( 36 | rmse_drr = reconstruction_error(ir.drr), 37 | rmse_pca = reconstruction_error(ir.pca) 38 | ) 39 | 40 | matplot(rmse, type = "l") 41 | plot(ir) 42 | plot(ir.drr) 43 | plot(ir.pca) 44 | } 45 | } 46 | \seealso{ 47 | Other Quality scores for dimensionality reduction: 48 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 49 | \code{\link{LCMC,dimRedResult-method}}, 50 | \code{\link{Q_NX,dimRedResult-method}}, 51 | \code{\link{Q_global,dimRedResult-method}}, 52 | \code{\link{Q_local,dimRedResult-method}}, 53 | \code{\link{R_NX,dimRedResult-method}}, 54 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 55 | \code{\link{distance_correlation,dimRedResult-method}}, 56 | \code{\link{mean_R_NX,dimRedResult-method}}, 57 | \code{\link{plot_R_NX}()}, 58 | \code{\link{quality,dimRedResult-method}}, 59 | \code{\link{reconstruction_rmse,dimRedResult-method}}, 60 | \code{\link{total_correlation,dimRedResult-method}} 61 | } 62 | \author{ 63 | Guido Kraemer 64 | } 65 | \concept{Quality scores for dimensionality reduction} 66 | -------------------------------------------------------------------------------- /man/reconstruction_rmse-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{reconstruction_rmse,dimRedResult-method} 4 | \alias{reconstruction_rmse,dimRedResult-method} 5 | \alias{reconstruction_rmse} 6 | \title{Method reconstruction_rmse} 7 | \usage{ 8 | \S4method{reconstruction_rmse}{dimRedResult}(object) 9 | } 10 | \arguments{ 11 | \item{object}{of class dimRedResult} 12 | } 13 | \description{ 14 | Calculate the reconstruction root mean squared error a dimensionality reduction, the method must have an inverse mapping. 15 | } 16 | \seealso{ 17 | Other Quality scores for dimensionality reduction: 18 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 19 | \code{\link{LCMC,dimRedResult-method}}, 20 | \code{\link{Q_NX,dimRedResult-method}}, 21 | \code{\link{Q_global,dimRedResult-method}}, 22 | \code{\link{Q_local,dimRedResult-method}}, 23 | \code{\link{R_NX,dimRedResult-method}}, 24 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 25 | \code{\link{distance_correlation,dimRedResult-method}}, 26 | \code{\link{mean_R_NX,dimRedResult-method}}, 27 | \code{\link{plot_R_NX}()}, 28 | \code{\link{quality,dimRedResult-method}}, 29 | \code{\link{reconstruction_error,dimRedResult-method}}, 30 | \code{\link{total_correlation,dimRedResult-method}} 31 | } 32 | \concept{Quality scores for dimensionality reduction} 33 | -------------------------------------------------------------------------------- /man/tSNE-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tsne.R 3 | \docType{class} 4 | \name{tSNE-class} 5 | \alias{tSNE-class} 6 | \alias{tSNE} 7 | \title{t-Distributed Stochastic Neighborhood Embedding} 8 | \description{ 9 | An S4 Class for t-SNE. 10 | } 11 | \details{ 12 | t-SNE is a method that uses Kullback-Leibler divergence between the 13 | distance matrices in high and low-dimensional space to embed the 14 | data. The method is very well suited to visualize complex 15 | structures in low dimensions. 16 | } 17 | \section{Slots}{ 18 | 19 | \describe{ 20 | \item{\code{fun}}{A function that does the embedding and returns a 21 | dimRedResult object.} 22 | 23 | \item{\code{stdpars}}{The standard parameters for the function.} 24 | }} 25 | 26 | \section{General usage}{ 27 | 28 | Dimensionality reduction methods are S4 Classes that either be used 29 | directly, in which case they have to be initialized and a full 30 | list with parameters has to be handed to the \code{@fun()} 31 | slot, or the method name be passed to the embed function and 32 | parameters can be given to the \code{...}, in which case 33 | missing parameters will be replaced by the ones in the 34 | \code{@stdpars}. 35 | } 36 | 37 | \section{Parameters}{ 38 | 39 | t-SNE can take the following parameters: 40 | \describe{ 41 | \item{d}{A distance function, defaults to euclidean distances} 42 | \item{perplexity}{The perplexity parameter, roughly equivalent to neighborhood size.} 43 | \item{theta}{Approximation for the nearest neighbour search, large values are more inaccurate.} 44 | \item{ndim}{The number of embedding dimensions.} 45 | } 46 | } 47 | 48 | \section{Implementation}{ 49 | 50 | 51 | Wraps around \code{\link[Rtsne]{Rtsne}}, which is very well 52 | documented. Setting \code{theta = 0} does a normal t-SNE, larger 53 | values for \code{theta < 1} use the Barnes-Hut algorithm which 54 | scales much nicer with data size. Larger values for perplexity take 55 | larger neighborhoods into account. 56 | } 57 | 58 | \examples{ 59 | \dontrun{ 60 | dat <- loadDataSet("3D S Curve", n = 300) 61 | emb <- embed(dat, "tSNE", perplexity = 80) 62 | plot(emb, type = "2vars") 63 | } 64 | } 65 | \references{ 66 | Maaten, L. van der, 2014. Accelerating t-SNE using Tree-Based 67 | Algorithms. Journal of Machine Learning Research 15, 3221-3245. 68 | 69 | van der Maaten, L., Hinton, G., 2008. Visualizing Data using 70 | t-SNE. J. Mach. Learn. Res. 9, 2579-2605. 71 | } 72 | \seealso{ 73 | Other dimensionality reduction methods: 74 | \code{\link{DRR-class}}, 75 | \code{\link{DiffusionMaps-class}}, 76 | \code{\link{DrL-class}}, 77 | \code{\link{FastICA-class}}, 78 | \code{\link{FruchtermanReingold-class}}, 79 | \code{\link{HLLE-class}}, 80 | \code{\link{Isomap-class}}, 81 | \code{\link{KamadaKawai-class}}, 82 | \code{\link{MDS-class}}, 83 | \code{\link{NNMF-class}}, 84 | \code{\link{PCA-class}}, 85 | \code{\link{PCA_L1-class}}, 86 | \code{\link{UMAP-class}}, 87 | \code{\link{dimRedMethod-class}}, 88 | \code{\link{dimRedMethodList}()}, 89 | \code{\link{kPCA-class}}, 90 | \code{\link{nMDS-class}} 91 | } 92 | \concept{dimensionality reduction methods} 93 | -------------------------------------------------------------------------------- /man/total_correlation-dimRedResult-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/quality.R 3 | \name{total_correlation,dimRedResult-method} 4 | \alias{total_correlation,dimRedResult-method} 5 | \alias{total_correlation} 6 | \title{Method total_correlation} 7 | \usage{ 8 | \S4method{total_correlation}{dimRedResult}( 9 | object, 10 | naxes = ndims(object), 11 | cor_method = "pearson", 12 | is.rotated = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{object}{of class dimRedResult} 17 | 18 | \item{naxes}{the number of axes to use for optimization.} 19 | 20 | \item{cor_method}{the correlation method to use.} 21 | 22 | \item{is.rotated}{if FALSE the object is rotated.} 23 | } 24 | \description{ 25 | Calculate the total correlation of the variables with the axes to 26 | assess the quality of a dimensionality reduction. 27 | } 28 | \seealso{ 29 | Other Quality scores for dimensionality reduction: 30 | \code{\link{AUC_lnK_R_NX,dimRedResult-method}}, 31 | \code{\link{LCMC,dimRedResult-method}}, 32 | \code{\link{Q_NX,dimRedResult-method}}, 33 | \code{\link{Q_global,dimRedResult-method}}, 34 | \code{\link{Q_local,dimRedResult-method}}, 35 | \code{\link{R_NX,dimRedResult-method}}, 36 | \code{\link{cophenetic_correlation,dimRedResult-method}}, 37 | \code{\link{distance_correlation,dimRedResult-method}}, 38 | \code{\link{mean_R_NX,dimRedResult-method}}, 39 | \code{\link{plot_R_NX}()}, 40 | \code{\link{quality,dimRedResult-method}}, 41 | \code{\link{reconstruction_error,dimRedResult-method}}, 42 | \code{\link{reconstruction_rmse,dimRedResult-method}} 43 | } 44 | \concept{Quality scores for dimensionality reduction} 45 | -------------------------------------------------------------------------------- /nonascii.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | perl -ne 'print "$. $_" if m/[\x80-\xFF]/' $1 5 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(dimRed) 3 | 4 | test_check("dimRed") 5 | -------------------------------------------------------------------------------- /tests/testthat/test_HLLE.R: -------------------------------------------------------------------------------- 1 | test_that("HLLE", { 2 | if(requireNamespace(dimRed:::getMethodDependencies("HLLE"), quietly = TRUE)) 3 | expect_error(embed(iris[1:4], "HLLE", ndim = 1, .mute = c("message", "output")), 4 | "ndim must be 2 or larger.") 5 | }) 6 | -------------------------------------------------------------------------------- /tests/testthat/test_PCA.R: -------------------------------------------------------------------------------- 1 | test_that("general data conversions", { 2 | irisData <- as(iris[, 1:4], "dimRedData") 3 | expect_equal(class(irisData)[1], "dimRedData") 4 | 5 | irisParsCS <- list(center = TRUE, scale. = TRUE) 6 | irisParsC <- list(center = TRUE, scale. = FALSE) 7 | irisParsS <- list(center = FALSE, scale. = TRUE) 8 | irisPars <- list(center = FALSE, scale. = FALSE) 9 | 10 | irisResCS <- do.call(function(...) embed(irisData, "PCA", ...), irisParsCS) 11 | irisResS <- do.call(function(...) embed(irisData, "PCA", ...), irisParsS) 12 | irisResC <- do.call(function(...) embed(irisData, "PCA", ...), irisParsC) 13 | irisRes <- do.call(function(...) embed(irisData, "PCA", ...), irisPars) 14 | 15 | expect_equal(2, getNDim(irisResCS)) 16 | expect_equal(2, getNDim(irisResS)) 17 | expect_equal(2, getNDim(irisResC)) 18 | expect_equal(2, getNDim(irisRes)) 19 | 20 | expect_equal(class(irisResCS)[1], "dimRedResult") 21 | expect_equal(class(irisResS)[1], "dimRedResult") 22 | expect_equal(class(irisResC)[1], "dimRedResult") 23 | expect_equal(class(irisRes)[1], "dimRedResult") 24 | 25 | expect_equal(irisResCS@apply(irisData), irisResCS@data) 26 | expect_equal(irisResS@apply(irisData), irisResS@data) 27 | expect_equal(irisResC@apply(irisData), irisResC@data) 28 | expect_equal(irisRes@apply(irisData), irisRes@data) 29 | 30 | expect(sqrt(mean( 31 | (irisResCS@inverse(irisResCS@data)@data - irisData@data) ^ 2 32 | )) < 0.3, 33 | "error too large" 34 | ) 35 | expect(sqrt(mean( 36 | (irisResS@inverse(irisResS@data)@data - irisData@data) ^ 2 37 | )) < 0.3, 38 | "error too large" 39 | ) 40 | expect(sqrt(mean( 41 | (irisResC@inverse(irisResC@data)@data - irisData@data) ^ 2 42 | )) < 0.3, 43 | "error too large" 44 | ) 45 | expect(sqrt(mean( 46 | (irisRes@inverse(irisRes@data)@data - irisData@data) ^ 2 47 | )) < 0.3, 48 | "error too large" 49 | ) 50 | 51 | scale2 <- function(x, center, scale.) scale(x, center, scale.) 52 | expect_equal( 53 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResCS), irisParsCS), 54 | getData( getDimRedData(irisResCS) ) 55 | ) 56 | expect_equal( 57 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResS), irisParsS), 58 | getData( getDimRedData(irisResS) ) 59 | ) 60 | expect_equal( 61 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResC), irisParsC), 62 | getData( getDimRedData(irisResC) ) 63 | ) 64 | expect_equal( 65 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisRes), irisPars), 66 | getData( getDimRedData(irisRes) ) 67 | ) 68 | }) 69 | -------------------------------------------------------------------------------- /tests/testthat/test_PCA_L1.R: -------------------------------------------------------------------------------- 1 | test_that("general data conversions", { 2 | skip_if_not_installed("pcaL1") 3 | irisData <- as(iris[, 1:4], "dimRedData") 4 | expect_equal(class(irisData)[1], "dimRedData") 5 | 6 | irisParsCS <- list(center = TRUE, .mute = c("message", "output"), ndim = 4, scale. = TRUE, projections = "l1", fun = "l1pca") 7 | irisParsC <- list(center = TRUE, .mute = c("message", "output"), ndim = 4, scale. = FALSE, projections = "l1", fun = "l1pca") 8 | irisParsS <- list(center = TRUE, .mute = c("message", "output"), ndim = 4, scale. = TRUE, projections = "l1", fun = "l1pcahp") 9 | irisPars <- list(center = FALSE, .mute = c("message", "output"), ndim = 4, scale. = FALSE, projections = "l1", fun = "l1pcastar") 10 | 11 | irisResCS <- do.call(function(...) embed(irisData, "PCA_L1", ...), irisParsCS) 12 | irisResS <- do.call(function(...) embed(irisData, "PCA_L1", ...), irisParsS) 13 | irisResC <- do.call(function(...) embed(irisData, "PCA_L1", ...), irisParsC) 14 | irisRes <- do.call(function(...) embed(irisData, "PCA_L1", ...), irisPars) 15 | 16 | expect_equal(4, getNDim(irisResCS)) 17 | expect_equal(4, getNDim(irisResS)) 18 | expect_equal(4, getNDim(irisResC)) 19 | expect_equal(4, getNDim(irisRes)) 20 | 21 | expect_equal(class(irisResCS)[1], "dimRedResult") 22 | expect_equal(class(irisResS)[1], "dimRedResult") 23 | expect_equal(class(irisResC)[1], "dimRedResult") 24 | expect_equal(class(irisRes)[1], "dimRedResult") 25 | 26 | expect_equal(irisResCS@apply(irisData), irisResCS@data) 27 | expect_equal(irisResS@apply(irisData), irisResS@data) 28 | expect_equal(irisResC@apply(irisData), irisResC@data) 29 | expect_equal(irisRes@apply(irisData), irisRes@data) 30 | 31 | expect(sqrt(mean( 32 | (irisResCS@inverse(irisResCS@data)@data - irisData@data) ^ 2 33 | )) < 0.3, 34 | "error too large" 35 | ) 36 | expect(sqrt(mean( 37 | (irisResS@inverse(irisResS@data)@data - irisData@data) ^ 2 38 | )) < 0.3, 39 | "error too large" 40 | ) 41 | expect(sqrt(mean( 42 | (irisResC@inverse(irisResC@data)@data - irisData@data) ^ 2 43 | )) < 0.3, 44 | "error too large" 45 | ) 46 | expect(sqrt(mean( 47 | (irisRes@inverse(irisRes@data)@data - irisData@data) ^ 2 48 | )) < 0.3, 49 | "error too large" 50 | ) 51 | 52 | scale2 <- function(x, center, scale.) scale(x, center, scale.) 53 | expect_equal( 54 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResCS), 55 | irisParsCS[c("center", "scale.")]), 56 | getData( getDimRedData(irisResCS) ), 57 | tolerance = 1e-2 58 | ) 59 | 60 | expect_equal( 61 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResS), 62 | irisParsS[c("center", "scale.")]), 63 | getData( getDimRedData(irisResS) ), 64 | tolerance = 1e-2 65 | ) 66 | 67 | expect_equal( 68 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisResC), 69 | irisParsC[c("center", "scale.")]), 70 | getData( getDimRedData(irisResC) ), 71 | tolerance = 1e-2 72 | ) 73 | expect_equal( 74 | do.call(function(...) scale2(iris[1:4], ...) %*% getRotationMatrix(irisRes), 75 | irisPars[c("center", "scale.")]), 76 | getData( getDimRedData(irisRes) ), 77 | tolerance = 1e-2 78 | ) 79 | 80 | expect_s4_class({ embed(iris[1:4], "PCA_L1", ndim = 1, 81 | .mute = c("message", "output")) }, 82 | "dimRedResult") 83 | }) 84 | -------------------------------------------------------------------------------- /tests/testthat/test_UMAP.R: -------------------------------------------------------------------------------- 1 | skip_if_no_umap_learn <- function() { 2 | if (!reticulate::py_module_available("umap") && 3 | Sys.getenv("BNET_FORCE_UMAP_TESTS") != 1) 4 | skip("umap-learn not available, install with `pip install umap-learn==0.4`") 5 | } 6 | 7 | test_that("UMAP python", { 8 | if (!requireNamespace("umap", quietly = TRUE)) { 9 | skip("umap not available") 10 | } 11 | 12 | skip_if_no_umap_learn() 13 | 14 | res1 <- embed(iris[1:4], "UMAP", .mute = c("message", "output")) 15 | res2 <- embed(iris[1:4], "UMAP", .mute = c("message", "output"), knn = 20) 16 | 17 | expect_s4_class(res1, "dimRedResult") 18 | expect_equal(res1@method, "UMAP") 19 | expect_equal(res1@pars$d, "euclidean") 20 | expect_equal(res1@pars$knn, 15) 21 | expect_equal(res1@pars$method, "umap-learn") 22 | expect_equal(res1@pars$ndim, 2) 23 | 24 | expect_s4_class(res2, "dimRedResult") 25 | expect_equal(res2@method, "UMAP") 26 | expect_equal(res2@pars$d, "euclidean") 27 | expect_equal(res2@pars$knn, 20) 28 | expect_equal(res2@pars$method, "umap-learn") 29 | expect_equal(res2@pars$ndim, 2) 30 | 31 | 32 | expect_true(any(res1@data@data != res2@data@data)) 33 | 34 | pred1 <- predict(res1, iris[1:4]) 35 | pred2 <- predict(res2, iris[1:4]) 36 | 37 | expect_equal(dim(pred1@data), dim(res1@data@data)) 38 | expect_equal(dim(pred2@data), dim(res2@data@data)) 39 | }) 40 | 41 | test_that("UMAP R", { 42 | if (!requireNamespace("umap", quietly = TRUE)) { 43 | skip("umap not available") 44 | } 45 | 46 | res1 <- embed(iris[1:4], "UMAP", method = "naive", .mute = c("message", "output")) 47 | res2 <- embed(iris[1:4], "UMAP", method = "naive", .mute = c("message", "output"), knn = 20) 48 | 49 | expect_s4_class(res1, "dimRedResult") 50 | expect_equal(res1@method, "UMAP") 51 | expect_equal(res1@pars$d, "euclidean") 52 | expect_equal(res1@pars$knn, 15) 53 | expect_equal(res1@pars$method, "naive") 54 | expect_equal(res1@pars$ndim, 2) 55 | 56 | expect_s4_class(res2, "dimRedResult") 57 | expect_equal(res2@method, "UMAP") 58 | expect_equal(res2@pars$d, "euclidean") 59 | expect_equal(res2@pars$knn, 20) 60 | expect_equal(res2@pars$method, "naive") 61 | expect_equal(res2@pars$ndim, 2) 62 | 63 | expect_true(any(res1@data@data != res2@data@data)) 64 | 65 | pred1 <- predict(res1, iris[1:4]) 66 | pred2 <- predict(res2, iris[1:4]) 67 | 68 | expect_equal(dim(pred1@data), dim(res1@data@data)) 69 | expect_equal(dim(pred2@data), dim(res2@data@data)) 70 | }) 71 | -------------------------------------------------------------------------------- /tests/testthat/test_dataSets.R: -------------------------------------------------------------------------------- 1 | test_that("datasets load", { 2 | for (d in dataSetList()) { 3 | ds <- loadDataSet(d) 4 | expect(inherits(ds, "dimRedData"), "must be of class 'dimRedData'") 5 | } 6 | }) 7 | -------------------------------------------------------------------------------- /tests/testthat/test_diffusion_maps.R: -------------------------------------------------------------------------------- 1 | test_that("DiffusionMaps", { 2 | if(!requireNamespace("diffusionMap", quietly = TRUE)) 3 | skip("diffusionMap not available") 4 | expect_s4_class(embed(iris[1:4], "DiffusionMaps", ndim = 1, 5 | .mute = c("message", "output")), 6 | "dimRedResult") 7 | x <- embed(iris[1:4], "DiffusionMaps", ndim = 1, 8 | .mute = c("message", "output")) 9 | expect_equal(dim(x@data@data), c(150, 1)) 10 | }) 11 | -------------------------------------------------------------------------------- /tests/testthat/test_dimRedData_class.R: -------------------------------------------------------------------------------- 1 | test_that("constructor", { 2 | expect_equal(dimRedData(), new("dimRedData", 3 | data = matrix(numeric(0), 4 | nrow = 0, ncol = 0), 5 | meta = data.frame())) 6 | expect_error(dimRedData(iris)) 7 | expect_s4_class(dimRedData(iris[, 1:4], iris[, 5]), "dimRedData") 8 | expect_s4_class(dimRedData(iris[, 1:4]), "dimRedData") 9 | expect_error(dimRedData(iris)) 10 | }) 11 | 12 | test_that("conversion functions", { 13 | expect_equal(as(iris[, 1:4], "dimRedData"), dimRedData(iris[, 1:4])) 14 | expect_error(as(iris, "dimRedData")) 15 | expect_equal(as(loadDataSet("Iris"), "data.frame"), 16 | as.data.frame(loadDataSet("Iris"))) 17 | expect_equal(as.dimRedData( 18 | Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, iris), 19 | loadDataSet("Iris"), 20 | ignore_attr = TRUE 21 | ) 22 | }) 23 | 24 | test_that("misc functions", { 25 | Iris <- loadDataSet("Iris") 26 | expect_equal(getData(Iris), Iris@data) 27 | expect_equal(getMeta(Iris), Iris@meta) 28 | ## No idea why this one is broken with --run-donttest --run-dontrun --timings 29 | ## Also broken for devtools::test("dimRed") 30 | expect_equal(nrow(Iris), 150) 31 | expect_equal(Iris[1:4], Iris[1:4, ]) 32 | expect_equal(Iris[1:4], Iris[c(rep(TRUE, 4), rep(FALSE, 146))]) 33 | expect_equal(Iris[1:4], Iris[c(rep(TRUE, 4), rep(FALSE, 146)), ]) 34 | }) 35 | -------------------------------------------------------------------------------- /tests/testthat/test_dimRedMethod-class.R: -------------------------------------------------------------------------------- 1 | test_that("pars matching", { 2 | for (m in dimRedMethodList()) { 3 | mo <- getMethodObject(m) 4 | expect( 5 | all.equal( 6 | mo@stdpars, 7 | matchPars(mo, list()) 8 | ), 9 | paste("par matching for", m, "failed") 10 | ) 11 | } 12 | 13 | expect_warning( 14 | embed(iris[1:4], "PCA", asdf = 1234), 15 | "Parameter matching: asdf is not a standard parameter, ignoring." 16 | ) 17 | }) 18 | -------------------------------------------------------------------------------- /tests/testthat/test_dimRedResult_class.R: -------------------------------------------------------------------------------- 1 | test_that("predict/inverse methods", { 2 | dat <- loadDataSet("Iris") 3 | emb <- embed(dat, "PCA", ndim = 4) 4 | pred <- predict(emb, dat) 5 | inv <- inverse(emb, pred) 6 | expect_equal(getDimRedData(emb), pred) 7 | expect_equal(dat, inv) 8 | 9 | if(requireNamespace("Rtsne", quietly = TRUE)) { 10 | emb2 <- embed(dat, "tSNE") 11 | expect_error(predict(emb2, dat)) 12 | expect_error(inverse(emb2, dat)) 13 | } 14 | }) 15 | 16 | test_that("conversion", { 17 | iris_data_frame_as <- as(embed(loadDataSet("Iris"), "PCA"), "data.frame") 18 | expect_equal(colnames(iris_data_frame_as), c("meta.Species", "PC1", "PC2", colnames(iris)[-5])) 19 | }) 20 | -------------------------------------------------------------------------------- /tests/testthat/test_drr.R: -------------------------------------------------------------------------------- 1 | test_that("drr forward and backward passes", { 2 | spiral <- loadDataSet("Helix", n = 200) 3 | 4 | drr_spiral <- embed(spiral, "DRR", ndim = 3, .mute = c("message", "output")) 5 | 6 | expect_equal(3, getNDim(drr_spiral)) 7 | dsa <- drr_spiral@apply(spiral) 8 | dsi <- drr_spiral@inverse(dsa) 9 | 10 | expect_equal(dsi, spiral) 11 | }) 12 | -------------------------------------------------------------------------------- /tests/testthat/test_embed.R: -------------------------------------------------------------------------------- 1 | test_that("standard method is PCA", { 2 | 3 | res <- embed(iris[1:4]) 4 | expect_equal(res@method, "PCA") 5 | 6 | }) 7 | 8 | test_that("correctly convert .keep.org.data argument", { 9 | res1 <- embed(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 10 | iris, "PCA", .keep.org.data = FALSE) 11 | 12 | 13 | res2 <- embed(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 14 | iris, "PCA", .keep.org.data = TRUE) 15 | 16 | attr(res2@org.data, "assign") <- NULL 17 | 18 | expect_equal( 19 | unname(as.matrix(iris[1:4])), 20 | unname(as.matrix(getOrgData(res2)@data)) 21 | ) 22 | 23 | }) 24 | -------------------------------------------------------------------------------- /tests/testthat/test_fastICA.R: -------------------------------------------------------------------------------- 1 | test_that("general data conversions", { 2 | if(!requireNamespace("fastICA", quietly = TRUE)) 3 | skip("FastICA not available") 4 | irisData <- as(iris[, 1:4], "dimRedData") 5 | expect_equal(class(irisData)[1], "dimRedData") 6 | 7 | irisRes <- embed(irisData, "FastICA") 8 | expect_equal(class(irisRes)[1], "dimRedResult") 9 | 10 | expect_equal(2, getNDim(irisRes)) 11 | 12 | expect_equal(irisRes@apply(irisData), irisRes@data) 13 | 14 | expect( 15 | sqrt( 16 | mean( 17 | (irisRes@inverse(irisRes@data)@data - irisData@data) ^ 2 18 | ) 19 | ) < 0.3, 20 | "error too large" 21 | ) 22 | 23 | expect_equal( 24 | scale(iris[1:4], TRUE, FALSE) %*% getRotationMatrix(irisRes), 25 | unname(as.matrix(getData( getDimRedData(irisRes) )) ) 26 | ) 27 | }) 28 | -------------------------------------------------------------------------------- /tests/testthat/test_high_level_functions.R: -------------------------------------------------------------------------------- 1 | test_that("high level functions working?", { 2 | embed_methods <- dimRedMethodList(filter = TRUE) 3 | quality_methods <- dimRedQualityList(filter = TRUE) 4 | scurve <- loadDataSet("3D S Curve", n = 300) 5 | for (i in seq_len(ncol(scurve@data))){ 6 | scurve@data[, i] <- scurve@data[, i] - min(scurve@data[, i]) 7 | } 8 | 9 | quality_results <- matrix(NA, length(embed_methods), 10 | length(quality_methods), 11 | dimnames = list(embed_methods, quality_methods)) 12 | embedded_data <- list() 13 | 14 | for (e in embed_methods) { 15 | message("embedding: ", e) 16 | 17 | if ( (e != "AutoEncoder" || reticulate::py_module_available("tensorflow")) && 18 | (e != "UMAP" || reticulate::py_module_available("umap-learn")) ) { 19 | 20 | suppressWarnings( 21 | embedded_data[[e]] <- embed( 22 | scurve, e, 23 | .mute = c("message", "output"))) 24 | 25 | for (q in quality_methods) { 26 | message(" quality: ", q) 27 | quality_results[e, q] <- tryCatch( 28 | suppressWarnings(quality(embedded_data[[e]], q, 29 | .mute = c("message", "output"))), 30 | error = function (e) NA 31 | ) 32 | } 33 | 34 | } 35 | } 36 | 37 | lapply(embedded_data, function(x) expect_equal(2, getNDim(x))) 38 | 39 | expect(inherits(quality_results, "matrix"), "should be matrix") 40 | expect(storage.mode(quality_results) == "double", 41 | 'storage should be "double"') 42 | }) 43 | -------------------------------------------------------------------------------- /tests/testthat/test_isomap.R: -------------------------------------------------------------------------------- 1 | 2 | ## no isomap specific tests, because forward method is not really 3 | ## exact. 4 | 5 | 6 | test_that("check vs vegan isomap", { 7 | if (!requireNamespace(c("vegan", dimRed:::getMethodDependencies("Isomap")), quietly = TRUE)) { 8 | skip("Not all required packages for isomap tests installed") 9 | } 10 | 11 | eps <- 1e-8 12 | a <- loadDataSet("3D S Curve", n = 200) 13 | 14 | vegiso <- vegan::isomap(dist(getData(a)), k = 8, ndim = 2) 15 | vegy <- vegan::scores(vegiso) 16 | 17 | drdiso <- embed(a, "Isomap", knn = 8, ndim = 2) 18 | drdy <- drdiso@data@data 19 | 20 | ## Randomly fails: 21 | ## expect_equal(drdy, vegy, ignore_attr = TRUE) 22 | 23 | err1 <- max(abs(drdy - vegy)) 24 | 25 | drdy[, 2] <- -drdy[, 2] 26 | err2 <- max(abs(drdy - vegy)) 27 | 28 | drdy[, 1] <- -drdy[, 1] 29 | err3 <- max(abs(drdy - vegy)) 30 | 31 | drdy[, 2] <- -drdy[, 2] 32 | err4 <- max(abs(drdy - vegy)) 33 | 34 | err <- min(err1, err2, err3, err4) 35 | 36 | expect_true(err < eps, info = paste0("err = ", err, 37 | ", eps = ", eps, 38 | ", expected err < eps")) 39 | 40 | }) 41 | 42 | 43 | test_that("check other.data", { 44 | if (!requireNamespace(dimRed:::getMethodDependencies("Isomap"), quietly = TRUE)) { 45 | skip("Not all required packages for isomap tests installed") 46 | } 47 | 48 | a <- loadDataSet("3D S Curve", n = 200) 49 | drdiso <- embed(a, "Isomap", knn = 8, ndim = 2, get_geod = TRUE) 50 | expect_true(inherits(getOtherData(drdiso)$geod, "dist")) 51 | }) 52 | -------------------------------------------------------------------------------- /tests/testthat/test_kPCA.R: -------------------------------------------------------------------------------- 1 | test_that("general data conversions", { 2 | data(iris) 3 | 4 | irisData <- loadDataSet("Iris") 5 | expect_equal(class(irisData)[1], "dimRedData") 6 | 7 | irisPars <- list() 8 | irisPars[[length(irisPars) + 1]] <- 9 | list(kernel = "rbfdot", 10 | kpar = list(sigma = 0.1)) 11 | irisPars[[length(irisPars) + 1]] <- 12 | list(kernel = "rbfdot", 13 | kpar = list(sigma = 1)) 14 | irisPars[[length(irisPars) + 1]] <- 15 | list(kernel = "polydot", 16 | kpar = list(degree = 3)) 17 | irisPars[[length(irisPars) + 1]] <- 18 | list(kernel = "vanilladot", 19 | kpar = list()) 20 | irisPars[[length(irisPars) + 1]] <- 21 | list(kernel = "laplacedot", 22 | kpar = list(sigma = 1)) 23 | irisPars[[length(irisPars) + 1]] <- 24 | list(kernel = "laplacedot", 25 | kpar = list(sigma = 0.1)) 26 | irisPars[[length(irisPars) + 1]] <- 27 | list(kernel = "besseldot", 28 | kpar = list(sigma = 0.1, 29 | order = 1, 30 | degree = 1)) 31 | irisPars[[length(irisPars) + 1]] <- 32 | list(kernel = "besseldot", 33 | kpar = list(sigma = 1, 34 | order = 2, 35 | degree = 3)) 36 | irisPars[[length(irisPars) + 1]] <- 37 | list(kernel = "splinedot", 38 | kpar = list()) 39 | 40 | irisRes <- lapply(irisPars, function(x) 41 | do.call( 42 | function(...) tryCatch(embed(.data = irisData, 43 | .method = "kPCA", ...), 44 | error = function(e) as.character(e)), 45 | x 46 | ) ) 47 | 48 | for (i in 1:length(irisRes)) { 49 | if (inherits(irisRes[[i]], "character")){ 50 | expect(grepl("singular", irisRes[[i]]), 51 | "singular") 52 | } else { 53 | expect(inherits(irisRes[[i]], "dimRedResult"), 54 | 'should be of class "dimRedResult"') 55 | } 56 | } 57 | 58 | ## This test fails with multithreaded blas 59 | ## for (i in 1:length(irisRes)){ 60 | ## if (inherits(irisRes[[i]], "dimRedResult")){ 61 | ## expect_equal(irisRes[[i]]@apply(irisData)@data[, 1:2], 62 | ## irisRes[[i]]@data@data) 63 | 64 | ## expect_equal(2, getNDim(irisRes[[i]])) 65 | 66 | ## ## the reverse is an approximate: 67 | 68 | ## expect_less_than( 69 | ## max( 70 | ## irisRes[[i]]@inverse(irisRes[[i]]@data)@data - irisData@data 71 | ## ), 300, 72 | ## ## paste0("inverse of kpca is an approximate, ", 73 | ## ## "so this may fail due to numerical inaccuracy") 74 | ## ) 75 | ## } 76 | ## } 77 | 78 | ## This one cannot calculate an inverse: 79 | kpca.fit <- embed(loadDataSet("3D S", n = 200), 80 | "kPCA", kernel = "splinedot", kpar = list()) 81 | expect( is.na(kpca.fit@inverse(1)), "The inverse should return NA" ) 82 | }) 83 | -------------------------------------------------------------------------------- /tests/testthat/test_misc_functions.R: -------------------------------------------------------------------------------- 1 | test_that("squared euclidean distance", { 2 | a <- matrix(rnorm(25), 5, 5) 3 | b <- matrix(rnorm(25), 5, 5) 4 | 5 | expect_equal( 6 | t(as.matrix(dist(rbind(a, b)))[6:10, 1:5] ^ 2), 7 | pdist2(a, b), 8 | ignore_attr = TRUE 9 | ) 10 | }) 11 | 12 | test_that("formula functions", { 13 | a <- matrix(rnorm(25), 5, 5) 14 | b <- matrix(rnorm(25), 5, 5) 15 | 16 | expect_true(rhs(a + b ~ c + d) == ~ c + d + 0) 17 | expect_true(lhs(a + b ~ c + d) == ~ a + b + 0) 18 | ## expect_equal(rhs(a + b ~ c + d), ~ c + d + 0) 19 | ## expect_equal(lhs(a + b ~ c + d), ~ a + b + 0) 20 | }) 21 | 22 | 23 | 24 | test_that("makeEpsGraph", { 25 | if(!requireNamespace("Matrix", quietly = TRUE)) 26 | skip("Matrix required") 27 | check_makeEpsGraph <- function(x, eps){ 28 | naive <- as.matrix(dist(x)) 29 | naive[naive >= eps] <- 0 30 | epsSp <- as.matrix(makeEpsSparseMatrix(x, eps)) 31 | all(naive == epsSp) 32 | } 33 | expect_true(check_makeEpsGraph(iris[1:4], 1000)) 34 | expect_true(check_makeEpsGraph(iris[1:4], 1)) 35 | expect_true(check_makeEpsGraph(iris[1:4], 0.5)) 36 | }) 37 | 38 | 39 | test_that("getRotationMatrixFail", { 40 | if(!requireNamespace("Rtsne", quietly = TRUE)) 41 | skip("Rtsne not available") 42 | 43 | irisData <- as(iris[, 1:4], "dimRedData") 44 | expect_equal(class(irisData)[1], "dimRedData") 45 | 46 | irisRes <- embed(irisData, "tSNE") 47 | 48 | expect_error(getRotationMatrix(irisRes), "Not implemented for") 49 | }) 50 | -------------------------------------------------------------------------------- /tests/testthat/test_quality.R: -------------------------------------------------------------------------------- 1 | test_that("quality", { 2 | 3 | irisData <- loadDataSet("Iris") 4 | # remove duplicates 5 | irisData <- irisData[!duplicated(irisData@data), ] 6 | 7 | parsPCA <- list(center = TRUE, scale. = TRUE) 8 | resPCA <- do.call(function(...) embed(irisData, "PCA", ...), parsPCA) 9 | 10 | if(requireNamespace("coRanking", quietly = TRUE)) 11 | expect_true(is.numeric(Q_local(resPCA))) 12 | if(requireNamespace("coRanking", quietly = TRUE)) 13 | expect_true(is.numeric(Q_global(resPCA))) 14 | if(requireNamespace("coRanking", quietly = TRUE)) 15 | expect_true(is.numeric(mean_R_NX(resPCA))) 16 | if(requireNamespace("optimx", quietly = TRUE)) 17 | expect_true(is.numeric(total_correlation(resPCA))) 18 | expect_true(is.numeric(cophenetic_correlation(resPCA))) 19 | if (requireNamespace("energy", quietly = TRUE)) 20 | expect_true(is.numeric(distance_correlation(resPCA))) 21 | expect_true(is.numeric(reconstruction_rmse(resPCA))) 22 | 23 | ## suppressWarnings( 24 | ## resQual <- list( 25 | ## Q_local(resPCA), 26 | ## Q_global(resPCA), 27 | ## mean_R_NX(resPCA), 28 | ## total_correlation(resPCA), 29 | ## cophenetic_correlation(resPCA), 30 | ## distance_correlation(resPCA), 31 | ## reconstruction_rmse(resPCA) 32 | ## ) 33 | ## ) 34 | 35 | ## lapply(resQual, function(x) expect_true(is.numeric(x))) 36 | }) 37 | 38 | test_that("Q_local ndim", { 39 | if(!requireNamespace("coRanking", quietly = TRUE)) { 40 | skip("coRanking not available") 41 | } 42 | 43 | irisData <- loadDataSet("Iris") 44 | irisData <- irisData[!duplicated(irisData@data)] 45 | 46 | parsPCA <- list(center = TRUE, scale. = FALSE, ndim = 4) 47 | resPCA <- do.call(function(...) embed(irisData, "PCA", ...), parsPCA) 48 | 49 | tmp <- sapply(1:4, function(x) quality(resPCA, "Q_local", ndim = x)) 50 | expect_equal(rank(tmp), 1:4) 51 | }) 52 | 53 | test_that("rmse_by_ndim", { 54 | if(!requireNamespace("DRR", quietly = TRUE)) { 55 | skip("DRR not available") 56 | } 57 | 58 | ir <- loadDataSet("Iris") 59 | 60 | set.seed(1) 61 | ir.drr <- embed(ir, "DRR", .mute = c("message", "output"), ndim = ndims(ir)) 62 | ir.pca <- embed(ir, "PCA", ndim = ndims(ir)) 63 | 64 | rmse_res <- data.frame( 65 | drr = reconstruction_error(ir.drr), 66 | pca = reconstruction_error(ir.pca) 67 | ) 68 | 69 | for (i in 1:length(rmse_res$pca)) { 70 | expect_true(rmse_res$pca[i] - rmse_res$drr[i] + 1e-12 > 0, info = paste0( 71 | "ndim = ", i, 72 | ", rmse pca = ", rmse_res$pca[i], 73 | ", rmse drr = ", rmse_res$drr[i] 74 | )) 75 | } 76 | # expect_true(all((rmse_res$pca - rmse_res$drr) + 1e-12 > 0)) 77 | 78 | expect_error(reconstruction_error(ir.pca, 5)) 79 | expect_error(reconstruction_error(ir.pca, 0)) 80 | }) 81 | 82 | test_that("AUC_lnK_R_NX", { 83 | if(!requireNamespace("coRanking", quietly = TRUE)) { 84 | skip("coRanking not available") 85 | } 86 | 87 | irisData <- loadDataSet("Iris") 88 | irisData <- irisData[!duplicated(irisData@data)] 89 | 90 | parsPCA <- list(center = TRUE, scale. = TRUE, ndim = 4) 91 | resPCA <- do.call(function(...) embed(irisData, "PCA", ...), parsPCA) 92 | 93 | expect_true(length(AUC_lnK_R_NX(resPCA, weight = "inv")) == 1) 94 | expect_true(length(AUC_lnK_R_NX(resPCA, weight = "log")) == 1) 95 | expect_true(length(AUC_lnK_R_NX(resPCA, weight = "ln")) == 1) 96 | expect_true(length(AUC_lnK_R_NX(resPCA, weight = "log10")) == 1) 97 | 98 | expect_true(AUC_lnK_R_NX(resPCA, weight = "log") == AUC_lnK_R_NX(resPCA, weight = "ln")) 99 | expect_error(AUC_lnK_R_NX(resPCA, weight = "asdf")) 100 | 101 | }) 102 | -------------------------------------------------------------------------------- /vignettes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | echo "BNET_BUILD_VIGNETTE: $(BNET_BUILD_VIGNETTE)" 3 | echo "_R_CHECK_DEPENDS_ONLY_: " $(_R_CHECK_DEPENDS_ONLY_) 4 | if ! [ "$(_R_CHECK_DEPENDS_ONLY_)" = "true" ]; then \ 5 | $(R_HOME)/bin/Rscript -e "knitr::knit2pdf('dimensionality-reduction.Rnw')"; \ 6 | $(R_HOME)/bin/Rscript -e "tools::compactPDF('dimensionality-reduction.pdf', gs_quality = 'ebook')"; \ 7 | rm -rf dimensionality-reduction.tex figure/ auto/; \ 8 | fi 9 | -------------------------------------------------------------------------------- /vignettes/classification_tree.tex: -------------------------------------------------------------------------------- 1 | \newcommand{\imp}[1] {\textbf{#1}} % style for implemented methods 2 | \newcommand{\noimp}[1] {#1} % style for not implemented methods 3 | \tikz[ 4 | % tree layout, 5 | grow cyclic, % 6 | level 1/.style={level distance=1.2cm, sibling 7 | angle=180, text width=1.5cm, font={\small}}, % 8 | level 2/.style={level distance=1.9cm, sibling angle=40, 9 | font={\scriptsize}},%, text width=1.4cm}, 10 | level 3/.style={level distance=2.2cm, sibling angle=30}, 11 | level 4/.style={level distance=2.3cm}, 12 | % text width=1.2cm, 13 | font=\tiny, 14 | innernode/.style={align=flush center},%, text width=1.2}, 15 | leaf/.style={% 16 | % draw, very thin, 17 | % fill=red!30, 18 | rounded corners, 19 | align=flush left, 20 | text width=, 21 | inner sep=2pt, 22 | font={\hangindent=0.2cm\scriptsize\sffamily}}, 23 | ]{ 24 | \node[innernode, draw, align=flush center, rounded corners, font={\normalsize\bfseries}]{ 25 | Dimensionality \\ reduction} 26 | child[] { node[innernode] {Convex} % level 1 27 | child[sibling angle=55]{ node[innernode] {Full spectral} % level 2 28 | child { node[innernode] {Euclidean distances} 29 | child { node[leaf, text width=1.3cm]{ 30 | \imp{PCA} \\ 31 | \imp{Classical scaling} 32 | } } } 33 | child { node[innernode] {Geodesic distances} 34 | child { node[leaf]{ 35 | \imp{Isomap} 36 | } } } 37 | child { node[innernode] {Kernel-based} 38 | child { node[leaf]{ 39 | \imp{Kernel PCA} \\ 40 | \noimp{MVU} 41 | } } } 42 | child { node[innernode] {Diffusion distance} 43 | child { node[leaf]{ 44 | \imp{Diffusion maps} 45 | } } } } 46 | child[] { node[innernode] {Removal of shared information 47 | by regression} %level 2 48 | child{ node[leaf]{ 49 | \imp{DRR} 50 | } } } 51 | child[sibling angle=55] { node[innernode] {Sparse spectral} % level 2 52 | child[sibling angle=45] { node[innernode] {Reconstruction weights} 53 | child {node[leaf]{ 54 | \imp{Local Linear Embedding} 55 | } } } 56 | child[sibling angle=45] { node[innernode] {Neighborhood graph Laplacian} 57 | child { node[leaf]{ 58 | \imp{Laplacian Eigenmaps} 59 | } } } 60 | child[sibling angle=45] { node[innernode] {Local tangent space} 61 | child { node[leaf, text width=2cm]{ 62 | \imp{Hessian LLE} \\ 63 | \noimp{Local tangent space alignment} 64 | } } } } } 65 | child[level distance=1.8cm] { node[innernode] {Non-convex} %level 1 66 | child { node[innernode] {Weighted Euclidean distances} % level 2 67 | child { node[leaf, text width=2cm]{ 68 | \imp{Non-linear MDS} \\ 69 | \noimp{Sammon's mapping} \\ 70 | \noimp{Stochastic Proximity Embedding} 71 | } } } 72 | child { node[innernode] {Alignment of local linear models} % level 2 73 | child { node[leaf]{ 74 | \noimp{LLC} \\ 75 | \noimp{Man.\ charting} 76 | } } } 77 | child { node[innernode] {Neural network} % level 2 78 | child { node[leaf]{ 79 | Autoencoder 80 | } } } 81 | child { node[innernode] {Discrete mapping} % level 2 82 | child { node[leaf,text width=2.5cm]{ 83 | \noimp{Self Organizing Maps} \\ 84 | \noimp{Generative Topographic Mapping} \\ 85 | \noimp{Elastic Net} 86 | } } } 87 | child { node[innernode] {Stochastic methods} % level 2 88 | child { node[leaf]{ 89 | \noimp{SNE} \\ 90 | \imp{t-SNE} \\ 91 | \noimp{NeRV} \\ 92 | \noimp{JNE} 93 | } } } 94 | child { node[innernode] {Force directed} % level 2 95 | child { node[leaf, text width=2cm]{ 96 | \imp{Kamada-Kawai} \\ 97 | \imp{Fruchtermann-Reingold} \\ 98 | \imp{DrL} 99 | } } } }; 100 | } 101 | 102 | %%% Local Variables: 103 | %%% mode: LaTeX 104 | %%% TeX-command-extra-options: "-shell-escape" 105 | %%% TeX-engine: default 106 | %%% TeX-master: "dimensionality-reduction" 107 | %%% End: -------------------------------------------------------------------------------- /vignettes/dimensionality-reduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gdkrmr/dimRed/6931165735ad8f42c67c39e1efb69e68e4019fa4/vignettes/dimensionality-reduction.pdf --------------------------------------------------------------------------------