num4anova 0.0.4-java → 0.0.6-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: ca0164a7092861bfca67f257129332375e90bfae762365194ced78ef75a293a7
4
- data.tar.gz: 16f17fb717e3d92c27c4c651e034704cd81609a3adc293555eac54f9209884be
3
+ metadata.gz: 4dd60df363d899af7ca69bca4cb1da1689548f083abaacfd6441d5a589bf3501
4
+ data.tar.gz: 84469385e378cde1eddc3521582aefd2f5b2adce84e76d0c8a4c23bdd40d9374
5
5
  SHA512:
6
- metadata.gz: e14efb373cde03fc5872f3dfb1d4186e5d2263b0b7597970d1629c562cfb6696ecf2cdbfc3325aa2d732e252241de093c156b7d9da21b51a8ea305f3e24e0e0f
7
- data.tar.gz: c9b7f310d00d060d1c6fee3e8e9f5c43fce812a65d51b192315c72a378de7e2bc775e1118346373a93047c1cd8880240d03d52309ec0d8f567481923f14e31b4
6
+ metadata.gz: 7a036fe08363781f31b3583d7c7a454131680413e9c3aaeb0ebe135eec930912807e3f84be688d8bf70e1e56eafdefef0a3425f07eb18d6d042549dc2f7dd893
7
+ data.tar.gz: 7d7c136e1e457c08ac341fc542be1590888c233c161034eeff0f67d3d1466b4879ccd49c5f2a83e2f42f18f28a0279d66909d768d616afabf24b59be36a7e4e4
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.6] - 2024-02-04
6
+
7
+ ### add
8
+ - add function of twoway2_anova.
9
+
10
+ ## [0.0.5] - 2024-02-01
11
+
12
+ ### add
13
+ - add function of twoway_anova.
14
+
5
15
  ## [0.0.4] - 2024-01-30
6
16
  ### add
7
17
  - add function of replicate_plot
@@ -58,7 +58,7 @@ public class OneWayLayout {
58
58
  OneWayAnovaTest oneway = new BartletTest();
59
59
 
60
60
  double statistic = oneway.calcTestStatistic(xi);
61
- return oneway.test(statistic, a);
61
+ return oneway.execute_test(statistic, a);
62
62
  }
