Introduction to Archetypal Package

Demetris T. Christopoulos

7/6/2019

Archetypal Analysis (AA)

Archetypal Analysis (AA) is different from Cluster Analysis (CA) because it focus on the extreme points that usually are closer to the Convex Hull (CH) and not inside the cloud of points as in CA or other centroid analyses.

Here we implement a view which is common in Econometrics. The usual data frame “df” is a matrix Y with dimension \(n \times d\), where n is the number or rows–observations and d is the number of variables or the dimension of the relevant Linear Space \(R^n\).

The output of our AA algorithm gives matrices A, B such that: \[ Y\sim\,A\,B\,Y \] or if we want to be more strict our \(kappas\times\,d\) matrix of archetypes \(B\,Y\) is such that next squared Frobenius norm is minimum \[ SSE=\|Y-A\,B\,Y \|^2 = minimum \] A (\(n\times\,kappas\)) and B (\(kappas\times\,n\) ) are row stochastic matrices. We also define the Variance Explained as next: \[ varexpl=\frac{SST-SSE}{SST} \] with SST the total sum of squares for elements of Y.

It is a suitable modification of PCHA algorithm, see [1], [2], which uses data frames without transposing them and has full control to all external and internal parameteres of it.

Compute AA for a data frame

Lets first create some 2D points that will certainly be a convex combination of three outer points:

library(archetypal)
p1=c(1,2);p2=c(3,5);p3=c(7,3) 
dp=rbind(p1,p2,p3);dp
##    [,1] [,2]
## p1    1    2
## p2    3    5
## p3    7    3
set.seed(9102)
pts=t(sapply(1:100, function(i,dp){
  cc=runif(3)
  cc=cc/sum(cc)
  colSums(dp*cc)
},dp))
df=data.frame(pts)
colnames(df)=c("x","y")
head(df)
##          x        y
## 1 3.642835 3.717916
## 2 3.581289 2.927728
## 3 2.723810 3.688769
## 4 3.128537 3.733146
## 5 5.123878 2.745874
## 6 3.719885 3.449477

Data frame dp is the three points of the outer triangle and df is our data set for AA, lets plot all:

The above mentioned data frame “df” can be loaded as:

# data("wd2")
# df=wd2

Since number of archetypes is kappas=3 due to the construction we run “archetypal” function with three archetypes:

aa=archetypal(df=df,kappas = 3,verbose = TRUE,rseed=9102,save_history = TRUE)
## Time for computing Projected Convex Hull was 0.01 secs
## Next projected convex hull initial solution will be used... 
##           x        y
## 34 5.687791 3.481611
## 62 1.961799 2.793497
## 5  5.123878 2.745874
##   
## Time for the 25 initial A updates was 0.17 secs 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##    Iteration |     VarExpl. |             SSE | |dSSE|/SSE |        muB |        muA |   Time(s)  |  nAup_nAdown |  nBup_nBdown 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##            1 |  0.994653094 | 1.347705806e+01 |   9.12e-01 |   1.55e+00 |   4.61e+00 |       0.12 |         10_2 |         10_2 
##            2 |  0.995609058 | 1.106751810e+01 |   2.18e-01 |   9.58e+00 |   7.14e+00 |       0.11 |         10_2 |         10_0 
##            3 |  0.995975063 | 1.014499058e+01 |   9.09e-02 |   1.48e+01 |   5.53e+00 |       0.12 |         10_3 |         10_2 
##            4 |  0.996104428 | 9.818920332e+00 |   3.32e-02 |   4.59e+01 |   4.28e+00 |       0.12 |         10_3 |         10_1 
##            5 |  0.996176923 | 9.636194502e+00 |   1.90e-02 |   3.55e+01 |   6.62e+00 |       0.12 |         10_2 |         10_3 
##            6 |  0.996236931 | 9.484943607e+00 |   1.59e-02 |   5.50e+01 |   5.13e+00 |       0.15 |         10_3 |         10_2 
##            7 |  0.996292095 | 9.345899589e+00 |   1.49e-02 |   4.26e+01 |   3.97e+00 |       0.13 |         10_3 |         10_3 
##            8 |  0.996349868 | 9.200281032e+00 |   1.58e-02 |   6.59e+01 |   6.14e+00 |       0.12 |         10_2 |         10_2 
##            9 |  0.996399028 | 9.076370793e+00 |   1.37e-02 |   5.10e+01 |   4.75e+00 |       0.12 |         10_3 |         10_3 
##           10 |  0.996454581 | 8.936348383e+00 |   1.57e-02 |   7.90e+01 |   7.36e+00 |       0.12 |         10_2 |         10_2 
##           11 |  0.996509124 | 8.798871505e+00 |   1.56e-02 |   1.22e+02 |   5.69e+00 |       0.12 |         10_3 |         10_2 
##           12 |  0.996575765 | 8.630898872e+00 |   1.95e-02 |   1.89e+02 |   4.41e+00 |       0.12 |         10_3 |         10_2 
##           13 |  0.996658886 | 8.421391410e+00 |   2.49e-02 |   1.46e+02 |   3.41e+00 |       0.12 |         10_3 |         10_3 
##           14 |  0.996745974 | 8.201881862e+00 |   2.68e-02 |   5.67e+01 |   2.64e+00 |       0.13 |         10_3 |         10_4 
##           15 |  0.996830638 | 7.988484299e+00 |   2.67e-02 |   8.77e+01 |   4.09e+00 |       0.12 |         10_2 |         10_2 
##           16 |  0.996903192 | 7.805608631e+00 |   2.34e-02 |   1.36e+02 |   3.16e+00 |       0.12 |         10_3 |         10_2 
##           17 |  0.996987535 | 7.593018618e+00 |   2.80e-02 |   1.05e+02 |   4.90e+00 |       0.12 |         10_2 |         10_3 
##           18 |  0.997092764 | 7.327787558e+00 |   3.62e-02 |   1.63e+02 |   3.79e+00 |       0.12 |         10_3 |         10_2 
##           19 |  0.997217140 | 7.014291695e+00 |   4.47e-02 |   1.26e+02 |   5.87e+00 |       0.12 |         10_2 |         10_3 
##           20 |  0.997360779 | 6.652245420e+00 |   5.44e-02 |   9.75e+01 |   4.54e+00 |       0.12 |         10_3 |         10_3 
##           21 |  0.997529773 | 6.226290374e+00 |   6.84e-02 |   3.77e+01 |   3.51e+00 |       0.13 |         10_3 |         10_4 
##           22 |  0.997781786 | 5.591083534e+00 |   1.14e-01 |   2.34e+02 |   5.44e+00 |       0.11 |         10_2 |         10_0 
##           23 |  0.998052835 | 4.907894187e+00 |   1.39e-01 |   1.81e+02 |   4.21e+00 |       0.13 |         10_3 |         10_3 
##           24 |  0.998337268 | 4.190972476e+00 |   1.71e-01 |   7.00e+01 |   6.52e+00 |       0.12 |         10_2 |         10_4 
##           25 |  0.998533807 | 3.695588871e+00 |   1.34e-01 |   6.77e+00 |   5.04e+00 |       0.13 |         10_3 |         10_6 
##           26 |  0.998722954 | 3.218836362e+00 |   1.48e-01 |   4.19e+01 |   3.90e+00 |       0.12 |         10_3 |         10_0 
##           27 |  0.998940627 | 2.670185932e+00 |   2.05e-01 |   3.24e+01 |   6.04e+00 |       0.12 |         10_2 |         10_3 
##           28 |  0.999115668 | 2.228987706e+00 |   1.98e-01 |   5.02e+01 |   4.68e+00 |       0.12 |         10_3 |         10_2 
##           29 |  0.999199655 | 2.017296581e+00 |   1.05e-01 |   9.71e+00 |   3.62e+00 |       0.13 |         10_3 |         10_5 
##           30 |  0.999278498 | 1.818570960e+00 |   1.09e-01 |   6.01e+01 |   5.60e+00 |       0.11 |         10_2 |         10_0 
##           31 |  0.999302789 | 1.757344443e+00 |   3.48e-02 |   4.65e+01 |   4.34e+00 |       0.13 |         10_3 |         10_3 
##           32 |  0.999306799 | 1.747236223e+00 |   5.79e-03 |   7.21e+01 |   3.36e+00 |       0.12 |         10_3 |         10_2 
##           33 |  0.999309631 | 1.740097295e+00 |   4.10e-03 |   1.12e+02 |   5.20e+00 |       0.12 |         10_2 |         10_2 
##           34 |  0.999312890 | 1.731884755e+00 |   4.74e-03 |   1.73e+02 |   4.02e+00 |       0.12 |         10_3 |         10_2 
##           35 |  0.999315462 | 1.725400065e+00 |   3.76e-03 |   1.34e+02 |   3.11e+00 |       0.12 |         10_3 |         10_3 
##           36 |  0.999316308 | 1.723268852e+00 |   1.24e-03 |   1.03e+02 |   4.82e+00 |       0.12 |         10_2 |         10_3 
##           37 |  0.999316888 | 1.721806612e+00 |   8.49e-04 |   1.60e+02 |   3.73e+00 |       0.12 |         10_3 |         10_2 
##           38 |  0.999317280 | 1.720817711e+00 |   5.75e-04 |   2.48e+02 |   5.77e+00 |       0.12 |         10_2 |         10_2 
##           39 |  0.999317566 | 1.720096700e+00 |   4.19e-04 |   3.84e+02 |   4.47e+00 |       0.12 |         10_3 |         10_2 
##           40 |  0.999317779 | 1.719562225e+00 |   3.11e-04 |   2.97e+02 |   3.46e+00 |       0.12 |         10_3 |         10_3 
##           41 |  0.999317948 | 1.719134135e+00 |   2.49e-04 |   4.60e+02 |   5.35e+00 |       0.12 |         10_2 |         10_2 
##           42 |  0.999318084 | 1.718793478e+00 |   1.98e-04 |   7.11e+02 |   4.14e+00 |       0.12 |         10_3 |         10_2 
##           43 |  0.999318191 | 1.718523157e+00 |   1.57e-04 |   5.51e+02 |   3.21e+00 |       0.12 |         10_3 |         10_3 
##           44 |  0.999318277 | 1.718304883e+00 |   1.27e-04 |   4.26e+02 |   4.96e+00 |       0.12 |         10_2 |         10_3 
##           45 |  0.999318348 | 1.718126847e+00 |   1.04e-04 |   3.30e+02 |   3.84e+00 |       0.13 |         10_3 |         10_3 
##           46 |  0.999318405 | 1.717983335e+00 |   8.35e-05 |   2.55e+02 |   2.97e+00 |       0.12 |         10_3 |         10_3 
##           47 |  0.999318448 | 1.717874514e+00 |   6.33e-05 |   3.95e+02 |   4.60e+00 |       0.12 |         10_2 |         10_2 
##           48 |  0.999318481 | 1.717792286e+00 |   4.79e-05 |   3.06e+02 |   3.56e+00 |       0.13 |         10_3 |         10_3 
##           49 |  0.999318505 | 1.717730137e+00 |   3.62e-05 |   4.73e+02 |   5.51e+00 |       0.12 |         10_2 |         10_2 
##           50 |  0.999318524 | 1.717683062e+00 |   2.74e-05 |   3.66e+02 |   4.27e+00 |       0.13 |         10_3 |         10_3 
##           51 |  0.999318538 | 1.717647246e+00 |   2.09e-05 |   5.67e+02 |   3.30e+00 |       0.12 |         10_3 |         10_2 
##           52 |  0.999318549 | 1.717620075e+00 |   1.58e-05 |   4.39e+02 |   5.11e+00 |       0.12 |         10_2 |         10_3 
##           53 |  0.999318557 | 1.717599464e+00 |   1.20e-05 |   3.40e+02 |   3.96e+00 |       0.12 |         10_3 |         10_3 
##           54 |  0.999318563 | 1.717583760e+00 |   9.14e-06 |   2.63e+02 |   6.12e+00 |       0.12 |         10_2 |         10_3 
##           55 |  0.999318568 | 1.717571796e+00 |   6.97e-06 |   4.07e+02 |   4.74e+00 |       0.12 |         10_3 |         10_2 
##           56 |  0.999318572 | 1.717562661e+00 |   5.32e-06 |   3.15e+02 |   7.34e+00 |       0.12 |         10_2 |         10_3 
##           57 |  0.999318575 | 1.717555734e+00 |   4.03e-06 |   4.88e+02 |   5.68e+00 |       0.12 |         10_3 |         10_2 
##           58 |  0.999318577 | 1.717550430e+00 |   3.09e-06 |   3.77e+02 |   4.39e+00 |       0.21 |         10_3 |         10_3 
##           59 |  0.999318578 | 1.717546370e+00 |   2.36e-06 |   5.84e+02 |   6.80e+00 |       0.11 |         10_2 |         10_2 
##           60 |  0.999318580 | 1.717543266e+00 |   1.81e-06 |   4.52e+02 |   5.27e+00 |       0.12 |         10_3 |         10_3 
##           61 |  0.999318580 | 1.717540901e+00 |   1.38e-06 |   3.50e+02 |   4.08e+00 |       0.12 |         10_3 |         10_3 
##           62 |  0.999318581 | 1.717539092e+00 |   1.05e-06 |   5.42e+02 |   6.31e+00 |       0.11 |         10_2 |         10_2 
##           63 |  0.999318582 | 1.717537742e+00 |   7.86e-07 |   4.19e+02 |   4.88e+00 |       0.12 |         10_3 |         10_3 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##   
##  BY =  
##             x        y
## [1,] 5.430757 3.146258
## [2,] 2.043435 2.710947
## [3,] 3.128401 4.781751
## 

