UNPKG

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