|
1 | 1 | #Region "Microsoft.VisualBasic::224bf7ecf7b281c8e3c321ac9a1b8803, Library\R.math\stats.vb"
|
2 | 2 |
|
3 |
| - ' Author: |
4 |
| - ' |
5 |
| - |
6 |
| - |
7 |
| - |
8 |
| - ' |
9 |
| - ' Copyright (c) 2018 GPL3 Licensed |
10 |
| - ' |
11 |
| - ' |
12 |
| - ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 |
| - ' |
14 |
| - ' |
15 |
| - ' This program is free software: you can redistribute it and/or modify |
16 |
| - ' it under the terms of the GNU General Public License as published by |
17 |
| - ' the Free Software Foundation, either version 3 of the License, or |
18 |
| - ' (at your option) any later version. |
19 |
| - ' |
20 |
| - ' This program is distributed in the hope that it will be useful, |
21 |
| - ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 |
| - ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 |
| - ' GNU General Public License for more details. |
24 |
| - ' |
25 |
| - ' You should have received a copy of the GNU General Public License |
26 |
| - ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 |
| - |
28 |
| - |
29 |
| - |
30 |
| - ' /********************************************************************************/ |
31 |
| - |
32 |
| - ' Summaries: |
33 |
| - |
34 |
| - ' Module stats |
35 |
| - ' |
36 |
| - ' Function: spline, tabulateMode |
37 |
| - ' |
38 |
| - ' Enum SplineAlgorithms |
39 |
| - ' |
40 |
| - ' Bezier, BSpline, CatmullRom, CubiSpline |
41 |
| - ' |
42 |
| - ' |
43 |
| - ' |
44 |
| - ' |
45 |
| - ' |
46 |
| - ' /********************************************************************************/ |
| 3 | +' Author: |
| 4 | +' |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +' |
| 9 | +' Copyright (c) 2018 GPL3 Licensed |
| 10 | +' |
| 11 | +' |
| 12 | +' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | +' |
| 14 | +' |
| 15 | +' This program is free software: you can redistribute it and/or modify |
| 16 | +' it under the terms of the GNU General Public License as published by |
| 17 | +' the Free Software Foundation, either version 3 of the License, or |
| 18 | +' (at your option) any later version. |
| 19 | +' |
| 20 | +' This program is distributed in the hope that it will be useful, |
| 21 | +' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | +' GNU General Public License for more details. |
| 24 | +' |
| 25 | +' You should have received a copy of the GNU General Public License |
| 26 | +' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +' /********************************************************************************/ |
| 31 | + |
| 32 | +' Summaries: |
| 33 | + |
| 34 | +' Module stats |
| 35 | +' |
| 36 | +' Function: spline, tabulateMode |
| 37 | +' |
| 38 | +' Enum SplineAlgorithms |
| 39 | +' |
| 40 | +' Bezier, BSpline, CatmullRom, CubiSpline |
| 41 | +' |
| 42 | +' |
| 43 | +' |
| 44 | +' |
| 45 | +' |
| 46 | +' /********************************************************************************/ |
47 | 47 |
|
48 | 48 | #End Region
|
49 | 49 |
|
50 | 50 | Imports Microsoft.VisualBasic.CommandLine.Reflection
|
51 | 51 | Imports Microsoft.VisualBasic.Linq
|
52 | 52 | Imports Microsoft.VisualBasic.Math.Distributions
|
| 53 | +Imports Microsoft.VisualBasic.Math.LinearAlgebra.Prcomp |
53 | 54 | Imports Microsoft.VisualBasic.Scripting.MetaData
|
54 | 55 | Imports SMRUCC.Rsharp.Runtime
|
| 56 | +Imports SMRUCC.Rsharp.Runtime.Internal.Object |
55 | 57 | Imports SMRUCC.Rsharp.Runtime.Interop
|
56 | 58 | Imports REnv = SMRUCC.Rsharp.Runtime
|
57 | 59 |
|
@@ -95,6 +97,77 @@ Module stats
|
95 | 97 | Return Bootstraping.TabulateMode(DirectCast(vec, Double()))
|
96 | 98 | End Function)
|
97 | 99 | End Function
|
| 100 | + |
| 101 | + ''' <summary> |
| 102 | + ''' ### Principal Components Analysis |
| 103 | + ''' |
| 104 | + ''' Performs a principal components analysis on the given data matrix |
| 105 | + ''' and returns the results as an object of class ``prcomp``. |
| 106 | + ''' |
| 107 | + ''' The calculation is done by a singular value decomposition of the |
| 108 | + ''' (centered and possibly scaled) data matrix, not by using eigen on |
| 109 | + ''' the covariance matrix. This is generally the preferred method for |
| 110 | + ''' numerical accuracy. The print method for these objects prints the |
| 111 | + ''' results in a nice format and the plot method produces a scree |
| 112 | + ''' plot. |
| 113 | + ''' |
| 114 | + ''' Unlike princomp, variances are computed With the usual divisor N - 1. |
| 115 | + ''' Note that scale = True cannot be used If there are zero Or constant |
| 116 | + ''' (For center = True) variables. |
| 117 | + ''' </summary> |
| 118 | + ''' <param name="x"> |
| 119 | + ''' a numeric or complex matrix (or data frame) which provides the |
| 120 | + ''' data for the principal components analysis. |
| 121 | + ''' </param> |
| 122 | + ''' <param name="center"> |
| 123 | + ''' a logical value indicating whether the variables should be shifted |
| 124 | + ''' to be zero centered. Alternately, a vector of length equal the |
| 125 | + ''' number of columns of x can be supplied. The value is passed to scale. |
| 126 | + ''' </param> |
| 127 | + ''' <param name="scale"> |
| 128 | + ''' a logical value indicating whether the variables should be scaled to |
| 129 | + ''' have unit variance before the analysis takes place. The default is |
| 130 | + ''' FALSE for consistency with S, but in general scaling is advisable. |
| 131 | + ''' Alternatively, a vector of length equal the number of columns of x |
| 132 | + ''' can be supplied. The value is passed to scale. |
| 133 | + ''' </param> |
| 134 | + ''' <returns></returns> |
| 135 | + ''' <remarks> |
| 136 | + ''' The signs of the columns of the rotation matrix are arbitrary, and |
| 137 | + ''' so may differ between different programs for PCA, and even between |
| 138 | + ''' different builds of R. |
| 139 | + ''' </remarks> |
| 140 | + <ExportAPI("prcomp")> |
| 141 | + <RApiReturn(GetType(PCA))> |
| 142 | + Public Function prcomp(<RRawVectorArgument> |
| 143 | + x As Object, |
| 144 | + Optional scale As Boolean = False, |
| 145 | + Optional center As Boolean = False, |
| 146 | + Optional env As Environment = Nothing) As Object |
| 147 | + If x Is Nothing Then |
| 148 | + Return Internal.debug.stop("'data' must be of a vector type, was 'NULL'", env) |
| 149 | + End If |
| 150 | + |
| 151 | + Dim matrix As Double()() |
| 152 | + |
| 153 | + If TypeOf x Is dataframe Then |
| 154 | + With DirectCast(x, dataframe) |
| 155 | + matrix = .nrows _ |
| 156 | + .Sequence _ |
| 157 | + .Select(Function(i) |
| 158 | + Return REnv.asVector(Of Double)(.getRowList(i, drop:=False)) |
| 159 | + End Function) _ |
| 160 | + .Select(Function(v) DirectCast(v, Double())) _ |
| 161 | + .ToArray |
| 162 | + End With |
| 163 | + Else |
| 164 | + Throw New NotImplementedException |
| 165 | + End If |
| 166 | + |
| 167 | + Dim PCA As New PCA(matrix, center, scale) |
| 168 | + |
| 169 | + Return PCA |
| 170 | + End Function |
98 | 171 | End Module
|
99 | 172 |
|
100 | 173 | Public Enum SplineAlgorithms
|
|
0 commit comments