Projections test

Krakow, July 10, 2023

> wdir <- "C:/2023/biblio/countries"
> setwd(wdir)

> bin <- function(A){ B <- A
+ for(i in 1:nrow(A)) for(j in 1:ncol(A)) B[i,j] <- ifelse(A[i,j]>0,1,0)
+ return(B)}

> CoN <- function(WC){
+   dN <- diag(rep(0,nrow(WC)))
+   for(c in 1:nrow(WC)) dN[c,c] <- 1/(sum(WC[c,])**2 - sum(WC[c,]**2))
+   Co <- t(WC) %*% dN %*% WC
+   diag(Co) <- 0
+   return(Co)
+ }

> Con <- function(WC){
+   dn <- diag(rep(0,nrow(WC)))
+   for(c in 1:nrow(WC)) dn[c,c] <- 1/(max(1,sum(WC[c,])**2))
+   Co <- t(WC) %*% dn %*% WC
+   return(Co)
+ }

> Projections <- function(WC){
+   k <- ncol(WC); N <- colnames(WC)
+   Cob <- Con <- CoC <- CoN <- matrix(0,nrow=k,ncol=k,dimnames=list(N,N))
+   for(w in 1:nrow(WC)){
+     Cw <- which(WC[w,]>0)
+     wdegw <- sum(WC[w,])
+     sqw <- sum(WC[w,]**2)
+     dnw <- 1/wdegw**2
+     dNw <- 1/(wdegw**2 - sqw)
+     for(e in Cw){
+       for(f in Cw){
+         Cob[e,f] <- Cob[e,f] + 1
+         CoC[e,f] <- CoC[e,f] + WC[w,e]
+         Con[e,f] <- Con[e,f] + WC[w,e]*dnw*WC[w,f]
+         if(e != f) CoN[e,f] <- CoN[e,f] + WC[w,e]*dNw*WC[w,f]
+   } } }  
+   return(list(CoC=CoC,Cob=Cob,Con=Con,CoN=CoN))
+ }

> WC <- cbind( c(0,2,1,3,2,1), c(2,1,3,0,3,0), c(1,0,1,2,1,3))
> rownames(WC) <- paste("w",1:6,sep=""); colnames(WC) <- c("c1","c2","c3")
> WC
   c1 c2 c3
w1  0  2  1
w2  2  1  0
w3  1  3  1
w4  3  0  2
w5  2  3  1
w6  1  0  3
> CoC <- t(WC) %*% bin(WC)
> CoC
   c1 c2 c3
c1  9  5  7
c2  7  9  8
c3  7  3  8
> Cob <- t(bin(WC)) %*% bin(WC)
> Cob
   c1 c2 c3
c1  5  3  4
c2  3  4  3
c3  4  3  5
> Con <- Con(WC)
> Con
          c1        c2        c3
c1 1.0180556 0.5088889 0.5230556
c2 0.5088889 1.1655556 0.4255556
c3 0.5230556 0.4255556 0.9013889
> sum(Con)
[1] 6
> CoN <- CoN(WC)
> CoN
         c1        c2        c3
c1 0.000000 0.9870130 1.1623377
c2 0.987013 0.0000000 0.8506494
c3 1.162338 0.8506494 0.0000000
> sum(CoN)
[1] 6
> 

> R <- Projections(WC)
> R
$CoC
   c1 c2 c3
c1  9  5  7
c2  7  9  8
c3  7  3  8

$Cob
   c1 c2 c3
c1  5  3  4
c2  3  4  3
c3  4  3  5

$Con
          c1        c2        c3
c1 1.0180556 0.5088889 0.5230556
c2 0.5088889 1.1655556 0.4255556
c3 0.5230556 0.4255556 0.9013889

$CoN
         c1        c2        c3
c1 0.000000 0.9870130 1.1623377
c2 0.987013 0.0000000 0.8506494
c3 1.162338 0.8506494 0.0000000
> 
pro/bib/euco/proj.txt · Last modified: 2023/08/09 02:14 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki