igraph / rigraph

igraph R package
https://r.igraph.org
532 stars 200 forks source link

warning in sample_forestfire() example #1292

Closed maelle closed 3 months ago

maelle commented 4 months ago
library("igraph")
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
g <- sample_forestfire(10000, fw.prob = 0.37, bw.factor = 0.32 / 0.37)
dd1 <- degree_distribution(g, mode = "in")
dd2 <- degree_distribution(g, mode = "out")
plot(seq(along.with = dd1) - 1, dd1, log = "xy")
#> Warning in xy.coords(x, y, xlabel, ylabel, log): 1 x value <= 0 omitted from
#> logarithmic plot
#> Warning in xy.coords(x, y, xlabel, ylabel, log): 570 y values <= 0 omitted from
#> logarithmic plot

Created on 2024-03-11 with reprex v2.1.0

clpippel commented 4 months ago

I wonder if there is a rounding problem in R base? See the example below.

set.seed(2024)
n <- 8
g <- sample_forestfire(n, fw.prob = 0.37, bw.factor = 0.32 / 0.37)
dd1 <- degree_distribution(g, mode = "in")
dd2 <- degree_distribution(g, mode = "out")

#  No warning.
Eps <- .Machine$longdouble.eps
base::plot(seq(along.with = dd1) - 1 + Eps, dd1 + Eps, log = "xy")

It is not a rounding error. Zero is not a valid value for a log="xy" plot. I don't understand why this has worked in the past, assuming it worked.

clpippel commented 4 months ago

In R vectors are one-based. The "-1" in the plot and point statement should be omitted. To eliminate the y value warnings, replace 0 in dd1, dd2 by a value one step below the nonzero minimum of dd1, dd2.

g <- sample_forestfire(10000, fw.prob = 0.37, bw.factor = 0.32 / 0.37)
dd1 <- degree_distribution(g, mode = "in")
dd2 <- degree_distribution(g, mode = "out")
dd1[abs(dd1) < .Machine$double.eps] <- min(dd1[dd1 > 0])/ exp(1)  # <---- below minimum
dd2[abs(dd2) < .Machine$double.eps] <- min(dd2[dd2 > 0])/ exp(1)  # <---- below minimum
plot(seq(along.with = dd1), dd1, log = "xy")
points(seq(along.with = dd2) , dd2, col = 2, pch = 2)
maelle commented 3 months ago

thank you! this makes sense. I'll try simplifying the example.

maelle commented 3 months ago
szhorvat commented 3 months ago
# The forest fire model produces graphs with a heavy tail degree distribution.
# Note that some in- or out-degrees are zero which will be excluded from the logarithmic plot.
maelle commented 3 months ago

Thank you @szhorvat!

Thanks again @clpippel!