Getting Started with NNS: Clustering and Regression

Fred Viole

library(NNS)
library(data.table)
require(knitr)
require(rgl)
require(meboot)
require(tdigest)
require(dtw)

Clustering and Regression

Below are some examples demonstrating unsupervised learning with NNS clustering and nonlinear regression using the resulting clusters. As always, for a more thorough description and definition, please view the References.

NNS Partitioning NNS.part

NNS.part is both a partitional and hierarchical clustering method. NNS iteratively partitions the joint distribution into partial moment quadrants, and then assigns a quadrant identification (1:4) at each partition.

NNS.part returns a data.table of observations along with their final quadrant identification. It also returns the regression points, which are the quadrant means used in NNS.reg.

X-only Partitioning

NNS.part offers a partitioning based on \(x\) values only NNS.part(x, y, type = "XONLY", ...), using the entire bandwidth in its regression point derivation, and shares the same limit condition as partitioning via both \(x\) and \(y\) values.

Note the partition identifications are limited to 1’s and 2’s (left and right of the partition respectively), not the 4 values per the \(x\) and \(y\) partitioning.

## $order
## [1] 4
## 
## $dt
##          x         y quadrant prior.quadrant
##   1: -5.00 -125.0000    q1111           q111
##   2: -4.95 -121.2874    q1111           q111
##   3: -4.90 -117.6490    q1111           q111
##   4: -4.85 -114.0841    q1111           q111
##   5: -4.80 -110.5920    q1111           q111
##  ---                                        
## 197:  4.80  110.5920    q2222           q222
## 198:  4.85  114.0841    q2222           q222
## 199:  4.90  117.6490    q2222           q222
## 200:  4.95  121.2874    q2222           q222
## 201:  5.00  125.0000    q2222           q222
## 
## $regression.points
##    quadrant          x           y
## 1:     q111 -4.3742412 -79.8807307
## 2:     q112 -3.0992681 -28.0828202
## 3:     q121 -1.8507319  -5.8599732
## 4:     q122 -0.5992681  -0.2594580
## 5:     q211  0.6507319   0.3130212
## 6:     q212  1.9007319   6.3553668
## 7:     q221  3.1507319  29.4685900
## 8:     q222  4.3992681  81.4792796

Clusters Used in Regression

The right column of plots shows the corresponding regression for the order of NNS partitioning.

NNS Regression NNS.reg

NNS.reg can fit any \(f(x)\), for both uni- and multivariate cases. NNS.reg returns a self-evident list of values provided below.

Univariate:

## $R2
## [1] 0.9999981
## 
## $SE
## [1] 0.0673289
## 
## $Prediction.Accuracy
## NULL
## 
## $equation
## NULL
## 
## $x.star
## NULL
## 
## $derivative
##      Coefficient X.Lower.Range X.Upper.Range
##  1: 71.742978330    -5.0000000    -4.9248866
##  2: 77.427903332    -4.9248866    -4.8497732
##  3: 65.641358760    -4.8497732    -4.5248036
##  4: 57.543612596    -4.5248036    -4.2251964
##  5: 49.686627234    -4.2251964    -3.9002268
##  6: 41.553459958    -3.9002268    -3.5502268
##  7: 34.175993685    -3.5502268    -3.2251964
##  8: 28.437319594    -3.2251964    -2.9251964
##  9: 23.062087442    -2.9251964    -2.6248036
## 10: 18.360793901    -2.6248036    -2.3251964
## 11: 14.178297291    -2.3251964    -2.0248036
## 12: 10.456886835    -2.0248036    -1.7002268
## 13:  6.977300577    -1.7002268    -1.3748036
## 14:  4.437645123    -1.3748036    -1.0497732
## 15:  2.320807548    -1.0497732    -0.7251964
## 16:  0.955558000    -0.7251964    -0.4251964
## 17:  0.202482468    -0.4251964    -0.1248036
## 18: -0.006244642    -0.1248036     0.1997732
## 19:  0.373496837     0.1997732     0.5251964
## 20:  1.344279645     0.5251964     0.8251964
## 21:  2.818450215     0.8251964     1.1251964
## 22:  4.907561876     1.1251964     1.4497732
## 23:  7.838461859     1.4497732     1.7751964
## 24: 11.096085720     1.7751964     2.0751964
## 25: 14.822923503     2.0751964     2.3751964
## 26: 19.163086925     2.3751964     2.6997732
## 27: 24.709882618     2.6997732     3.0251964
## 28: 30.214647912     3.0251964     3.3248036
## 29: 36.217817567     3.3248036     3.6248036
## 30: 42.707110267     3.6248036     3.9251964
## 31: 49.875177674     3.9251964     4.2251964
## 32: 57.363511771     4.2251964     4.5502268
## 33: 67.056773376     4.5502268     4.8751964
## 34: 76.431256478     4.8751964     4.9375982
## 35: 72.660139857     4.9375982     5.0000000
##      Coefficient X.Lower.Range X.Upper.Range
## 
## $Point.est
## NULL
## 
## $regression.points
##              x             y
##  1: -5.0000000 -1.250000e+02
##  2: -4.9248866 -1.196111e+02
##  3: -4.8497732 -1.137953e+02
##  4: -4.5248036 -9.246383e+01
##  5: -4.2251964 -7.522334e+01
##  6: -3.9002268 -5.907670e+01
##  7: -3.5502268 -4.453298e+01
##  8: -3.2251964 -3.342475e+01
##  9: -2.9251964 -2.489355e+01
## 10: -2.6248036 -1.796587e+01
## 11: -2.3251964 -1.246484e+01
## 12: -2.0248036 -8.205784e+00
## 13: -1.7002268 -4.811721e+00
## 14: -1.3748036 -2.541146e+00
## 15: -1.0497732 -1.098776e+00
## 16: -0.7251964 -3.454957e-01
## 17: -0.4251964 -5.882825e-02
## 18: -0.1248036  1.996003e-03
## 19:  0.1997732 -3.086304e-05
## 20:  0.5251964  1.215136e-01
## 21:  0.8251964  5.247975e-01
## 22:  1.1251964  1.370333e+00
## 23:  1.4497732  2.963214e+00
## 24:  1.7751964  5.514030e+00
## 25:  2.0751964  8.842856e+00
## 26:  2.3751964  1.328973e+01
## 27:  2.6997732  1.950963e+01
## 28:  3.0251964  2.755080e+01
## 29:  3.3248036  3.660332e+01
## 30:  3.6248036  4.746867e+01
## 31:  3.9251964  6.029757e+01
## 32:  4.2251964  7.526013e+01
## 33:  4.5502268  9.390501e+01
## 34:  4.8751964  1.156964e+02
## 35:  4.9375982  1.204659e+02
## 36:  5.0000000  1.250000e+02
##              x             y
## 
## $Fitted.xy
##          x         y     y.hat  NNS.ID gradient   residuals
##   1: -5.00 -125.0000 -125.0000 q111111 71.74298  0.00000000
##   2: -4.95 -121.2874 -121.4129 q111111 71.74298 -0.12547608
##   3: -4.90 -117.6490 -117.6842 q111111 77.42790 -0.03522358
##   4: -4.85 -114.0841 -113.8128 q111111 77.42790  0.27129658
##   5: -4.80 -110.5920 -110.5281 q111112 65.64136  0.06391217
##  ---                                                       
## 197:  4.80  110.5920  110.6540 q222221 67.05677  0.06200021
## 198:  4.85  114.0841  114.0068 q222221 67.05677 -0.07728613
## 199:  4.90  117.6490  117.5922 q222222 76.43126 -0.05680110
## 200:  4.95  121.2874  121.3670 q222222 72.66014  0.07961801
## 201:  5.00  125.0000  125.0000 q222222 72.66014  0.00000000

Multivariate:

Multivariate regressions return a plot of \(y\) and \(\hat{y}\), as well as the regression points ($RPM) and partitions ($rhs.partitions) for each regressor.

