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 c = console;
|
17 |
|
18 | var x = R.samples(R.steps(0,100, 10), 10, {replace:true}).value();
|
19 | c.log("x=", x);
|
20 |
|
21 | var x = R.samples(R.steps(1,6), 10, {replace:false}).value();
|
22 | c.log("x=%j max=%d min=%d mean=%d", x, R.max(x), R.min(x), R.mean(x));
|
23 |
|
24 | c.log("cov(x,x)=", R.cov(x,x).toFixed(2));
|
25 | c.log("cor(x,x)=", R.cor(x,x).toFixed(2)); // 相關係數
|
26 | c.log("factorial(10)=", R.factorial(10)); // 階層 n!
|
27 | c.log("lfactorial(10)=", R.lfactorial(10).toFixed(2)); // log(n!)
|
28 | c.log("choose(5,2)=", R.choose(5,2)); // 組合 C(n,m)
|
29 | c.log("lchoose(5,2)=", R.lchoose(5,2).toFixed(2)); // log C(n,m)
|
30 | c.log("permutation(5,2)=", R.permutation(5,2)); // P(n,m)
|
31 |
|
32 | c.log("runif(10, -5, -1)=", R.runif(10, -5, -1).str());
|
33 | c.log("dunif(-3, -5, -1)=", R.dunif(-3, -5, -1));
|
34 | c.log("punif(-3, -5, -1)=", R.punif(-3, -5, -1));
|
35 | c.log("qunif(0.5, -5, -1)=", R.qunif(0.5, -5, -1));
|
36 |
|
37 | var x = R.rnorm(10, 0, 1);
|
38 | c.log("x.str()=", x.str());
|
39 | c.log("x.sortBy()=", x.sortBy().str());
|
40 | c.log("str(x)=", R.str(x.value()));
|
41 |
|
42 | c.log("rbinom(10, 5, 0.5)=", R.rbinom(10,5,0.5));
|
43 | c.log("dbinom(4, 5, 0.5)=", R.dbinom(4,5,0.5));
|
44 | c.log("dbinom(5, 5, 0.5)=", R.dbinom(5,5,0.5));
|
45 | c.log("pbinom(4, 5, 0.5)=", R.pbinom(4,5,0.5));
|
46 | c.log("qbinom(0.9, 5, 0.5)=", R.qbinom(0.9,5,0.5));
|
47 |
|
48 | c.log("sd(x)=", x.sd().toFixed(2));
|
49 |
|
50 | var t1=R.ttest({x:x.value(), mu:0} );
|
51 | R.report(t1);
|
52 |
|
53 | var A = [[1,2,3],[4,5,6],[7,3,9]];
|
54 | var iA = R.M.inv(A);
|
55 | c.log("A=", R.str(A));
|
56 | c.log("iA=", R.str(iA));
|
57 | var AiA = R.M.dot(A, iA);
|
58 | c.log("AiA=", R.str(AiA));
|
59 | ```
|
60 |
|
61 | ## run
|
62 |
|
63 | ```
|
64 | D:\Dropbox\github\rlab>node rtest
|
65 | x= [ 10, 50, 20, 0, 80, 100, 90, 30, 40, 60 ]
|
66 | x=[5,5,5,4,1,3,4,3,2,1] max=5 min=1 mean=3.3
|
67 | cov(x,x)= 1.57
|
68 | cor(x,x)= 1.00
|
69 | factorial(10)= 3628800
|
70 | lfactorial(10)= 15.10
|
71 | choose(5,2)= 10
|
72 | lchoose(5,2)= 2.30
|
73 | permutation(5,2)= 20
|
74 | runif(10, -5, -1)= [-3.16, -2.57, -2.12, -4.13, -2.3, -2.14, -2.55, -4.78, -1.77
|
75 | , -3.66]
|
76 | dunif(-3, -5, -1)= 0.25
|
77 | punif(-3, -5, -1)= 0.5
|
78 | qunif(0.5, -5, -1)= -3
|
79 | x.str()= [-0.15, 0.2, 0.53, -0.45, -0.34, -0.98, 1.25, 0.44, 1.61, 0.11]
|
80 | x.sortBy()= [-0.98, -0.45, -0.34, -0.15, 0.11, 0.2, 0.44, 0.53, 1.25, 1.61]
|
81 | str(x)= [-0.15, 0.2, 0.53, -0.45, -0.34, -0.98, 1.25, 0.44, 1.61, 0.11]
|
82 | rbinom(10, 5, 0.5)= [ 2, 3, 2, 3, 1, 5, 2, 2, 1, 2 ]
|
83 | dbinom(4, 5, 0.5)= 0.15625
|
84 | dbinom(5, 5, 0.5)= 0.03125
|
85 | pbinom(4, 5, 0.5)= 0.96875
|
86 | qbinom(0.9, 5, 0.5)= 4
|
87 | sd(x)= 0.78
|
88 | =========== report ==========
|
89 | name : "ttest(X)"
|
90 | h : "H0:mu=0"
|
91 | alpha : 0.05
|
92 | op : "="
|
93 | pvalue : 0.39
|
94 | ci : [-0.34, 0.78]
|
95 | df : 9
|
96 | mean : 0.22
|
97 | sd : 0.78
|
98 | A= [[1, 2, 3], [4, 5, 6], [7, 3, 9]]
|
99 | iA= [[-0.9, 0.3, 0.1], [-0.2, 0.4, -0.2], [0.77, -0.37, 0.1]]
|
100 | AiA= [[1, 0, -0.], [0, 1, -0.], [0, 0., 1.]]
|
101 |
|
102 | ```
|
103 |
|
104 |
|