What is the result? A list with names:

names(aa)
##  [1] "BY"              "A"               "B"              
##  [4] "SSE"             "varexpl"         "initialsolution"
##  [7] "freqstable"      "iterations"      "time"           
## [10] "converges"       "nAup"            "nAdown"         
## [13] "nBup"            "nBdown"          "run_results"

More specifically:

  1. BY is the archetypes as matrix, each row is an archetype

  2. A, B matrices, SSE and varexpl as they were defined in (AA)

  3. initialsolution gives the rows that were used as initial points in algorithm

  4. freqstable is a frequncy table for all candidate initial points found

  5. iterations are the number of main iterations done by algorithm

  6. time is the seconds ellapsed for the entire process

  7. converges is a TRUE/FALSE flag to inform us if convergence criteria were achieved before the maximum iterations (default=2000) reached

  8. nAup, nAdown, nBup, nBdown are for deep inspection of PCHA algorithm

  9. run_results is a list with “iterations” lists each one having next components:
    • SSE
    • varexpl
    • B
    • BY

The archetypes are indeed on the outer boundary, more precicely they form a principal convex hull of data points:

If you observe a little bit more the above Figure, then you’ll see that the inner triangle is approximately similar to the outer one, which is the true solution, although not present inside the data set.

Lets plot the convergence process for SSE and all iterations:

It seems that all the “hard work” has been done during the first 30 iterations.

But we can check the quality of our solution. Look how the final archetypes are precisely being created from data points and what are the relevant used weights for that task:

BB=aa$B
yy=check_Bmatrix(B=BB,print.details = TRUE)
## Archetype 1 is a mixture of only next rows with weights: 
##   
##        34         5 
## 0.5441943 0.4558057 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 2 is a mixture of only next rows with weights: 
##   
##        62        52 
## 0.7808615 0.2191385 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 3 is a mixture of only next rows with weights: 
##   
##         86         91 
## 0.90628955 0.09371045 
## Weights add to: 
## [1] 1
##   
## 
# yy$used_rows
# yy$used_weights

What is exactly the CH of our data set? We can use the “chull” function and find it since it is a 2D data set:

ch=chull(df)
ch
## [1]  5 26 52 62 43 91 86 89 34
df[ch,]
##           x        y
## 5  5.123878 2.745874
## 26 3.806534 2.546629
## 52 2.334330 2.416794
## 62 1.961799 2.793497
## 43 2.031007 3.513241
## 91 2.428351 4.022825
## 86 3.200786 4.860224
## 89 5.653335 3.618327
## 34 5.687791 3.481611

So our used rows in AA indeed belong to CH:

