num4anova 0.0.3-java → 0.0.4-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89f91d9c210bb998f14a726f1c7147470cba19fba721724625b7ec87e4c88ad9
4
- data.tar.gz: a79f6e3ca4adc650193065ae62037cfed0580e5f7b9c5e1d06109d74f17a05d6
3
+ metadata.gz: ca0164a7092861bfca67f257129332375e90bfae762365194ced78ef75a293a7
4
+ data.tar.gz: 16f17fb717e3d92c27c4c651e034704cd81609a3adc293555eac54f9209884be
5
5
  SHA512:
6
- metadata.gz: 97a1409dfb791766292e8d6c0a49502555911529632d45b72aa6c68bc6fda7e82ed813fff4c005c9c76745a2ad05fe79d1131e23879da18afcc88d0652617c29
7
- data.tar.gz: 4cd28879971daf44d20ff8122120327e8a74519f658a22e052724af316387c057cfb315a6d1cb23324bbd08634c066c1045594d6c8804f1f87e12d31b6acd0fb
6
+ metadata.gz: e14efb373cde03fc5872f3dfb1d4186e5d2263b0b7597970d1629c562cfb6696ecf2cdbfc3325aa2d732e252241de093c156b7d9da21b51a8ea305f3e24e0e0f
7
+ data.tar.gz: c9b7f310d00d060d1c6fee3e8e9f5c43fce812a65d51b192315c72a378de7e2bc775e1118346373a93047c1cd8880240d03d52309ec0d8f567481923f14e31b4
data/CHANGELOG.md CHANGED
@@ -2,10 +2,15 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.4] - 2024-01-30
6
+ ### add
7
+ - add function of replicate_plot
8
+ - add function of replicate_test
9
+
5
10
  ## [0.0.3] - 2024-01-26
6
11
 
7
12
  ### add
8
- - add function of dunnet_test.
13
+ - add function of dunnet_test
9
14
 
10
15
  ## [0.0.2] - 2024-01-23
11
16
 
@@ -249,6 +249,7 @@ public class MultiComp {
249
249
  private double[] n = null;
250
250
  protected int getK() { return k;}
251
251
  protected int getV() { return v;}
252
+
252
253
  public double[][] calcTestStatistic(double[][] xi) {
253
254
  k = xi.length;
254
255
  mean = new double[k];
@@ -267,8 +268,10 @@ public class MultiComp {
267
268
  private double calcVe(double[][] xi) {
268
269
  double sumSq = 0.0;
269
270
  int sumN = 0;
271
+
270
272
  for(int i = 0; i < k; i++) {
271
273
  DescriptiveStatistics stat = new DescriptiveStatistics();
274
+
272
275
  Arrays.stream(xi[i]).forEach(stat::addValue);
273
276
  mean[i] = stat.getMean();
274
277
  n[i] = stat.getN();
@@ -286,7 +289,7 @@ public class MultiComp {
286
289
  int v = super.getV();
287
290
  int k = super.getK();
288
291
  double den = k - 1;
289
- double p = 1.0 - a / den;
292
+
290
293
  TDistribution tDist = new TDistribution(v);
291
294
  double l_val = tDist.inverseCumulativeProbability(a / den);
292
295
  double r_val = tDist.inverseCumulativeProbability(1.0 - a / den);
@@ -28,6 +28,7 @@ import java.util.ArrayList;
28
28
  import org.apache.commons.math3.stat.inference.OneWayAnova;
29
29
  import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
30
30
  import org.apache.commons.math3.distribution.ChiSquaredDistribution;
31
+ import org.apache.commons.math3.distribution.FDistribution;
31
32
  import java.util.Map;
32
33
  public class OneWayLayout {
33
34
  private static OneWayLayout oneWay = new OneWayLayout();
@@ -59,6 +60,18 @@ public class OneWayLayout {
59
60
  double statistic = oneway.calcTestStatistic(xi);
60
61
  return oneway.test(statistic, a);
61
62
  }
63
+ public void replicatePlot(String dname, Map<String, double[]> vals) {
64
+ ChartPlot plot = new ReplicateChartPlot();
65
+
66
+ JFreeChart chart = plot.createChart("反復測定", dname, vals);
67
+ plot.writeJPEG("replicate.jpeg", chart, 800, 500);
68
+ }
69
+ public boolean replicateTest(double[][] xi, double a) {
70
+ OneWayAnovaTest oneway = new ReplicateTest();
71
+
72
+ double statistic = oneway.calcTestStatistic(xi);
73
+ return oneway.test(statistic, a);
74
+ }
62
75
  /*********************************/
63
76
  /* interface define */
64
77
  /*********************************/
@@ -196,6 +209,7 @@ public class OneWayLayout {
196
209
  }
197
210
  }
198
211
  }
212
+ // バートレット検定
199
213
  private class BartletTest implements OneWayAnovaTest {
200
214
  private int n = 0;
201
215
  public double calcTestStatistic(double[][] xi) {
@@ -226,6 +240,7 @@ public class OneWayLayout {
226
240
  double invSumN = 0.0;
227
241
  int sumN = 0;
228
242
  DescriptiveStatistics stat = new DescriptiveStatistics();
243
+
229
244
  for(int i = 0; i < n; i++) {
230
245
  Arrays.stream(xi[i]).forEach(stat::addValue);
231
246
  invSumN += 1.0 / (stat.getN() - 1.0);
@@ -243,5 +258,127 @@ public class OneWayLayout {
243
258
  return (r_val < statistic) ? true : false;
244
259
  }
245
260
  }
261
+ // 反復測定Plot
262
+ private class ReplicateChartPlot implements ChartPlot {
263
+ public JFreeChart createChart(String title, String dname, Map<String, double[]> vals) {
264
+ CategoryPlot plot = createPlot(dname, vals);
265
+ ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
266
+ JFreeChart chart = new JFreeChart(title, plot);
267
+
268
+ ChartUtils.applyCurrentTheme(chart);
269
+ return chart;
270
+ }
271
+ private CategoryPlot createPlot( String dname, Map<String, double[]> vals) {
272
+ CreatePlot plotImpl = new ReplicatePlot();
273
+
274
+ return plotImpl.createPlot(dname, vals);
275
+ }
276
+ private class ReplicatePlot implements CreatePlot {
277
+ public CategoryPlot createPlot(String dname, Map<String, double[]> vals) {
278
+ LineAndShapeRenderer renderer = new LineAndShapeRenderer(true, true);
279
+
280
+ renderer.setDefaultToolTipGenerator(
281
+ new StandardCategoryToolTipGenerator()
282
+ );
283
+ CategoryPlot plot = new CategoryPlot();
284
+
285
+ plot.setOrientation(PlotOrientation.VERTICAL);
286
+ plot.mapDatasetToRangeAxis(0,0);
287
+ plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
288
+
289
+ /*--- 横軸 ---*/
290
+ CategoryAxis categoryAxis = new CategoryAxis("因子");
291
+ plot.setDomainAxis(categoryAxis);
292
+
293
+ /*--- 縦軸 ---*/
294
+ NumberAxis valueAxis0 = new NumberAxis("推定周辺平均");
295
+ plot.setRangeAxis(valueAxis0);
296
+
297
+ plot.setRenderer(0, renderer);
298
+ plot.setDataset(0, createDataset(dname, vals));
299
+ return plot;
300
+ }
301
+ private CategoryDataset createDataset(String dname, Map<String, double[]> vals) {
302
+ DefaultCategoryDataset data = new DefaultCategoryDataset();
303
+ DescriptiveStatistics stat = new DescriptiveStatistics();
304
+
305
+ for(Map.Entry<String, double[]> entry : vals.entrySet()) {
306
+ double[] v = entry.getValue();
307
+
308
+ Arrays.stream(v).forEach(stat::addValue);
309
+ data.addValue(stat.getMean(), dname, entry.getKey());
310
+ stat.clear();
311
+ }
312
+ return data;
313
+ }
314
+ }
315
+ }
316
+ private class ReplicateTest implements OneWayAnovaTest {
317
+ private int a1 = 0;
318
+ private int b1 = 0;
319
+ public double calcTestStatistic(double[][] xi) {
320
+ b1 = xi[0].length;
321
+ a1 = xi.length;
322
+ double st = calcSt(xi, a1, b1);
323
+ double sa = calcSa(xi, a1, b1);
324
+ double sb = calcSb(xi, a1, b1);
325
+ double se = st - sa - sb;
326
+ double meanSa = sa / (a1 - 1);
327
+ double meanSe = se / ((a1 - 1) * (b1 - 1));
328
+
329
+ return meanSa / meanSe ;
330
+ }
331
+ private double calcSt(double[][] xi, int a, int b) {
332
+ DescriptiveStatistics stat = new DescriptiveStatistics();
333
+ double sumSt1 = 0.0;
334
+ double sumSt2 = 0.0;
335
+
336
+ for(int i = 0; i < a; i++) {
337
+ Arrays.stream(xi[i]).forEach(stat::addValue);
338
+ sumSt1 += stat.getSumsq();
339
+ sumSt2 += stat.getSum();
340
+ stat.clear();
341
+ }
342
+ return sumSt1 - sumSt2 * sumSt2 / (a * b);
343
+ }
344
+ private double calcSa(double[][] xi, int a, int b) {
345
+ double sumSa1 = 0.0;
346
+ double sumSa2 = 0.0;
347
+ double[] an = new double[a];
348
+
349
+ for(int i = 0; i < a; i++) {
350
+ for(int j = 0; j < b; j++) {
351
+ an[i] += xi[i][j];
352
+ sumSa2 += xi[i][j];
353
+ }
354
+ }
355
+ DescriptiveStatistics stat = new DescriptiveStatistics();
356
+ Arrays.stream(an).forEach(stat::addValue);
357
+ sumSa1 = stat.getSumsq() / b;
358
+ return sumSa1 - sumSa2 * sumSa2 / (a * b);
359
+ }
360
+ private double calcSb(double[][] xi, int a, int b) {
361
+ double[] bn = new double[b];
362
+ double sumSb1 = 0.0;
363
+ double sumSb2 = 0.0;
364
+
365
+ for(int i = 0; i < a; i++) {
366
+ for(int j = 0; j < b; j++) {
367
+ bn[j] += xi[i][j];
368
+ sumSb2 += xi[i][j];
369
+ }
370
+ }
371
+ DescriptiveStatistics stat = new DescriptiveStatistics();
372
+ Arrays.stream(bn).forEach(stat::addValue);
373
+ sumSb1 = stat.getSumsq() / a;
374
+ return sumSb1 - sumSb2 * sumSb2 / (a * b);
375
+ }
376
+ public boolean test(double statistic, double a) {
377
+ FDistribution fDist = new FDistribution(a1 - 1, (a1 - 1) * (b1 - 1));
378
+ double f = fDist.inverseCumulativeProbability(1.0 - a);
379
+
380
+ return (statistic >= f) ? true : false;
381
+ }
382
+ }
246
383
  }
247
384
 
data/lib/num4anova.rb CHANGED
@@ -72,13 +72,13 @@ module Num4AnovaLib
72
72
  # @param [double] a 有意水準
73
73
  # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
74
74
  # @example
75
- # xi = [
76
- # [12.2, 18.8, 18.2],
77
- # [22.2, 20.5, 14.6],
78
- # [20.8, 19.5, 26.3],
79
- # [26.4, 32.5, 31.3],
80
- # [24.5, 21.2, 22.4],
81
- # ]
75
+ # xi = [
76
+ # [12.2, 18.8, 18.2],
77
+ # [22.2, 20.5, 14.6],
78
+ # [20.8, 19.5, 26.3],
79
+ # [26.4, 32.5, 31.3],
80
+ # [24.5, 21.2, 22.4],
81
+ # ]
82
82
  # oneWay = Num4AnovaLib::OneWayLayoutLib.new
83
83
  # oneWay.oneWay.oneway_anova(xi, 0.05)
84
84
  # => true
@@ -92,19 +92,63 @@ module Num4AnovaLib
92
92
  # @param [double] a 有意水準
93
93
  # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
94
94
  # @example
95
- # xi = [
96
- # [12.2, 18.8, 18.2],
97
- # [22.2, 20.5, 14.6],
98
- # [20.8, 19.5, 26.3],
99
- # [26.4, 32.5, 31.3],
100
- # [24.5, 21.2, 22.4],
101
- # ]
95
+ # xi = [
96
+ # [12.2, 18.8, 18.2],
97
+ # [22.2, 20.5, 14.6],
98
+ # [20.8, 19.5, 26.3],
99
+ # [26.4, 32.5, 31.3],
100
+ # [24.5, 21.2, 22.4],
101
+ # ]
102
102
  # oneWay = Num4AnovaLib::OneWayLayoutLib.new
103
103
  # oneWay.bartlet(xi, 0.05)
104
104
  # => true
105
105
  def bartlet(xi, a)
106
106
  return @oneWay.bartletTest(xi.to_java(Java::double[]), a)
107
107
  end
108
+ # 反復測定Plot
109
+ #
110
+ # @overload replicate_plot(dname, vals)
111
+ # @param [String] dname データ名
112
+ # @param [Hash] vals Hash(String, double[])
113
+ # @return [void] replicate.jpegファイルを出力
114
+ # @example
115
+ # vals = {
116
+ # "stageA1" => [27, 52, 18, 21, 32],
117
+ # "stageA2" => [52, 72, 31, 50, 45],
118
+ # "stageA3" => [47, 54, 29, 43, 32],
119
+ # "stageA4" => [28, 50, 22, 26, 29],
120
+ # }
121
+ # oneWay = Num4AnovaLib::OneWayLayoutLib.new
122
+ # oneWay.replicate_plot("LDH", vals)
123
+ # => replicate.jpeg
124
+ # @note
125
+ # グラフは、jfreechartを使用
126
+ def replicate_plot(dname, vals)
127
+ o = HashMap.new
128
+ vals.each{|k, v|
129
+ o[k] = v.to_java(Java::double)
130
+ }
131
+ return @oneWay.replicatePlot(dname, o)
132
+ end
133
+ # 反復測定検定
134
+ #
135
+ # @overload replicate_test(xi, a)
136
+ # @param [array] xi データ(double[][])
137
+ # @param [double] a 有意水準
138
+ # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
139
+ # @example
140
+ # xi = [
141
+ # [27, 52, 18, 21, 32],
142
+ # [52, 72, 31, 50, 45],
143
+ # [47, 54, 29, 43, 32],
144
+ # [28, 50, 22, 26, 29],
145
+ # ]
146
+ # oneWay = Num4AnovaLib::OneWayLayoutLib.new
147
+ # oneWay.replicate_test("LDH", vals)
148
+ # => true
149
+ def replicate_test(xi, a)
150
+ return @oneWay.replicateTest(xi.to_java(Java::double[]), a)
151
+ end
108
152
  end
109
153
  end
110
154
 
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.3
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-01-26 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake