[GRASS-SVN] r53742 - in grass/trunk: include/defs lib/stats raster/r.series

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Nov 9 03:28:03 PST 2012


Author: glynn
Date: 2012-11-09 03:28:03 -0800 (Fri, 09 Nov 2012)
New Revision: 53742

Modified:
   grass/trunk/include/defs/stats.h
   grass/trunk/lib/stats/c_reg.c
   grass/trunk/raster/r.series/main.c
   grass/trunk/raster/r.series/r.series.html
Log:
Add t-value for linear regression


Modified: grass/trunk/include/defs/stats.h
===================================================================
--- grass/trunk/include/defs/stats.h	2012-11-08 23:35:44 UTC (rev 53741)
+++ grass/trunk/include/defs/stats.h	2012-11-09 11:28:03 UTC (rev 53742)
@@ -22,6 +22,7 @@
 extern stat_func c_reg_m;
 extern stat_func c_reg_c;
 extern stat_func c_reg_r2;
+extern stat_func c_reg_t;
 extern stat_func c_quart1;
 extern stat_func c_quart3;
 extern stat_func c_perc90;
@@ -42,6 +43,7 @@
 extern stat_func_w w_reg_m;
 extern stat_func_w w_reg_c;
 extern stat_func_w w_reg_r2;
+extern stat_func_w w_reg_t;
 extern stat_func_w w_stddev;
 extern stat_func_w w_sum;
 extern stat_func_w w_var;

Modified: grass/trunk/lib/stats/c_reg.c
===================================================================
--- grass/trunk/lib/stats/c_reg.c	2012-11-08 23:35:44 UTC (rev 53741)
+++ grass/trunk/lib/stats/c_reg.c	2012-11-09 11:28:03 UTC (rev 53742)
@@ -1,15 +1,22 @@
+#include <math.h>
+
 #include <grass/gis.h>
 #include <grass/raster.h>
 
-#define REGRESSION_SLOPE	0
-#define REGRESSION_OFFSET	1
-#define REGRESSION_COEFF_DET	2
+enum {
+    REGRESSION_SLOPE     = 0,
+    REGRESSION_OFFSET    = 1,
+    REGRESSION_COEFF_DET = 2,
+    REGRESSION_T         = 3,
+    REGRESSION_P         = 4
+};
 
 static void regression(DCELL * result, DCELL * values, int n, int which)
 {
     DCELL xsum, ysum;
     DCELL xbar, ybar;
     DCELL numer, denom, denom2;
+    DCELL Rsq;
     int count;
     int i;
 
@@ -42,16 +49,16 @@
     denom = 0.0;
     for (i = 0; i < n; i++)
 	if (!Rast_is_d_null_value(&values[i]))
-	    denom += (DCELL) i *i;
-
+	    denom += (DCELL) i * i;
     denom -= count * xbar * xbar;
 
-    if (which == REGRESSION_COEFF_DET) {
+    if (which >= REGRESSION_COEFF_DET) {
 	denom2 = 0.0;
 	for (i = 0; i < n; i++)
 	    if (!Rast_is_d_null_value(&values[i]))
 		denom2 += values[i] * values[i];
 	denom2 -= count * ybar * ybar;
+	Rsq = (numer * numer) / (denom * denom2);
     }
 
     switch (which) {
@@ -62,8 +69,11 @@
 	*result = ybar - xbar * numer / denom;
 	break;
     case REGRESSION_COEFF_DET:
-	*result = (numer * numer) / (denom * denom2);
+	*result = Rsq;
 	break;
+    case REGRESSION_T:
+	*result = sqrt(Rsq * (count - 2) / (1 - Rsq));
+	break;
     default:
 	Rast_set_d_null_value(result, 1);
 	break;
@@ -89,11 +99,17 @@
     regression(result, values, n, REGRESSION_COEFF_DET);
 }
 
+void c_reg_t(DCELL * result, DCELL * values, int n, const void *closure)
+{
+    regression(result, values, n, REGRESSION_T);
+}
+
 static void regression_w(DCELL * result, DCELL(*values)[2], int n, int which)
 {
     DCELL xsum, ysum;
     DCELL xbar, ybar;
     DCELL numer, denom, denom2;
+    DCELL Rsq;
     int count;
     int i;
 
@@ -136,6 +152,7 @@
 	    if (!Rast_is_d_null_value(&values[i][0]))
 		denom2 += values[i][0] * values[i][0] * values[i][1];
 	denom2 -= count * ybar * ybar;
+	Rsq = (numer * numer) / (denom * denom2);
     }
 
     switch (which) {
@@ -146,8 +163,11 @@
 	*result = ybar - xbar * numer / denom;
 	break;
     case REGRESSION_COEFF_DET:
-	*result = (numer * numer) / (denom * denom2);
+	*result = Rsq;
 	break;
+    case REGRESSION_T:
+	*result = sqrt(Rsq * (count - 2) / (1 - Rsq));
+	break;
     default:
 	Rast_set_d_null_value(result, 1);
 	break;
@@ -172,3 +192,8 @@
 {
     regression_w(result, values, n, REGRESSION_COEFF_DET);
 }
+
+void w_reg_t(DCELL * result, DCELL(*values)[2], int n, const void *closure)
+{
+    regression_w(result, values, n, REGRESSION_T);
+}

Modified: grass/trunk/raster/r.series/main.c
===================================================================
--- grass/trunk/raster/r.series/main.c	2012-11-08 23:35:44 UTC (rev 53741)
+++ grass/trunk/raster/r.series/main.c	2012-11-09 11:28:03 UTC (rev 53742)
@@ -45,6 +45,7 @@
     {c_reg_m,  0, "slope",      "linear regression slope"},
     {c_reg_c,  0, "offset",     "linear regression offset"},
     {c_reg_r2, 0, "detcoeff",   "linear regression coefficient of determination"},
+    {c_reg_t,  0, "tvalue",     "linear regression t-value"},
     {c_quart1, 0, "quart1",     "first quartile"},
     {c_quart3, 0, "quart3",     "third quartile"},
     {c_perc90, 0, "perc90",     "ninetieth percentile"},

Modified: grass/trunk/raster/r.series/r.series.html
===================================================================
--- grass/trunk/raster/r.series/r.series.html	2012-11-08 23:35:44 UTC (rev 53741)
+++ grass/trunk/raster/r.series/r.series.html	2012-11-09 11:28:03 UTC (rev 53742)
@@ -19,6 +19,7 @@
  <li>slope: linear regression slope
  <li>offset: linear regression offset
  <li>detcoeff: linear regression coefficient of determination
+ <li>tvalue: linear regression t-value
  <li>min_raster: raster map number with the minimum time-series value
  <li>max_raster: raster map number with the maximum time-series value
  </ul> 
@@ -45,7 +46,7 @@
 The <em>low,high</em> thresholds are floating point, so use <em>-inf</em> or
 <em>inf</em> for a single threshold (e.g., <em>range=0,inf</em> to ignore
 negative values, or <em>range=-inf,-200.4</em> to ignore values above -200.4).
-<p>Linear regression (slope, offset, coefficient of determination) assumes equal time intervals.
+<p>Linear regression (slope, offset, coefficient of determination, t-value) assumes equal time intervals.
 If the data have irregular time intervals, NULL raster maps can be inserted into time series
 to make time intervals equal (see example).
 <p>Number of raster maps to be processed is given by the limit of the



More information about the grass-commit mailing list