You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中计算Erdos-Renyi随机网络的R-squared值?

Calculating R-squared (Squared Pearson Correlation) for an Erdos-Renyi Network in R

Hey there! Let's work through how to compute the R-squared (squared Pearson correlation coefficient) for your Erdos-Renyi network. First, a quick note on your network generation code: there's a small parameter mix-up. When using type = "gnm" in erdos.renyi.game, the function expects n (number of nodes) and m (number of edges) as the core arguments—your original code included an extra 447 that doesn't fit. Let's start with the corrected network setup.

Step 1: Generate Your Erdos-Renyi Network

First, load igraph and create your network properly:

library(igraph)
# Corrected: n=281 nodes, m=465 edges, undirected ER network
g <- erdos.renyi.game(n = 281, m = 465, type = "gnm", directed = FALSE)

Step 2: Define What R-squared Means Here

In network analysis, when we talk about R-squared for an ER model, we almost always refer to the goodness-of-fit between the observed degree distribution of your network and the expected degree distribution from the theoretical ER model (which follows a binomial distribution). If you meant something else (like correlation between node attributes), let me know—but we'll focus on the standard degree distribution fit first.

Step 3: Calculate Observed Degree Distribution

Get the degree of each node, then compute the frequency of each degree value (normalized by total nodes):

# Extract degrees for all nodes
observed_degrees <- degree(g)

# Get all possible degree values from 0 to the maximum observed degree
max_degree <- max(observed_degrees)
degree_values <- 0:max_degree

# Calculate normalized observed frequencies (proportion of nodes with each degree)
observed_freq <- table(factor(observed_degrees, levels = degree_values)) / vcount(g)
observed_freq <- as.numeric(observed_freq)  # Convert to numeric vector

Step 4: Calculate Expected ER Degree Distribution

The ER model's degree distribution is a binomial distribution. First compute the edge probability p, then generate the expected frequencies:

# Calculate edge probability p for the ER model (undirected)
n_nodes <- vcount(g)
n_edges <- ecount(g)
p <- (2 * n_edges) / (n_nodes * (n_nodes - 1))  # Total possible edges: n(n-1)/2

# Generate expected binomial frequencies for each degree value
expected_freq <- dbinom(degree_values, size = n_nodes - 1, prob = p)

Step 5: Compute R-squared (Squared Pearson Correlation)

R-squared here is just the square of the Pearson correlation between the observed and expected frequency vectors. You can compute it directly, or via linear regression (the result will be identical):

# Direct calculation using Pearson correlation
pearson_cor <- cor(observed_freq, expected_freq)
r_squared <- pearson_cor^2

# Alternative: Get R-squared from linear regression
fit <- lm(expected_freq ~ observed_freq)
r_squared_lm <- summary(fit)$r.squared

# Print the result
cat("R-squared (Pearson correlation squared):", round(r_squared, 4), "\n")

A Quick Note

If you're looking for R-squared related to something other than degree distribution (e.g., correlation between two node-level attributes), you'd just extract those attributes as vectors and use cor(attr1, attr2)^2 directly. But for evaluating how well your network matches the ER model, the degree distribution fit is the standard use case.

内容的提问来源于stack exchange,提问作者Charles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:04:42