[statnet_help] Assistance with Calculating Degree Centrality in
Directed and Valued Networks Using the sna Package in R
CHU-DING LING via statnet_help
statnet_help at u.washington.edu
Tue Sep 3 21:28:34 PDT 2024
Dear Carter,
Thanks again for your suggestions. I have some further questions regarding
inconsistencies between the Bonacich’s power centrality (Beta centrality)
from *igraph* and *sna* for an undirected and valued network. Enclosed
please find the example codes and the associated annotations. Despite
setting the ‘exponent’ and ‘rescale’ parameters consistently between the
two packages, I am noticing some discrepancies in the results.
I have a couple of questions which I was hoping you could help with:
*1. How can we get the highest beta value before calculating Bonacich’s
power centrality?* Currently I use another unpublished package to obtain
the value. I would like to understand the process or any specific
considerations required to obtain the highest beta value with *sna* or
*igraph*.
*2. How can we achieve consistent results between ‘igraph’ and ‘sna’?*
Despite aligning the ‘exponent’ and ‘rescale’ parameters across both
packages, the results still differ. Are there any additional parameters or
factors I should consider to ensure the outputs are consistent?
I would greatly appreciate any guidance or insights you could provide on
these matters.
Thank you for your time and assistance.
Best regards,
Chuding
Carter T. Butts via statnet_help <statnet_help at u.washington.edu>
于2024年8月30日周五 16:00写道:
> Hi, Chuding -
>
> Glad that fixed it. With respect to edgelists vs. adjacency matrices,
> they will give you equivalent results. If you seem to be getting different
> results, check the two objects: somewhere in your code, you've presumably
> made them non-equivalent....
>
> Best,
>
> -Carter
> On 8/26/24 5:59 AM, CHU-DING LING wrote:
>
> Carter,
>
>
> Thank you for your suggestions! The problem has been resolved.
>
>
>
> Initially, I imported a matrix from a CSV file and stored it as a matrix
> class object. I then converted it into a network class object since many
> functions in *sna* require objects to be of the network class. However, I
> noticed that the edge weights were lost during the conversion from the
> matrix object to the network object, which caused the results from the
> degree() function not to account for edge weights.
>
>
>
> Actually, the degree() function can directly handle the matrix object. I
> also used as.sociomatrix.sna() to convert the original matrix object into
> another matrix object with a different name. Both approaches produced the
> same degree centrality results for the directed and valued network.
>
>
>
> I also experimented with the as.edgelist.sna() function to convert the
> original matrix object into an edgelist object. However, when I calculated
> the degree centrality of this object, it produced incorrect results, with a
> greater number of elements than the number of nodes in my network. I
> appreciate if you can give some insights on this issue.
>
>
>
> Thanks in advance!
>
>
>
> Chuding
>
> Carter T. Butts via statnet_help <statnet_help at u.washington.edu>
> 于2024年8月25日周日 05:15写道:
>
>> H, Chuding -
>>
>> The degree() function already exploits edge values; this is its default
>> behavior. If you wish to *ignore* edge values, you need to set the
>> "ignore.eval" argument to TRUE.
>>
>> If you are not getting valued degree calculations from degree() using the
>> defaults, then you are not passing it valued data. This may be due to a
>> preprocessing error (so check your inputs). Another possible failure mode
>> is that you are passing it a network object that has value information
>> stored as an edge attribute, and are expecting degree() to use those edge
>> values. Since a network object can have any number of edge attributes (or
>> none at all), and they can be of any data type (i.e., not necessarily
>> numeric), degree() can't automagically know what is intended in that case,
>> and will therefore treat the data as unvalued. An easy way to use edge
>> attribute information is to wrap your object in a call like
>> as.edgelist.sna(<mynet>,attrname=<whateveredgeattributeIwanttouse>), which
>> will extract from the object the specific valued network that you want to
>> analyze. That's especially handy if you have several different edge values
>> you want to store in the same network object. Of course, you can also use
>> that same trick to make a "working" edgelist at the top of your script that
>> you reuse for multiple calculations. (The same can be done with adjacency
>> matrices rather than edgelists, if one prefers. See e.g.
>> ?as.sociomatrix.sna.)
>>
>> Hope that helps,
>>
>> -Carter
>> On 8/23/24 9:07 PM, CHU-DING LING via statnet_help wrote:
>>
>> Dear all,
>>
>>
>>
>> I hope this message finds you well. I am currently working on a project
>> that involves social network analysis using the *sna* package in R. I am
>> reaching out to seek your expertise on a particular issue I have
>> encountered regarding the calculation of degree centrality in directed and
>> valued networks.
>>
>>
>>
>> I am working with a directed network where edges have associated weights.
>> My goal is to accurately calculate both the in-degree and out-degree
>> centrality of nodes while considering the edge weights. I attempted to
>> calculate the degree centrality using the degree function in the *sna*
>> package. While this function works well for unweighted networks, I realized
>> that it does not account for edge weights.
>>
>>
>>
>> Could you please advise on the best method or function within the *sna*
>> package to accurately calculate the degree centrality in this context?
>> Though I can make it with *igraph* or other packages, I am particularly
>> interested in whether *sna* could directly handle weighted edges in
>> directed networks.
>>
>>
>>
>> Your guidance would be invaluable, and I would greatly appreciate any
>> suggestions or resources you might be able to provide. Thank you for your
>> time and consideration. I look forward to your insights.
>>
>>
>>
>> Best,
>>
>> Chuding
>>
>> _______________________________________________
>> statnet_help mailing liststatnet_help at u.washington.eduhttps://urldefense.com/v3/__http://mailman13.u.washington.edu/mailman/listinfo/statnet_help__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Npw4CFqLg3mWAfPesSBtoa6UjvVqK_t7JYrixPsjqKAxzUTjhOvoeAxG6tO4iWruplppJ7ZQGd8FOLXV8VLxBP8-nfnt$
>>
>> _______________________________________________
>> statnet_help mailing list
>> statnet_help at u.washington.edu
>> http://mailman13.u.washington.edu/mailman/listinfo/statnet_help
>> <https://urldefense.com/v3/__http://mailman13.u.washington.edu/mailman/listinfo/statnet_help__;!!CzAuKJ42GuquVTTmVmPViYEvSg!K6AnX5T7ncI4niN0TJ-TlovKAm3m3t5-Q3ZLpsyePCkGVbHm5aIUd8RF7svulqI3hJGWibMjch4bTC5nxZg$>
>>
> _______________________________________________
> statnet_help mailing list
> statnet_help at u.washington.edu
> http://mailman13.u.washington.edu/mailman/listinfo/statnet_help
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman13.u.washington.edu/pipermail/statnet_help/attachments/20240904/4ae59881/attachment-0001.html>
-------------- next part --------------
## Step 1: Create a 5*5 undirected and valued matrix
set.seed(123) # Set seed for reproducibility
matrix_values <- sample(1:4, 15, replace = TRUE) # Generate random weights for the upper triangle
undirected_matrix <- matrix(0, 5, 5) # Initialize a 5x5 matrix
# Fill the upper triangle with random values
undirected_matrix[upper.tri(undirected_matrix)] <- matrix_values
# Make the matrix symmetric to represent an undirected graph
undirected_matrix <- undirected_matrix + t(undirected_matrix)
undirected_matrix
## Step 2: Convert the matrix to an igraph object
library(igraph)
undirected_matrix_graph <- graph_from_adjacency_matrix(undirected_matrix,
mode = c("undirected"),
weighted = TRUE,
diag = FALSE)
## Step 3: Calculate Bonacich's power centrality (Beta centrality)
# 0.1025942 in below specification is the highest Beta value
# from the results of running xBetaCentrality() in xUCINET package.
# I do not know how to obtain the highest Beta value with igraph or sna.
Results_from_igraph <- power_centrality(
undirected_matrix_graph,
nodes = V(undirected_matrix_graph),
loops = FALSE,
exponent = 0.1025942,
rescale = FALSE,
tol = 1e-07,
sparse = TRUE
)
Results_from_igraph <- data.frame(Results_from_igraph = Results_from_igraph)
library(sna)
Results_from_sna <- bonpow(undirected_matrix,
gmode="graph",
diag=FALSE,
tmaxdev=FALSE,
exponent=0.1025942,
rescale=FALSE,
tol=1e-07)
Results_from_sna <- data.frame(Results_from_sna = Results_from_sna)
## Step 4: Combine the two results and compare them
# The values in 'Results_from_igraph' show no variance,
# which differs from the observation with my own dataset.
# However, with my own dataset,
# the results are still not consistent with those in 'Results_from_sna'.
CombinedResults <- cbind(Results_from_igraph, Results_from_sna)
CombinedResults
cor(CombinedResults)
More information about the statnet_help
mailing list