yy$used_rows
## [[1]]
## [1] 34  5
## 
## [[2]]
## [1] 62 52
## 
## [[3]]
## [1] 86 91
unlist(yy$used_rows)%in%ch
## [1] TRUE TRUE TRUE TRUE TRUE TRUE

Question Can we further decrease the computation time?

Answer Yes, if we use cleverly the B matrix

Lets plot the final used points:

Watch now that the final archetypes are points “somewhere” on the line segments connecting the final used points, with weights given by function “check_Bmatrix()”. From the relevant weights we observe that archetypes are closer to the first points of every list element, so it is reasonable to try as initial solution the vector of rows c(34,62,86), because they have the greatest weights:

aa2=archetypal(df=df,kappas = 3,initialrows =  c(34,62,86), verbose = TRUE,rseed=9102,save_history = TRUE)
## [1] 34 62 86
## The initial solution that will be used is 
##           x        y
## 34 5.687791 3.481611
## 62 1.961799 2.793497
## 86 3.200786 4.860224
##   
## Time for the 25 initial A updates was 0.16 secs 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##    Iteration |     VarExpl. |             SSE | |dSSE|/SSE |        muB |        muA |   Time(s)  |  nAup_nAdown |  nBup_nBdown 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##            1 |  0.998844044 | 2.913625251e+00 |   1.01e-01 |   6.19e+00 |   4.61e+00 |       0.12 |         10_2 |         10_0 
##            2 |  0.998937373 | 2.678386004e+00 |   8.78e-02 |   4.79e+00 |   3.57e+00 |       0.14 |         10_3 |         10_3 
##            3 |  0.999022646 | 2.463454357e+00 |   8.72e-02 |   1.48e+01 |   5.53e+00 |       0.13 |         10_2 |         10_1 
##            4 |  0.999079122 | 2.321104569e+00 |   6.13e-02 |   1.15e+01 |   4.28e+00 |       0.12 |         10_3 |         10_3 
##            5 |  0.999179373 | 2.068418440e+00 |   1.22e-01 |   7.11e+01 |   3.31e+00 |       0.11 |         10_3 |         10_0 
##            6 |  0.999230012 | 1.940781145e+00 |   6.58e-02 |   5.50e+01 |   5.13e+00 |       0.12 |         10_2 |         10_3 
##            7 |  0.999279601 | 1.815789254e+00 |   6.88e-02 |   3.41e+02 |   3.97e+00 |       0.11 |         10_3 |         10_0 
##            8 |  0.999298508 | 1.768135034e+00 |   2.70e-02 |   5.27e+02 |   3.07e+00 |       0.12 |         10_3 |         10_2 
##            9 |  0.999304992 | 1.751789949e+00 |   9.33e-03 |   4.08e+02 |   4.75e+00 |       0.12 |         10_2 |         10_3 
##           10 |  0.999308397 | 1.743209207e+00 |   4.92e-03 |   1.58e+02 |   3.68e+00 |       0.12 |         10_3 |         10_4 
##           11 |  0.999310639 | 1.737557661e+00 |   3.25e-03 |   2.45e+02 |   5.69e+00 |       0.12 |         10_2 |         10_2 
##           12 |  0.999312188 | 1.733652783e+00 |   2.25e-03 |   1.89e+02 |   4.41e+00 |       0.12 |         10_3 |         10_3 
##           13 |  0.999313370 | 1.730674835e+00 |   1.72e-03 |   2.93e+02 |   3.41e+00 |       0.12 |         10_3 |         10_2 
##           14 |  0.999314285 | 1.728366708e+00 |   1.34e-03 |   2.27e+02 |   5.28e+00 |       0.12 |         10_2 |         10_3 
##           15 |  0.999314992 | 1.726585923e+00 |   1.03e-03 |   1.75e+02 |   4.09e+00 |       0.12 |         10_3 |         10_3 
##           16 |  0.999315561 | 1.725152779e+00 |   8.31e-04 |   2.72e+02 |   3.16e+00 |       0.12 |         10_3 |         10_2 
##           17 |  0.999315994 | 1.724059984e+00 |   6.34e-04 |   4.20e+02 |   4.90e+00 |       0.11 |         10_2 |         10_2 
##           18 |  0.999316397 | 1.723043677e+00 |   5.90e-04 |   3.25e+02 |   3.79e+00 |       0.12 |         10_3 |         10_3 
##           19 |  0.999316756 | 1.722138599e+00 |   5.26e-04 |   5.04e+02 |   5.87e+00 |       0.12 |         10_2 |         10_2 
##           20 |  0.999317062 | 1.721367096e+00 |   4.48e-04 |   3.90e+02 |   4.54e+00 |       0.12 |         10_3 |         10_3 
##           21 |  0.999317371 | 1.720589077e+00 |   4.52e-04 |   6.04e+02 |   3.51e+00 |       0.12 |         10_3 |         10_2 
##           22 |  0.999317628 | 1.719942402e+00 |   3.76e-04 |   4.67e+02 |   5.44e+00 |       0.12 |         10_2 |         10_3 
##           23 |  0.999317860 | 1.719357154e+00 |   3.40e-04 |   7.23e+02 |   4.21e+00 |       0.12 |         10_3 |         10_2 
##           24 |  0.999318111 | 1.718724225e+00 |   3.68e-04 |   5.60e+02 |   6.52e+00 |       0.12 |         10_2 |         10_3 
##           25 |  0.999318232 | 1.718418540e+00 |   1.78e-04 |   4.33e+02 |   5.04e+00 |       0.12 |         10_3 |         10_3 
##           26 |  0.999318308 | 1.718227927e+00 |   1.11e-04 |   6.70e+02 |   3.90e+00 |       0.12 |         10_3 |         10_2 
##           27 |  0.999318371 | 1.718069665e+00 |   9.21e-05 |   5.19e+02 |   6.04e+00 |       0.12 |         10_2 |         10_3 
##           28 |  0.999318422 | 1.717939337e+00 |   7.59e-05 |   4.02e+02 |   4.68e+00 |       0.12 |         10_3 |         10_3 
##           29 |  0.999318462 | 1.717839100e+00 |   5.84e-05 |   3.11e+02 |   3.62e+00 |       0.12 |         10_3 |         10_3 
##           30 |  0.999318492 | 1.717764369e+00 |   4.35e-05 |   4.81e+02 |   5.60e+00 |       0.11 |         10_2 |         10_2 
##           31 |  0.999318514 | 1.717707986e+00 |   3.28e-05 |   3.72e+02 |   4.34e+00 |       0.12 |         10_3 |         10_3 
##           32 |  0.999318531 | 1.717665521e+00 |   2.47e-05 |   2.88e+02 |   6.71e+00 |       0.12 |         10_2 |         10_3 
##           33 |  0.999318544 | 1.717633583e+00 |   1.86e-05 |   4.46e+02 |   5.20e+00 |       0.12 |         10_3 |         10_2 
##           34 |  0.999318553 | 1.717609434e+00 |   1.41e-05 |   3.45e+02 |   4.02e+00 |       0.13 |         10_3 |         10_3 
##           35 |  0.999318561 | 1.717591244e+00 |   1.06e-05 |   5.34e+02 |   6.22e+00 |       0.12 |         10_2 |         10_2 
##           36 |  0.999318566 | 1.717577448e+00 |   8.03e-06 |   4.14e+02 |   4.82e+00 |       0.12 |         10_3 |         10_3 
##           37 |  0.999318570 | 1.717567005e+00 |   6.08e-06 |   6.40e+02 |   3.73e+00 |       0.12 |         10_3 |         10_2 
##           38 |  0.999318573 | 1.717558973e+00 |   4.68e-06 |   4.96e+02 |   5.77e+00 |       0.12 |         10_2 |         10_3 
##           39 |  0.999318576 | 1.717552844e+00 |   3.57e-06 |   3.84e+02 |   4.47e+00 |       0.12 |         10_3 |         10_3 
##           40 |  0.999318578 | 1.717548207e+00 |   2.70e-06 |   2.97e+02 |   6.91e+00 |       0.12 |         10_2 |         10_3 
##           41 |  0.999318579 | 1.717544660e+00 |   2.07e-06 |   4.60e+02 |   5.35e+00 |       0.12 |         10_3 |         10_2 
##           42 |  0.999318580 | 1.717541996e+00 |   1.55e-06 |   7.11e+02 |   4.14e+00 |       0.12 |         10_3 |         10_2 
##           43 |  0.999318581 | 1.717539937e+00 |   1.20e-06 |   5.51e+02 |   6.41e+00 |       0.12 |         10_2 |         10_3 
##           44 |  0.999318581 | 1.717538368e+00 |   9.13e-07 |   4.26e+02 |   4.96e+00 |       0.12 |         10_3 |         10_3 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##   
##  BY =  
##             x        y
## [1,] 5.430821 3.146342
## [2,] 2.043495 2.710886
## [3,] 3.128494 4.781853
## 
yy2=check_Bmatrix(aa2$B,print.details = TRUE)
## Archetype 1 is a mixture of only next rows with weights: 
##   
##        34         5 
## 0.5443082 0.4556918 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 2 is a mixture of only next rows with weights: 
##   
##     62     52 
## 0.7807 0.2193 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 3 is a mixture of only next rows with weights: 
##   
##         86         91 
## 0.90641072 0.09358928 
## Weights add to: 
## [1] 1
##   
## 

