Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
##
## download all `ergm` sources from v3.0-1 to today
##
library(rvest)
d <- "ergm-sources"
dir.create(d, showWarnings = FALSE)
# current source
u <- "https://cran.r-project.org/web/packages/ergm/index.html"
h <- read_html(u) %>%
html_nodes(xpath = "//a[contains(@href, '.tar.gz')]") %>%
html_attr("href") %>%
basename
u <- "https://cran.r-project.org/src/contrib/"
f <- file.path(d, h)
if (!file.exists(f)) {
download.file(paste0(u, h), f, mode = "wb", quiet = TRUE)
}
# past sources
u <- "https://cran.r-project.org/src/contrib/Archive/ergm/"
h <- read_html(u) %>%
html_nodes(xpath = "//a[contains(@href, '.tar.gz')]") %>%
html_attr("href")
# subset to v3.x (March 2012 and later)
h <- h[ grepl("_3\\.", h) ]
for (i in h) {
f <- file.path(d, i)
if (!file.exists(f)) {
download.file(paste0(u, i), f, mode = "wb", quiet = TRUE)
}
}
##
## check `broom` `tidy` and `glance` against `ergm` model and summary objects
##
require(dplyr)
require(remotes)
require(tibble)
# load `broom`
devtools::load_all('/Users/fr/Desktop/broom')
d <- "ergm-sources"
f <- list.files(d, pattern = "\\.tar\\.gz$")
# sort newest --> oldest
v <- as.integer(gsub("\\D", "", f))
f <- rev(f[ order(v) ])
t <- list() # store results from `tidy`
g <- list() # store results from `glance`
sink("output-2019-06-26.txt")
for (i in f) {
cat("\n[INSTALLING]", i, "\n")
remotes::install_local(file.path(d, i), force = TRUE, quiet = TRUE)
library(ergm)
v <- as.character(packageVersion("ergm"))
cat("[LOADED] ergm", v, "\n")
data(florentine)
gest <- suppressMessages(ergm(flomarriage ~ edges + absdiff("wealth")))
# model$coef exists
stopifnot("coef" %in% names(gest))
gest_s <- summary(gest)
# summary(model)$coefs exists
stopifnot("coefs" %in% names(gest_s))
gest_coefs <- gest_s$coefs
# summary(model)$coefs checks out
stopifnot(class(gest_coefs) == "data.frame" && nrow(gest_coefs) == 2)
cat(
"`summary(gest)$coefs` contains:",
paste(names(gest_coefs), collapse = ", "),
"\n"
)
# model checks out
if (!dplyr::near(stats::AIC(gest), 105.9514, tol = 10^-4)) {
warning("ergm ", v, " returned a different AIC")
}
t <- tidy(gest) %>%
tibble::add_column(ergm_version = v, .before = 1) %>%
bind_rows(t, .)
# no need to test line below, all computations internal to `broom`
# tidy(gest, exponentiate = TRUE, conf.int = TRUE, conf.level = 0.99)
g <- glance(gest, deviance = TRUE, mcmc = TRUE) %>%
tibble::add_column(ergm_version = v, .before = 1) %>%
bind_rows(g, .)
}
sink()
# all `tidy` results identical at 0.0001
select(t, -ergm_version) %>%
mutate_if(is.numeric, round, 4) %>%
dplyr::group_split(term) %>%
lapply(distinct) %>%
lapply(nrow) # should be 1 and 1
# all `glance` results identical at 0.0001
mutate_if(g, is.numeric, round, 4) %>%
select(-ergm_version) %>%
distinct %>%
nrow # should be 1
[INSTALLING] ergm_3.10.4.tar.gz
[LOADED] ergm 3.10.4
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.10.1.tar.gz
[LOADED] ergm 3.10.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.9.4.tar.gz
[LOADED] ergm 3.9.4
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.8.0.tar.gz
[LOADED] ergm 3.8.0
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.7.1.tar.gz
[LOADED] ergm 3.7.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.7.0.tar.gz
[LOADED] ergm 3.7.0
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.6.1.tar.gz
[LOADED] ergm 3.6.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.6.0.tar.gz
[LOADED] ergm 3.6.0
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.5.1.tar.gz
[LOADED] ergm 3.5.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.4.0.tar.gz
[LOADED] ergm 3.4.0
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.2.4.tar.gz
[LOADED] ergm 3.2.4
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.2.3.tar.gz
[LOADED] ergm 3.2.3
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.1.3.tar.gz
[LOADED] ergm 3.1.3
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.1.2.tar.gz
[LOADED] ergm 3.1.2
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.1.1.tar.gz
[LOADED] ergm 3.1.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.1-0.tar.gz
[LOADED] ergm 3.1.0
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.0-3.tar.gz
[LOADED] ergm 3.0.3
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
[INSTALLING] ergm_3.0-1.tar.gz
[LOADED] ergm 3.0.1
`summary(gest)$coefs` contains: Estimate, Std. Error, z value, Pr(>|z|)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment