When using SPSSINC TRANS, you have a wider array of functions to compute on cases in SPSS. Within the local session, you can create your own python functions within a BEGIN PROGRAM and END PROGRAM block. In SPSSINC TRANS you pass in the values in the current dataset, but you can also create functions that use data in the local python environment as well. An example use case follows in which you create a network in the local python environment using SPSS data, and then calculate several network statistics on the nodes. Here is a simple hierarchical network dataset that signifies managers and subordinates in an agency.
DATA LIST FREE / Man Sub (2F1.0).
DATASET NAME Boss.
We can subsequently turn this into a NetworkX graph with the code below. Some of my prior SPSS examples using NetworkX had a bit more complicated code using loops and turning the SPSS dataset into the network object. But actually the way SPSS dumps the data in python (as a tuples nested within a list) is how the
function expects it in NetworkX, so no looping required (and it automatically creates the nodes list from the edge data).
BEGIN PROGRAM Python.
import networkx as nx
import spss, spssdata
alldata = spssdata.Spssdata().fetchall() #get SPSS data
G = nx.DiGraph() #create empty graph
G.add_edges_from(alldata) #add edges into graph
Note now that we have the graph object
in the local python environment for this particular SPSS session. We can then make our own functions that references
, but takes other inputs. Here I have examples for the geodesic distance between two nodes, closeness and degree centrality, and the average degree of the neighbors.
BEGIN PROGRAM Python.
#average degree of neighbors
Here is the node list in a second SPSS dataset that we will calculate the mentioned statistics on. For large graphs, this is nice because you can select out a smaller subset of nodes and only worry about the calculations on that subset. For a crime analysis example, I may be monitoring a particular set of chronic offenders, and I want to calculate how close every arrested person within the month is to the set of chronic offenders.
DATA LIST FREE / Employ (F1.0).
DATASET NAME Emp.
DATASET ACTIVATE Emp.
Now we have all the necessary ingredients to calculate our network statistics on these nodes. Here are examples of using
to calculate the network statistics in the local SPSS dataset.
*Geodesic distance from 1.
SPSSINC TRANS RESULT=Dist TYPE=0
SPSSINC TRANS RESULT=Cent TYPE=0
SPSSINC TRANS RESULT=Deg TYPE=0
*Average neighbor degree.
SPSSINC TRANS RESULT=NeighDeg TYPE=0