The same solution as before, but with 44 instead of 63 ierations!

Now we’ ll try a 3D example.

library(plot3D)
#
p1=c(3,0,0);p2=c(0,5,0);p3=c(3,5,7);p4=c(0,0,0);
dp=data.frame(rbind(p1,p2,p3,p4));dp=dp[chull(dp),];colnames(dp)=c("x","y","z")
set.seed(9102)
df=data.frame(t(sapply(1:100, function(i,dp){
  cc=runif(4)
  cc=cc/sum(cc)
  colSums(dp*cc)
},dp)))
colnames(df)=c("x","y","z")
scatter3D(x=dp$x,y=dp$y,z=dp$z,colvar=NULL,lwd = 2, d = 3,xlab='x',ylab='y',zlab='z',theta=120,phi=15,
          main = "Generators and Data Points", bty ="g",ticktype = "detailed",col='black',pch=10,cex=2.5)
points3D(x=df$x,y=df$y,z=df$z,col='blue',add=T,pch=19)

The above data frame (without the generating edges) can be loaded as:

# data("wd3")
# df=wd3

(Data frames are equal at least untill the 16th decimal point.)

We run “archetypal” function with kappas=4 (due to the construction procedure) and then we also check the final B matrix:

aa3=archetypal(df=df,kappas = 4,verbose = TRUE,rseed=9102,save_history = TRUE)
## Time for computing Projected Convex Hull was 0 secs
## Next projected convex hull initial solution will be used... 
##            x        y          z
## 61 2.2751335 3.993475 4.67136711
## 82 0.8598104 1.072867 0.06680034
## 64 2.0259680 0.923847 0.02359727
## 67 1.8788059 4.375142 4.34740317
##   
## Time for the 25 initial A updates was 0.16 secs 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##    Iteration |     VarExpl. |             SSE | |dSSE|/SSE |        muB |        muA |   Time(s)  |  nAup_nAdown |  nBup_nBdown 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##            1 |  0.986295732 | 1.925235941e+01 |   5.65e-01 |   1.55e+00 |   1.15e+00 |       0.12 |         10_2 |         10_2 
##            2 |  0.989115020 | 1.529169951e+01 |   2.59e-01 |   4.79e+00 |   1.79e+00 |       0.11 |         10_2 |         10_1 
##            3 |  0.990389790 | 1.350084634e+01 |   1.33e-01 |   7.42e+00 |   6.91e-01 |       0.12 |         10_4 |         10_2 
##            4 |  0.991364303 | 1.213180723e+01 |   1.13e-01 |   2.87e+00 |   1.07e+00 |       0.13 |         10_2 |         10_4 
##            5 |  0.992136917 | 1.104640460e+01 |   9.83e-02 |   1.78e+01 |   8.28e-01 |       0.11 |         10_3 |         10_0 
##            6 |  0.992767362 | 1.016072764e+01 |   8.72e-02 |   2.75e+01 |   1.28e+00 |       0.12 |         10_2 |         10_2 
##            7 |  0.993439834 | 9.216010540e+00 |   1.03e-01 |   1.06e+01 |   9.92e-01 |       0.13 |         10_3 |         10_4 
##            8 |  0.993924157 | 8.535612242e+00 |   7.97e-02 |   6.59e+01 |   7.68e-01 |       0.11 |         10_3 |         10_0 
##            9 |  0.994329244 | 7.966527035e+00 |   7.14e-02 |   5.10e+01 |   1.19e+00 |       0.12 |         10_2 |         10_3 
##           10 |  0.994698932 | 7.447173808e+00 |   6.97e-02 |   7.90e+01 |   9.20e-01 |       0.12 |         10_3 |         10_2 
##           11 |  0.995019266 | 6.997154965e+00 |   6.43e-02 |   7.64e+00 |   7.12e-01 |       0.13 |         10_3 |         10_6 
##           12 |  0.995327806 | 6.563703274e+00 |   6.60e-02 |   4.73e+01 |   1.10e+00 |       0.11 |         10_2 |         10_0 
##           13 |  0.995604349 | 6.175204028e+00 |   6.29e-02 |   7.32e+01 |   1.71e+00 |       0.12 |         10_2 |         10_2 
##           14 |  0.995841301 | 5.842322967e+00 |   5.70e-02 |   2.83e+01 |   1.32e+00 |       0.13 |         10_3 |         10_4 
##           15 |  0.996036127 | 5.568623748e+00 |   4.92e-02 |   4.39e+01 |   1.02e+00 |       0.12 |         10_3 |         10_2 
##           16 |  0.996126456 | 5.441724918e+00 |   2.33e-02 |   1.36e+02 |   7.91e-01 |       0.12 |         10_3 |         10_1 
##           17 |  0.996213008 | 5.320133060e+00 |   2.29e-02 |   1.05e+02 |   1.22e+00 |       0.12 |         10_2 |         10_3 
##           18 |  0.996300423 | 5.197328630e+00 |   2.36e-02 |   8.14e+01 |   9.47e-01 |       0.12 |         10_3 |         10_3 
##           19 |  0.996386967 | 5.075747722e+00 |   2.40e-02 |   1.26e+02 |   7.33e-01 |       0.12 |         10_3 |         10_2 
##           20 |  0.996488873 | 4.932586250e+00 |   2.90e-02 |   9.75e+01 |   1.14e+00 |       0.12 |         10_2 |         10_3 
##           21 |  0.996596072 | 4.781987533e+00 |   3.15e-02 |   1.51e+02 |   8.78e-01 |       0.12 |         10_3 |         10_2 
##           22 |  0.996688041 | 4.652785438e+00 |   2.78e-02 |   1.17e+02 |   1.36e+00 |       0.12 |         10_2 |         10_3 
##           23 |  0.996763946 | 4.546151642e+00 |   2.35e-02 |   9.04e+01 |   5.26e-01 |       0.12 |         10_4 |         10_3 
##           24 |  0.996831009 | 4.451937688e+00 |   2.12e-02 |   7.00e+01 |   8.15e-01 |       0.12 |         10_2 |         10_3 
##           25 |  0.996889750 | 4.369415949e+00 |   1.89e-02 |   5.41e+01 |   1.26e+00 |       0.12 |         10_2 |         10_3 
##           26 |  0.996941748 | 4.296367214e+00 |   1.70e-02 |   4.19e+01 |   9.76e-01 |       0.12 |         10_3 |         10_3 
##           27 |  0.996987696 | 4.231816585e+00 |   1.53e-02 |   6.49e+01 |   7.55e-01 |       0.12 |         10_3 |         10_2 
##           28 |  0.997031430 | 4.170377203e+00 |   1.47e-02 |   5.02e+01 |   1.17e+00 |       0.12 |         10_2 |         10_3 
##           29 |  0.997068224 | 4.118687390e+00 |   1.26e-02 |   3.89e+01 |   9.05e-01 |       0.13 |         10_3 |         10_3 
##           30 |  0.997102410 | 4.070661375e+00 |   1.18e-02 |   6.01e+01 |   7.00e-01 |       0.12 |         10_3 |         10_2 
##           31 |  0.997135453 | 4.024241618e+00 |   1.15e-02 |   4.65e+01 |   1.08e+00 |       0.12 |         10_2 |         10_3 
##           32 |  0.997164341 | 3.983657939e+00 |   1.02e-02 |   3.60e+01 |   8.39e-01 |       0.12 |         10_3 |         10_3 
##           33 |  0.997192803 | 3.943673636e+00 |   1.01e-02 |   1.12e+02 |   1.30e+00 |       0.11 |         10_2 |         10_1 
##           34 |  0.997220310 | 3.905030157e+00 |   9.90e-03 |   2.16e+01 |   1.01e+00 |       0.13 |         10_3 |         10_5 
##           35 |  0.997242453 | 3.873923126e+00 |   8.03e-03 |   1.34e+02 |   7.78e-01 |       0.11 |         10_3 |         10_0 
##           36 |  0.997259426 | 3.850078651e+00 |   6.19e-03 |   2.59e+01 |   1.20e+00 |       0.13 |         10_2 |         10_5 
##           37 |  0.997275557 | 3.827417134e+00 |   5.92e-03 |   1.60e+02 |   9.32e-01 |       0.11 |         10_3 |         10_0 
##           38 |  0.997289824 | 3.807374292e+00 |   5.26e-03 |   1.24e+02 |   1.44e+00 |       0.12 |         10_2 |         10_3 
##           39 |  0.997298310 | 3.795452686e+00 |   3.14e-03 |   1.92e+02 |   1.12e+00 |       0.12 |         10_3 |         10_2 
##           40 |  0.997305503 | 3.785347335e+00 |   2.67e-03 |   2.97e+02 |   8.64e-01 |       0.12 |         10_3 |         10_2 
##           41 |  0.997311858 | 3.776420676e+00 |   2.36e-03 |   5.74e+01 |   1.34e+00 |       0.13 |         10_2 |         10_5 
##           42 |  0.997318344 | 3.767308100e+00 |   2.42e-03 |   3.56e+02 |   1.04e+00 |       0.12 |         10_3 |         10_0 
##           43 |  0.997323812 | 3.759626246e+00 |   2.04e-03 |   1.38e+02 |   8.01e-01 |       0.13 |         10_3 |         10_4 
##           44 |  0.997328432 | 3.753136621e+00 |   1.73e-03 |   2.13e+02 |   1.24e+00 |       0.12 |         10_2 |         10_2 
##           45 |  0.997332795 | 3.747007066e+00 |   1.64e-03 |   1.65e+02 |   9.60e-01 |       0.12 |         10_3 |         10_3 
##           46 |  0.997336804 | 3.741375592e+00 |   1.51e-03 |   1.28e+02 |   7.43e-01 |       0.13 |         10_3 |         10_3 
##           47 |  0.997340254 | 3.736528764e+00 |   1.30e-03 |   1.98e+02 |   1.15e+00 |       0.12 |         10_2 |         10_2 
##           48 |  0.997343406 | 3.732099974e+00 |   1.19e-03 |   1.53e+02 |   8.90e-01 |       0.12 |         10_3 |         10_3 
##           49 |  0.997346215 | 3.728153975e+00 |   1.06e-03 |   1.18e+02 |   1.38e+00 |       0.12 |         10_2 |         10_3 
##           50 |  0.997348576 | 3.724836458e+00 |   8.91e-04 |   1.83e+02 |   5.33e-01 |       0.13 |         10_4 |         10_2 
##           51 |  0.997350778 | 3.721744111e+00 |   8.31e-04 |   1.42e+02 |   8.26e-01 |       0.21 |         10_2 |         10_3 
##           52 |  0.997352729 | 3.719002219e+00 |   7.37e-04 |   2.19e+02 |   1.28e+00 |       0.12 |         10_2 |         10_2 
##           53 |  0.997354533 | 3.716468658e+00 |   6.82e-04 |   1.70e+02 |   9.89e-01 |       0.13 |         10_3 |         10_3 
##           54 |  0.997356136 | 3.714216299e+00 |   6.06e-04 |   2.63e+02 |   1.53e+00 |       0.12 |         10_2 |         10_2 
##           55 |  0.997357559 | 3.712217453e+00 |   5.38e-04 |   2.04e+02 |   1.18e+00 |       0.12 |         10_3 |         10_3 
##           56 |  0.997358847 | 3.710408002e+00 |   4.88e-04 |   1.58e+02 |   1.83e+00 |       0.12 |         10_2 |         10_3 
##           57 |  0.997359988 | 3.708805570e+00 |   4.32e-04 |   1.22e+02 |   7.10e-01 |       0.13 |         10_4 |         10_3 
##           58 |  0.997360996 | 3.707389236e+00 |   3.82e-04 |   1.89e+02 |   1.10e+00 |       0.12 |         10_2 |         10_2 
##           59 |  0.997361909 | 3.706106133e+00 |   3.46e-04 |   1.46e+02 |   8.50e-01 |       0.12 |         10_3 |         10_3 
##           60 |  0.997362673 | 3.705032370e+00 |   2.90e-04 |   2.26e+02 |   1.32e+00 |       0.12 |         10_2 |         10_2 
##           61 |  0.997363403 | 3.704007510e+00 |   2.77e-04 |   3.50e+02 |   1.02e+00 |       0.12 |         10_3 |         10_2 
##           62 |  0.997364037 | 3.703117180e+00 |   2.40e-04 |   2.71e+02 |   7.89e-01 |       0.12 |         10_3 |         10_3 
##           63 |  0.997364579 | 3.702354937e+00 |   2.06e-04 |   2.10e+02 |   1.22e+00 |       0.12 |         10_2 |         10_3 
##           64 |  0.997365073 | 3.701661051e+00 |   1.87e-04 |   3.24e+02 |   9.45e-01 |       0.12 |         10_3 |         10_2 
##           65 |  0.997365522 | 3.701030943e+00 |   1.70e-04 |   2.51e+02 |   7.31e-01 |       0.12 |         10_3 |         10_3 
##           66 |  0.997365911 | 3.700483473e+00 |   1.48e-04 |   1.94e+02 |   1.13e+00 |       0.12 |         10_2 |         10_3 
##           67 |  0.997366259 | 3.699994620e+00 |   1.32e-04 |   3.01e+02 |   1.75e+00 |       0.12 |         10_2 |         10_2 
##           68 |  0.997366584 | 3.699538425e+00 |   1.23e-04 |   1.16e+02 |   6.78e-01 |       0.13 |         10_4 |         10_4 
##           69 |  0.997366861 | 3.699149868e+00 |   1.05e-04 |   1.80e+02 |   1.05e+00 |       0.12 |         10_2 |         10_2 
##           70 |  0.997367098 | 3.698815993e+00 |   9.03e-05 |   1.39e+02 |   1.62e+00 |       0.12 |         10_2 |         10_3 
##           71 |  0.997367309 | 3.698520852e+00 |   7.98e-05 |   2.16e+02 |   1.26e+00 |       0.12 |         10_3 |         10_2 
##           72 |  0.997367494 | 3.698260367e+00 |   7.04e-05 |   3.34e+02 |   9.73e-01 |       0.12 |         10_3 |         10_2 
##           73 |  0.997367657 | 3.698030731e+00 |   6.21e-05 |   1.29e+02 |   1.51e+00 |       0.12 |         10_2 |         10_4 
##           74 |  0.997367802 | 3.697827455e+00 |   5.50e-05 |   2.00e+02 |   1.17e+00 |       0.12 |         10_3 |         10_2 
##           75 |  0.997367931 | 3.697646846e+00 |   4.88e-05 |   1.55e+02 |   1.80e+00 |       0.12 |         10_2 |         10_3 
##           76 |  0.997368044 | 3.697487347e+00 |   4.31e-05 |   2.40e+02 |   6.98e-01 |       0.12 |         10_4 |         10_2 
##           77 |  0.997368144 | 3.697346585e+00 |   3.81e-05 |   1.86e+02 |   1.08e+00 |       0.12 |         10_2 |         10_3 
##           78 |  0.997368233 | 3.697222022e+00 |   3.37e-05 |   2.87e+02 |   8.37e-01 |       0.12 |         10_3 |         10_2 
##           79 |  0.997368311 | 3.697112153e+00 |   2.97e-05 |   2.22e+02 |   1.30e+00 |       0.12 |         10_2 |         10_3 
##           80 |  0.997368380 | 3.697014914e+00 |   2.63e-05 |   1.72e+02 |   1.00e+00 |       0.12 |         10_3 |         10_3 
##           81 |  0.997368442 | 3.696928716e+00 |   2.33e-05 |   1.33e+02 |   7.76e-01 |       0.12 |         10_3 |         10_3 
##           82 |  0.997368496 | 3.696852948e+00 |   2.05e-05 |   2.06e+02 |   1.20e+00 |       0.12 |         10_2 |         10_2 
##           83 |  0.997368544 | 3.696785512e+00 |   1.82e-05 |   1.60e+02 |   9.29e-01 |       0.12 |         10_3 |         10_3 
##           84 |  0.997368586 | 3.696725957e+00 |   1.61e-05 |   2.47e+02 |   7.19e-01 |       0.12 |         10_3 |         10_2 
##           85 |  0.997368624 | 3.696673449e+00 |   1.42e-05 |   1.91e+02 |   1.11e+00 |       0.12 |         10_2 |         10_3 
##           86 |  0.997368657 | 3.696626871e+00 |   1.26e-05 |   2.96e+02 |   8.62e-01 |       0.12 |         10_3 |         10_2 
##           87 |  0.997368686 | 3.696585659e+00 |   1.11e-05 |   2.29e+02 |   1.33e+00 |       0.12 |         10_2 |         10_3 
##           88 |  0.997368712 | 3.696549153e+00 |   9.88e-06 |   1.77e+02 |   5.16e-01 |       0.12 |         10_4 |         10_3 
##           89 |  0.997368735 | 3.696516821e+00 |   8.75e-06 |   2.74e+02 |   7.99e-01 |       0.12 |         10_2 |         10_2 
##           90 |  0.997368755 | 3.696488503e+00 |   7.66e-06 |   2.12e+02 |   6.18e-01 |       0.12 |         10_3 |         10_3 
##           91 |  0.997368773 | 3.696463456e+00 |   6.78e-06 |   1.64e+02 |   9.57e-01 |       0.12 |         10_2 |         10_3 
##           92 |  0.997368789 | 3.696441282e+00 |   6.00e-06 |   2.55e+02 |   7.41e-01 |       0.12 |         10_3 |         10_2 
##           93 |  0.997368803 | 3.696421619e+00 |   5.32e-06 |   1.97e+02 |   1.15e+00 |       0.12 |         10_2 |         10_3 
##           94 |  0.997368815 | 3.696404337e+00 |   4.68e-06 |   3.05e+02 |   8.88e-01 |       0.12 |         10_3 |         10_2 
##           95 |  0.997368826 | 3.696389004e+00 |   4.15e-06 |   2.36e+02 |   1.37e+00 |       0.12 |         10_2 |         10_3 
##           96 |  0.997368836 | 3.696375519e+00 |   3.65e-06 |   1.83e+02 |   1.06e+00 |       0.12 |         10_3 |         10_3 
##           97 |  0.997368844 | 3.696363592e+00 |   3.23e-06 |   2.83e+02 |   8.23e-01 |       0.12 |         10_3 |         10_2 
##           98 |  0.997368852 | 3.696353158e+00 |   2.82e-06 |   2.19e+02 |   1.27e+00 |       0.12 |         10_2 |         10_3 
##           99 |  0.997368858 | 3.696343766e+00 |   2.54e-06 |   3.39e+02 |   9.86e-01 |       0.12 |         10_3 |         10_2 
##          100 |  0.997368864 | 3.696335510e+00 |   2.23e-06 |   2.62e+02 |   7.63e-01 |       0.12 |         10_3 |         10_3 
##          101 |  0.997368869 | 3.696328248e+00 |   1.96e-06 |   2.03e+02 |   1.18e+00 |       0.12 |         10_2 |         10_3 
##          102 |  0.997368874 | 3.696321843e+00 |   1.73e-06 |   3.14e+02 |   9.14e-01 |       0.12 |         10_3 |         10_2 
##          103 |  0.997368878 | 3.696316068e+00 |   1.56e-06 |   2.43e+02 |   1.42e+00 |       0.14 |         10_2 |         10_3 
##          104 |  0.997368882 | 3.696311045e+00 |   1.36e-06 |   1.88e+02 |   1.10e+00 |       0.14 |         10_3 |         10_3 
##          105 |  0.997368885 | 3.696306649e+00 |   1.19e-06 |   2.91e+02 |   8.48e-01 |       0.14 |         10_3 |         10_2 
##          106 |  0.997368887 | 3.696302676e+00 |   1.07e-06 |   2.25e+02 |   1.31e+00 |       0.14 |         10_2 |         10_3 
##          107 |  0.997368890 | 3.696299203e+00 |   9.40e-07 |   3.49e+02 |   1.02e+00 |       0.12 |         10_3 |         10_2 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##   
##  BY =  
##              x         y         z
## [1,] 2.2751335 3.9934754 4.6713671
## [2,] 0.7122153 0.8875855 0.2730237
## [3,] 0.7056238 3.9337320 1.0005031
## [4,] 1.9953569 0.9697649 0.2951150
## 
yy3=check_Bmatrix(aa3$B)
## Archetype 1 is a mixture of only next rows with weights: 
##   
## 61 
##  1 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 2 is a mixture of only next rows with weights: 
##   
## 56 
##  1 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 3 is a mixture of only next rows with weights: 
##   
##        84         9 
## 0.5076314 0.4923686 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 4 is a mixture of only next rows with weights: 
##   
##        64        90 
## 0.8289871 0.1710129 
## Weights add to: 
## [1] 1
##   
## 