63
63
  public void replicatePlot(String dname, Map<String, double[]> vals) {
64
64
  ChartPlot plot = new ReplicateChartPlot();
@@ -70,7 +70,7 @@ public class OneWayLayout {
70
70
  OneWayAnovaTest oneway = new ReplicateTest();
71
71
 
72
72
  double statistic = oneway.calcTestStatistic(xi);
73
- return oneway.test(statistic, a);
73
+ return oneway.execute_test(statistic, a);
74
74
  }
75
75
  /*********************************/
76
76
  /* interface define */
@@ -91,7 +91,7 @@ public class OneWayLayout {
91
91
  }
92
92
  private interface OneWayAnovaTest {
93
93
  double calcTestStatistic(double[][] xi);
94
- boolean test(double statistic, double a);
94
+ boolean execute_test(double statistic, double a);
95
95
  }
96
96
  /*********************************/
97
97
  /* class define */
@@ -251,7 +251,7 @@ public class OneWayLayout {
251
251
  * (invSumN - 1.0 / (sumN - n));
252
252
  return ln2L / deno;
253
253
  }
254
- public boolean test(double statistic, double a) {
254
+ public boolean execute_test(double statistic, double a) {
255
255
  ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(n - 1);
256
256
  double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
257
257
 
@@ -373,7 +373,7 @@ public class OneWayLayout {
373
373
  sumSb1 = stat.getSumsq() / a;
374
374
  return sumSb1 - sumSb2 * sumSb2 / (a * b);
375
375
  }
376
- public boolean test(double statistic, double a) {
376
+ public boolean execute_test(double statistic, double a) {
377
377
  FDistribution fDist = new FDistribution(a1 - 1, (a1 - 1) * (b1 - 1));
378
378
  double f = fDist.inverseCumulativeProbability(1.0 - a);
379
379
 
@@ -0,0 +1,286 @@
1
+ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
2
+ import org.apache.commons.math3.distribution.FDistribution;
3
+ import java.util.Arrays;
4
+ public class TwoWayLayout {
5
+ private static TwoWayLayout twoWay = new TwoWayLayout();
6
+ public static TwoWayLayout getInstance() {
7
+ return twoWay;
8
+ }
9
+ public boolean[] twowayAnova(double[][][] xij, double a) {
10
+ TwoWayAnovaTest twoway = new TwoWayAnova();
11
+
12
+ double[] statistic = twoway.calcTestStatistic(xij);
13
+ return twoway.execute_test(statistic, a);
14
+ }
15
+ public boolean[] twoway2Anova(double[][] xij, double a) {
16
+ TwoWay2AnovaTest twoway = new TwoWay2Anova();
17
+
18
+ double[] statistic = twoway.calcTestStatistic(xij);
19
+ return twoway.execute_test(statistic, a);
20
+ }
21
+ /*********************************/
22
+ /* interface define */
23
+ /*********************************/
24
+ private interface TwoWayAnovaTest {
25
+ double[] calcTestStatistic(double[][][] xij);
26
+ boolean[] execute_test(double statistic[], double a);
27
+ }
28
+ private interface TwoWay2AnovaTest {
29
+ double[] calcTestStatistic(double[][] xij);
30
+ boolean[] execute_test(double statistic[], double a);
31
+ }
32
+ /*********************************/
33
+ /* class define */
34
+ /*********************************/
35
+ // 二元配置の分散分析(繰り返し数が等しい時)
36
+ private class TwoWayAnova implements TwoWayAnovaTest {
37
+ private int a = 0;
38
+ private int b = 0;
39
+ private int n = 0;
40
+ private int an = 0;
41
+ private int bn = 0;
42
+ private int abn = 0;
43
+ private int en = 0;
44
+ public double[] calcTestStatistic(double[][][] xij) {
45
+ double statistic[] = new double[3];
46
+
47
+ a = xij.length;
48
+ b = xij[0].length;
49
+ n = xij[0][0].length;
50
+ an = a- 1;
51
+ bn = b - 1;
52
+ abn = (a- 1) * (b - 1);
53
+ en = a * b * (n - 1);
54
+
55
+ double[][] meanXij = calcMeanXij(xij);
56
+ double[] meanAn = calcMeanAn(meanXij);
57
+ double[] meanBn = calcMeanBn(meanXij);
58
+ double meanABn = calcMeanABn(meanAn);
59
+
60
+ double anDrift = calcAnDrift(meanAn, meanABn); // 水準Ai間変動
61
+ double bnDrift = calcBnDrift(meanBn, meanABn); // 水準Bj間変動
62
+ // 交互作用の変動
63
+ double interaDrift = calcInteraDrift(meanXij, meanAn, meanBn, meanABn);
64
+ double benchDrift = calcBenchDrift(xij, meanXij); // 水準内変動
65
+ double va = b * n * anDrift / an;
66
+ double vb = a * n * bnDrift / bn;
67
+ double vab = n * interaDrift / abn;
68
+ double ve = benchDrift / en;
69
+
70
+ statistic[0] = va / ve;
71
+ statistic[1] = vb / ve;
72
+ statistic[2] = vab/ ve;
73
+ return statistic;
74
+ }
75
+ private double[][] calcMeanXij(double[][][] xij) {
76
+ DescriptiveStatistics stat = new DescriptiveStatistics();
77
+ double[][] meanXij = new double[a][b];
78
+
79
+ for(int i = 0; i < a; i++) {
80
+ for(int j = 0; j < b; j++) {
81
+ Arrays.stream(xij[i][j]).forEach(stat::addValue);
82
+ meanXij[i][j] = stat.getMean();
83
+ stat.clear();
84
+ }
85
+ }
86
+ return meanXij;
87
+ }
88
+ private double[] calcMeanAn(double[][] meanXij) {
89
+ double[] an = new double[a];
90
+
91
+ for(int i = 0; i < a; i++) {
92
+ double sumSa = 0.0;
93
+ for(int j = 0; j < b; j++) {
94
+ an[i] += meanXij[i][j] / b;
95
+ }
96
+ }
97
+ return an;
98
+ }
99
+ private double[] calcMeanBn(double[][] meanXij) {
100
+ double[] bn = new double[b];
101
+
102
+ for(int i = 0; i < a; i++) {
103
+ for(int j = 0; j < b; j++) {
104
+ bn[j] += meanXij[i][j] / a;
105
+ }
106
+ }
107
+ return bn;
108
+ }
109
+ private double calcMeanABn(double[] meanAn) {
110
+ DescriptiveStatistics stat = new DescriptiveStatistics();
111
+
112
+ Arrays.stream(meanAn).forEach(stat::addValue);
113
+ return stat.getMean();
114
+ }
115
+ // 水準Ai間変動
116
+ private double calcAnDrift(double[] meanAn, double meanABn) {
117
+ double sumDrift = 0.0;
118
+
119
+ for(int i =0; i < meanAn.length; i++) {
120
+ double diffXi = meanAn[i] - meanABn;
121
+
122
+ sumDrift += diffXi * diffXi;
123
+ }
124
+ return sumDrift;
125
+ }
126
+ // 水準Bj間変動
127
+ private double calcBnDrift(double[] meanBn, double meanABn) {
128
+ double sumDrift = 0.0;
129
+
130
+ for(int j = 0; j < meanBn.length; j++) {
131
+ double diffXj = meanBn[j] - meanABn;
132
+
133
+ sumDrift += diffXj * diffXj;
134
+ }
135
+ return sumDrift;
136
+ }
137
+ // 交互作用の変動
138
+ private double calcInteraDrift(double[][] meanXij, double[] meanAn, double[] meanBn, double meanABn) {
139
+ double sumDrift = 0.0;
140
+
141
+ for(int i = 0; i< a; i++) {
142
+ for(int j = 0; j < b; j++) {
143
+ double diffXj = meanXij[i][j] - meanAn[i] - meanBn[j] + meanABn;
144
+
145
+ sumDrift += diffXj * diffXj;
146
+ }
147
+ }
148
+ return sumDrift;
149
+ }
150
+ // 水準内変動
151
+ private double calcBenchDrift(double[][][] xij, double[][] meanXij) {
152
+ double sumDrift = 0.0;
153
+
154
+ for(int i = 0; i < a; i++) {
155
+ for(int j = 0; j < b; j++) {
156
+ for(int k = 0; k < xij[i][j].length; k++) {
157
+ double diffXj = xij[i][j][k] - meanXij[i][j];
158
+
159
+ sumDrift += diffXj * diffXj;
160
+ }
161
+ }
162
+ }
163
+ return sumDrift;
164
+ }
165
+ public boolean[] execute_test(double statistic[], double a) {
166
+ boolean[] ret = new boolean[3];
167
+
168
+ ret[0] = evaluation(new FDistribution(an, en), statistic[0], a);
169
+ ret[1] = evaluation(new FDistribution(bn, en), statistic[1], a);
170
+ ret[2] = evaluation(new FDistribution(abn, en), statistic[2], a);
171
+ return ret;
172
+ }
173
+ private boolean evaluation(FDistribution fDist, double statistic, double a) {
174
+ double r_val = fDist.inverseCumulativeProbability(1.0 - a);
175
+
176
+ return (statistic >= r_val) ? true : false;
177
+ }
178
+ }
179
+ // 二元配置の分散分析(繰り返しのない時)
180
+ private class TwoWay2Anova implements TwoWay2AnovaTest {
181
+ private int a = 0;
182
+ private int b = 0;
183
+ private int an = 0;
184
+ private int bn = 0;
185
+ private int en = 0;
186
+ public double[] calcTestStatistic(double[][] xij) {
187
+ double statistic[] = new double[2];
188
+
189
+ a = xij.length;
190
+ b = xij[0].length;
191
+ an = a- 1;
192
+ bn = b - 1;
193
+ en = (a- 1) * (b - 1);
194
+
195
+ double[] meanAn = calcMeanAn(xij);
196
+ double[] meanBn = calcMeanBn(xij);
197
+ double meanAB = calcMeanAB(meanAn);
198
+
199
+ double anDrift = calcAnDrift(meanAn, meanAB); // 水準Ai間変動
200
+ double bnDrift = calcBnDrift(meanBn, meanAB); // 水準Bj間変動
201
+ double benchDrift = calcBenchDrift(xij, meanAn, meanBn, meanAB); // 水準内変動
202
+ double va = anDrift / an;
203
+ double vb = bnDrift / bn;
204
+ double ve = benchDrift / en;
205
+
206
+ statistic[0] = va / ve;
207
+ statistic[1] = vb / ve;
208
+ return statistic;
209
+ }
210
+ private double[] calcMeanAn(double[][] xij) {
211
+ double[] an = new double[a];
212
+
213
+ for(int i = 0; i < a; i++) {
214
+ for(int j = 0; j < b; j++) {
215
+ an[i] += xij[i][j] / b;
216
+ }
217
+ }
218
+ return an;
219
+ }
220
+ private double[] calcMeanBn(double[][] xij) {
221
+ double[] bn = new double[b];
222
+
223
+ for(int i = 0; i < a; i++) {
224
+ for(int j = 0; j < b; j++) {
225
+ bn[j] += xij[i][j] / a;
226
+ }
227
+ }
228
+ return bn;
229
+ }
230
+ private double calcMeanAB(double[] meanAn) {
231
+ DescriptiveStatistics stat = new DescriptiveStatistics();
232
+
233
+ Arrays.stream(meanAn).forEach(stat::addValue);
234
+ return stat.getMean();
235
+ }
236
+ // 水準Ai間変動
237
+ private double calcAnDrift(double[] meanAn, double meanAB) {
238
+ double sumDrift = 0.0;
239
+
240
+ for(int i =0; i < meanAn.length; i++) {
241
+ double diffXi = meanAn[i] - meanAB;
242
+
243
+ sumDrift += diffXi * diffXi;
244
+ }
245
+ return b * sumDrift;
246
+ }
247
+ // 水準Bj間変動
248
+ private double calcBnDrift(double[] meanBn, double meanAB) {
249
+ double sumDrift = 0.0;
250
+
251
+ for(int j = 0; j < meanBn.length; j++) {
252
+ double diffXj = meanBn[j] - meanAB;
253
+
254
+ sumDrift += diffXj * diffXj;
255
+ }
256
+ return a * sumDrift;
257
+ }
258
+ // 水準内変動
259
+ private double calcBenchDrift(double[][] xij, double[] meanAn, double[] meanBn, double meanAB) {
260
+ double sumDrift = 0.0;
261
+
262
+ for(int i = 0; i < a; i++) {
263
+ for(int j = 0; j < b; j++) {
264
+ double diffXj = xij[i][j] - meanAn[i] - meanBn[j] + meanAB;
265
+
266
+ sumDrift += diffXj * diffXj;
267
+ }
268
+ }
269
+ return sumDrift;
270
+ }
271
+
272
+ public boolean[] execute_test(double statistic[], double a) {
273
+ boolean[] ret = new boolean[2];
274
+
275
+ ret[0] = evaluation(new FDistribution(an, en), statistic[0], a);
276
+ ret[1] = evaluation(new FDistribution(bn, en), statistic[1], a);
277
+ return ret;
278
+ }
279
+ private boolean evaluation(FDistribution fDist, double statistic, double a) {
280
+ double r_val = fDist.inverseCumulativeProbability(1.0 - a);
281
+
282
+ return (statistic >= r_val) ? true : false;
283
+ }
284
+ }
285
+ }
286
+
data/lib/num4anova.rb CHANGED
@@ -4,6 +4,7 @@ require 'jfreechart-1.5.4.jar'
4
4
  require 'commons-math3-3.6.1.jar'
5
5
 
6
6
  java_import 'OneWayLayout'
7
+ java_import 'TwoWayLayout'
7
8
  java_import 'java.util.HashMap'
8
9
  # 分散分析を行う
9
10
  # (Apache commoms math3使用)
@@ -144,11 +145,78 @@ module Num4AnovaLib
144
145
  # [28, 50, 22, 26, 29],
145
146
  # ]
146
147
  # oneWay = Num4AnovaLib::OneWayLayoutLib.new
147
- # oneWay.replicate_test("LDH", vals)
148
+ # oneWay.replicate_test(xi, 0.05)
148
149
  # => true
149
150
  def replicate_test(xi, a)
150
151
  return @oneWay.replicateTest(xi.to_java(Java::double[]), a)
151
152
  end
152
153
  end
154
+
155
+ # 二元配置の分散分析
156
+ class TwoWayLayoutLib
157
+ def initialize
158
+ @twoWay = TwoWayLayout.getInstance()
159
+ end
160
+ # 二元配置の分散分析
161
+ # (繰り返し数が等しい時)
162
+ #
163
+ # @overload twoway_anova(xij, a)
164
+ # @param [array] xij データ(double[][][])
165
+ # @param [double] a 有意水準
166
+ # @return [Array] 検定結果(boolean[] true:棄却域内 false:棄却域外)
167
+ # @example
168
+ # xij = [
169
+ # [
170
+ # [13.2, 15.7, 11.9],
171
+ # [16.1, 15.7, 15.1],
172
+ # [9.1, 10.3, 8.2],
173
+ # ],
174
+ # [
175
+ # [22.8, 25.7, 18.5],
176
+ # [24.5, 21.2, 24.2],
177
+ # [11.9, 14.3, 13.7],
178
+ # ],
179
+ # [
180
+ # [21.8, 26.3, 32.1],
181
+ # [26.9, 31.3, 28.3],
182
+ # [15.1, 13.6, 16.2],
183
+ # ],
184
+ # [
185
+ # [25.7, 28.8, 29.5],
186
+ # [30.1, 33.8, 29.6],
187
+ # [15.2, 17.3, 14.8],
188
+ # ],
189
+ # ]
190
+ # twoWay = Num4AnovaLib::TwoWayLayoutLib.new
191
+ # twoWay.twoway_anova(xij, 0.05)
192
+ # =>
193
+ # [true, true, true]
194
+ def twoway_anova(xij, a)
195
+ ret = @twoWay.twowayAnova(xij.to_java(Java::double[][]), a)
196
+ return ret.to_a
197
+ end
198
+ # 二元配置の分散分析
199
+ # (繰り返しのない時)
200
+ #
201
+ # @overload twoway2_anova(xij, a)
202
+ # @param [array] xij データ(double[][])
203
+ # @param [double] a 有意水準
204
+ # @return [Array] 検定結果(boolean[] true:棄却域内 false:棄却域外)
205
+ # @example
206
+ # xij = [
207
+ # [13.6, 15.6, 9.2],
208
+ # [22.3, 23.3, 13.3],
209
+ # [26.7, 28.8, 15.0],
210
+ # [28.0, 31.2, 15.8],
211
+ # ]
212
+ # twoWay = Num4AnovaLib::TwoWayLayoutLib.new
213
+ # twoWay.twoway2_anova(xij, 0.05)
214
+ # =>
215
+ # [true, true]
216
+ def twoway2_anova(xij, a)
217
+ ret = @twoWay.twoway2Anova(xij.to_java(Java::double[]), a)
218
+ return ret.to_a
219
+ end
220
+ end
153
221
  end
154
222
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4anova
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-30 00:00:00.000000000 Z
11
+ date: 2024-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -63,6 +63,7 @@ files:
63
63
  - Rakefile
64
64
  - ext/num4anova/MultiComp.java
65
65
  - ext/num4anova/OneWayLayout.java
66
+ - ext/num4anova/TwoWayLayout.java
66
67
  - lib/commons-math3-3.6.1.jar
67
68
  - lib/dunnet.rb
68
69
  - lib/jcommon-1.0.23.jar