# p2_Advice.txt
# g =number of lawyers = 71
# Data : Inter-lawyers networking data from a law firm data
# Reference: Lazega, E and Pattison (1999) Multiplexity, generalized exchange and cooperation in organisations
# : a case study, Social Networks 21, 67-90
# Data provided by Emmanuel Lazega
# Data on three relations: Advice, Coworker and  Friendship 

model {
	for (i in 1:g-1) { for (j in i+1:g) { y100[i,j] ~ dbern(p00[i,j]) ; log(p00[i,j]) <- lambda[i,j]
	  y101[i,j] ~ dbern(p01[i,j]) ; log(p01[i,j]) <- lambda[i,j] + thet[i,j]
		y110[i,j] ~ dbern(p10[i,j]) ; log(p10[i,j]) <- lambda[i,j] + thet[j,i]
	  	y111[i,j] ~ dbern(p11[i,j]) 
		log(p11[i,j]) <- lambda[i,j] + thet[i,j] + thet[j,i] + rho[i,j]
		thet[i,j] <- theta + a[i] + b[j] 
		thet[j,i] <- theta + a[j] + b[i] 
	lambda[i,j] <- -log(1 + exp(thet[i,j]) + exp(thet[j,i])+exp(thet[i,j]+thet[j,i]+rho[i,j]))
	rho[i,j] <-  phi
	# + del.phi*abs(years[i]-years[j])
		# Pr[i,j] = Prob(X_ij = 1) = Prob that lawyer i "relates" to lawyer j
	Pr[i,j] <- 1 - p00[i,j] - p01[i,j]
	Pr[j,i] <- 1 - p00[i,j] - p10[i,j]

	# Center a, b
	for( i in 1:g) { alpha[i] <- a[i] - mean(a[]) ; beta[i] <- b[i] - mean(b[])  }
	
	# Bivariate Normal prior for a[i], b[i]
	for (i in 1:g) { a[i] <- aa[i,1] ;  b[i] <- aa[i,2] ; aa[i,1:2] ~ dmnorm(zero[1:2],S2[,]) }
		zero[1] <- 0 ; zero[2] <- 0 
	phi ~ dnorm(mu.phi,tau.phi) ;   theta ~ dnorm(mu.theta,tau.phi) ; del.phi ~ dnorm(0,0.0001)
	
		mu.phi ~ dnorm(0,0.0001) ; tau.phi ~ dgamma(0.0001,0.0001)
		mu.theta ~ dnorm(0,0.0001) ; tau.theta ~ dgamma(0.0001,0.0001)
		
	
	# S2 is covariance matrix of (a,b)	
	# Inverse gamma prior for siga and sigb, uniform(-1,1) for corr.ab
#	taua ~ dgamma(0.0001,0.0001); taub ~ dgamma(0.0001,0.0001)
#	corr.ab ~ dunif(-1,1) ; siga <- 1/taua  ; sigb <- 1/taub
	
#	Sigma[1,1] <- siga  ; Sigma[2,2] <- sigb  
#	Sigma[1,2] <- corr.ab*sqrt(siga*sigb)  ; Sigma[2,1] <- Sigma[1,2]
#	for ( i in 1:2) { for (j in 1:2) {S2[i, j] <- inverse(Sigma[,] , i, j) }}		

# Wishart prior for S2
		 for (i in 1 : 2) { for (j in 1 : 2) { Sigma[i, j] <- inverse(S2[ , ], i, j)}}  # for WinBUGS1.3
		siga <- Sigma[1,1] ; sigb <- Sigma[2,2] 
		corr.ab <- Sigma[1,2]/(sqrt(Sigma[1,1]*Sigma[2,2]))
		S2[1:2,1:2] ~ dwish(Omega[,], 2)
	}

----------------------------------------------------------------------
# p2_AdviceBlock.txt
# g =number of lawyers = 71
# Data : Inter-lawyera networking data from a law firm data
# Reference: Lazega, E and Pattison (1999) Multiplexity, generalized exchange and cooperation in organisations
# : a case study, Social Networks 21, 67-90
# Data provided by Emmanuel Lazega
# Data on three relations: Advice, Coworker and  Friendship 
# Advice data analysed in this program 
# Block model with Status = 1 (partner), =0 (associate) for differential theta and rho

model {
	for (i in 1:g-1) { for (j in i+1:g) { y100[i,j] ~ dbern(p00[i,j]) ; log(p00[i,j]) <- lambda[i,j]
	  y101[i,j] ~ dbern(p01[i,j]) ; log(p01[i,j]) <- lambda[i,j] + thet[i,j]
		y110[i,j] ~ dbern(p10[i,j]) ; log(p10[i,j]) <- lambda[i,j] + thet[j,i]
	  	y111[i,j] ~ dbern(p11[i,j]) 
		log(p11[i,j]) <- lambda[i,j] + thet[i,j] + thet[j,i] + rho[i,j]
		thet[i,j] <- theta + a[i] + b[j] + del.status*equals(status[i], status[j])
		thet[j,i] <- theta + a[j] + b[i] + del.status*equals(status[i], status[j])
		rho[i,j] <- rho0 + del.rho*abs(years[i]-years[j])	
	lambda[i,j] <- -log(1 + exp(thet[i,j]) + exp(thet[j,i])+exp(thet[i,j]+thet[j,i]+rho[i,j]))
		 
		# Pr[i,j] = Prob(X_ij = 1) = Prob that lawyer i "relates" to lawyer j
	Pr[i,j] <- 1 - p00[i,j] - p01[i,j]
	Pr[j,i] <- 1 - p00[i,j] - p10[i,j]
			
	
	# Center a, b
	for( i in 1:g) { alpha[i] <- a[i] - mean(a[]) ; beta[i] <- b[i] - mean(b[])  }
	
	# Bivariate Normal prior for a[i], b[i], seperate for partners and associates
		for (i in 1:g) { a[i] <- (1-status[i])*aa[i,1] + status[i]*bb[i,1]  
		b[i] <- (1-status[i])*aa[i,2] + status[i]*bb[i,2]  
		aa[i,1:2] ~ dmnorm(zero[1:2],S2[,])  # for associates
		bb[i,1:2] ~ dmnorm(zero[1:2],S1[,]) } # for partners
		zero[1] <- 0 ; zero[2] <- 0 
		rho0 ~ dnorm(0,0.0001) ;   theta ~ dnorm(0,0.0001) ; del.status ~ dnorm(0,0.0001) ;
	eta.status ~ dnorm(0,0.0001)
	del.rho ~ dnorm(0,1)
	# S1 and S2 are precision matrices of (a,b)	1:Partner  2:Associates
	# Inverse gamma prior for siga and sigb, uniform(-1,1) for corr.ab
	taua1 ~ dgamma(0.0001,0.0001); taub1 ~ dgamma(0.0001,0.0001)
	corr.ab1 ~ dunif(-1,1) ; siga1 <- 1/taua1  ; sigb1 <- 1/taub1
	Sigma1[1,1] <- siga1  ; Sigma1[2,2] <- sigb1  
	Sigma1[1,2] <- corr.ab1*sqrt(siga1*sigb1)  ; Sigma1[2,1] <- Sigma1[1,2]
	for ( i in 1:2) { for (j in 1:2) {S1[i, j] <- inverse(Sigma1[,] , i, j) }}	
	
	taua2 ~ dgamma(0.0001,0.0001); taub2 ~ dgamma(0.0001,0.0001)
	corr.ab2 ~ dunif(-1,1) ; siga2 <- 1/taua2  ; sigb2 <- 1/taub2
	Sigma2[1,1] <- siga2  ; Sigma2[2,2] <- sigb2  
	Sigma2[1,2] <- corr.ab2*sqrt(siga2*sigb2)  ; Sigma2[2,1] <- Sigma2[1,2]
	for ( i in 1:2) { for (j in 1:2) {S2[i, j] <- inverse(Sigma2[,] , i, j) }}	
		
	}
		
