clustering {rexposome} | R Documentation

## Method to perform clustering on the samples of an ExposomeSet

### Description

This method allows to create an ExposomeClust object from an ExposomeSet object by clustering samples through the exposure levels. The method is flexible to accept any clustering method (`method`) that allows to obtain a classification (`cmethod`) of the samples. The function assigned to argument `method` must have an argument called `data`, waiting for the matrix of exposures (samples as rows, exposures as columns). If the result object of the `method` has no accessor `$classification`, then a `cmethod` is required and will be applied on the result of `method` to obtain a labelled vector with the samples' classification.

### Usage

    clustering(object, method, cmethod, ..., warnings = TRUE)

### Arguments

`object` | `ExposomeSet` containing the exposures used for the clustering process
`method` | Function applied to the exposures of `object`. This function must has an argument named as `data` that will receive the matrix of exposures.
`cmethod` | (optional) Function to obtain the classification from the object generated with `method`.
`...` | Passed to content of `method`.
`warnings` | (default `TRUE`) If set to `FALSE` warnings will not be displayed.

### Value

`ExposomeClust` with the original exposures and the classification of each exposure.

### Note

The function assigned to `cmethod` will be directy applied to the result of the `method` as: `cmethod(model)`; being `model` the result of `method`.

### See Also

classification to see how to obtain the classification of the samples from an ExposomeClust, plotClassification to plot the groups

### Examples

    data("exposome")

    # EXAMPLE 1: Clustering with mclust
    library(mclust)
    c <- clustering(expo[12:32, ], method = Mclust, G = 2)
    table(classification(c))  # This works since the result of Mclust has an accessor
                       # $classification

    # EXAMPLE 2: Cluseting with flexmix
    library(flexmix)
    # First we carete a function to apply flexmix to the ExposomeSet
    flexmix_clust <- function(data, ...) {
      data <- as.matrix(data)
      flexmix(formula = data~1, ...)
    }

    # Then if we apply the method to the ExposomeSet it will crash:
    # c <- clustering(expo[12:32, ], method = flexmix_clust, k = 2, model = FLXMCmvnorm())
    # Because the method does not know how to obtain the classification for the result
    # since flexmix has not an accessor called $classiciation

    # We create a function to get the classification
    flexmix_clas <- function(model, ...) {
      return(clusters(model))
    }

    # We put it to the ExposomeClust
    c <- clustering(expo[12:32, ], method = flexmix_clust, cmethod = flexmix_clas,
        k = 2, model = FLXMCmvnorm())
    classification(c) # This works because the ExposomeClust has a way to get
                      # the classification

[Package rexposome version 1.12.4 Index]