1 | rlab -- A JavaScript Scientific Library like R
|
2 |
|
3 | ## introduction
|
4 |
|
5 | The rlab is a A JavaScript Scientific Library like R.
|
6 |
|
7 | It's based on `lodash.js , jStat.js and numeric.js`
|
8 |
|
9 | There is a webIDE server based on rlab , you may start it by :
|
10 |
|
11 | ```
|
12 | node rserver.js
|
13 | ```
|
14 |
|
15 | ## install
|
16 |
|
17 | ```
|
18 | npm install rlab
|
19 | ```
|
20 |
|
21 | ## use rlab
|
22 |
|
23 | file : rtest.js
|
24 |
|
25 | ```javascript
|
26 | var R = require("rlab");
|
27 | var M = R.M;
|
28 | var c = console;
|
29 |
|
30 | var dice = R.steps(1,6);
|
31 | c.log("dice=", dice);
|
32 |
|
33 | var x = R.samples(dice, 6, {replace:false});
|
34 | c.log("x=", x);
|
35 |
|
36 | var x = R(dice).samples(6, {replace:false}).value();
|
37 | c.log("chain1:x=", x);
|
38 |
|
39 | var x = R(dice).samples(10).str();
|
40 | c.log("chain2:x=", x);
|
41 |
|
42 | var x = R.samples(dice, 10);
|
43 | c.log("x=", x, "max=", R.max(x), "min=", R.min(x),
|
44 | "mean=", R.mean(x), "sd=", R.sd(x));
|
45 |
|
46 | c.log("cov(x,x)=", R.cov(x,x));
|
47 | c.log("cor(x,x)=", R.cor(x,x)); // 相關係數
|
48 | c.log("factorial(10)=", R.factorial(10)); // 階層 n!
|
49 | c.log("lfactorial(10)=", R.lfactorial(10).toFixed(4)); // log(n!)
|
50 | c.log("choose(5,2)=", R.choose(5,2)); // 組合 C(n,m)
|
51 | c.log("lchoose(5,2)=", R.lchoose(5,2)); // log C(n,m)
|
52 | c.log("permutation(5,2)=", R.permutation(5,2)); // P(n,m)
|
53 | c.log("runif(10, -5, -1)=", R.runif(10, -5, -1).str());
|
54 | // c.log(".chain(10).runif(-5,-1)=", R.chain(10).runif(-5,-1));
|
55 | c.log("dunif(-3, -5, -1)=", R.dunif(-3, -5, -1));
|
56 | c.log("punif(-3, -5, -1)=", R.punif(-3, -5, -1));
|
57 | c.log("qunif(0.5, -5, -1)=", R.qunif(0.5, -5, -1));
|
58 |
|
59 | var x = R.rnorm(10, 0, 1);
|
60 | c.log("x=", R.str(x));
|
61 | c.log("x.sort()=", x.sort().str());
|
62 |
|
63 | c.log("rbinom(10, 5, 0.5)=", R.rbinom(10,5,0.5));
|
64 | c.log("dbinom(4, 5, 0.5)=", R.dbinom(4,5,0.5));
|
65 | c.log("dbinom(5, 5, 0.5)=", R.dbinom(5,5,0.5));
|
66 | c.log("pbinom(4, 5, 0.5)=", R.pbinom(4,5,0.5));
|
67 | c.log("qbinom(0.9, 5, 0.5)=", R.qbinom(0.9,5,0.5));
|
68 |
|
69 | var t1=R.ttest({x:x, mu:0} );
|
70 | R.report(t1);
|
71 |
|
72 | var A = [[1,2,3],[4,5,6],[7,3,9]];
|
73 | var iA = M.inv(A);
|
74 | c.log("A=", R.str(A));
|
75 | c.log("iA=", R.str(iA));
|
76 | var AiA = M.dot(A, iA);
|
77 |
|
78 | c.log("AiA=", R.str(AiA));
|
79 |
|
80 | c.log("====iA=====\n", M.str(iA))
|
81 | ```
|
82 |
|
83 | ## run
|
84 |
|
85 | ```
|
86 | D:\Dropbox\github\rlab>node rtest
|
87 | dice= [ 1, 2, 3, 4, 5, 6 ]
|
88 | x= [ 2, 1, 3, 4, 6, 5 ]
|
89 | chain1:x= [ 6, 2, 3, 5, 1, 4 ]
|
90 | chain2:x= [1, 2, 5, 6, 6, 3, 6, 6, 2, 5]
|
91 | x= [ 5, 6, 3, 4, 6, 3, 4, 2, 5, 2 ] max= 6 min= 2 mean= 4 sd= 1.4907119849998598
|
92 |
|
93 | cov(x,x)= 1.4907119849998598
|
94 | cor(x,x)= 1
|
95 | factorial(10)= 3628800
|
96 | lfactorial(10)= 15.1044
|
97 | choose(5,2)= 10
|
98 | lchoose(5,2)= 2.302585092994045
|
99 | permutation(5,2)= 20
|
100 | runif(10, -5, -1)= [-3.3, -2.68, -3.5, -2.96, -4.48, -1.9, -2.12, -2.02, -4.59,
|
101 | -4.09]
|
102 | dunif(-3, -5, -1)= 0.25
|
103 | punif(-3, -5, -1)= 0.5
|
104 | qunif(0.5, -5, -1)= -3
|
105 | x= [0.79, 0.49, 1.01, -1.13, 0.19, 0.4, -0.14, 1.01, 0.1, -1]
|
106 | x.sort()= [-0.14, -1, -1.13, 0.1, 0.19, 0.4, 0.49, 0.79, 1.01, 1.01]
|
107 | rbinom(10, 5, 0.5)= [ 3, 2, 3, 2, 3, 2, 3, 1, 2, 1 ]
|
108 | dbinom(4, 5, 0.5)= 0.15625
|
109 | dbinom(5, 5, 0.5)= 0.03125
|
110 | pbinom(4, 5, 0.5)= 0.96875
|
111 | qbinom(0.9, 5, 0.5)= 4
|
112 | =========== report ==========
|
113 | name : "ttest(X)"
|
114 | h : "H0:mu=0"
|
115 | alpha : 0.05
|
116 | op : "="
|
117 | pvalue : 0.49
|
118 | ci : [-0.37, 0.71]
|
119 | df : 9
|
120 | mean : 0.17
|
121 | sd : 0.75
|
122 | A= [[1, 2, 3], [4, 5, 6], [7, 3, 9]]
|
123 | iA= [[-0.9, 0.3, 0.1], [-0.2, 0.4, -0.2], [0.77, -0.37, 0.1]]
|
124 | AiA= [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
125 | ====iA=====
|
126 | [[ -0.9, 0.3, 0.1],
|
127 | [ -0.2, 0.4, -0.2],
|
128 | [ 0.77, -0.37, 0.1]]
|
129 | ```
|
130 |
|
131 |
|