Finding your place in the hierarchy

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding your place in the hierarchy

Peter Løvgreen (101plg)

Dear SPSS-users

 

I have a file containing employee_id and supervisor_id.

Employee_id holds everyone including the supervisors. The only one not having a supervisor is THE BOSS!

 

I would like to write a loop that takes the supervisor of the first employee, search through the employee’s for him and add’s 1 to the ‘place in the hierarchy’-variable.

I would loop until supervisor_id is missing for all employee’s.

 

The number for each employee in the ‘place in the hierarchy’-variable would then hold the number of supervisors above the employee (I hope).

 

I just don’t know how to program this in SPSS – could you please help me?

 

Regards

Peter Løvgreen

Statistisk specialkonsulent 
 

Direktoratet for Kriminalforsorgen
Koncernledelsessekretariatet

Koncern Jura og Statistik
Strandgade 100

1401 København K

www.kriminalforsorgen.dk



===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Finding your place in the hierarchy

David Marso
Administrator
Probably good idea to describe how the data are stored as a start.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

SV: Finding your place in the hierarchy

Peter Løvgreen (101plg)
Sorry for my inadequate description, David.

Data are stored in an SPSS data file with two variables: employee_id and supervisor_id.
I have approx. 5.000 rows.

Regards

Peter Løvgreen

Direktoratet for Kriminalforsorgen
Direkte tlf.: +45 7255 4684

-----Oprindelig meddelelse-----
Fra: SPSSX(r) Discussion [mailto:[hidden email]] På vegne af David Marso
Sendt: 15. juni 2015 14:01
Til: [hidden email]
Emne: Re: Finding your place in the hierarchy

Probably good idea to describe how the data are stored as a start.




-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Finding-your-place-in-the-hierarchy-tp5729822p5729824.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: SV: Finding your place in the hierarchy

Andy W
Consider the manager-subordinate relationship to be a graph where each relationship is an edge, and the employees are nodes. The order in the hierarchy is then simply the geodesic distance between the boss and every sub-ordinate. Here is an example of how to get this information using Python and the NetworkX library from your original edge list.

*****************************************************.
*Edge list.
DATA LIST FREE / Man Sub (2F1.0).
BEGIN DATA
1 2
2 3
2 4
3 5
3 6
4 7
4 8
END DATA.
DATASET NAME Boss.

*Make networkX graph from SPSS tuples data.
BEGIN PROGRAM.
import networkx

def to_graph(l):
    G = networkx.DiGraph()
    for part in l:
        # each sublist is a bunch of nodes
        G.add_nodes_from(part)
        # it also imlies a number of edges:
        G.add_edges_from(to_edges(part))
    return G

def to_edges(l):
    """
        treat `l` as a Graph and returns it's edges
        to_edges(['a','b','c','d']) -> [(a,b), (b,c),(c,d)]
    """
    it = iter(l)
    last = next(it)

    for current in it:
        yield last, current
        last = current    

#grab SPSS data
import spss, spssdata
alldata = spssdata.Spssdata().fetchall()

#turn SPSS data into graph
G = to_graph(alldata)
peop = networkx.nodes(G) #return all the people in the network
END PROGRAM.

*Now make the list of the geodesic distances from the boss.
DATASET DECLARE GeoDist.
BEGIN PROGRAM.
#now make an SPSS dataset
spss.StartDataStep()
datasetObj = spss.Dataset(name='GeoDist')
datasetObj.varlist.append('Dist',0)
datasetObj.varlist.append('ID',0)

boss = peop[0]
for i in peop:
  Dist = networkx.shortest_path_length(G,source=boss,target=i)
  datasetObj.cases.append([Dist,i])

spss.EndDataStep()
END PROGRAM.

*Now lets make a graph to make sure our network is really hierarchical.
DATASET ACTIVATE GeoDist.
FORMATS ID Dist (F1.0).
STRING LabelID (A20).
COMPUTE LabelID = CONCAT("ID:",STRING(ID,F1.0)," Dist:",STRING(Dist,F1.0)).
EXECUTE.
*Tree plot.
GGRAPH
  /GRAPHDATASET NAME="edges" DATASET = "Boss" VARIABLES=Man Sub
  /GRAPHDATASET NAME="nodes" DATASET = "GeoDist" VARIABLES=Dist ID LabelID
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
 SOURCE: e=userSource(id("edges"))
 DATA: Man=col(source(e), name("Man"), unit.category())
 DATA: Sub=col(source(e), name("Sub"), unit.category())
 SOURCE: n=userSource(id("nodes"))
 DATA: ID=col(source(n), name("ID"), unit.category())
 DATA: Dist=col(source(n), name("Dist"))
 DATA: LabelID=col(source(n), name("LabelID"), unit.category())
 GUIDE: axis(dim(1), null())
 GUIDE: legend(aesthetic(aesthetic.color.interior), null())
 COORD: rect(dim(1,2), reflect(dim(2)))
 ELEMENT: edge(position(layout.tree(node(ID),from(Man), to(Sub), root("1"))))
 ELEMENT: point(position(layout.tree(node(ID),from(Man), to(Sub), root("1"))), label(LabelID), size(size."14"))
END GPL.
*****************************************************.

See these series of related blog posts of mine for working with network data. In the last post I describe an algorithm to obtain this neighbor distance in pure SPSS.

 - https://andrewpwheeler.wordpress.com/2014/04/22/finding-subgroups-in-a-graph-using-networkx-and-spss/ (for turning SPSS data into NetworkX python data)
 - https://andrewpwheeler.wordpress.com/2013/12/19/network-xmas-tree-in-spss/ (for making the graph)
 - https://andrewpwheeler.wordpress.com/2013/07/19/querying-graph-neighbors-in-spss/ (for a way to do this in base SPSS, more convoluted though)
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/