|
Networks / Pajek |
Package for Large Network
Analysis |
How to analyse networks and vectors sent from Pajek to program R
Suppose, that vectors v1, v2, v3
and networks n1, n2, n3
were sent from Pajek to R.
In the following some simple commands in R
using these vectors and networks (matrices) are listed.
- Vector operations
v1+v2
vsum <- v1+v2
v1sq <- v1^2
a <- sqrt(v1*v2)
...
- Basic statistics
var - variance, cov - covariance, cor - correlation
sum(v1)
length(v1)
mean(v1)
summary(v1)
var(v1)
cov(v1,v2)
cor(v1,v2)
- Graphics (charts)
plot(v1)
plot(v1,v2)
boxplot(v1,v2)
hist(v1)
if vector values are integers, (e.g. from interval 1..10)
it is sometimes better to provide breaks among classes:
hist(v1,br=0:10)
or
hist(v1, br=-1:10)
- Exporting graphics
to pdf file
pdf("c:/temp/test.pdf")
hist(v1)
dev.off()
to ps file
postscript("c:/temp/test.ps")
hist(v1)
dev.off()
to windows meta file
> win.metafile("c:/temp/test.wmf")
> hist(v1)
> dev.off()
- Bivariate and multivariate analysis
- cross-tabulation
table(v1,v2)
to get chi-square test
tabl <- table(v1,v2)
summary(tabl)
- comparing means (t-test)
t.test(v1,v2)
- comparing variances
var.test(v1, v2)
- regression
linear model
linm <- lm(v1 ~ v2)
summary(linm)
with more variables
linm <- lm(v1 ~ v2 + v3)
summary(linm)
nonlinear regression
nlm <- lm(v1 ~ v2 + v3^2)
summary(linm)
- function that saves vector from R to Pajek input file
savevector <- function(v,direct){
write(c(paste("*Vertices",length(v)), v), file = direct, ncolumns=1)}
Sample call
savevector(v1,"c:/temp/test.vec")
- function that loads vector(s) to R from Pajek input file
loadvector <- function(direct){
vv<-read.table(file=direct,skip=1)
if (dim(vv)[2]==1)
vv<-vv[[1]]
vv
}
Sample call
v9<-loadvector("c:/temp/test.vec")
If there is only one vector in test.vec,
result is vector v9 otherwise
first vector is v9[[1]], second v9[[2]],...
- Matrix operations
- Transpose network
t(n1)
- Eigenvalues/eigenvectors
eigen(n1)
- Hubs and authorities
hubs <- eigen(n1 %*% t(n1)) $ vec[,1]
auth <- eigen(t(n1) %*% n1) $ vec[,1]
- function that saves ordinary or 2-mode matrix from R to Pajek input file (*Matrix)
savematrix <- function(n,direct,twomode=1){
if ((dim(n)[1] == dim(n)[2]) & (twomode!=2))
{ write(paste("*Vertices",dim(n)[1]), file = direct);
write(paste(seq(1,length=dim(n)[1]),' "',rownames(n),
'"',sep=""), file = direct,append=TRUE);
write("*Matrix", file = direct,append=TRUE);
write(t(n),file = direct,ncolumns=dim(n)[1],
append=TRUE) }
else
{ write(paste("*Vertices",sum(dim(n)),dim(n)[1]),
file = direct);
write(paste(1:dim(n)[1],' "',rownames(n),'"',sep=""),
file = direct,append=TRUE);
write(paste(seq(dim(n)[1]+1,length=dim(n)[2]),' "',
colnames(n),'"',sep=""), file = direct,append=TRUE);
write("*Matrix", file = direct, append=TRUE);
write(t(n),file = direct, ncolumns=dim(n)[2],append=TRUE)}
}
Sample call
savematrix(n1,"c:/temp/test.mat")
To request a 2-mode matrix (in the case that number of rows and columns is the same):
savematrix(n1,"c:/temp/test.mat",2)
- function that saves ordinary or 2-mode matrix from R to Pajek input file (*Arcs, *Edges)
savenetwork <- function(n,direct,twomode=1){
if ((dim(n)[1] == dim(n)[2]) & (twomode!=2))
{ write(paste("*Vertices",dim(n)[1]), file = direct);
write(paste(seq(1,length=dim(n)[1]),' "',rownames(n),
'"',sep=""), file = direct,append=TRUE);
write("*Arcs", file = direct,append=TRUE);
for (i in 1:dim(n)[1]) {
for (j in 1:dim(n)[2]) {
if (n[i,j]!=0) {write(paste(i,j,n[i,j]),file = direct,append=TRUE)}
}
}
}
else
{ write(paste("*Vertices",sum(dim(n)),dim(n)[1]),
file = direct);
write(paste(1:dim(n)[1],' "',rownames(n),'"',sep=""),
file = direct,append=TRUE);
write(paste(seq(dim(n)[1]+1,length=dim(n)[2]),' "',
colnames(n),'"',sep=""), file = direct,append=TRUE);
write("*Edges", file = direct, append=TRUE);
for (i in 1:dim(n)[1]) {
for (j in 1:dim(n)[2]) {
if (n[i,j]!=0) {write(paste(i,j+dim(n)[1],n[i,j]),file = direct,append=TRUE)}
}
}
}
}
Sample call
savenetwork(n1,"c:/temp/test.mat")
To request a 2-mode network (in the case that number of rows and columns is the same):
savenetwork(n1,"c:/temp/test.mat",2)
- function that loads ordinary or 2-mode matrix to R from Pajek input file (*Matrix)
loadmatrix <- function(direct){
nn<-read.table(file=direct,nrows=1)
if (length(nn) == 2)
{ xx<-read.table(file=direct,skip=1,nrows=nn[[2]],fill=TRUE)
n<-read.table(file=direct,skip=nn[[2]]+2)
rownames(n)<-xx[[2]]
colnames(n)<-xx[[2]] }
else
{xxrow<-read.table(file=direct,skip=1,nrows=nn[[3]],fill=TRUE)
xxcol<-read.table(file=direct,skip=nn[[3]]+1,
nrows=nn[[2]]-nn[[3]],fill=TRUE)
n<-read.table(file=direct,skip=nn[[2]]+2)
rownames(n)<-xxrow[[2]]
colnames(n)<-xxcol[[2]] }
as.matrix(n)
}
Sample call
n9<-loadmatrix("c:/temp/test.mat")
How
to?; Pajek;
Vlado/Networks
|