Well done, but can we work with less iterations? Lets choose the greatest weighted points from the finally used and then run “archetypal” with those “initialrows”:

irows=sapply(yy3$used_rows, function(x){x[1]});irows
## [1] 61 56 84 64
aa4=archetypal(df=df,kappas = 4,initialrows = irows,verbose = TRUE,rseed=9102,save_history = TRUE)
## [1] 61 56 84 64
## The initial solution that will be used is 
##            x         y          z
## 61 2.2751335 3.9934754 4.67136711
## 56 0.7122153 0.8875855 0.27302372
## 84 0.9195260 4.4026407 1.52208421
## 64 2.0259680 0.9238470 0.02359727
##   
## Time for the 25 initial A updates was 0.14 secs 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##    Iteration |     VarExpl. |             SSE | |dSSE|/SSE |        muB |        muA |   Time(s)  |  nAup_nAdown |  nBup_nBdown 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##            1 |  0.996837453 | 4.442884982e+00 |   1.50e-01 |   6.19e+00 |   1.15e+00 |       0.11 |         10_2 |         10_0 
##            2 |  0.997041543 | 4.156170136e+00 |   6.90e-02 |   3.83e+01 |   8.93e-01 |       0.11 |         10_3 |         10_0 
##            3 |  0.997149141 | 4.005011781e+00 |   3.77e-02 |   2.37e+02 |   1.38e+00 |       0.11 |         10_2 |         10_0 
##            4 |  0.997215040 | 3.912434881e+00 |   2.37e-02 |   9.19e+01 |   1.07e+00 |       0.12 |         10_3 |         10_4 
##            5 |  0.997250373 | 3.862796374e+00 |   1.29e-02 |   1.42e+02 |   8.28e-01 |       0.12 |         10_3 |         10_2 
##            6 |  0.997273465 | 3.830356100e+00 |   8.47e-03 |   2.20e+02 |   6.41e-01 |       0.12 |         10_3 |         10_2 
##            7 |  0.997290010 | 3.807112649e+00 |   6.11e-03 |   1.70e+02 |   9.92e-01 |       0.12 |         10_2 |         10_3 
##            8 |  0.997302650 | 3.789356563e+00 |   4.69e-03 |   2.64e+02 |   7.68e-01 |       0.12 |         10_3 |         10_2 
##            9 |  0.997312563 | 3.775430350e+00 |   3.69e-03 |   2.04e+02 |   5.94e-01 |       0.12 |         10_3 |         10_3 
##           10 |  0.997320595 | 3.764146663e+00 |   3.00e-03 |   1.58e+02 |   9.20e-01 |       0.12 |         10_2 |         10_3 
##           11 |  0.997327226 | 3.754830012e+00 |   2.48e-03 |   1.22e+02 |   1.42e+00 |       0.12 |         10_2 |         10_3 
##           12 |  0.997332723 | 3.747107836e+00 |   2.06e-03 |   1.89e+02 |   1.10e+00 |       0.12 |         10_3 |         10_2 
##           13 |  0.997337411 | 3.740521741e+00 |   1.76e-03 |   1.46e+02 |   8.53e-01 |       0.12 |         10_3 |         10_3 
##           14 |  0.997341358 | 3.734977221e+00 |   1.48e-03 |   2.27e+02 |   1.32e+00 |       0.12 |         10_2 |         10_2 
##           15 |  0.997344775 | 3.730176696e+00 |   1.29e-03 |   1.75e+02 |   1.02e+00 |       0.12 |         10_3 |         10_3 
##           16 |  0.997347707 | 3.726057510e+00 |   1.11e-03 |   2.72e+02 |   7.91e-01 |       0.12 |         10_3 |         10_2 
##           17 |  0.997350230 | 3.722514177e+00 |   9.52e-04 |   2.10e+02 |   1.22e+00 |       0.12 |         10_2 |         10_3 
##           18 |  0.997352439 | 3.719409867e+00 |   8.35e-04 |   1.63e+02 |   9.47e-01 |       0.12 |         10_3 |         10_3 
##           19 |  0.997354360 | 3.716711295e+00 |   7.26e-04 |   1.26e+02 |   7.33e-01 |       0.12 |         10_3 |         10_3 
##           20 |  0.997356037 | 3.714354995e+00 |   6.34e-04 |   1.95e+02 |   1.14e+00 |       0.12 |         10_2 |         10_2 
##           21 |  0.997357522 | 3.712268968e+00 |   5.62e-04 |   1.51e+02 |   8.78e-01 |       0.12 |         10_3 |         10_3 
##           22 |  0.997358830 | 3.710432339e+00 |   4.95e-04 |   2.34e+02 |   1.36e+00 |       0.12 |         10_2 |         10_2 
##           23 |  0.997359984 | 3.708810413e+00 |   4.37e-04 |   1.81e+02 |   1.05e+00 |       0.12 |         10_3 |         10_3 
##           24 |  0.997361001 | 3.707381391e+00 |   3.85e-04 |   2.80e+02 |   8.15e-01 |       0.12 |         10_3 |         10_2 
##           25 |  0.997361902 | 3.706115961e+00 |   3.41e-04 |   2.17e+02 |   1.26e+00 |       0.12 |         10_2 |         10_3 
##           26 |  0.997362698 | 3.704998326e+00 |   3.02e-04 |   1.68e+02 |   9.76e-01 |       0.12 |         10_3 |         10_3 
##           27 |  0.997363406 | 3.704003140e+00 |   2.69e-04 |   1.30e+02 |   7.55e-01 |       0.12 |         10_3 |         10_3 
##           28 |  0.997364032 | 3.703123923e+00 |   2.37e-04 |   2.01e+02 |   1.17e+00 |       0.12 |         10_2 |         10_2 
##           29 |  0.997364587 | 3.702344848e+00 |   2.10e-04 |   1.55e+02 |   9.05e-01 |       0.12 |         10_3 |         10_3 
##           30 |  0.997365079 | 3.701653624e+00 |   1.87e-04 |   2.41e+02 |   1.40e+00 |       0.12 |         10_2 |         10_2 
##           31 |  0.997365516 | 3.701039431e+00 |   1.66e-04 |   1.86e+02 |   1.08e+00 |       0.12 |         10_3 |         10_3 
##           32 |  0.997365903 | 3.700495382e+00 |   1.47e-04 |   1.44e+02 |   8.39e-01 |       0.12 |         10_3 |         10_3 
##           33 |  0.997366246 | 3.700013974e+00 |   1.30e-04 |   2.23e+02 |   1.30e+00 |       0.12 |         10_2 |         10_2 
##           34 |  0.997366550 | 3.699585957e+00 |   1.16e-04 |   1.73e+02 |   1.01e+00 |       0.12 |         10_3 |         10_3 
##           35 |  0.997366820 | 3.699206645e+00 |   1.03e-04 |   1.34e+02 |   7.78e-01 |       0.12 |         10_3 |         10_3 
##           36 |  0.997367058 | 3.698872274e+00 |   9.04e-05 |   2.07e+02 |   1.20e+00 |       0.12 |         10_2 |         10_2 
##           37 |  0.997367271 | 3.698573555e+00 |   8.08e-05 |   1.60e+02 |   9.32e-01 |       0.12 |         10_3 |         10_3 
##           38 |  0.997367459 | 3.698309699e+00 |   7.13e-05 |   2.48e+02 |   7.21e-01 |       0.12 |         10_3 |         10_2 
##           39 |  0.997367625 | 3.698076510e+00 |   6.31e-05 |   1.92e+02 |   1.12e+00 |       0.12 |         10_2 |         10_3 
##           40 |  0.997367772 | 3.697869288e+00 |   5.60e-05 |   2.97e+02 |   8.64e-01 |       0.12 |         10_3 |         10_2 
##           41 |  0.997367903 | 3.697685546e+00 |   4.97e-05 |   2.30e+02 |   1.34e+00 |       0.12 |         10_2 |         10_3 
##           42 |  0.997368019 | 3.697522717e+00 |   4.40e-05 |   1.78e+02 |   1.04e+00 |       0.12 |         10_3 |         10_3 
##           43 |  0.997368122 | 3.697378354e+00 |   3.90e-05 |   2.75e+02 |   8.01e-01 |       0.12 |         10_3 |         10_2 
##           44 |  0.997368213 | 3.697250735e+00 |   3.45e-05 |   2.13e+02 |   6.20e-01 |       0.12 |         10_3 |         10_3 
##           45 |  0.997368293 | 3.697137799e+00 |   3.05e-05 |   1.65e+02 |   9.60e-01 |       0.12 |         10_2 |         10_3 
##           46 |  0.997368364 | 3.697037981e+00 |   2.70e-05 |   2.55e+02 |   7.43e-01 |       0.12 |         10_3 |         10_2 
##           47 |  0.997368427 | 3.696949254e+00 |   2.40e-05 |   1.98e+02 |   5.75e-01 |       0.12 |         10_3 |         10_3 
##           48 |  0.997368483 | 3.696871016e+00 |   2.12e-05 |   1.53e+02 |   8.90e-01 |       0.12 |         10_2 |         10_3 
##           49 |  0.997368532 | 3.696801551e+00 |   1.88e-05 |   2.37e+02 |   1.38e+00 |       0.12 |         10_2 |         10_2 
##           50 |  0.997368576 | 3.696740148e+00 |   1.66e-05 |   1.83e+02 |   1.07e+00 |       0.12 |         10_3 |         10_3 
##           51 |  0.997368615 | 3.696685942e+00 |   1.47e-05 |   1.42e+02 |   8.26e-01 |       0.12 |         10_3 |         10_3 
##           52 |  0.997368649 | 3.696638129e+00 |   1.29e-05 |   2.19e+02 |   1.28e+00 |       0.12 |         10_2 |         10_2 
##           53 |  0.997368679 | 3.696595862e+00 |   1.14e-05 |   3.40e+02 |   9.89e-01 |       0.12 |         10_3 |         10_2 
##           54 |  0.997368706 | 3.696558295e+00 |   1.02e-05 |   1.31e+02 |   7.65e-01 |       0.12 |         10_3 |         10_4 
##           55 |  0.997368729 | 3.696525248e+00 |   8.94e-06 |   2.04e+02 |   1.18e+00 |       0.12 |         10_2 |         10_2 
##           56 |  0.997368750 | 3.696495908e+00 |   7.94e-06 |   1.58e+02 |   9.17e-01 |       0.12 |         10_3 |         10_3 
##           57 |  0.997368768 | 3.696469852e+00 |   7.05e-06 |   2.44e+02 |   7.10e-01 |       0.12 |         10_3 |         10_2 
##           58 |  0.997368785 | 3.696446950e+00 |   6.20e-06 |   1.89e+02 |   1.10e+00 |       0.12 |         10_2 |         10_3 
##           59 |  0.997368799 | 3.696426616e+00 |   5.50e-06 |   2.92e+02 |   8.50e-01 |       0.12 |         10_3 |         10_2 
##           60 |  0.997368812 | 3.696408628e+00 |   4.87e-06 |   2.26e+02 |   1.32e+00 |       0.12 |         10_2 |         10_3 
##           61 |  0.997368823 | 3.696392852e+00 |   4.27e-06 |   1.75e+02 |   1.02e+00 |       0.12 |         10_3 |         10_3 
##           62 |  0.997368833 | 3.696378827e+00 |   3.79e-06 |   1.35e+02 |   1.58e+00 |       0.12 |         10_2 |         10_3 
##           63 |  0.997368842 | 3.696366596e+00 |   3.31e-06 |   2.10e+02 |   1.22e+00 |       0.12 |         10_3 |         10_2 
##           64 |  0.997368850 | 3.696355602e+00 |   2.97e-06 |   1.62e+02 |   9.45e-01 |       0.12 |         10_3 |         10_3 
##           65 |  0.997368857 | 3.696345892e+00 |   2.63e-06 |   2.51e+02 |   7.31e-01 |       0.12 |         10_3 |         10_2 
##           66 |  0.997368863 | 3.696337309e+00 |   2.32e-06 |   1.94e+02 |   1.13e+00 |       0.13 |         10_2 |         10_3 
##           67 |  0.997368868 | 3.696329760e+00 |   2.04e-06 |   3.01e+02 |   8.76e-01 |       0.11 |         10_3 |         10_2 
##           68 |  0.997368873 | 3.696323083e+00 |   1.81e-06 |   2.33e+02 |   1.36e+00 |       0.12 |         10_2 |         10_3 
##           69 |  0.997368877 | 3.696317259e+00 |   1.58e-06 |   1.80e+02 |   1.05e+00 |       0.12 |         10_3 |         10_3 
##           70 |  0.997368881 | 3.696312049e+00 |   1.41e-06 |   2.79e+02 |   8.12e-01 |       0.12 |         10_3 |         10_2 
##           71 |  0.997368884 | 3.696307468e+00 |   1.24e-06 |   2.16e+02 |   1.26e+00 |       0.12 |         10_2 |         10_3 
##           72 |  0.997368887 | 3.696303396e+00 |   1.10e-06 |   1.67e+02 |   9.73e-01 |       0.12 |         10_3 |         10_3 
##           73 |  0.997368890 | 3.696299805e+00 |   9.71e-07 |   2.59e+02 |   1.51e+00 |       0.11 |         10_2 |         10_2 
## |------------|--------------|-----------------|------------|------------|------------|------------|--------------|-------------| 
##   
##  BY =  
##              x         y         z
## [1,] 2.2751335 3.9934754 4.6713671
## [2,] 0.7122153 0.8875855 0.2730237
## [3,] 0.7056483 3.9337857 1.0005628
## [4,] 1.9953587 0.9697620 0.2950984
## 
yy4=check_Bmatrix(aa4$B)
## Archetype 1 is a mixture of only next rows with weights: 
##   
## 61 
##  1 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 2 is a mixture of only next rows with weights: 
##   
## 56 
##  1 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 3 is a mixture of only next rows with weights: 
##   
##        84         9 
## 0.5076878 0.4923122 
## Weights add to: 
## [1] 1
##   
##   
## Archetype 4 is a mixture of only next rows with weights: 
##   
##        64        90 
## 0.8289975 0.1710025 
## Weights add to: 
## [1] 1
##   
## 

