UNPKG

7.32 kBMarkdownView Raw
1rlab -- A JavaScript Scientific Library like R
2
3## Introduction
4
5The rlab is a A JavaScript Scientific Library like R.
6
7It's based on `lodash.js , jStat.js and numeric.js`
8
9## Install
10
11```
12npm install rlab
13```
14
15## Use rlab in console mode
16
17file : probabilityEx.js
18
19```javascript
20var R = require("rlab");
21var dice = R.steps(1,6);
22log("sample(1:6, 10)", R.samples(dice, 10));
23log("runif(10,0,1)=", R.runif(10, 0, 1).str());
24log("rnorm(10,5,1)=", R.rnorm(10, 5, 1).str());
25log("dnorm(5,5,1)=", R.dnorm(5, 5, 1));
26log("pnorm(5,5,1)=", R.pnorm(5, 5, 1));
27log("qnorm(0.5,5,1)=", R.qnorm(0.5, 5, 1));
28log("rbinom(10, 5, 0.5)=", R.rbinom(10,5,0.5));
29log("dbinom(4, 5, 0.5)=", R.dbinom(4,5,0.5));
30log("dbinom(5, 5, 0.5)=", R.dbinom(5,5,0.5));
31log("pbinom(4, 5, 0.5)=", R.pbinom(4,5,0.5));
32log("qbinom(0.9, 5, 0.5)=", R.qbinom(0.9,5,0.5));
33
34```
35
36run :
37
38```
39$ node probabilityEx.js
40sample(1:6, 10) [ 3, 5, 3, 2, 3, 3, 1, 2, 4, 3 ]
41runif(10,0,1)= [0.9119,0.5899,0.6839,0.1350,0.6894,0.9512,0.8186,0.5826,0.4279,0
42.5125]
43rnorm(10,5,1)= [5.8961,5.4312,6.0002,5.3623,5.5281,4.4413,6.2144,5.7173,5.3111,1
44.3146]
45dnorm(5,5,1)= 0.3989422804014327
46pnorm(5,5,1)= 0.5
47qnorm(0.5,5,1)= 5
48rbinom(10, 5, 0.5)= [ 2, 1, 2, 2, 4, 4, 1, 4, 3, 2 ]
49dbinom(4, 5, 0.5)= 0.15625
50dbinom(5, 5, 0.5)= 0.03125
51pbinom(4, 5, 0.5)= 0.96875
52qbinom(0.9, 5, 0.5)= 4
53```
54
55file : statisticsEx.js
56
57```javascript
58var R = require("rlab");
59var v = [1,3,5];
60log("v.max()=", v.max());
61log("v.min()=", v.min());
62log("v.sum()=", v.sum());
63log("v.normalize()=", v.normalize());
64log("v.normalize().sum()=", v.normalize().sum());
65log("v.product()=", v.product());
66log("v.mean()=", v.mean());
67log("v.range()=", v.range());
68log("v.median()=", v.median());
69log("v.variance()=", v.variance());
70log("v.sd()=", v.sd(), " sd^2=", v.sd()*v.sd());
71log("v.cov(v)=", v.cov(v), "v.cor(v)=", v.cor(v));
72log("factorial(5)=", R.factorial(5));
73```
74
75run :
76
77```
78$ node statisticsEx.js
79v.max()= 5
80v.min()= 1
81v.sum()= 9
82v.normalize()= [ 0.1111111111111111, 0.3333333333333333, 0.5555555555555556 ]
83v.normalize().sum()= 1
84v.product()= 15
85v.mean()= 1
86v.range()= 4
87v.median()= 3
88v.variance()= 2.6666666666666665
89v.sd()= 1.632993161855452 sd^2= 2.6666666666666665
90v.cov(v)= 4 v.cor(v)= 1
91factorial(5)= 120
92```
93
94file : testEx.js
95
96```javascript
97var R = require("rlab");
98var v = [1,3,5];
99
100var x = R.rnorm(10, 0, 0.1);
101log("x=", x.str());
102log("x.sort()=", x.sort().str());
103
104var t1=R.ttest({x:x, mu:0});
105R.report(t1);
106```
107
108run :
109
110```
111$ node testEx.js
112x= [-0.1405,0.0495,-0.1850,0.0824,0.0687,-0.0854,-0.1049,-0.1171,0.0947,-0.1592]
113
114x.sort()= [-0.0854,-0.1049,-0.1171,-0.1405,-0.1592,-0.1850,0.0495,0.0687,0.0824,
1150.0947]
116=========== report ==========
117name : ttest(X)
118h : H0:mu=0
119alpha : 0.0500
120op : =
121pvalue : 0.0003
122ci : [-0.2599,-0.1101]
123df : 9.0000
124mean : -0.1850
125sd : 0.1047
126```
127
128file : matrixEx.js
129
130```javascript
131var M = require("rlab").M;
132var v = [1,2,3];
133log("v.sin()=", v.sin());
134log("v.norm2()=", v.norm2());
135log("v.norm2Squared()=", v.norm2Squared());
136
137var A = [[1,2,3],[4,5,6],[7,3,9]];
138var AiA = A.inv().dot(A);
139log("AiA=\n", AiA.strM());
140log("AiA.tr()=\n", AiA.tr().strM());
141log("A=\n", A.str());
142log("A.mul(0.1)=\n", A.mul(0.1).strM());
143log("A.row(1)=", A.row(1));
144log("A.col(1)=", A.col(1));
145log("A.sumM()=", A.sumM());
146log("A.rowSum()=", A.rowSum());
147log("A.colSum()=", A.colSum());
148log("A.mean(row)=", A.rowMean().str());
149log("A.mean(col)=", A.colMean().str());
150
151var D = M.diag(v);
152log("D=", D);
153
154var Eλ = M.eigR(A);
155var E = Eλ.E, λ=Eλ.lambda;
156log("E*[λ]*E-1=", E.dot(λ.diag()).dot(E.inv()).strM());
157```
158
159run :
160
161```
162$ node matrixEx.js
163v.sin()= [ 0.8414709848078965, 0.9092974268256817, 0.1411200080598672 ]
164v.norm2()= 3.7416573867739413
165v.norm2Squared()= 14
166AiA=
167 [[ 1, 1.11e-16, -1.11e-16],
168 [ 0, 1, 4.441e-16],
169 [ -3.331e-16, -3.331e-16, 1]]
170AiA.tr()=
171 [[ 1, 0, -3.331e-16],
172 [ 1.11e-16, 1, -3.331e-16],
173 [ -1.11e-16, 4.441e-16, 1]]
174A=
175 [[1.0000,2.0000,3.0000],[4.0000,5.0000,6.0000],[7.0000,3.0000,9.0000]]
176A.mul(0.1)=
177 [[ 0.1, 0.2, 0.3],
178 [ 0.4, 0.5, 0.6],
179 [ 0.7, 0.3, 0.9]]
180A.row(1)= [ 4, 5, 6 ]
181A.col(1)= [ 2, 5, 3 ]
182A.sumM()= 40
183A.rowSum(2)= [ 6, 15, 19 ]
184A.colSum(2)= [ 12, 10, 18 ]
185A.mean(row)= [2.0000,5.0000,6.3333]
186A.mean(col)= [4.0000,3.3333,6.0000]
187D= [ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]
188E*[λ]*E-1= [[ 1, 2, 3],
189 [ 4, 5, 6],
190 [ 7, 3, 9]]
191```
192
193file : differentialEx.js
194
195```javascript
196var R = require("rlab");
197
198var d = R.D.d, i=R.D.i, sin=R.sin, PI = R.PI, x2=(x)=>x*x;
199
200log('d(x^2,2)=', d(x2, 2));
201log('d(sin(x/4),pi/4)=', d(sin, PI/4));
202log('i(x^2,0,1)=', i(x2,0,1));
203log('i(sin(x),0,pi/2)=', i(sin,0,PI/2));
204
205```
206
207run :
208
209```
210D:\Dropbox\github\rlab\example>node differentialEx.js
211d(x^2,2)= 4.000999999999699
212d(sin(x/4),pi/4)= 0.7067531099743674
213i(x^2,0,1)= 0.33283350000000095
214i(sin(x),0,pi/2)= 0.9997035898637557
215```
216
217## Run rlab on Web
218
219Fdbserver is a used in the server.js of rlab. You have to install fdbserver before start the server.js
220
221```
222$ git clone https://github.com/ccckmit/rlab
223...
224$ cd rlab
225...
226$ npm install fdbserver
227rlab@0.7.1 D:\Dropbox\github\rlab
228+-- chai@3.5.0 extraneous
229+-- express@4.13.4 extraneous
230`-- fdbserver@1.6.6 extraneous
231
232$ node server.js
233```
234
235A demo for rlab is on my web site <http://ccc.nqu.edu.tw/rlab/rlab.html> .
236
237The following figure is Screen Shot for Rlab GUI.
238
239![Run rlab on Web](img/RlabIDE.png)
240
241If you visit the site in https protocol, there will be a Button for 'Save File', and menus for 'Login' and 'Logout'. These function can only be used in the https mode, not in the http mode.
242
243You have to login to save code. The default code path is in 'file/' directory with the file name you choose to upload.
244
245## Rebuild Web Version
246
247You have to wrap rlab for web by browserify in the following command.
248
249```
250$npm run-script build-web
251
252> rlab@0.5.4 build-web D:\Dropbox\github\rlab
253> browserify web/_rlab.js -o web/rlab.js
254```
255
256When you modify the rlab source, make sure to wrap it again.
257
258
259## Author
260
261Author: ccckmit
262
263Email : ccckmit@gmail.com
264
265## License
266
267The rlab project is licensed in MIT license.
268
269Copyright (c) 2013 rlab
270
271Permission is hereby granted, free of charge, to any person obtaining a copy
272of this software and associated documentation files (the "Software"), to deal
273in the Software without restriction, including without limitation the rights
274to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
275copies of the Software, and to permit persons to whom the Software is
276furnished to do so, subject to the following conditions:
277
278The above copyright notice and this permission notice shall be included in
279all copies or substantial portions of the Software.
280
281THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
282IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
283FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
284AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
285LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
286OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
287THE SOFTWARE.
288
289
290