
x=seq(0,100,20)
 kk=kmeans(x,centers=2)
 kk$centers
#  [,1]
#1   20
#2   80



library(MASS)
library(archetypes)

huge=200

no.scalefn <- function(x, ...){
  return(x)
}
no.rescalefn <- function(x, zs, ...){
  if ( is.null(zs) )
    return(matrix(NA, nrow = 0, ncol = 0))
  return(zs)
}

ginv.zalphasfn <- function(alphas, x, ...) {
  require(MASS)  
  return(t(ginv(alphas %*% t(alphas)) %*% alphas %*% t(x)))
}

stepArchetypes_mod <- function (data, k, nrep = 3, verbose = TRUE){
  mycall <- match.call()
  as <- list()
  for (i in 1:length(k)) {
    as[[i]] <- list()
    class(as[[i]]) <- "repArchetypes"
    for (j in seq_len(nrep)) {
      if (verbose) 
        cat("\n*** k=", k[i], ", rep=", j, ":\n", sep = "")
      as[[i]][[j]] <- archetypes(data, k = k[i], family = archetypesFamily("original",scalefn = no.scalefn,rescalefn = no.rescalefn))
    }
  }
  return(structure(as, class = "stepArchetypes", call = mycall))
}



 set.seed(1234)
 lass10 <- stepArchetypes_mod(data = x, k = 1:2, verbose = FALSE, nrep =100) 

 ai=bestModel(lass10[[2]])
 ai$archetypes
    # [,1] [,2]
#[1,]  100    0

 ai$alphas
  #   [,1] [,2]
#[1,]  0.0  1.0
#[2,]  0.2  0.8
#[3,]  0.4  0.6
#[4,]  0.6  0.4
#[5,]  0.8  0.2
#[6,]  1.0  0.0
