R/RcppExports.R

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#'@importFrom Rcpp sourceCpp
#'@useDynLib lowmemtkmeans
NULL

#'@title Calculates BIC for a given clustering.
#'@description
#'Computes Bayesian information criterion for a given clustering of a data set.
#'@details
#'Bayesian information criterion (BIC) is calculated using the formula, BIC =  -2 * log(L) + k*log(n).
#'k is the number of free parameters, in this case is m*k + k - 1.
#'n is the number of observations (rows of data).
#'L is the liklihood for the given set of cluster centres.
#'
#'@param data a matrix (n x m). Rows are observations, columns are predictors.
#'@param centres matrix of cluster means (k x m), where k is the number of clusters.
#'@return BIC value
#'@examples
#'iris_mat <- as.matrix(iris[,1:4])
#'iris_centres2 <- tkmeans(iris_mat, 2 , 0.1, c(1,1,1,1), 1, 10, 0.001) # 2 clusters
#'iris_centres3 <- tkmeans(iris_mat, 3 , 0.1, c(1,1,1,1), 1, 10, 0.001) # 3 clusters
#'cluster_BIC(iris_mat, iris_centres2)
#'cluster_BIC(iris_mat, iris_centres3)
#'@export
cluster_BIC <- function(data, centres) {
    .Call('lowmemtkmeans_cluster_BIC', PACKAGE = 'lowmemtkmeans', data, centres)
}

tkmeans <- function(M, k, alpha, weights, nstart = 1L, iter = 10L, tol = 0.0001, verbose = FALSE) {
    .Call('lowmemtkmeans_tkmeans', PACKAGE = 'lowmemtkmeans', M, k, alpha, weights, nstart, iter, tol, verbose)
}

#'@title Rescales a matrix in place.
#'@description
#'Recales matrix so that each column has a mean of 0 and a standard deviation of 1.
#'The original matrix is overwritten in place. The function returns the means and standard deviations of each column used to rescale it.
#'@details
#'The key advantage of this method is that it can be applied to very large matrices without having to make a second copy in memory and the orginal can still be restored using the saved information.
#'
#'@param M matrix of data (n x m)
#'@return Returns a matrix of size (2 x m). The first row contains the column means. The second row contains the column standard dveiations. NOTE: The original matrix, M, is overwritten.
#'@examples
#'m = matrix(rnorm(24, 1, 2),4, 6)
#'scale_params = scale_mat_inplace(m)
#'sweep(sweep(m,2,scale_params[2,],'*'),2,scale_params [1,], '+') # orginal matrix restored
#'@export
scale_mat_inplace <- function(M) {
    .Call('lowmemtkmeans_scale_mat_inplace', PACKAGE = 'lowmemtkmeans', M)
}

#'@title Allocates each rw (observation) in data to the nearest cluster centre.
#'@description
#'For each observation the euclidean distance to each of the cluster centres is calculated and cluster with the smallest distance is return for that observation.
#'@param data a matrix (n x m) to be clustered
#'@param centres matrix of cluster means (k x m), wher k is the number of clusters.
#'@return vector of cluster allocations, n values ranging from 1 to k.
#'@examples
#'iris_mat <- as.matrix(iris[,1:4])
#'centres<- tkmeans(iris_mat, 3 , 0.2, c(1,1,1,1), 1, 10, 0.001)
#' nearest_cluster(iris_mat, centres)
#'@export
nearest_cluster <- function(data, centres) {
    .Call('lowmemtkmeans_nearest_cluster', PACKAGE = 'lowmemtkmeans', data, centres)
}

Try the lowmemtkmeans package in your browser

Any scripts or data that you put into this service are public.

lowmemtkmeans documentation built on May 1, 2019, 7:58 p.m.