[GRASS-SVN] r56324 - in grass-addons/grass7/raster: . r.vif

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 20 05:02:09 PDT 2013


Author: pvanbosgeo
Date: 2013-05-20 05:02:08 -0700 (Mon, 20 May 2013)
New Revision: 56324

Added:
   grass-addons/grass7/raster/r.vif/
   grass-addons/grass7/raster/r.vif/r.vif
   grass-addons/grass7/raster/r.vif/r.vif.html
Log:
A shell script that calculates the variance inflation factor (VIF) for a set of input variables (rasters)

Added: grass-addons/grass7/raster/r.vif/r.vif
===================================================================
--- grass-addons/grass7/raster/r.vif/r.vif	                        (rev 0)
+++ grass-addons/grass7/raster/r.vif/r.vif	2013-05-20 12:02:08 UTC (rev 56324)
@@ -0,0 +1,161 @@
+#!/bin/sh
+# 
+#set -x
+########################################################################
+# 
+# MODULE:       r.vif
+# AUTHOR(S):    Paulo van Breugel <p.vanbreugel AT gmail.com>
+# PURPOSE:      Calculate the variance inflaction factor of set of
+#				variables
+# Dependency:	r.regression.multi
+#
+# COPYRIGHT: (C) 2013 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+# 
+#            This program is free software under the GNU General Public 
+#            License (>=v2). Read the file COPYING that comes with GRASS 
+#            for details. 
+# 
+########################################################################
+#
+#%Module 
+#% description: Calculate the variance inflaction factor
+#%End 
+
+#%option
+#% key: maps
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: variables
+#% key_desc: name
+#% required: yes
+#% multiple: yes
+#%end
+
+#%option
+#% key: output_file
+#% type: string
+#% gisprompt: new
+#% description: Name of output text file
+#% key_desc: name
+#% required: no
+#%end
+
+
+#=======================================================================
+## GRASS team recommandations
+#=======================================================================
+
+## Check if in GRASS
+if  [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+
+## check for awk
+if [ ! -x "$(which awk)" ] ; then
+    g.message -e "<awk> required, please install <awk> or <gawk> first"
+    exit 1
+fi
+
+## To parse the code into interactive menu
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+## set environment so that awk works properly in all languages ##
+unset LC_ALL
+export LC_NUMERIC=C
+
+## what to do in case of user break:
+exitprocedure()
+{
+    echo "User break!"
+    cleanup
+    exit 1
+}
+
+## shell check for user break (signal list: trap -l)
+trap "exitprocedure" 2 3 15
+
+#=======================================================================
+## function
+#=======================================================================
+
+cleanup()
+{
+   g.mremove -f rast="rnicheOVERLAP$$_*" --quiet
+   g.mremove -f rast="rnicheEQUIV$$_*" --quiet
+}
+trap "cleanup" 2 3 15
+
+
+#=======================================================================
+## Check if input maps exist
+#=======================================================================
+
+OPF=${GIS_OPT_OUTPUT_FILE}
+MAPS=${GIS_OPT_MAPS}
+
+# test for missing input raster maps
+oIFS=$IFS
+IFS=,
+for nvar in $MAPS ; do
+    tstIN=`echo $nvar | awk 'BEGIN{FS="@"}{print $1}'`
+    g.findfile element=cell file=${tstIN} > /dev/null
+    if [ $? -gt 0 ] ; then 
+        g.message -e 'The map '${tstIN}' is missing'
+    exit 1
+    fi
+done
+IFS=$oIFS
+unset tstIN
+
+#=======================================================================
+## Test if file exists. If so, append _v1 to file name
+#=======================================================================
+
+k=1
+OPFN=$OPF
+while [ -f "$OPF" ]; do
+   a1=`echo $OPFN | awk -F. '{print $1}'`
+   a2=`echo $OPFN | awk -F. '{print $2}'`
+   OPF=${a1}_v${k}.${a2}
+   k=$((k + 1))
+done
+if [ $k -gt 1 ]; then
+   g.message -w "There is already a file $OPFN
+   Will use ${OPF} instead"
+fi
+
+#=======================================================================
+## Calculate VIF and write to standard output (& optionally to file)
+#=======================================================================
+
+# Write text file with column headers
+if [ -n "$OPF" ]; then 
+    echo "variable,vif,sqrtvif" > "$OPF"
+fi
+
+# Calculate VIF for each variable
+MAPS=`echo $GIS_OPT_MAPS | sed 's;,; ;g'`
+for nvar in ${MAPS} ; do
+	expl=`echo $MAPS | eval sed '-e s/$nvar/''/g'`
+	expl=`echo $expl | sed 's; ;,;g'`
+	eval `r.regression.multi -g --quiet mapx=$expl mapy=$nvar`
+	vif=`echo "1/(1-$Rsq)" | bc -l`
+	sqrtvif=`echo "sqrt($vif)" | bc -l`
+	
+	# Write to std output
+	echo "VIF for $nvar = "$vif
+	echo "sqrt(VIF) for $nvar = "$sqrtvif
+	
+	# Write to user-defined txt file
+	if [ -n "$OPF" ] ; then 
+        echo "$nvar,$vif,$sqrtvif" >> "$OPF"
+    fi
+	
+done
+
+


Property changes on: grass-addons/grass7/raster/r.vif/r.vif
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-addons/grass7/raster/r.vif/r.vif.html
===================================================================
--- grass-addons/grass7/raster/r.vif/r.vif.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.vif/r.vif.html	2013-05-20 12:02:08 UTC (rev 56324)
@@ -0,0 +1,15 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.vif</em> 
+
+<p>This module will compute variance inflaction factor (VIF) and the square root of the VIF. The VIF quantifies how much the variance (the square of the estimate's standard deviation) of an estimated regression coefficient is increased because of collinearity. The square root of VIF is a measure of how much larger the standard error is, compared with what it would be if that variable were uncorrelated with the other predictor variables in the model. 
+
+<h2>Notes</h2>
+
+<p> This add-on doesn't work in GRASS 6.* as it depends on r.regression.multi, which is available in GRASS 7 main only. 
+
+<h2>AUTHOR</h2>
+
+Contact: <a href="http://ecodiv.org/contact.html">Paulo van Breugel</a>
+
+



More information about the grass-commit mailing list