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