Looped version

From InterSciWiki

Jump to: navigation, search
#download
http://intersci.ss.uci.edu/wiki/pub/PajekR.r
#put in an appropriate directory (as below for PC) to call as scource
net=source("C:/Program Files/pajek/PAJEK/PajekR.r")
library(sna)
gplot(n1)
gplot(n1,  coord = cbind(4*v2,4-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) #mode = coord
n2=n1
gplot(n2,  coord = cbind(4*v2,4-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) #mode = coord
#skip these
#gplot(n1,  coord = cbind(v1,1-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) 
#gplot(n1, mode = "fruchtermanreingold",gmode = "digraph", vertex.col=v1) #mode = 
#gplot(n1, mode = "mds",gmode = "digraph", vertex.col=v1) #mode = "fruchtermanreingold"
#gplot3d(n1, mode = "mds",gmode = "digraph", vertex.col=v1) #mode = "fruchtermanreingold"
is.matrix(n1)
is.matrix(n2)
dim(n2)
lensqrt=length(n1)^.5
library(gtools)
m=max(v1)
n2=n1
m3=n2
for (igen in 2:m) {#0
n2 <- m3
numnodes=0
#create permutation
F1=0 #female row i save FOR THIS GENERATION
for(i in 1:lensqrt) {#1
 for(j in 1:lensqrt) {#2
                      if (v1[i]==igen & n2[i,j]==2) {#3
                      numnodes=numnodes+1
                      F1[numnodes] <- i 
                      }#3 put row i in the permutation list
 }#2
}#1
numnodes=0
FJ=0 #female col j save FOR THIS GENERATION
for(i in 1:lensqrt) {#1
 for(j in 1:lensqrt) {#2
                     if (v1[i]==igen & n2[i,j]==2) {#3
                     numnodes=numnodes+1
                     FJ[numnodes] <- j 
                     }#3 put col j i in a list of the same order
 }#2
}#1
gplot(m2,  coord = cbind(4*v2,4-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) #mode = coord
numnodes 
#?("if")  ?("for") help - not much use
m2=n2
num=length(F1)
f1=0
f2=0
fj=0
f1  <- F1[1:num] #to permute i list
fj  <- FJ[1:num] #matching j list
f1 #rows to permute
f2 <- permute(f1) #permute to permute list
f2 #these are the permuted-i locations
#permutation created for columns
numnodes
##CREATE NEW NETWORK
#delete the old ties of type 2
for (i in 1:lensqrt) {#1
 for (j in 1:lensqrt) {#2
                      if (v1[i]==igen & n2[i,j]==2) m2[i,j] <- 0
 }#2
}#1
 #create permuted tie as type 2 #3
 for (i in 1:num) m2[f2[i],fj[i]] <- 2 #3 #IS THIS RIGHT FOR ROW AND COLUMN?
 for (i in 1:num) m2[fj[i],f2[i]] <- 2 #3 #IS THIS RIGHT FOR ROW AND COLUMN?
m3=m2
gplot(m3,  coord = cbind(4*v2,4-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) #mode = coord
}#0







m3=as.numeric(m2)
n3=as.numeric(n2)
nn=as.numeric(n1)
nn
n3
m3
#ADD LABELS align=right
f2
fj
#THE NODE NUMBERING HERE IS OFF BY ONE
#The next steps are to make the single generation computation into a callable function.
#http://stats.lse.ac.uk/penzer/ST419materials/CSchpt3.pdf
#Then ADD LABELS
  1. gplot(m2, coord = cbind(4*v2,4-v1),gmode = "digraph", vertex.col=v1, vertex.cex=2) #mode = coord
Personal tools