forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANOVA.scala
30 lines (22 loc) · 924 Bytes
/
ANOVA.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Wei Chen - ANOVA
// 2017-08-30
package com.scalaml.algorithm
class ANOVA(data: Array[Array[Double]]) {
private val n = data.size // number of groups
private val avg_i = data.map(a => a.sum / a.size) // average of each group
private val avg_a = avg_i.sum / n // average all
// between-groups sum of squares
val ssg: Double = data.zip(avg_i).map { case (a, i) => a.size * Math.pow(i - avg_a, 2) }.sum
// within-groups sum of squares (error)
val sse: Double = data.zip(avg_i).map { case (a, i) => a.map(d => Math.pow(d - i, 2)).sum }.sum
// between-groups' degree of freedom
val dfg: Double = n - 1
// within-groups' degree of freedom (error)
val dfe: Double = data.map(_.size).sum - n
// between-groups mean square
val msg: Double = ssg / dfg
// within-groups mean square (error)
val mse: Double = sse / dfe
// f value
val f: Double = msg / mse
}