Yes, we obtained a reduction of \(33\%\) in iterations with exactly the same results and SSE, varexpl.

Now it is time to plot all the above results, like the 2D demo case.

There exist two archetypes as exact CH vertices, one lies closer to a CH vertex and another one is at the middle of the line segment connecting two CH other vertices.

Additionally we observe that archetypes are close to the “invisible” generators of the data set (circled cross, not included in data frame).

Searching for the most efficient initial points

Extensive work on AA has shown next strong results:

  1. if initial solution is far from convex hull (CH) vertices, then probably AA will stuck
  2. not all CH vertices are suitable as candidate initial points, but only the outmost of them
  3. if we choose the finally used points with greatest weights, then computation is faster

In order to take into account the above empirical results, we have developed five functions and now we run them and check if their outputs are close to CH vertices for the 3D data frame we studied just before. Of course we need the CH vertices of new 3D data frame. Now we are going to use “convhulln” from package “geometry” for computing the CH vertices:

ch=unique(do.call(c,as.list(geometry::convhulln(df,'Fx'))))
ch
##  [1] 90 56 65 67 24  4  9 61 96 64 89 82 23 84 29 10 33

Projected Convex Hull

This is a method that can be used for all type of data frames, despite the number of variables \(d\). That is the reason for being the default option in “method” used for finding initial solution.

yy1=find_outmost_projected_convexhull_points(df,kappas=4)
yy1$outmost
## [1] 61 82 64 67
yy1$outmostall
## [1] 61 82 64 67
yy1$outmostall%in%ch
## [1] TRUE TRUE TRUE TRUE

Convex Hull

This method actually can be used for data frames with \(d\leq\,6\) variables, see http://www.qhull.org But if it can be used, it gives the best results due to the PCHA theory, see [1] for details.

yy2=find_outmost_convexhull_points(df,kappas=4)
yy2$outmost
## [1] 61 64 82 67
yy2$outmostall
## [1] 61 64 82 67
yy2$outmostall%in%ch
## [1] TRUE TRUE TRUE TRUE

Partitioned Convex Hull

This method is an approximation of CH when number of variables are \(d>6\). It creates partitions of mutually exclusive points, then it computes CH for each set and makes the union of vertices. Finally it computes the CH of that union, if it is feasible. (We avoid running because it uses parallel computing. Please check your machine and then run it.)

# yy3=find_outmost_partitioned_convexhull_points(df,kappas=4,nworkers = 10)
# yy3$outmost
# yy3$outmostall
# yy3$outmostall%in%ch
# 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10 .   
# Time difference of 2.769091 secs
# [1] 84  3
# [1] 61 64 82 67
# [1] 61 64 82 67
# [1] TRUE TRUE TRUE TRUE

