cluster analysis - How to calculate BIC for k-means clustering in R -
i've been using k-means cluster data in r i'd able assess fit vs. model complexity of clustering using baysiean information criterion (bic) , aic. code i've been using in r is:
kcldata <- kmeans(data, centers=2, nstart= 100)
but i'd able extract bic , log likelihood. appreciated!
for else landing here, there's method proposed sherry towers @ http://sherrytowers.com/2013/10/24/k-means-clustering/, uses output stats::kmeans
. quote:
the aic can calculated following function:
kmeansaic = function(fit){ m = ncol(fit$centers) n = length(fit$cluster) k = nrow(fit$centers) d = fit$tot.withinss return(d + 2*m*k) }
from stats::aic
, can see bic can calculated in similar way aic. easy way bic replace return()
in above function, this:
return(data.frame(aic = d + 2*m*k, bic = d + log(n)*m*k))
so use follows:
fit <- kmeans(x = data,centers = 6) kmeansaic(fit)
Comments
Post a Comment