#!usr/bin/env Rscript
# Supporting function for nonstand_AA.m
#
# based on code from Sohan Seth and Manuel Eugster in classic_aa.R

set.seed(1)

library("archetypes")
library(R.matlab)
#library(Anthropometry)
source('LstepArchetypesMod.R')
inp <- readMat('classic_aa_Input.mat')

# matFeatSam available in the workspace (from Matlab)
# data("toy")
# matFeatSam <- t(toy)

as <- LstepArchetypesMod(t(inp$matFeatSam), k = inp$nLat, nrep = 10)
a <- bestModel(as)

s <- list(matSamLat = a$betas,  # W
          matLatSam = a$alpha,  # H
          obj = a$rss)

writeMat('nonstand_aa_Output.mat',matSamLat = t(s$matSamLat), matLatSam = t(s$matLatSam), obj = s$obj)
save(a, file = "bestModel.RData")