198 lines
22 KiB
HTML
198 lines
22 KiB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 18. Miscellaneous formatting and tools</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="pt04.html" title="Part IV. Creating linear and non-linear graphs"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 18. Miscellaneous formatting and tools</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Part IV. Creating linear and non-linear graphs</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="chapter" title="Chapter 18. Miscellaneous formatting and tools"><div class="titlepage"><div><div><h2 class="title"><a name="id2578622"></a>Chapter 18. Miscellaneous formatting and tools</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch18.html#sec.linear-regression-analysis">18.1. Linear regression analysis</a></span></dt></dl></div>
|
||
|
||
<p>This chapter tries to collect all other formatting options (and tricks) available in
|
||
the library that doesn't really fit anywhere else</p>
|
||
<div class="sect1" title="Linear regression analysis"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec.linear-regression-analysis"></a>Linear regression analysis</h2></div></div></div>
|
||
|
||
<p>The library offers support to do basic linear regression analysis with the help of
|
||
the utility : <code class="code">class LinearRegression</code> defined in the utility module
|
||
"<code class="filename">jpgraph_utils.inc.php</code>". With this class it is possible to
|
||
make a linear estimation of data and calculate some of the basic statistics of the
|
||
data, i.e. correlation coefficient and standard error.</p>
|
||
<p>The <code class="code">LinearRegression</code> class is instantiated with the data to be
|
||
analyzed and it is then possible to both get hold of the statistics that corresponds
|
||
to the data and also to automatically calculate a range of estimated y-values for a
|
||
given set of x-coordinates.</p>
|
||
<p>The following code snippet shows how to instantiate a regression analysis with
|
||
some data</p>
|
||
<p>
|
||
</p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code">
|
||
</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">jpgraph_utils.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Some data to be analyzed (both x, and y-data must be specified)</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$datay</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-code"> ... </span><span class="hl-brackets">)</span><span class="hl-code"> ;
|
||
</span><span class="hl-var">$datax</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-code"> ... </span><span class="hl-brackets">)</span><span class="hl-code"> ;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Instantiate the linear regression class</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$linreg</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">LinearRegression</span><span class="hl-brackets">(</span><span class="hl-var">$datax</span><span class="hl-code">, </span><span class="hl-var">$datay</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Get the basic statistics</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-reserved">list</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$stderr</span><span class="hl-code">, </span><span class="hl-var">$corr</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> = </span><span class="hl-var">$linreg</span><span class="hl-code">-></span><span class="hl-identifier">GetStat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Get a set of estimated y-value for x-values in range [0,20]</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-reserved">list</span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">, </span><span class="hl-var">$esty</span><span class="hl-brackets">)</span><span class="hl-code"> = </span><span class="hl-var">$linreg</span><span class="hl-code">-></span><span class="hl-identifier">GetY</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">20</span><span class="hl-brackets">)</span><span class="hl-code">
|
||
|
||
...
|
||
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div><p>
|
||
</p>
|
||
<p>The methods available in the <code class="code">LinearRegression</code> class that can be used
|
||
are</p>
|
||
<p>
|
||
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
|
||
<p><code class="code">LinearRegression::GetStat()</code></p>
|
||
<p>Returns an array with (standard error, correlation coefficient,
|
||
determination coefficient)</p>
|
||
<p>The closer the correlation coefficient is to 1 the more of the data
|
||
variation can be explained by a linear estimate.</p>
|
||
</li><li class="listitem">
|
||
<p><code class="code">LinearRegression::GetAB()</code></p>
|
||
<p>Return an array of the linear coefficients (a,b) where the linear
|
||
estimation is <span class="italic">y = a + b*x</span></p>
|
||
</li><li class="listitem">
|
||
<p><code class="code">LinearRegression::GetY($aMinX, $aMaxX, $aStep=1)</code></p>
|
||
<p>Return an array with (xdata, ydata) corresponding to an x-range
|
||
between x values in range [<code class="code">$aMinX</code>, <code class="code">$aMaxX</code>]
|
||
with steps of <code class="code">$aStep</code></p>
|
||
</li></ul></div><p>
|
||
</p>
|
||
<p>The following example shows how to use this utility class to plot both the
|
||
original data as well as the estimated linear line.</p>
|
||
<p>
|
||
</p><div class="example"><a name="example.example16.6"></a><p class="title"><b>Example 18.1. Linear regression using utility class (<code class="filename">example16.6.php</code>) </b></p><div class="example-contents"> <div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>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
|
||
40
|
||
41
|
||
42
|
||
43
|
||
44
|
||
45
|
||
46
|
||
47
|
||
48
|
||
49
|
||
50
|
||
51
|
||
52
|
||
53
|
||
54
|
||
55
|
||
56
|
||
57
|
||
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content="text/plain; charset=utf-8"</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph_scatter.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph_line.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph_utils.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Create some "fake" regression data</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$datay</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$datax</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$a</span><span class="hl-code">= </span><span class="hl-number">3</span><span class="hl-number">.2</span><span class="hl-code">;
|
||
</span><span class="hl-var">$b</span><span class="hl-code">= </span><span class="hl-number">2</span><span class="hl-number">.5</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-var">$x</span><span class="hl-code"> < </span><span class="hl-number">20</span><span class="hl-code">; ++</span><span class="hl-var">$x</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
|
||
</span><span class="hl-var">$datax</span><span class="hl-brackets">[</span><span class="hl-var">$x</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$x</span><span class="hl-code">;
|
||
</span><span class="hl-var">$datay</span><span class="hl-brackets">[</span><span class="hl-var">$x</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$a</span><span class="hl-code"> + </span><span class="hl-var">$b</span><span class="hl-code">*</span><span class="hl-var">$x</span><span class="hl-code"> + </span><span class="hl-identifier">rand</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-number">20</span><span class="hl-code">,</span><span class="hl-number">20</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-brackets">}</span><span class="hl-code">
|
||
|
||
</span><span class="hl-var">$lr</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">LinearRegression</span><span class="hl-brackets">(</span><span class="hl-var">$datax</span><span class="hl-code">, </span><span class="hl-var">$datay</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">list</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$stderr</span><span class="hl-code">, </span><span class="hl-var">$corr</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> = </span><span class="hl-var">$lr</span><span class="hl-code">-></span><span class="hl-identifier">GetStat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-reserved">list</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$xd</span><span class="hl-code">, </span><span class="hl-var">$yd</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> = </span><span class="hl-var">$lr</span><span class="hl-code">-></span><span class="hl-identifier">GetY</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">19</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Create the graph</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$graph</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Graph</span><span class="hl-brackets">(</span><span class="hl-number">300</span><span class="hl-code">,</span><span class="hl-number">250</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">SetScale</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">linlin</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup title</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">title</span><span class="hl-code">-></span><span class="hl-identifier">Set</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Linear regression</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">title</span><span class="hl-code">-></span><span class="hl-identifier">SetFont</span><span class="hl-brackets">(</span><span class="hl-identifier">FF_ARIAL</span><span class="hl-code">,</span><span class="hl-identifier">FS_BOLD</span><span class="hl-code">,</span><span class="hl-number">14</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">subtitle</span><span class="hl-code">-></span><span class="hl-identifier">Set</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">(stderr=</span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-identifier">sprintf</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%.2f</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$stderr</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">, corr=</span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-identifier">sprintf</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%.2f</span><span class="hl-quotes">'</span><span class="hl-code">,</span><span class="hl-var">$corr</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">)</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">subtitle</span><span class="hl-code">-></span><span class="hl-identifier">SetFont</span><span class="hl-brackets">(</span><span class="hl-identifier">FF_ARIAL</span><span class="hl-code">,</span><span class="hl-identifier">FS_NORMAL</span><span class="hl-code">,</span><span class="hl-number">12</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> make sure that the X-axis is always at the</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> bottom at the plot and not just at Y=0 which is</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> the default position</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">xaxis</span><span class="hl-code">-></span><span class="hl-identifier">SetPos</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">min</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Create the scatter plot with some nice colors</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ScatterPlot</span><span class="hl-brackets">(</span><span class="hl-var">$datay</span><span class="hl-code">,</span><span class="hl-var">$datax</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code">-></span><span class="hl-identifier">mark</span><span class="hl-code">-></span><span class="hl-identifier">SetType</span><span class="hl-brackets">(</span><span class="hl-identifier">MARK_FILLEDCIRCLE</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code">-></span><span class="hl-identifier">mark</span><span class="hl-code">-></span><span class="hl-identifier">SetFillColor</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">red</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code">-></span><span class="hl-identifier">SetColor</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">blue</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code">-></span><span class="hl-identifier">SetWeight</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$sp1</span><span class="hl-code">-></span><span class="hl-identifier">mark</span><span class="hl-code">-></span><span class="hl-identifier">SetWidth</span><span class="hl-brackets">(</span><span class="hl-number">4</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Create the regression line</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$lplot</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">LinePlot</span><span class="hl-brackets">(</span><span class="hl-var">$yd</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$lplot</span><span class="hl-code">-></span><span class="hl-identifier">SetWeight</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$lplot</span><span class="hl-code">-></span><span class="hl-identifier">SetColor</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">navy</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> Add the pltos to the line</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-var">$sp1</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-var">$lplot</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-comment">//</span><span class="hl-comment"> ... and stroke</span><span class="hl-comment"></span><span class="hl-code">
|
||
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
|
||
|
||
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div></div></div><p><br class="example-break"> </p><div class="figure"><a name="fig.example16.6"></a><p class="title"><b>Figure 18.1. Linear regression using utility class <code class="uri"><a class="uri" href="example_src/example16.6.html" target="_top">(<code class="filename">example16.6.php</code>)</a></code> </b></p><div class="figure-contents"> <span class="inlinemediaobject"><img src="images/example16.6.png" alt="Linear regression using utility class (example16.6.php)"></span> </div></div><p><br class="figure-break">
|
||
</p>
|
||
</div>
|
||
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="pt04.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>
|