num4regana 0.0.2-java → 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad6fa72619eb7a02bdc561494b9aae197c37b6a8558fd21106bdf15210a9af81
4
- data.tar.gz: c7a55ac44970c164dc7379a11955f29416392ceba4605118f8bcbba459724e94
3
+ metadata.gz: d2c06b0a7caeac79ec2e1d451dccabd387449af22574e60520125374bd4296d7
4
+ data.tar.gz: 5744160fa6bec668de85a0ae0fd4b71625b6f4976a9b01e831ca71b4d654ec8c
5
5
  SHA512:
6
- metadata.gz: dd9e09e45d35384a3bfa9eb50a0132f4b3b3e970c2f97d241264a18a08986c3a46753d2292caf10c0ae1fcab8ec4f45f6456c97fe14df2ee9334168cd429a60a
7
- data.tar.gz: 1cf8a55a5444c5e1e1a151adbb4d142bceaa9344cde28a859848b5642ce36660b6b919410907f8964105ec35df991591670ba8722658a708822e0840d790ce23
6
+ metadata.gz: 7030429dc48f2807211cf07446cd6fbcee4094fe55410d3d54ef01c9151ead39123ef869da446e6497db1f7e95ea59810281111a0f2a05922cdd3396fc9da6ba
7
+ data.tar.gz: 3967a10a750896e1b1a8f6bba7de42ae6a88862db18af456ba49f0018fc9e9e9d931aa792aceaa3e37d1c281dabfb52e461de484b0f349e59570e8e8ae73f3fe
data/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.4] - 2024-09-13
6
+
7
+ ### add
8
+ - add function of getAIC
9
+
10
+ ## [0.0.3] - 2024-09-05
11
+
12
+ ### add
13
+ - add GLM
14
+ logistic regression analystis
15
+ poisson regression analystis
16
+
5
17
  ## [0.0.2] - 2024-08-08
6
18
 
7
19
  ### fix
