Skip to content

Commit 9b686b8

Browse files
committed
Merge branch 'master' of https://github.com/SMRUCC/R-sharp
2 parents 4015481 + 4e3b8cb commit 9b686b8

File tree

22 files changed

+638
-319
lines changed

22 files changed

+638
-319
lines changed

Library/R.math/stats.vb

+117-44
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
11
#Region "Microsoft.VisualBasic::224bf7ecf7b281c8e3c321ac9a1b8803, Library\R.math\stats.vb"
22

3-
' Author:
4-
'
5-
6-
7-
' xieguigang ([email protected])
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+
' xieguigang ([email protected])
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+
' /********************************************************************************/
4747

4848
#End Region
4949

5050
Imports Microsoft.VisualBasic.CommandLine.Reflection
5151
Imports Microsoft.VisualBasic.Linq
5252
Imports Microsoft.VisualBasic.Math.Distributions
53+
Imports Microsoft.VisualBasic.Math.LinearAlgebra.Prcomp
5354
Imports Microsoft.VisualBasic.Scripting.MetaData
5455
Imports SMRUCC.Rsharp.Runtime
56+
Imports SMRUCC.Rsharp.Runtime.Internal.Object
5557
Imports SMRUCC.Rsharp.Runtime.Interop
5658
Imports REnv = SMRUCC.Rsharp.Runtime
5759

@@ -95,6 +97,77 @@ Module stats
9597
Return Bootstraping.TabulateMode(DirectCast(vec, Double()))
9698
End Function)
9799
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
98171
End Module
99172

100173
Public Enum SplineAlgorithms

Library/R.plot/plots.vb

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Imports Microsoft.VisualBasic.Imaging.Driver
6666
Imports Microsoft.VisualBasic.Math.Calculus
6767
Imports Microsoft.VisualBasic.Math.Calculus.Dynamics.Data
6868
Imports Microsoft.VisualBasic.Math.Distributions.BinBox
69+
Imports Microsoft.VisualBasic.Math.Interpolation
6970
Imports Microsoft.VisualBasic.Scripting.MetaData
7071
Imports Microsoft.VisualBasic.Scripting.Runtime
7172
Imports SMRUCC.Rsharp

R#/Extensions.vb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Public Module Extensions
8080
Return val
8181
ElseIf TypeOf val Is Encodings Then
8282
Return DirectCast(val, Encodings).CodePage
83-
ElseIf val.GetType Like BinaryExpression.characters Then
83+
ElseIf val.GetType Like RType.characters Then
8484
Dim encodingName$ = Scripting.ToString(Runtime.asVector(Of String)(val).AsObjectEnumerator.First)
8585
Dim encodingVal As Encoding = TextEncodings.ParseEncodingsName(encodingName).CodePage
8686

R#/Interpreter/ExecuteEngine/ExpressionSymbols/DataSet/SequenceLiteral.vb

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545

4646
Imports System.Runtime.CompilerServices
4747
Imports Microsoft.VisualBasic.Language
48-
Imports SMRUCC.Rsharp.Interpreter.ExecuteEngine.ExpressionSymbols.Operators
4948
Imports SMRUCC.Rsharp.Runtime
5049
Imports SMRUCC.Rsharp.Runtime.Components
50+
Imports SMRUCC.Rsharp.Runtime.Interop
5151

5252
Namespace Interpreter.ExecuteEngine.ExpressionSymbols.DataSets
5353

@@ -73,7 +73,7 @@ Namespace Interpreter.ExecuteEngine.ExpressionSymbols.DataSets
7373
.Any(Function(num)
7474
Dim ntype As Type = num.GetType
7575

76-
If ntype Like BinaryExpression.floats Then
76+
If ntype Like RType.floats Then
7777
Return True
7878
Else
7979
Return False

R#/Interpreter/ExecuteEngine/ExpressionSymbols/DataSet/SymbolIndexer.vb

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ Namespace Interpreter.ExecuteEngine.ExpressionSymbols.DataSets
157157

158158
If key Is Nothing Then
159159
Return Internal.debug.stop("dataframe index could not be nothing!", envir)
160-
ElseIf key.GetType Like BinaryExpression.integers Then
161-
Return obj.GetColumnVector(CInt(key))
160+
ElseIf key.GetType Like RType.integers Then
161+
Return obj.getColumnVector(CInt(key))
162162
Else
163-
Return obj.GetColumnVector(Scripting.ToString(key))
163+
Return obj.getColumnVector(Scripting.ToString(key))
164164
End If
165165
Else
166166
' dataframe projection

0 commit comments

Comments
 (0)