Furthest Sum

This is the default method used in PCHA. Here we just apply it many times and then find the unique points. The most frequent ones are used as initial solution. (We don’t run for same reasons as Partitioned Convex Hull).

# yy4=find_furthestsum_points(df, kappas=4, nfurthest = 100, nworkers = 10,sortrows = TRUE)
# yy4$outmost
# yy4$outmostall
# yy4$outmostall%in%ch
# [1] 56 61 64 67
# [1] 56 61 64 67
# [1] TRUE TRUE TRUE TRUE

Outmost

This method is the most naive one, but also the most simple. Keep in mind that for a data frame with dimensions \(n\times\,d\) you’ll need \(\frac{8\,n^2}{2^{30}}=\frac{n^2}{2^{27}}\) GB RAM for numeric entries, so use it with caution!

yy5=find_outmost_points(df,kappas=4)
yy5$outmost
## [1] 61 64 82 56
yy5$outmostall
## [1] 61 64 82 56 67
yy5$outmostall%in%ch
## [1] TRUE TRUE TRUE TRUE TRUE

From our results we observe that Projected and Partitioned Convex Hull methods gave the same results as the Convex Hull.

This is extremely useful because, as we can see from trials or if we just read http://www.qhull.org , computing CH is not a feasible process when number of variables increases. Actually only if \(d\leq\,6\) it is meaningful to directly compute it.

Please send your comments, suggestions or bugs found to dchristop@econ.uoa.gr

References

[1] M Morup and LK Hansen, “Archetypal analysis for machine learning and data mining”, Neurocomputing (Elsevier, 2012). https://doi.org/10.1016/j.neucom.2011.06.033.

[2] Source: http://www.mortenmorup.dk/index_files/Page327.htm , last accessed 2019-06-07