@@ -0,0 +1,62 @@
1
+ import java.util.Arrays;
2
+
3
+ abstract class AbstratGLM {
4
+ private final double eta = 0.005;
5
+ abstract double regression(double[] b, double[] xi);
6
+ abstract double linkFunc(double q);
7
+ // 勾配降下法
8
+ protected double[] grand_metod(double[] yi, double[] b, double[][] xij) {
9
+ // 交差エントロピー計算
10
+ double[] ei = calcE(yi, b, xij);
11
+
12
+ // パラメータ更新
13
+ for(int i = 0; i < ei.length; i++) {
14
+ b[i] -= eta * ei[i];
15
+ }
16
+ return b;
17
+ }
18
+ // AIC
19
+ protected double calcAIC(double[] b, double[][] xij) {
20
+ // 尤度計算
21
+ double maxL = calcL(b,xij);
22
+ int k = 1 + xij[0].length;
23
+
24
+ return -2 * (maxL - k);
25
+ }
26
+ // 交差エントロピー計算
27
+ private double[] calcE(double[] yi, double[] b, double[][] xij) {
28
+ double[] xi = new double[1 + xij[0].length];
29
+ double[] ei = new double[1 + xij[0].length];
30
+
31
+ Arrays.fill(ei, 0.0);
32
+ for(int i = 0; i < yi.length; i++) {
33
+ xi[0] = 1.0;
34
+ System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
35
+
36
+ double q = regression(b, xi);
37
+ double p = linkFunc(q);
38
+
39
+ for(int j = 0; j < xi.length; j++) {
40
+ ei[j] += (p - yi[i]) * xi[j];
41
+ }
42
+ }
43
+
44
+ return ei;
45
+ }
46
+ // 尤度計算(パラメータ)
47
+ private double calcL(double[] b, double[][] xij) {
48
+ double l = 0.0;
49
+ double[] xi = new double[1 + xij[0].length];
50
+
51
+ for(int i = 0; i < xij.length; i++) {
52
+ xi[0] = 1.0;
53
+ System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
54
+ double q = regression(b, xi);
55
+ double p = linkFunc(q);
56
+
57
+ l += Math.log(p);
58
+ }
59
+ return l;
60
+ }
61
+ }
62
+
@@ -0,0 +1,18 @@
1
+ import java.util.Arrays;
2
+ import org.apache.commons.math3.distribution.BetaDistribution;
3
+
4
+ abstract class AbstratGLMM {
5
+ abstract double rereion(double[] b, double[] xi, double r);
6
+ abstract double linkFunc(double q);
7
+ protected double[] mcmc(double[] yi, double[] b, double[][] xij) {
8
+ double[] bnew = new double[1 + xij[0].length];
9
+ BetaDistribution beDist = new BetaDistribution(1,1);
10
+
11
+ for(int i= 0; i < bnew.length; i++) {
12
+ System.out.printf("%f ", beDist.sample());
13
+ }
14
+ System.out.println();
15
+ return null;
16
+ }
17
+ }
18
+
@@ -0,0 +1,68 @@
1
+ import java.util.Arrays;
2
+ import java.util.Map;
3
+
4
+ public class LogitRegAna extends AbstratGLM {
5
+ private final int NUM = 1000;
6
+ private static LogitRegAna regana = new LogitRegAna();
7
+ public static LogitRegAna getInstance() {
8
+ return regana;
9
+ }
10
+ public LineReg nonLineRegAna(double[] yi, double xij[][]) {
11
+ double[] b = initB(xij[0].length);
12
+
13
+ for (int i = 0; i < NUM; i++) {
14
+ b = grand_metod(yi, b, xij);
15
+ }
16
+ return new LineReg(b);
17
+ }
18
+ public double getAIC(Map<String, Object> regCoe, double[][] xij) {
19
+ double[] b = new double[1 + xij[0].length];
20
+
21
+ b[0] = (double)regCoe.get("intercept");
22
+ System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
23
+ return calcAIC(b, xij);
24
+ }
25
+ private double[] initB(int xsie) {
26
+ double[] b = new double[1 + xsie];
27
+
28
+ Arrays.fill(b, 0.0);
29
+ return b;
30
+ }
31
+ // q = b0 + b1 * x0
32
+ double regression(double[] b, double[] xi) {
33
+ double ret = 0.0;
34
+
35
+ for(int i = 0; i < xi.length; i++) {
36
+ ret += b[i] * xi[i];
37
+ }
38
+ return ret;
39
+ }
40
+ // p = 1 / (1 + exp( -q))
41
+ double linkFunc(double q) {
42
+ return 1.0 / (1.0 + Math.exp(-1.0 * q));
43
+ }
44
+ /*********************************/
45
+ /* interface define */
46
+ /*********************************/
47
+ /*********************************/
48
+ /* class define */
49
+ /*********************************/
50
+ public class LineReg {
51
+ private double a = 0.0;
52
+ private double[] b = null;
53
+ public LineReg(double[] b) {
54
+ this.a = b[0];
55
+ this.b = new double[b.length - 1];
56
+ for (int i = 0; i < this.b.length; i++) {
57
+ this.b[i] = b[i + 1];
58
+ }
59
+ }
60
+ public double getIntercept() {
61
+ return a;
62
+ }
63
+ public double[] getSlope() {
64
+ return b;
65
+ }
66
+ }
67
+ }
68
+
@@ -12,33 +12,23 @@ public class MultRegAna {
12
12
  return regana;
13
13
  }
14
14
  public LineReg lineRegAna(double[] yi, double xij[][]) {
15
- double[][] data = createData(yi, xij);
16
- LineRegAna line = createLineRegAna(data);
15
+ LineRegAna line = createLineRegAna(yi, xij);
17
16
 
18
17
  return line.lineRegAna(yi, xij);
19
18
  }
20
19
  public double getR2(double[] yi, double xij[][]) {
21
- double[][] data = createData(yi, xij);
22
- LineRegAna line = createLineRegAna(data);
20
+ LineRegAna line = createLineRegAna(yi, xij);
23
21
 
24
22
  return line.getR2(yi, xij);
25
23
  }
26
24
  public double getAdjR2(double[] yi, double xij[][]) {
27
- double[][] data = createData(yi, xij);
28
- LineRegAna line = createLineRegAna(data);
25
+ LineRegAna line = createLineRegAna(yi, xij);
29
26
 
30
27
  return line.getAdjR2(yi, xij);
31
28
  }
