Skip to content

Commit

Permalink
added diagnostic plots
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenjw committed Jun 4, 2017
1 parent 6935976 commit 4ddc9e0
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/api/index.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/api/scalaglm/Glm.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,22 @@ <h3>Value Members</h3>

<p class="shortcomment cmt">p-values for the regression coefficients
</p>
</li><li name="scalaglm.Glm#plots" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="plots:breeze.plot.Figure"></a><a id="plots:Figure"></a>
<span class="permalink">
<a href="../scalaglm/Glm.html#plots:breeze.plot.Figure" title="Permalink">
<i class="material-icons"></i>
</a>
</span>
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">plots</span><span class="result">: <span class="extype" name="breeze.plot.Figure">Figure</span></span>
</span>


</li><li name="scalaglm.Glm#pp" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="pp:Int"></a>
<span class="permalink">
Expand Down
16 changes: 16 additions & 0 deletions docs/api/scalaglm/Lm.html
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,22 @@ <h3>Value Members</h3>

<p class="shortcomment cmt">The p-value associated with the f-statistic
</p>
</li><li name="scalaglm.Lm#plots" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="plots:breeze.plot.Figure"></a><a id="plots:Figure"></a>
<span class="permalink">
<a href="../scalaglm/Lm.html#plots:breeze.plot.Figure" title="Permalink">
<i class="material-icons"></i>
</a>
</span>
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">plots</span><span class="result">: <span class="extype" name="breeze.plot.Figure">Figure</span></span>
</span>


</li><li name="scalaglm.Lm#pp" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="pp:Int"></a>
<span class="permalink">
Expand Down
17 changes: 17 additions & 0 deletions docs/api/scalaglm/Pca.html
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,23 @@ <h3>Value Members</h3>
</span>

<p class="shortcomment cmt">Number of variables
</p>
</li><li name="scalaglm.Pca#plots" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="plots:breeze.plot.Figure"></a><a id="plots:Figure"></a>
<span class="permalink">
<a href="../scalaglm/Pca.html#plots:breeze.plot.Figure" title="Permalink">
<i class="material-icons"></i>
</a>
</span>
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">plots</span><span class="result">: <span class="extype" name="breeze.plot.Figure">Figure</span></span>
</span>

<p class="shortcomment cmt">Diagnostic plots for the PCA
</p>
</li><li name="scalaglm.Pca#propvar" visbl="pub" class="indented0 " data-isabs="false" fullComment="no" group="Ungrouped">
<a id="propvar:breeze.linalg.DenseVector[Double]"></a><a id="propvar:DenseVector[Double]"></a>
Expand Down
36 changes: 36 additions & 0 deletions docs/api/scalaglm/Utils$.html
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,42 @@ <h3>Value Members</h3>
</span>

<div class="fullcomment"><dl class="attributes block"> <dt>Definition Classes</dt><dd>AnyRef</dd></dl></div>
</li><li name="scalaglm.Utils#pairs" visbl="pub" class="indented0 " data-isabs="false" fullComment="yes" group="Ungrouped">
<a id="pairs(mat:breeze.linalg.DenseMatrix[Double]):breeze.plot.Figure"></a><a id="pairs(DenseMatrix[Double]):Figure"></a>
<span class="permalink">
<a href="../scalaglm/Utils$.html#pairs(mat:breeze.linalg.DenseMatrix[Double]):breeze.plot.Figure" title="Permalink">
<i class="material-icons"></i>
</a>
</span>
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">pairs</span><span class="params">(<span name="mat">mat: <span class="extype" name="breeze.linalg.DenseMatrix">DenseMatrix</span>[<span class="extype" name="scala.Double">Double</span>]</span>)</span><span class="result">: <span class="extype" name="breeze.plot.Figure">Figure</span></span>
</span>

