forked from darrenjw/scala-glm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearRegression.scala
40 lines (29 loc) · 1.09 KB
/
LinearRegression.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
31
32
33
34
35
36
37
38
39
/*
LinearRegression.scala
Linear regression modelling example
*/
import scalaglm.*
import breeze.linalg.*
@main def linearRegression() =
val url = "http://archive.ics.uci.edu/ml/machine-learning-databases/00291/airfoil_self_noise.dat"
val fileName = "self-noise.csv"
// download the file to disk if it hasn't been already
val file = new java.io.File(fileName)
if !file.exists then
val s = new java.io.PrintWriter(file)
val data = scala.io.Source.fromURL(url).getLines()
data.foreach(l => s.write(l.trim.split('\t').filter(_ != "").mkString("", ",", "\n")))
s.close
// read the file from disk
val mat = csvread(file)
println("Dim: " + mat.rows + " " + mat.cols)
val fig = Utils.pairs(mat, List("Freq", "Angle", "Chord", "Velo", "Thick", "Sound"))
val y = mat(::, 5) // response is the final column
val X = mat(::, 0 to 4)
val mod = Lm(y, X, List("Freq", "Angle", "Chord", "Velo", "Thick"))
mod.summary
mod.plots.saveas("LinearRegression.png")
// test without name list
Lm(y,X,false).summary
Lm(y,X).summary
// eof