32
- private double[][] createData(double[] yi, double xij[][]) {
33
- double[][] data = new double[yi.length][1 + xij[0].length];
29
+ private LineRegAna createLineRegAna(double[] yi, double xij[][]) {
30
+ double[][] data = createData(yi, xij);
34
31
 
35
- for (int i = 0; i < yi.length; i++) {
36
- data[i][0] = yi[i];
37
- System.arraycopy(xij[i], 0, data[i], 1, xij[0].length);
38
- }
39
- return data;
40
- }
41
- private LineRegAna createLineRegAna(double data[][]) {
42
32
  // 等分散性の検定
43
33
  if (false == bartletTest(data)) { // 等分散性
44
34
  return new OLSMultRegAna();
@@ -47,6 +37,15 @@ public class MultRegAna {
47
37
  return new GLSMultRegAna(data);
48
38
  }
49
39
  }
40
+ private double[][] createData(double[] yi, double xij[][]) {
41
+ double[][] data = new double[yi.length][1 + xij[0].length];
42
+
43
+ for (int i = 0; i < yi.length; i++) {
44
+ data[i][0] = yi[i];
45
+ System.arraycopy(xij[i], 0, data[i], 1, xij[0].length);
46
+ }
47
+ return data;
48
+ }
50
49
  private boolean bartletTest(double data[][]) {
51
50
  OneWayAnovaTest anova = new BartletTest();
52
51
  double statistic = anova.calcTestStatistic(data);
@@ -0,0 +1,68 @@
1
+ import java.util.Arrays;
2
+ import java.util.Map;
3
+
4
+ public class PoissonRegAna extends AbstratGLM {
5
+ private final int NUM = 1000;
6
+ private static PoissonRegAna regana = new PoissonRegAna();
7
+ public static PoissonRegAna getInstance() {
8
+ return regana;
9
+ }
10
+ public LineReg nonLineRegAna(double[] yi, double[][] xij) {
11
+ double[] b = initB(xij[0].length);
12
+
13
+ for (int i = 0; i < NUM; i++) {
14
+ b = grand_metod(yi, b, xij);
15
+ }
16
+ return new LineReg(b);
17
+ }
18
+ public double getAIC(Map<String, Object> regCoe, double[][] xij) {
19
+ double[] b = new double[1 + xij[0].length];
20
+
21
+ b[0] = (double)regCoe.get("intercept");
22
+ System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
23
+ return calcAIC(b, xij);
24
+ }
25
+ private double[] initB(int xsie) {
26
+ double[] b = new double[1 + xsie];
27
+
28
+ Arrays.fill(b, 0.0);
29
+ return b;
30
+ }
31
+ // q = b0 + b1 * x0
32
+ double regression(double[] b, double[] xi) {
33
+ double ret = 0.0;
34
+
35
+ for(int i = 0; i < xi.length; i++) {
36
+ ret += b[i] * xi[i];
37
+ }
38
+ return ret;
39
+ }
40
+ // p = exp(q)
41
+ double linkFunc(double q) {
42
+ return Math.exp(q);
43
+ }
44
+ /*********************************/
45
+ /* interface define */
46
+ /*********************************/
47
+ /*********************************/
48
+ /* class define */
49
+ /*********************************/
50
+ public class LineReg {
51
+ private double a = 0.0;
52
+ private double[] b = null;
53
+ public LineReg(double[] b) {
54
+ this.a = b[0];
55
+ this.b = new double[b.length - 1];
56
+ for (int i = 0; i < this.b.length; i++) {
57
+ this.b[i] = b[i + 1];
58
+ }
59
+ }
60
+ public double getIntercept() {
61
+ return a;
62
+ }
63
+ public double[] getSlope() {
64
+ return b;
65
+ }
66
+ }
67
+ }
68
+
@@ -0,0 +1,71 @@
1
+ import java.util.Arrays;
2
+ import org.apache.commons.math3.distribution.NormalDistribution;
3
+ import java.util.Map;
4
+
5
+ public class ProBitRegAna extends AbstratGLM {
6
+ private final int NUM = 1000;
7
+ private static ProBitRegAna regana = new ProBitRegAna();
8
+ private NormalDistribution ndist = new NormalDistribution(0, 1);
9
+
10
+ public static ProBitRegAna getInstance() {
11
+ return regana;
12
+ }
13
+ public LineReg nonLineRegAna(double[] yi, double[][] xij) {
14
+ double[] b = initB(xij[0].length);
15
+
16
+ for (int i = 0; i < NUM; i++) {
17
+ b = grand_metod(yi, b, xij);
18
+ }
19
+ return new LineReg(b);
20
+ }
21
+ public double getAIC(Map<String, Object> regCoe, double[][] xij) {
22
+ double[] b = new double[1 + xij[0].length];
23
+
24
+ b[0] = (double)regCoe.get("intercept");
25
+ System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
26
+ return calcAIC(b, xij);
27
+ }
28
+ private double[] initB(int xsie) {
29
+ double[] b = new double[1 + xsie];
30
+
31
+ Arrays.fill(b, 0.0);
32
+ return b;
33
+ }
34
+ // q = b0 + b1 * x0
35
+ double regression(double[] b, double[] xi) {
36
+ double ret = 0.0;
37
+
38
+ for(int i = 0; i < xi.length; i++) {
39
+ ret += b[i] * xi[i];
40
+ }
41
+ return ret;
42
+ }
43
+ //
44
+ double linkFunc(double q) {
45
+ return ndist.cumulativeProbability(q);
46
+ }
47
+ /*********************************/
48
+ /* interface define */
49
+ /*********************************/
50
+ /*********************************/
51
+ /* class define */
52
+ /*********************************/
53
+ public class LineReg {
54
+ private double a = 0.0;
55
+ private double[] b = null;
56
+ public LineReg(double[] b) {
57
+ this.a = b[0];
58
+ this.b = new double[b.length - 1];
59
+ for (int i = 0; i < this.b.length; i++) {
60
+ this.b[i] = b[i + 1];
61
+ }
62
+ }
63
+ public double getIntercept() {
64
+ return a;
65
+ }
66
+ public double[] getSlope() {
67
+ return b;
68
+ }
69
+ }
70
+ }
71
+
@@ -0,0 +1,287 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'LogitRegAna'
6
+ java_import 'PoissonRegAna'
7
+ java_import 'ProBitRegAna'
8
+ java_import 'java.util.HashMap'
9
+
10
+ # 一般化線形回帰分析
11
+ # (Apache commoms math3使用)
12
+ module Num4GLMRegAnaLib
13
+ # (2項)ロジスティック回帰分析
14
+ class LogitRegAnaLib
15
+ def initialize
16
+ @multana = LogitRegAna.getInstance()
17
+ end
18
+ # (2項)ロジスティック回帰分析
19
+ #
20
+ # @overload non_line_reg_ana(yi, xij)
21
+ # @param [Array] yi yの値(double[])
22
+ # @param [Array] xij xの値(double[][])
23
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
24
+ # @example
25
+ # glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
26
+ # glsxij = [
27
+ # [1, 24],
28
+ # [1, 18],
29
+ # [0, 15],
30
+ # [1, 16],
31
+ # [0, 10],
32
+ # [1, 26],
33
+ # [1, 2],
34
+ # [0, 24],
35
+ # [1, 18],
36
+ # [1, 22],
37
+ # [1, 3],
38
+ # [1, 6],
39
+ # [0, 15],
40
+ # [0, 12],
41
+ # [1, 6],
42
+ # [0, 6],
43
+ # [1, 12],
44
+ # [0, 12],
45
+ # [1, 18],
46
+ # [1, 3],
47
+ # [1, 8],
48
+ # [0, 9],
49
+ # [0, 12],
50
+ # [0, 6],
51
+ # [0, 8],
52
+ # [1, 12],
53
+ # ]
54
+ # regana = Num4RegAnaLib::LogitRegAnaLib.new
55
+ # regana.non_line_reg_ana(glsyi, glsxij)
56
+ # =>
57
+ # {
58
+ # "intercept": -6.2313, # 定数項
59
+ # "slope": [2.5995, 0.1652], # 回帰係数
60
+ # }
61
+ def non_line_reg_ana(yi, xij)
62
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
63
+ retRb = {
64
+ "intercept": multRet.getIntercept(), # 定数項
65
+ "slope": multRet.getSlope().to_a, # 回帰係数
66
+ }
67
+ return retRb
68
+ end
69
+ # AIC
70
+ #
71
+ # @overload get_aic(regcoe, xij)
72
+ # @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
73
+ # @param [Array] xij xの値(double[][])
74
+ # @return double AIC値
75
+ # @example
76
+ # reg = {
77
+ # :intercept=> -6.2313, # 定数項
78
+ # :slope=> [2.5995, 0.1652], # 回帰係数
79
+ # }
80
+ # xij = [
81
+ # [1, 24],
82
+ # [1, 18],
83
+ # [0, 15],
84
+ # [1, 16],
85
+ # [0, 10],
86
+ # [1, 26],
87
+ # [1, 2],
88
+ # [0, 24],
89
+ # [1, 18],
90
+ # [1, 22],
91
+ # [1, 3],
92
+ # [1, 6],
93
+ # [0, 15],
94
+ # [0, 12],
95
+ # [1, 6],
96
+ # [0, 6],
97
+ # [1, 12],
98
+ # [0, 12],
99
+ # [1, 18],
100
+ # [1, 3],
101
+ # [1, 8],
102
+ # [0, 9],
103
+ # [0, 12],
104
+ # [0, 6],
105
+ # [0, 8],
106
+ # [1, 12],
107
+ # ]
108
+ # regana = Num4RegAnaLib::LogitRegAnaLib.new
109
+ # regana.get_aic(reg, xij)
110
+ # => 155.612
111
+ def get_aic(regcoe, xij)
112
+ o = HashMap.new
113
+ o["intercept"] = regcoe[:intercept]
114
+ o["slope"] = regcoe[:slope].to_java(Java::double)
115
+ @multana.getAIC(o, xij.to_java(Java::double[]))
116
+ end
117
+ end
118
+ # ポアソン回帰分析
119
+ class PoissonRegAnaLib
120
+ def initialize
121
+ @multana = PoissonRegAna.getInstance()
122
+ end
123
+ # ポアソン回帰分析
124
+ #
125
+ # @overload non_line_reg_ana(yi, xij)
126
+ # @param [Array] yi yの値(double[])
127
+ # @param [Array] xij xの値(double[][])
128
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
129
+ # @example
130
+ # glsyi = [4, 10, 7, 14]
131
+ # glsxij = [
132
+ # [1],
133
+ # [2],
134
+ # [3],
135
+ # [4],
136
+ # ]
137
+ # regana = Num4RegAnaLib::PoissonRegAnaLib.new
138
+ # regana.non_line_reg_ana(glsyi, glsxij)
139
+ # =>
140
+ # {
141
+ # "intercept": 1.3138, # 定数項
142
+ # "slope": [0.3173], # 回帰係数
143
+ # }
144
+ def non_line_reg_ana(yi, xij)
145
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
146
+ retRb = {
147
+ "intercept": multRet.getIntercept(), # 定数項
148
+ "slope": multRet.getSlope().to_a, # 回帰係数
149
+ }
150
+ return retRb
151
+ end
152
+ # AIC
153
+ #
154
+ # @overload get_aic(regcoe, xij)
155
+ # @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
156
+ # @param [Array] xij xの値(double[][])
157
+ # @return double AIC値
158
+ # @example
159
+ # reg = {
160
+ # :intercept => 1.3138, # 定数項
161
+ # :slope => [0.3173], # 回帰係数
162
+ # }
163
+ # xij = [
164
+ # [1],
165
+ # [2],
166
+ # [3],
167
+ # [4],
168
+ # ]
169
+ # regana = Num4RegAnaLib::PoissonRegAnaLib.new
170
+ # regana.get_aic(reg, xij)
171
+ # => -12.856
172
+ def get_aic(regcoe, xij)
173
+ o = HashMap.new
174
+ o["intercept"] = regcoe[:intercept]
175
+ o["slope"] = regcoe[:slope].to_java(Java::double)
176
+ @multana.getAIC(o, xij.to_java(Java::double[]))
177
+ end
178
+ end
179
+ # プロビット回帰分析
180
+ class ProBitRegAnaLib
181
+ def initialize
182
+ @multana = ProBitRegAna.getInstance()
183
+ end
184
+ # プロビット回帰分析
185
+ #
186
+ # @overload non_line_reg_ana(yi, xij)
187
+ # @param [Array] yi yの値(double[])
188
+ # @param [Array] xij xの値(double[][])
189
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
190
+ # @example
191
+ # glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
192
+ # glsxij = [
193
+ # [1, 24],
194
+ # [1, 18],
195
+ # [0, 15],
196
+ # [1, 16],
197
+ # [0, 10],
198
+ # [1, 26],
199
+ # [1, 2],
200
+ # [0, 24],
201
+ # [1, 18],
202
+ # [1, 22],
203
+ # [1, 3],
204
+ # [1, 6],
205
+ # [0, 15],
206
+ # [0, 12],
207
+ # [1, 6],
208
+ # [0, 6],
209
+ # [1, 12],
210
+ # [0, 12],
211
+ # [1, 18],
212
+ # [1, 3],
213
+ # [1, 8],
214
+ # [0, 9],
215
+ # [0, 12],
216
+ # [0, 6],
217
+ # [0, 8],
218
+ # [1, 12],
219
+ # ]
220
+ # regana = Num4RegAnaLib::ProBitRegAnaLib.new
221
+ # regana.non_line_reg_ana(glsyi, glsxij)
222
+ # =>
223
+ # {
224
+ # "intercept": -5.0497, # 定数項
225
+ # "slope": [2.2379, 0.2973], # 回帰係数
226
+ # }
227
+ def non_line_reg_ana(yi, xij)
228
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
229
+ retRb = {
230
+ "intercept": multRet.getIntercept(), # 定数項
231
+ "slope": multRet.getSlope().to_a, # 回帰係数
232
+ }
233
+ return retRb
234
+ end
235
+ # AIC
236
+ #
237
+ # @overload get_aic(regcoe, xij)
238
+ # @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
239
+ # @param [Array] xij xの値(double[][])
240
+ # @return double AIC値
241
+ # @example
242
+ # reg = {
243
+ # :intercept=> -5.0497, # 定数項
244
+ # :slope=> [2.2379, 0.2973], # 回帰係数
245
+ # }
246
+ # xij = [
247
+ # [1, 24],
248
+ # [1, 18],
249
+ # [0, 15],
250
+ # [1, 16],
251
+ # [0, 10],
252
+ # [1, 26],
253
+ # [1, 2],
254
+ # [0, 24],
255
+ # [1, 18],
256
+ # [1, 22],
257
+ # [1, 3],
258
+ # [1, 6],
259
+ # [0, 15],
260
+ # [0, 12],
261
+ # [1, 6],
262
+ # [0, 6],
263
+ # [1, 12],
264
+ # [0, 12],
265
+ # [1, 18],
266
+ # [1, 3],
267
+ # [1, 8],
268
+ # [0, 9],
269
+ # [0, 12],
270
+ # [0, 6],
271
+ # [0, 8],
272
+ # [1, 12],
273
+ # ]
274
+ # regana = Num4RegAnaLib::ProBitRegAnaLib.new
275
+ # regana.get_aic(reg, xij)
276
+ # => 119.674
277
+ def get_aic(regcoe, xij)
278
+ o = HashMap.new
279
+ o["intercept"] = regcoe[:intercept]
280
+ o["slope"] = regcoe[:slope].to_java(Java::double)
281
+ @multana.getAIC(o, xij.to_java(Java::double[]))
282
+ end
283
+ end
284
+ end
285
+
286
+
287
+
@@ -0,0 +1,165 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'SmplRegAna'
6
+ java_import 'MultRegAna'
7
+ # 線形回帰分析
8
+ # (Apache commoms math3使用)
9
+ module Num4LineRegAnaLib
10
+ # 単回帰分析
11
+ class SmplRegAnaLib
12
+ def initialize
13
+ @regana = SmplRegAna.getInstance()
14
+ end
15
+ # 単回帰分析
16
+ #
17
+ # @overload line_reg_ana(yi, xi)
18
+ # @param [Array] yi yの値(double[])
19
+ # @param [Array] xi xの値(double[])
20
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
21
+ # @example
22
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
23
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
24
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
25
+ # regana.line_reg_ana(yi, xi)
26
+ # =>
27
+ # {
28
+ # "intercept": 99.075, # 定数項
29
+ # "slope": 2.145, # 回帰係数
30
+ # }
31
+ def line_reg_ana(yi, xi)
32
+ ret = @regana.lineRegAna(yi.to_java(Java::double), xi.to_java(Java::double))
33
+ retRb = {
34
+ "intercept": ret.getIntercept(), # 定数項
35
+ "slope": ret.getSlope(), # 回帰係数
36
+ }
37
+ return retRb
38
+ end
39
+ # 決定係数
40
+ #
41
+ # @overload getr2(yi, xi)
42
+ # @param [Array] yi yの値(double[])
43
+ # @param [Array] xi xの値(double[])
44
+ # @return [double] 決定係数
45
+ # @example
46
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
47
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
48
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
49
+ # regana.getr2(yi, xi)
50
+ # => 0.893
51
+ def getr2(yi, xi)
52
+ return @regana.getR2(yi.to_java(Java::double), xi.to_java(Java::double))
53
+ end
54
+ # 相関係数
55
+ #
56
+ # @overload getr(yi, xi)
57
+ # @param [Array] yi yの値(double[])
58
+ # @param [Array] xi xの値(double[])
59
+ # @return [double] 決定係数
60
+ # @example
61
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
62
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
63
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
64
+ # regana.getr(yi, xi)
65
+ # => 0.945
66
+ def getr(yi, xi)
67
+ return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
+ end
69
+ end
70
+ # 重回帰分析(最小2乗法:等分散性checkあり)
71
+ class OLSMultRegAnaLib
72
+ def initialize
73
+ @multana = MultRegAna.getInstance()
74
+ end
75
+ # 重回帰分析
76
+ #
77
+ # @overload line_reg_ana(yi, xij)
78
+ # @param [Array] yi yの値(double[])
79
+ # @param [Array] xij xの値(double[][])
80
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
81
+ # @example
82
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
83
+ # olsxij = [
84
+ # [17.5, 30],
85
+ # [17.0, 25],
86
+ # [18.5, 20],
87
+ # [16.0, 30],
88
+ # [19.0, 45],
89
+ # [19.5, 35],
90
+ # [16.0, 25],
91
+ # [18.0, 35],
92
+ # [19.0, 35],
93
+ # [19.5, 40],
94
+ # ]
95
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
96
+ # regana.line_reg_ana(olsyi, olsxij)
97
+ # =>
98
+ # {
99
+ # "intercept": -34.71, # 定数項
100
+ # "slope": [3.47, 0.53], # 回帰係数
101
+ # }
102
+ def line_reg_ana(yi, xij)
103
+ multRet = @multana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
+
105
+ retRb = {
106
+ "intercept": multRet.getIntercept(), # 定数項
107
+ "slope": multRet.getSlope().to_a, # 回帰係数
108
+ }
109
+ return retRb
110
+ end
111
+ # 決定係数
112
+ #
113
+ # @overload getr2(yi, xij)
114
+ # @param [Array] yi yの値(double[])
115
+ # @param [Array] xij xの値(double[][])
116
+ # @return [double] 決定係数
117
+ # @example
118
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
119
+ # olsxij = [
120
+ # [17.5, 30],
121
+ # [17.0, 25],
122
+ # [18.5, 20],
123
+ # [16.0, 30],
124
+ # [19.0, 45],
125
+ # [19.5, 35],
126
+ # [16.0, 25],
127
+ # [18.0, 35],
128
+ # [19.0, 35],
129
+ # [19.5, 40],
130
+ # ]
131
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
132
+ # regana.getr2(yi, xi)
133
+ # => 0.858
134
+ def getr2(yi, xij)
135
+ return @multana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
136
+ end
137
+ # 自由度調整済み決定係数
138
+ #
139
+ # @overload getadjr2(yi, xij)
140
+ # @param [Array] yi yの値(double[])
141
+ # @param [Array] xij xの値(double[][])
142
+ # @return [double] 決定係数
143
+ # @example
144
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
145
+ # olsxij = [
146
+ # [17.5, 30],
147
+ # [17.0, 25],
148
+ # [18.5, 20],
149
+ # [16.0, 30],
150
+ # [19.0, 45],
151
+ # [19.5, 35],
152
+ # [16.0, 25],
153
+ # [18.0, 35],
154
+ # [19.0, 35],
155
+ # [19.5, 40],
156
+ # ]
157
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
158
+ # regana.getadjr2(yi, xij)
159
+ # => 0.8176
160
+ def getadjr2(yi, xij)
161
+ return @multana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
162
+ end
163
+ end
164
+ end
165
+
data/lib/num4regana.rb CHANGED
@@ -1,166 +1,10 @@
1
- require 'java'
2
- require 'num4regana.jar'
3
- require 'commons-math3-3.6.1.jar'
1
+ require_relative('num4lineregana')
2
+ require_relative('num4glmregana')
4
3
 
5
- java_import 'SmplRegAna'
6
- java_import 'MultRegAna'
7
4
  # 回帰分析
8
- # (Apache commoms math3使用)
9
5
  module Num4RegAnaLib
10
- # 単回帰分析
11
- class SmplRegAnaLib
12
- def initialize
13
- @regana = SmplRegAna.getInstance()
14
- end
15
- # 単回帰分析
16
- #
17
- # @overload line_reg_ana(yi, xi)
18
- # @param [Array] yi yの値(double[])
19
- # @param [Array] xi xの値(double[])
20
- # @return [Hash] (intercept:定数項 slope:回帰係数)
21
- # @example
22
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
23
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
24
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
25
- # regana.line_reg_ana(yi, xi)
26
- # =>
27
- # {
28
- # "intercept": 99.075, # 定数項
29
- # "slope": 2.145, # 回帰係数
30
- # }
31
- def line_reg_ana(yi, xi)
32
- ret = @regana.lineRegAna(yi.to_java(Java::double), xi.to_java(Java::double))
33
- retRb = {
34
- "intercept": ret.getIntercept(), # 定数項
35
- "slope": ret.getSlope(), # 回帰係数
36
- }
37
- return retRb
38
- end
39
- # 決定係数
40
- #
41
- # @overload getr2(yi, xi)
42
- # @param [Array] yi yの値(double[])
43
- # @param [Array] xi xの値(double[])
44
- # @return [double] 決定係数
45
- # @example
46
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
47
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
48
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
49
- # regana.getr2(yi, xi)
50
- # => 0.893
51
- def getr2(yi, xi)
52
- return @regana.getR2(yi.to_java(Java::double), xi.to_java(Java::double))
53
- end
54
- # 相関係数
55
- #
56
- # @overload getr(yi, xi)
57
- # @param [Array] yi yの値(double[])
58
- # @param [Array] xi xの値(double[])
59
- # @return [double] 決定係数
60
- # @example
61
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
62
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
63
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
64
- # regana.getr(yi, xi)
65
- # => 0.945
66
- def getr(yi, xi)
67
- return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
- end
69
- end
70
- # 重回帰分析(最小2乗法:等分散性checkあり)
71
- class OLSMultRegAnaLib
72
- def initialize
73
- @multana = MultRegAna.getInstance()
74
- end
75
- # 重回帰分析
76
- #
77
- # @overload line_reg_ana(yi, xij)
78
- # @param [Array] yi yの値(double[])
79
- # @param [Array] xij xの値(double[][])
80
- # @return [Hash] (intercept:定数項 slope:回帰係数)
81
- # @example
82
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
83
- # olsxij = [
84
- # [17.5, 30],
85
- # [17.0, 25],
86
- # [18.5, 20],
87
- # [16.0, 30],
88
- # [19.0, 45],
89
- # [19.5, 35],
90
- # [16.0, 25],
91
- # [18.0, 35],
92
- # [19.0, 35],
93
- # [19.5, 40],
94
- # ]
95
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
96
- # regana.line_reg_ana(olsyi, olsxij)
97
- # =>
98
- # {
99
- # "intercept": -34.71, # 定数項
100
- # "slope": [3.47, 0.53], # 回帰係数
101
- # }
102
- def line_reg_ana(yi, xij)
103
- multRet = @multana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
-
105
- retRb = {
106
- "intercept": multRet.getIntercept(), # 定数項
107
- "slope": multRet.getSlope().to_a, # 回帰係数
108
- }
109
- return retRb
110
- end
111
- # 決定係数
112
- #
113
- # @overload getr2(yi, xij)
114
- # @param [Array] yi yの値(double[])
115
- # @param [Array] xij xの値(double[][])
116
- # @return [double] 決定係数
117
- # @example
118
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
119
- # olsxij = [
120
- # [17.5, 30],
121
- # [17.0, 25],
122
- # [18.5, 20],
123
- # [16.0, 30],
124
- # [19.0, 45],
125
- # [19.5, 35],
126
- # [16.0, 25],
127
- # [18.0, 35],
128
- # [19.0, 35],
129
- # [19.5, 40],
130
- # ]
131
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
132
- # regana.getr2(yi, xi)
133
- # => 0.858
134
- def getr2(yi, xij)
135
- return @multana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
136
- end
137
- # 自由度調整済み決定係数
138
- #
139
- # @overload getadjr2(yi, xij)
140
- # @param [Array] yi yの値(double[])
141
- # @param [Array] xij xの値(double[][])
142
- # @return [double] 決定係数
143
- # @example
144
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
145
- # olsxij = [
146
- # [17.5, 30],
147
- # [17.0, 25],
148
- # [18.5, 20],
149
- # [16.0, 30],
150
- # [19.0, 45],
151
- # [19.5, 35],
152
- # [16.0, 25],
153
- # [18.0, 35],
154
- # [19.0, 35],
155
- # [19.5, 40],
156
- # ]
157
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
158
- # regana.getadjr2(yi, xij)
159
- # => 0.8176
160
- def getadjr2(yi, xij)
161
- return @multana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
162
- end
163
-
164
- end
6
+ include Num4LineRegAnaLib
7
+ include Num4GLMRegAnaLib
165
8
  end
166
9
 
10
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4regana
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-08 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -61,9 +61,16 @@ files:
61
61
  - Gemfile
62
62
  - LICENSE
63
63
  - Rakefile
64
+ - ext/num4regana/AbstratGLM.java
65
+ - ext/num4regana/AbstratGLMM.java
66
+ - ext/num4regana/LogitRegAna.java
64
67
  - ext/num4regana/MultRegAna.java
68
+ - ext/num4regana/PoissonRegAna.java
69
+ - ext/num4regana/ProBitRegAna.java
65
70
  - ext/num4regana/SmplRegAna.java
66
71
  - lib/commons-math3-3.6.1.jar
72
+ - lib/num4glmregana.rb
73
+ - lib/num4lineregana.rb
67
74
  - lib/num4regana.rb
68
75
  homepage: http://github.com/siranovel/num4regana
69
76
  licenses: