How can I perform cross validation on my grid file via Surfer automation?

This article contains a sample script for performing jackknifing (or cross-validation) on a grid. It grids the data set n times, leaving one of the data points out each time, and then calculates the Sum of Squares of Differences.

To run this script:

  1. Copy the script below, or download the attached BAS file: Jackknife.bas.
  2. In a Windows Explorer window, navigate to C:\Program Files\Golden Software\Surfer 16.
  3. Double click on Scripter.exe to launch Scripter.
  4. Press Ctrl+A to select all of the existing lines then press Delete.
  5. If you copied this script, press Ctrl+V to paste it into Scripter. If you downloaded it, click File | Open, select the BAS file from your downloads directory, and click Open.
  6. Click Script | Run to run the script.


'Jackknife.bas grids the data set n times, leaving one of the data points out
' each time.  Also known as cross-validation.
'The default file path for the output grids is the samples directory.
'If you do not have permission to save files to Program Files folders, this path must be updated on line 27 Sub Main Debug.Clear
 Debug.Print "----- ";Time;" -----" On Error Resume Next Set Surf = GetObject(, "Surfer.Application") If Err Then 'Don't clear Err so new errors will stop script execution. Err.Clear Set Surf = CreateObject("Surfer.Application") surf.Documents.Add(srfDocPlot) If Err Then MsgBox Err.Description Exit Sub End If End If surf.Visible = True Set plotdoc1 = surf.Documents("Plot1") Set plotwin1 = surf.Windows("Plot1:1") path1 =surf.Path+"\samples\" datafile1 = GetFilePath("demogrid.dat", "dat;xls", path1, "Get Data File", 0) Set shapes1 = plotdoc1.Shapes 'surf.Open datafile1 Set wksdoc1 = surf.Documents.Open(datafile1) 'Set range from column A to C. Set xyrange = wksdoc1.Columns(col1:=1, col2:=3) Set xystats = xyrange.Statistics 'Attempt to skip header row and find first data row. With xystats firstrow = .FirstRow While Val(wksdoc1.Cells(firstrow,1)) = 0 And _ Trim(wksdoc1.Cells(firstrow,1)) <> "0" firstrow=firstrow+1 Wend lastrow = .LastRow xmin = .Minimum xmax = .Maximum ymin = .Minimum(2) ymax = .Maximum(2) End With Debug.Print firstrow;lastrow;xmin;xmax;ymin;ymax surf.GridData(datafile1, outgrid:= path1 + "file1.grd", _ showreport := False, _ algorithm:=srfKriging) 'Add header to column D. wksdoc1.Cells(1,4)="Jackknife" 'Grid with exclusion filter. 'Expand grid by 1.0 data unit so edges aren't blanked. For i = firstrow To lastrow x1 = wksdoc1.Cells(i,1) y1 = wksdoc1.Cells(i,2) exclusionstring = "x = " + x1 + " and y = " + y1 surf.GridData(datafile1, _ algorithm:=srfKriging, _ exclusionfilter := exclusionstring, _ outgrid := path1 + "temp.grd", _ showreport := False, _ xMin:=xmin-1, xMax:=xmax+1, yMin:=ymin-1, yMax:=ymax+1) Set grid1 = surf.NewGrid grid1.LoadFile(path1 + "temp.grd", False) wksdoc1.Cells(i,4) = grid1.Interpolate(Val(x1),Val(y1)) jackdiff = wksdoc1.Cells(i,3) - wksdoc1.Cells(i,4) sumsquares = sumsquares + (jackdiff * jackdiff) For j = 1 To 4 Debug.Print wksdoc1.Cells(i,j);" "; Next j Debug.Print "" Next i Debug.Print "Sum of Squares of Differences = ";sumsquares
End Sub


Updated November 13, 2018

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.