knnClassify {twoddpcr} | R Documentation

## Use the k-nearest neighbour algorithm to classify the wells in a `ddpcrWell` or `ddpcrPlate` object, or in a data frame.

### Description

If `droplets` is a data frame, the droplets are classified using the k-nearest neighbour algorithm against a training data set.

If `droplets` is a `ddpcrWell` object, the droplets in the well are classified and returned in another `ddpcrWell` object.

If `droplets` is a `ddpcrPlate` object, the wells are combined and classified together, with the resulting classification assigned to the `ddpcrPlate` object.

### Usage

    knnClassify(droplets, trainData, cl, k, prob = 0, ...)

    ## S4 method for signature 'data.frame'
    knnClassify(droplets, trainData, cl, k, prob = 0,
      fullTable = TRUE)

    ## S4 method for signature 'ddpcrWell'
    knnClassify(droplets, trainData, cl, k, prob = 0)

    ## S4 method for signature 'ddpcrPlate'
    knnClassify(droplets, trainData, cl, k, prob = 0)

### Arguments

`droplets` | A `ddpcrWell` or `ddpcrPlate` object, or a data frame with columns `Ch1.Amplitude` and `Ch2.Amplitude`.
`trainData` | A data frame of training data with columns `Ch1.Amplitude` and `Ch2.Amplitude`.
`cl` | A vector of classes corresponding to `trainData`.
`k` | The number of nearest neighbours to use in the algorithm.
`prob` | The minimal proportion of votes for the winning class needed to assert that a droplet belongs to the class. This figure should be a float between 0 and 1. For example, if 0.6 then at least 60 k-nearest neighbours need to be of one class, otherwise it is classified as "Rain". Defaults to 0, i.e. we do not use "Rain".
`...` | Other options depending on the type of `droplets`.
`fullTable` | If `TRUE`, returns a full data frame of droplets and their classification; if `FALSE`, simply returns the classified vector. Defaults to `TRUE`.

### Value

An object with the new classification.

If `droplets` is a data frame, return data frame or factor (depending on the value of `fullTable`) of the droplet classification under the k-NN algorithm.

### Author(s)

Anthony Chiu, anthony.chiu@cruk.manchester.ac.uk

### See Also

This method uses the `knn` function.

To manually set and retrieve classifications, use the `wellClassification`, `plateClassification` and `plateClassificationMethod` methods.

`kmeansClassify` provides a wrapper for the k-means clustering algorithm.

### Examples

    ### Use the KRASdata dataset for all of these examples.

    ## Use k-means clustering to classify one sample. Use this as training
    ## data for the K-Nearest Neighbour algorithm.
    trainingData <- KRASdata[["E03"]]
    trainingData <- kmeansClassify(trainingData)$data

    ## Classify a dataframe using k-NN with k = 1 and the above training data.
    aWell <- knnClassify(
               KRASdata[["F03"]],
               trainData=trainingData[, c("Ch1.Amplitude", "Ch2.Amplitude")],
               cl=trainingData$class,
               k=1)
    dropletPlot(aWell, cMethod="class")  # visualising the classification

    ## We can change k to a larger number, here with a ddpcrWell object.
    aWell <- ddpcrWell(well=KRASdata[["E03"]])
    aWell <- knnClassify(
               aWell,
               trainData=trainingData[, c("Ch1.Amplitude", "Ch2.Amplitude")],
               cl=trainingData$class,
               k=3)
    dropletPlot(aWell, cMethod="knn")  # visualising the classification

    ## Changing the 'prob' parameter means that droplets with less than 'prob'
    ## of the votes will not be classified. We do this for a ddpcrPlate
    ## object.
    krasPlate <- ddpcrPlate(wells=KRASdata[c("E03", "H03", "C04", "F04")])
    krasPlate <- knnClassify(
                   krasPlate,
                   trainData=trainingData[, c("Ch1.Amplitude", "Ch2.Amplitude")],
                   cl=trainingData$class,
                   k=3,
               prob=0.6)
    dropletPlot(krasPlate, cMethod="knn")  # visualising the classification

[Package twoddpcr version 1.11.0 Index]