## $R2
## [1] 1
## 
## $rhs.partitions
##         Var1 Var2
##     1: -5.00   -5
##     2: -4.95   -5
##     3: -4.90   -5
##     4: -4.85   -5
##     5: -4.80   -5
##    ---           
## 40397:  4.80    5
## 40398:  4.85    5
## 40399:  4.90    5
## 40400:  4.95    5
## 40401:  5.00    5
## 
## $RPM
##        Var1  Var2         y.hat
##     1: -4.8 -4.80 -7.105427e-15
##     2: -4.8 -2.55 -8.726063e+01
##     3: -4.8 -2.50 -8.806700e+01
##     4: -4.8 -2.45 -8.883587e+01
##     5: -4.8 -2.40 -8.956800e+01
##    ---                         
## 40397: -2.6 -2.80  3.776000e+00
## 40398: -2.6 -2.75  2.770875e+00
## 40399: -2.6 -2.70  1.807000e+00
## 40400: -2.6 -2.65  8.836250e-01
## 40401: -2.6 -2.60  1.776357e-15
## 
## $Point.est
## NULL
## 
## $Fitted.xy
##         Var1 Var2          y      y.hat      NNS.ID residuals
##     1: -5.00   -5   0.000000   0.000000     201.201         0
##     2: -4.95   -5   3.562625   3.562625     402.201         0
##     3: -4.90   -5   7.051000   7.051000     603.201         0
##     4: -4.85   -5  10.465875  10.465875     804.201         0
##     5: -4.80   -5  13.808000  13.808000    1005.201         0
##    ---                                                       
## 40397:  4.80    5 -13.808000 -13.808000 39597.40401         0
## 40398:  4.85    5 -10.465875 -10.465875 39798.40401         0
## 40399:  4.90    5  -7.051000  -7.051000 39999.40401         0
## 40400:  4.95    5  -3.562625  -3.562625 40200.40401         0
## 40401:  5.00    5   0.000000   0.000000 40401.40401         0

Inter/Extrapolation

NNS.reg can inter- or extrapolate any point of interest. The NNS.reg(x, y, point.est = ...) parameter permits any sized data of similar dimensions to \(x\) and called specifically with $Point.est.

NNS Dimension Reduction Regression

NNS.reg also provides a dimension reduction regression by including a parameter NNS.reg(x, y, dim.red.method = "cor", ...). Reducing all regressors to a single dimension using the returned equation $equation.

##        Variable Coefficient
## 1: Sepal.Length   0.7980781
## 2:  Sepal.Width  -0.4402896
## 3: Petal.Length   0.9354305
## 4:  Petal.Width   0.9381792
## 5:  DENOMINATOR   4.0000000

Thus, our model for this regression would be: \[Species = \frac{0.798*Sepal.Length -0.44*Sepal.Width +0.935*Petal.Length +0.938*Petal.Width}{4} \]

Threshold

NNS.reg(x, y, dim.red.method = "cor", threshold = ...) offers a method of reducing regressors further by controlling the absolute value of required correlation.

##        Variable Coefficient
## 1: Sepal.Length   0.7980781
## 2:  Sepal.Width   0.0000000
## 3: Petal.Length   0.9354305
## 4:  Petal.Width   0.9381792
## 5:  DENOMINATOR   3.0000000

Thus, our model for this further reduced dimension regression would be: \[Species = \frac{\: 0.798*Sepal.Length - 0*Sepal.Width +0.935*Petal.Length +0.938*Petal.Width}{3} \]

and the point.est = (...) operates in the same manner as the full regression above, again called with $Point.est.

##  [1] 1 1 1 1 1 1 1 1 1 1

Classification

For a classification problem, we simply set NNS.reg(x, y, type = "CLASS", ...).

NNS.reg(iris[ , 1 : 4], iris[ , 5], type = "CLASS", point.est = iris[1:10, 1 : 4], location = "topleft", ncores = 1)$Point.est

##  [1] 1 1 1 1 1 1 1 1 1 1

Cross-Validation NNS.stack

The NNS.stack() routine cross-validates for a given objective function the n.best parameter in the multivariate NNS.reg function as well as the threshold parameter in the dimension reduction NNS.reg version. NNS.stack can be used for classification NNS.stack(..., type = "CLASS", ...) or continuous dependent variables NNS.stack(..., type = NULL, ...).

Any objective function obj.fn can be called using expression() with the terms predicted and actual.

Note: For mixed data type regressors / features, it is suggested to use NNS.stack(..., order = "max", ...).

NNS.stack(IVs.train = iris[ , 1 : 4], 
          DV.train = iris[ , 5], 
          IVs.test = iris[1:10, 1 : 4],
          obj.fn = expression( mean(round(predicted) == actual) ),
          objective = "max",
          type = "CLASS", folds = 1, ncores = 1)
## $OBJfn.reg
## [1] 1
## 
## $NNS.reg.n.best
## [1] 1
## 
## $OBJfn.dim.red
## [1] 0.9807692
## 
## $NNS.dim.red.threshold
## [1] 0.785
## 
## $reg
##  [1] 1 1 1 1 1 1 1 1 1 1
## 
## $dim.red
##  [1] 1 1 1 1 1 1 1 1 1 1
## 
## $stack
##  [1] 1 1 1 1 1 1 1 1 1 1

References

If the user is so motivated, detailed arguments further examples are provided within the following: