
####READING DATA

##Read matrices

A <- read.table("MPNetA.txt", header = FALSE)
B <- read.table("MPNetB.txt", header = FALSE)
X <- read.table("MPNetX.txt", header = FALSE)

##Read table with attribute variables
AttrExp<-read.table("MPNet_Cat_Exp.txt",header=T)
AttrWorkCat<-read.table("MPNet_Cat_Work.txt",header=T)


##Read table with weighted ties between actors and tasks
EffPerProblem<-read.table("MPNet_Bin_EffPerProblem_Attr.txt",header=T)

##Reading table containing all weighted ties between actors and tasks (regardless of how much/little the actor worked with the tasks, nor if the task was later removed from the analyses. This is later used to summarize the average effectiveness per actor and the task effect)
AttrEffPerAllRatings<-read.table("SW_MPNet_Bin_EffPerProblem_All_Attr.txt",header=T)


####CALCULATE DEPENDENT VARIABLE (performance vector)

depVar<-unlist(EffPerProblem)
depVar_cen<-depVar[-which(depVar==-1)]


####CALCULATION OF TERMS

##number of actors and number of tasks for term calculations
n <- nrow(A)
m <- nrow(B)

##Term 1: Inverse Triangles (Actor-task triangle; ATri in Tables 1,A1)
#For every actor-task diad, code calculates how many actors they are both connected to
#Resulting vector "InvTri" is later truncated to those actor-task diads that are not -1 (are connected)

InvTri <- vector()
for(k in 1:m){
	for(i in 1:n){
		t <- length(which(A[i, which(X[ ,k] == 1)] == 1))
		InvTri <- append(InvTri, t)
	}
}

InvTri_cen <- InvTri[-which(depVar==-1)]


##Term 1 normalised by number of actors connected to the task (ATri in Tables 1,2,A1)

InvTriN <- vector()
for(k in 1:m){
	for(i in 1:n){
		t <- length(which(A[i, which(X[ ,k] == 1)] == 1))
		InvTriN <- append(InvTriN, t/(length(which(X[ ,k] == 1))-1))
	}
}

InvTriN_cen <- InvTriN[-which(depVar==-1)]


##Term 2: Actor collaborations (ACool in Tables 1,A1)
#For every actor-task diad, code calculates how many collaborations actor i has
#Resulting vector "Col" is later truncated to those actor-task diads that are not -1 in X

Col <- vector()
for (k in 1:m){
	for(i in 1:n)
	{
		c <- length(which(A[i, ] == 1))
		Col <- append(Col, c)
	}
}

Col_cen <- Col[-which(depVar==-1)]


##Term 2 normalised by number of actors connected to actor i (ACool in Table A1)
##For every actor-task diad, code calculates how many collaborations actor i has but where alter is not connected to the actor's task

Col2 <- vector()
for (k in 1:m){
	for(i in 1:n)
	{
		c2 <- length(which(A[i,which(X[ ,k]==0)] == 1))
		Col2 <- append(Col2, c2)
	}
}

Col2_cen <- Col2[-which(depVar==-1)]


	

##Term 3: Task-actor triangle (Tri in Table 1, A1)
#For every actor-task diad, code calculates how many other tasks are connected to the focal actor (i) that are also connected to task k

Tri <- vector()
for (k in 1:m){
	for(i in 1:n){
		t <- length(which(B[k, which(X[i, ] == 1)] == 1))		
		Tri <- append(Tri, t)
	}
}


Tri_cen <- Tri[-which(depVar==-1)]  #extracts diads that are different to -1



##Term 3 normalised by number of tasks connected to the focal actor's task (Tri in Table 2, A1)

TriN <- vector()
for (k in 1:m){
	for(i in 1:n){
		t <- length(which(B[k, which(X[i, ] == 1)] == 1))		
		TriN <- append(  TriN, t/(  length(which(B[k,] == 1 ))  )  )
	}
}

TriN_cen <- TriN[-which(depVar==-1)]  

TriN_cen[is.na(TriN_cen)] <- 0


##Term 4: Tendency of ratings (AP in Table 1,2)- calculated using all responses (even to tasks where actors did not work much or tasks that were removed from analysis later on)

#Create 14 versions of the EffPerf matrix (the complete one with 14 tasks) with one column removed in order from 1 to 14. 
#Each matrix will serve to calculate one vector of average performance scores for each task that will be assessed (hence the need to remove one column in each correponding to the scores for the task assessed)
#Then the relevant vectors will be integrated into one long vector for the X matrix 

Mlst = list()
for (k in 1:m){
	TempMatrix = AttrEffPerAllRatings[,-k]
	Mlst[[k]] = apply(TempMatrix,1,function(x) mean(x[x>0]))
}

Ave <- unlist(Mlst)
Ave_cen <- Ave[-which(depVar==-1)]
Ave_cen[is.na(Ave_cen)] <- 0


##Term 5: Influence effect through actor-task triangle. The performance of the focal actor’s collaborators on the task in question (ATriP in Table 2, A1).
#Weight matrix in the network autocorrelation model

AMatrix <- as.matrix(A)
W1 <- diag(11) %x% AMatrix
WRel <- W1 / InvTri
diag(WRel)<- 0
WRel_cen <- WRel[-which(depVar==-1),-which(depVar==-1)]
WRel_cen[is.na(WRel_cen)] <- 0



## Term 6: Actor experience (Exp in Table A1)

ActorExp <- unlist(AttrExp)
Exp <- vector()

for(k in 1:m){
	for(i in 1:n){
	t <- 	ActorExp[i]
	Exp <- append(Exp, t)
	}
}

Exp_cen <- Exp[-which(depVar==-1)]


##Term 7: Actor Profesionalisation (Work in Table 2, A1)

ActorWork <- unlist(AttrWorkCat)
WorkCat <- vector()

for(k in 1:m){
	for(i in 1:n){
	t <- ActorWork[i]
	WorkCat <- append(WorkCat, t)
	}
}
WorkCat_cen <- WorkCat[-which(depVar==-1)]



##Term 8: Task performance control variable (for each of the tasks; TP in Table 2, A1)

M2 <- matrix(0,n*m,m)
fatdiag(M2, steps=11)<-1
TaskDum <- M2[,-10]
TaskDum_cen <- TaskDum[-which(depVar==-1),]



## Term 8: Actor and task square. Square with no diagonals (ATsqr in Table 1, A1) 
Sq_NoDiag <- vector()
B_noDiag <- vector()
A_noDiag <- vector()
for (k in 1:m){
	for(i in 1:n){
		if( (length(which(A[i, ] == 1)) != 0) & (length(which(B[k, ] == 1)) != 0)){ # Only calculate if actor has links to other actors and if there are interconnected tasks
						
			Bk <- which(B[k, ] == 1) # indeces of tasks in B that are connected to k
			
			Ai <- which(A[i, ] == 1) # indeces of actors in A that are connected to i
			Xi <- which(X[i,] == 1) # indeces of tasks in X that are connected to i 
			Xk <- which(X[ ,k] == 1) # indeces of actors in X that are connected to k
			B_noDiag <- Bk[!Bk %in% Xi] # All the index numbers of tasks that are connected to k that are not connected to i (no diagonal)
			A_noDiag <- Ai[!Ai %in% Xk] # All the index numbers of actors that are connected to i that are not connected to k (no diagonal)
			if( (length (B_noDiag) != 0) & (length(A_noDiag)!= 0)){		
				Num_squares <- length(which( X[ A_noDiag , B_noDiag] == 1)) # number of squares with no diagonals 
				Sq_NoDiag  <- append(Sq_NoDiag , Num_squares)
			
			} else {	
			Num_squares <- 0   # if there are no index numbers left after discounting the diagonals 
			Sq_NoDiag  <- append(Sq_NoDiag , Num_squares)
			}
		} else {
		Sq_NoDiag <- append(Sq_NoDiag, 0)
		}
	}
}

Sq_NoDiag[Sq_NoDiag<0] <- 0
Sq_NoDiag_cen <- Sq_NoDiag[-which(depVar==-1)]



## Term8 normalised by number of actors connected to interconnected tasks (other than actor i) (ATsqr in Table 1, 2) 
Sq_NoDiag <- vector()
Sq_NoDiagN <- vector()
B_noDiag <- vector()
A_noDiag <- vector()
for (k in 1:m){
	for(i in 1:n){
		if( (length(which(A[i, ] == 1)) != 0) & (length(which(B[k, ] == 1)) != 0)){ # Only calculate if actor has links to other actors and if there are interconnected tasks
						
			Bk <- which(B[k, ] == 1) # indeces of tasks in B that are connected to k
			
			Ai <- which(A[i, ] == 1) # indeces of actors in A that are connected to i
			Xi <- which(X[i,] == 1) # indeces of tasks in X that are connected to i 
			Xk <- which(X[ ,k] == 1) # indeces of actors in X that are connected to k
			B_noDiag <- Bk[!Bk %in% Xi] # All the index numbers of tasks that are connected to k that are not connected to i (no diagonal)
			A_noDiag <- Ai[!Ai %in% Xk] # All the index numbers of actors that are connected to i that are not connected to k (no diagonal)
			
			 
			Bi <- Bk[Bk %in% Xi] #indeces of tasks that are connected to k that are also connected to i
			if( length(Bi) > 0 ){
			NumActIntTask <- length(which( X[ ,Bk] == 1)) - 1 #number of actors connected to interconnected tasks other than i
			} else {
			NumActIntTask <- length(which( X[ ,Bk] == 1)) #number of actors connected to interconnected tasks other than i
			}

			if( (length (B_noDiag) != 0) & (length(A_noDiag)!= 0)){		
				Num_squares <- length(which( X[ A_noDiag , B_noDiag] == 1)) # number of squares with no diagonals 
				Sq_NoDiagN  <- append(Sq_NoDiagN , Num_squares/NumActIntTask)
			
			} else {	
			Num_squares <- 0   # if there are no index numbers left after discounting the diagonals 
			Sq_NoDiagN  <- append(Sq_NoDiagN , Num_squares/NumActIntTask)
			}
		} else {
		Sq_NoDiagN <- append(Sq_NoDiagN, 0)
		}
	}
}

