R 中的社交网络和遗传算法

发布于 2024-10-12 23:27:19 字数 841 浏览 2 评论 0原文

我正在尝试从一篇文章中实现网络核心外围测量(链接:Borgatti & Everett 2000) in R。作者采用的基本方法是:

  1. < 占据左上角。
  2. 根据步骤 1 中的行/列排列创建理想模式矩阵

  3. 评估两个矩阵之间的相关性

    >

根据作者的说法,第一步的技巧是找到与其诱导模式矩阵相关性最高的矩阵的行/列排列,他们建议使用遗传算法来找到最佳行/列排列。我陷入了算法的第一步:

  1. 如何在 R 中创建随机行/列矩阵排列以保留列/行条目的顺序?

  2. 一旦我评估了矩阵排列和模式矩阵之间的适合度,我如何基于“最适合”矩阵“培育”新的矩阵排列?

谢谢。

I am trying to implement a network core-periphery measure from an article (link: Borgatti & Everett 2000) in R. The basic approach applied by the authors is to:

  1. Arrange the rows and columns of the network matrix so that actors that are well connected to each other occupy the top left corner.

  2. Create an ideal pattern matrix based on the row/column arrangement in step 1

  3. Assess the correlation between the two matrices

According to the authors the trick in step one is to find the row/column arrangement of the matrix that correlates the highest with its induced pattern matrix, and they recommend using a genetic algorithm to find the best row/column arrangement. I am stuck at the first steps of the algorithm:

  1. How do I in R create random row/column matrix arrangements that preserve the order of the column/row entries?

  2. Once I have assessed the fit between the matrix arrangements and the patterns matrices, how do I "breed" new matrix arrangements based on the "fittest" matrices?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-10-19 23:27:19

给定特定大小的矩阵,您可以生成随机的行/列矩阵排列。

#create fake data
mydata.block1 <- matrix (rep(1, times=100), ncol=10)
mydata.block2 <- matrix (rep(0, times=900), ncol=90)
mydata.block3 <- matrix (rep(0, times=900), ncol=10)
mydata.block4 <- matrix (rep(1, times=8100), ncol=90)

mydata <- rbind(cbind (mydata.block1, mydata.block2), cbind (mydata.block3, mydata.block4))

#Mix mydata
mix.order <- sample(1:dim (mydata)[1])
mydata <- mydata[mix.order,mix.order]

#create 100 random orderings

##preallocate matrix
rand.samp <- matrix (rep(NA, times=10000), ncol=100)
##create orderings
for (i in 1:100){
rand.samp[i,] <- sample(1:dim (mydata)[1])
}
##Eliminate duplicate orderings (unlikely to occur)
rand.samp <- unique (rand.samp)


#Reorder and measure fitness
##preallocate fitness measure
fit.meas <- rep (NA, times=100)

for (i in 1:100){
mydata.reordered <- mydata[rand.samp[i,],rand.samp[i,]]
fit.meas[i] <- myfitnessfunc(mydata.reordered)
}

在测量了适应度之后,您将需要某种方法来确定哪些区域对适应度有贡献,并在改变其他区域(“品种”)的同时修复这些区域。 )。也许 dist() 会有一些用处。也许热图或聚类 hclust() 也有用?您能否提供有关如何确定局部适应性的更多详细信息?

Given a matrix of a particular size, you can generate random row/column matrix arrangements as such

#create fake data
mydata.block1 <- matrix (rep(1, times=100), ncol=10)
mydata.block2 <- matrix (rep(0, times=900), ncol=90)
mydata.block3 <- matrix (rep(0, times=900), ncol=10)
mydata.block4 <- matrix (rep(1, times=8100), ncol=90)

mydata <- rbind(cbind (mydata.block1, mydata.block2), cbind (mydata.block3, mydata.block4))

#Mix mydata
mix.order <- sample(1:dim (mydata)[1])
mydata <- mydata[mix.order,mix.order]

#create 100 random orderings

##preallocate matrix
rand.samp <- matrix (rep(NA, times=10000), ncol=100)
##create orderings
for (i in 1:100){
rand.samp[i,] <- sample(1:dim (mydata)[1])
}
##Eliminate duplicate orderings (unlikely to occur)
rand.samp <- unique (rand.samp)


#Reorder and measure fitness
##preallocate fitness measure
fit.meas <- rep (NA, times=100)

for (i in 1:100){
mydata.reordered <- mydata[rand.samp[i,],rand.samp[i,]]
fit.meas[i] <- myfitnessfunc(mydata.reordered)
}

After you have measured fitness, you will need some way to determine which areas are contributing to the fitness and fix those while altering other areas ("breed"). Perhaps dist() will be of some use. Maybe a heatmap or clustering, hclust(), would also be of use? Can you provide more detail on how you would determine localized fitness?

烟雨扶苏 2024-10-19 23:27:19

OneWhoIsUnnamed 的响应与我解释您对#1 的需求相同。

这是两个邻接矩阵的基于适应度的重组方法,#2:

假设您有两个矩阵 A 和 B,它们的适应度核心 Fa 和 Fb 分别为 2.3 和 1.1。通过构造新矩阵 C 来培育矩阵,其中 C_{i} = A_{i} 的概率为 Fa/(Fa+Fb) 或 C_{i} = B_{i} 的概率为 1-Fa/(Fa+Fb )。这只是无限的育种基质方式之一。 M是A和B基于适应度的配对结果。

# lets define a function to create random adjacency matrices
random_adjacent <- function(dimension)
{
    ret  <- matrix(runif(dimension^2)>0.5,dimension,dimension)
    retl <- ret * lower.tri(ret)
    return( retl + t(retl) )
}
# set fitness
Fa <- 2.3
Fb <- 1.1
# initialize matrices
A  <- random_adjacent(4)
B  <- random_adjacent(4)
# compute symmetric fitness probability matrix
C  <- matrix(runif(16)<Fa/(Fa+Fb),4,4)
Cl <- C * lower.tri(C) # take the lower triangular portion
C  <- Cl + t(Cl)       # reflect the lower triangular portion into the upper
# compute mated result
M  <- matrix(0,4,4)
M[C]  <- A[C]
M[!C] <- B[!C]

OneWhoIsUnnamed response is the same as I interpreted your need for #1.

Here's a fitness based recombination method for two adjacency matrices, #2:

Say you have two matrices, A and B, who have fitness cores Fa and Fb of 2.3 and 1.1, respectively. Breed the matrices by constructing a new matrix, C, where C_{i} = A_{i} with probability Fa/(Fa+Fb) or C_{i} = B_{i} with probability 1-Fa/(Fa+Fb). This is just one of unlimited ways of breeding matrices. M is the mated result of A and B based on their fitness.

# lets define a function to create random adjacency matrices
random_adjacent <- function(dimension)
{
    ret  <- matrix(runif(dimension^2)>0.5,dimension,dimension)
    retl <- ret * lower.tri(ret)
    return( retl + t(retl) )
}
# set fitness
Fa <- 2.3
Fb <- 1.1
# initialize matrices
A  <- random_adjacent(4)
B  <- random_adjacent(4)
# compute symmetric fitness probability matrix
C  <- matrix(runif(16)<Fa/(Fa+Fb),4,4)
Cl <- C * lower.tri(C) # take the lower triangular portion
C  <- Cl + t(Cl)       # reflect the lower triangular portion into the upper
# compute mated result
M  <- matrix(0,4,4)
M[C]  <- A[C]
M[!C] <- B[!C]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文