More Related Content
Similar to linieaire regressie
Similar to linieaire regressie (20)
linieaire regressie
- 1. <?php
/**
* Description of Regression
* formule = y = ax + b
* @author peltenburg
*/
include_once("../PrognoseCreatorGemeenten/DBConnection.php");
include_once("../PrognoseCreatorGemeenten/aantalJongeren.php");
class Regression {
private $xAx_Points;
private $yAx_Points;
public function __construct($x, $y) {
$db_handle = DBConnect::getInstance();
$aantalJongeren = new aantalJongeren($db_handle);
$jaar = $aantalJongeren->showJaar();
$this->xAx_Points = $jaar;
$totaalAantalJongeren = $aantalJongeren->showAantalJongeren();
$this->yAx_Points = $totaalAantalJongeren;
}
public function NumberOfMeasurePoints($x) {
// calculate number points
$x = $this->xAx_Points;
if (isset($_POST['PrognoseJaar'])) {
$xIngevoerd = (double) $_POST['PrognoseJaar'];
array_push($x, $xIngevoerd);
$s = array_unique($x);
$n = count($s);
return $n;
} else {
$_POST['PrognoseJaar'] = 2012;
$xIngevoerd = (double) $_POST['PrognoseJaar'];
array_push($x, $xIngevoerd);
$s = array_unique($x);
$n = count($s);
return $n;
}
}
public function checkArrayLength($y) {
// ensure both arrays of points are the same size
$y = $this->yAx_Points;
array_push($y, (double) $this->calculateY());
$n = $this->NumberOfMeasurePoints($x);
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.",
E_USER_ERROR);
} else {
- 2. echo "arrays are of the same length";
}
}
public function y_Sum() {
$y = $this->yAx_Points;
$y_sum = array_sum($y);
return $y_sum;
}
public function x_sum() {
$x = $this->xAx_Points;
$x_sum = array_sum($x);
return $x_sum;
}
public function calculateA() {
/*
* a= (y2-y1)/(x2-x1)(waarde y op meetmoment 2 - waarde y op meetmoment 1)
*
* als de lijn over meerdere punten wordt berekend moet je gebruikmaken van
* a = SOM (x-x(gem))) * (y - y(gem)) / SOM (x - x (gem))
*/
$y = $this->yAx_Points;
$x = $this->xAx_Points;
$y2 = end($y);
$y1 = $y[0];
$x2 = end($x);
$x1 = $x[0];
$n = $this->NumberOfMeasurePoints($x) - 1;
$xGem = $this->x_sum() / $n;
$totaal_xGem = array();
for ($i = 0; $i < $n; $i++) {
$x_xGem = $x[$i] - $xGem;
$totaal_xGem[] = $x_xGem;
}
$x_x_gem_2 = array();
for ($i = 0; $i < count($totaal_xGem); $i++) {
$x_x_gem_2[] = $totaal_xGem[$i] * $totaal_xGem[$i];
}
$sum_x_x_gem_2 = array_sum($x_x_gem_2);
$n = $this->NumberOfMeasurePoints($x) - 1;
$yGem = $this->y_Sum() / $n;
$totaal_yGem = array();
for ($i = 0; $i < $n; $i++) {
$y_yGem = $y[$i] - $yGem;
$totaal_yGem[] = $y_yGem;
}
- 3. $x_xGemXy_yGem = array();
for ($i = 0; $i < $n; $i++) {
$x_xGemXy_yGem[] = $totaal_yGem[$i] * $totaal_xGem[$i];
}
$Sum_x_xGemXy_yGem = array_sum($x_xGemXy_yGem);
$a = $Sum_x_xGemXy_yGem / $sum_x_x_gem_2;
return $a;
}
public function calculateB() {
// calculate intercept
/*
* b = y(gem)-ax(gem)
*/
$x = $this->xAx_Points;
$x_sum = $this->x_sum();
$y_sum = $this->y_sum();
$n = $this->NumberOfMeasurePoints($this->xAx_Points) - 1;
$a = $this->calculateA();
$yMean = (double) $y_sum / $n;
$xMean = (double) $x_sum / $n;
$aTimesXmean = (double) (($a) * $xMean);
$bTotaal = $yMean - ($aTimesXmean);
return $bTotaal;
}
public function calculateY() {
/*
* y = ax+b
*/
$a = $this->calculateA();
$b = $this->calculateB();
$x = $this->xAx_Points;
$xIngevoerd = (double) $_POST['PrognoseJaar'];
array_push($x, $xIngevoerd);
$y = $a * end($x) + $b;
return $y;
}
}