Sq_NoDiagN[Sq_NoDiagN<0] <- 0
Sq_NoDiagN_cen <- Sq_NoDiagN[-which(depVar==-1)]



## Term 9: Actor and task square with diagonals (ATsqrd in Table 1, A1) 
Sq_Diag <- vector()
B_Diag <- vector()
A_Diag <- vector()
for (k in 1:m){
	for(i in 1:n){
		if( (length(which(A[i, ] == 1)) != 0) & (length(which(B[k, ] == 1)) != 0)){ # Only calculate if actor has links to other actors and if there are interconnected tasks
						
			Bk <- which(B[k, ] == 1) # index numbers of tasks in B that are connected to k
			Ai <- which(A[i, ] == 1) # index nubmers of actors in A that are connected to i
			Xi <- which(X[i,] == 1) # index numbers of tasks in X that are connected to i 
			Xk <- which(X[ ,k] == 1) # index numbers of actors in X that are connected to k
			B_Diag <- Bk[Bk %in% Xi] # All the index numbers of tasks that are connected to k that are also connected to i (there is a diagonal from actor i to an interconnected task)
			A_Diag <- Ai[Ai %in% Xk] # All the index numbers of actors that are connected to i that are also connected to k (there is a diagonal from collaborator to task k)
			if( (length (B_Diag) != 0) & (length(A_Diag)!= 0)){		
				Num_squares <- length(which( X[ A_Diag , B_Diag] == 1)) # number of squares with diagonals 
				Sq_Diag  <- append(Sq_Diag , Num_squares)
			
			} else {	
			Num_squares <- 0   # if there are no index numbers left after discounting the diagonals 
			Sq_Diag  <- append(Sq_Diag , Num_squares)
			}
		} else {
		Sq_Diag <- append(Sq_Diag, 0)
		}
	}
}

Sq_Diag_cen <- Sq_Diag[-which(depVar==-1)]




## Term 9 normalised by the number of actors connected to interconnected tasks other than actor i (ATsqrd in Table 2, A1) 
Sq_DiagN <- vector()
B_Diag <- vector()
A_Diag <- vector()
for (k in 1:m){
	for(i in 1:n){
		if( (length(which(A[i, ] == 1)) != 0) & (length(which(B[k, ] == 1)) != 0)){ # Only calculate if actor has links to other actors and if there are interconnected tasks
						
			Bk <- which(B[k, ] == 1) # index numbers of tasks in B that are connected to k
			Ai <- which(A[i, ] == 1) # index nubmers of actors in A that are connected to i
			Xi <- which(X[i,] == 1) # index numbers of tasks in X that are connected to i 
			Xk <- which(X[ ,k] == 1) # index numbers of actors in X that are connected to k
			B_Diag <- Bk[Bk %in% Xi] # All the index numbers of tasks that are connected to k that are also connected to i (there is a diagonal from actor i to an interconnected task)
			A_Diag <- Ai[Ai %in% Xk] # All the index numbers of actors that are connected to i that are also connected to k (there is a diagonal from collaborator to task k)
			

 
			Bi <- Bk[Bk %in% Xi] #indeces of tasks that are connected to k that are also connected to i
			if( length(Bi) > 0 ){
			NumActIntTask <- length(which( X[ ,Bk] == 1)) - 1 
			} else {
			NumActIntTask <- length(which( X[ ,Bk] == 1)) #number of actors connected to interconnected tasks other than i
			}

			if( (length (B_Diag) != 0) & (length(A_Diag)!= 0)){		
				Num_squares <- length(which( X[ A_Diag , B_Diag] == 1)) # number of squares with diagonals 
				Sq_DiagN  <- append(Sq_DiagN , Num_squares/NumActIntTask)
			
			} else {	
			Num_squares <- 0   # if there are no index numbers left after discounting the diagonals 
			Sq_DiagN  <- append(Sq_DiagN , Num_squares/NumActIntTask)
			}
		} else {
		Sq_DiagN <- append(Sq_DiagN, 0)
		}
	}
}

Sq_DiagN_cen <- Sq_DiagN[-which(depVar==-1)]



##RUNNING THE MODEL (for model in Table 2 - Swedish case)

model<-lnam(y=depVar_cen,x=cbind(1, InvTriN_cen, Sq_DiagN_cen,TriN_cen,Ave_cen,WorkCat_cen,TaskDum_cen),W1=list(WRel_cen))

