Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions R/games.R
Original file line number Diff line number Diff line change
Expand Up @@ -2149,9 +2149,13 @@ smallworld <- function(...) constructor_spec(sample_smallworld, ...)
#' @param n Number of vertices.
#' @param edges Number of edges per step.
#' @param agebins Number of aging bins.
#' Must be at least 1.
#' This determines how finely the aging process is discretized.
#' @param pref Vector (`sample_last_cit()` and `sample_cit_types()` or
#' matrix (`sample_cit_cit_types()`) giving the (unnormalized) citation
#' probabilities for the different vertex types.
#' For `sample_last_cit()`, this should be a numeric vector of length `agebins + 1`.
#' A common choice is a power-law decay, e.g., `(1:(agebins + 1))^-3`.
#' @param directed Logical scalar, whether to generate directed networks.
#' @param types Vector of length \sQuote{`n`}, the types of the vertices.
#' Types are numbered from zero.
Expand All @@ -2161,12 +2165,20 @@ smallworld <- function(...) constructor_spec(sample_smallworld, ...)
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @keywords graphs
#' @family games
#' @examples
#' # Create a citation graph with 100 vertices, 5 age bins,
#' # and preferential attachment following a t^-3 power-law decay
#' g <- sample_last_cit(100, edges = 1, agebins = 5, pref = (1:6)^-3)
#'
#' # The preference vector determines how likely vertices in each age bin
#' # are to receive citations. Newer vertices (lower indices) are preferred.
#' g2 <- sample_last_cit(200, edges = 2, agebins = 10, pref = (1:11)^-2)
#' @export
sample_last_cit <- function(
n,
edges = 1,
agebins = n / 7100,
pref = (1:(agebins + 1))^-3,
agebins,
pref,
directed = TRUE
) {
on.exit(.Call(R_igraph_finalizer))
Expand Down
4 changes: 3 additions & 1 deletion man/cited.type.game.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/citing.cited.type.game.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions man/lastcit.game.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions man/sample_last_cit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/Makevars
*.gcda
*.gcno
*.dd
4 changes: 0 additions & 4 deletions src/cpp11.dd

This file was deleted.

4 changes: 0 additions & 4 deletions src/cpprinterface.dd

This file was deleted.

3 changes: 0 additions & 3 deletions src/init.dd

This file was deleted.

4 changes: 0 additions & 4 deletions src/rinterface.dd

This file was deleted.

5 changes: 0 additions & 5 deletions src/rinterface_extra.dd

This file was deleted.

3 changes: 0 additions & 3 deletions src/rrandom.dd

This file was deleted.

3 changes: 0 additions & 3 deletions src/simpleraytracer.dd

This file was deleted.

3 changes: 0 additions & 3 deletions src/uuid.dd

This file was deleted.

44 changes: 44 additions & 0 deletions tests/testthat/test-games.R
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,47 @@ test_that("Dot product rng gives warnings", {
paste0("Greater than 1 connection probability ", "in dot-product graph")
)
})

test_that("sample_last_cit() works with explicit parameters", {
withr::local_seed(42)
# Basic test with small graph
g <- sample_last_cit(100, edges = 1, agebins = 5, pref = (1:6)^-3)
expect_equal(vcount(g), 100)
expect_equal(ecount(g), 99)
expect_true(is_directed(g))

# Test with more edges per step
g2 <- sample_last_cit(50, edges = 2, agebins = 3, pref = (1:4)^-2)
expect_equal(vcount(g2), 50)
expect_equal(ecount(g2), 98) # (50 - 1) * 2

# Test undirected version
g3 <- sample_last_cit(
30,
edges = 1,
agebins = 2,
pref = c(1, 0.5, 0.25),
directed = FALSE
)
expect_equal(vcount(g3), 30)
expect_false(is_directed(g3))
})

test_that("sample_last_cit() requires agebins and pref parameters", {
expect_error(
sample_last_cit(100),
'argument "agebins" is missing, with no default'
)

expect_error(
sample_last_cit(100, agebins = 5),
'argument "pref" is missing, with no default'
)
})

test_that("sample_last_cit() validates agebins > 0", {
expect_error(
sample_last_cit(100, edges = 1, agebins = 0, pref = c(1)),
"Number of age bins must be at least 1"
)
})