<p class="shortcomment cmt">pairs plot (for unlabeled variables)
</p><div class="fullcomment"><div class="comment cmt"><p>pairs plot (for unlabeled variables)
</p></div><dl class="paramcmts block"><dt class="param">mat</dt><dd class="cmt"><p>a matrix with variables in columns and observations in rows</p></dd><dt>returns</dt><dd class="cmt"><p>The breeze-viz Figure object</p></dd></dl></div>
</li><li name="scalaglm.Utils#pairs" visbl="pub" class="indented0 " data-isabs="false" fullComment="yes" group="Ungrouped">
<a id="pairs(mat:breeze.linalg.DenseMatrix[Double],names:Seq[String]):breeze.plot.Figure"></a><a id="pairs(DenseMatrix[Double],Seq[String]):Figure"></a>
<span class="permalink">
<a href="../scalaglm/Utils$.html#pairs(mat:breeze.linalg.DenseMatrix[Double],names:Seq[String]):breeze.plot.Figure" title="Permalink">
<i class="material-icons"></i>
</a>
</span>
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">pairs</span><span class="params">(<span name="mat">mat: <span class="extype" name="breeze.linalg.DenseMatrix">DenseMatrix</span>[<span class="extype" name="scala.Double">Double</span>]</span>, <span name="names">names: <span class="extype" name="scala.Seq">Seq</span>[<span class="extype" name="scala.Predef.String">String</span>]</span>)</span><span class="result">: <span class="extype" name="breeze.plot.Figure">Figure</span></span>
</span>

<p class="shortcomment cmt">pairs plot
</p><div class="fullcomment"><div class="comment cmt"><p>pairs plot
</p></div><dl class="paramcmts block"><dt class="param">mat</dt><dd class="cmt"><p>a matrix with variables in columns and observations in rows</p></dd><dt class="param">names</dt><dd class="cmt"><p>a sequence of variable names</p></dd><dt>returns</dt><dd class="cmt"><p>The breeze-viz Figure object</p></dd></dl></div>
</li><li name="scala.AnyRef#synchronized" visbl="pub" class="indented0 " data-isabs="false" fullComment="yes" group="Ungrouped">
<a id="synchronized[T0](x$1:=&gt;T0):T0"></a><a id="synchronized[T0](⇒T0):T0"></a>
<span class="permalink">
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/scalaglm/Glm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ case class Glm(y: DenseVector[Double],

lazy val fitted = predict(response = true).fitted

import breeze.plot._
def plots: Figure = {
val fig = Figure("Linear regression diagnostics")
val p0 = fig.subplot(1,1,0)
p0 += plot(fitted,y,'+')
p0 += plot(fitted,fitted)
p0.title = "Observations against fitted values"
p0.xlabel = "Fitted value"
p0.ylabel = "Observation"
fig
}


} // case class Glm

object Glm {
Expand Down
17 changes: 16 additions & 1 deletion src/main/scala/scalaglm/Lm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,22 @@ case class Lm(y: DenseVector[Double],
import breeze.plot._
def plots: Figure = {
val fig = Figure("Linear regression diagnostics")

val p0 = fig.subplot(2,2,0)
p0 += plot(fitted,y,'+')
p0 += plot(fitted,fitted)
p0.title = "Observations against fitted values"
p0.xlabel = "Fitted value"
p0.ylabel = "Observation"
val p1 = fig.subplot(2,2,1)
p1 += plot(fitted,residuals,'+')
p1.title = "Residuals against fitted values"
p1.xlabel = "Fitted value"
p1.ylabel = "Residual"
val p2 = fig.subplot(2,2,2)
p2 += breeze.plot.hist(residuals)
p2.title = "Histogram of residuals"
p2.xlabel = "Residual"
// TODO: Add a Q-Q plot..
fig
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/scala/scalaglm/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ object Utils {
}


import breeze.plot._
/**
* pairs plot
*
* @param mat a matrix with variables in columns and observations in rows
* @param names a sequence of variable names
*
* @return The breeze-viz Figure object
*/
def pairs(mat: DenseMatrix[Double], names: Seq[String]): Figure = {
require(mat.cols == names.length)
val fig = Figure("Scatterplot matrix")
val p = mat.cols
(0 until p).foreach{i =>
(0 until p).foreach {j =>
val pij = fig.subplot(p,p,i*p+j)
if (i == j) {
pij += hist(mat(::,i))
pij.title = names(i)
pij.xlabel = i.toString
} else {
pij += plot(mat(::,j),mat(::,i),'.')
pij.xlabel = j.toString
pij.ylabel = i.toString
}
}
}
fig
}

/**
* pairs plot (for unlabeled variables)
*
* @param mat a matrix with variables in columns and observations in rows
*
* @return The breeze-viz Figure object
*/
def pairs(mat: DenseMatrix[Double]): Figure = {
val names = (1 to mat.cols) map ("V%02d".format(_))
pairs(mat, names)
}


/**
* Example of a main runner method - not for general use - will probably get removed
* in due course
Expand Down

0 comments on commit 4ddc9e0

Please sign in to comment.