-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from snlab-ch/develop
v0.12.6
- Loading branch information
Showing
43 changed files
with
2,857 additions
and
1,461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
#' @importFrom generics tidy | ||
#' @export | ||
generics::tidy | ||
|
||
#' @method tidy netlm | ||
#' @importFrom stats quantile | ||
#' @export | ||
tidy.netlm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { | ||
|
||
result <- dplyr::tibble(term = x$names, | ||
estimate = x$coefficients, | ||
# std.error = NA_real_, | ||
statistic = x$tstat, | ||
p.value = x$pgreqabs) | ||
|
||
if (conf.int) { | ||
ci <- apply(x$dist, 2, stats::quantile, c(.025, .975)) | ||
ci <- cbind(data.frame(term = x$names), t(ci)) | ||
names(ci) <- c("term", "conf.low", "conf.high") | ||
result <- dplyr::left_join(result, ci, by = "term") | ||
} | ||
|
||
result | ||
} | ||
|
||
#' @method tidy netlogit | ||
#' @importFrom stats quantile | ||
#' @export | ||
tidy.netlogit <- function(x, conf.int = FALSE, conf.level = 0.95, | ||
exponentiate = FALSE, ...) { | ||
|
||
result <- dplyr::tibble(term = x$names, | ||
estimate = `if`(exponentiate, | ||
exp(x$coefficients), | ||
x$coefficients), | ||
# std.error = NA_real_, | ||
statistic = x$tstat, | ||
p.value = x$pgreqabs) | ||
|
||
if (conf.int) { | ||
ci <- apply(x$dist, 2, stats::quantile, c(.025, .975)) | ||
ci <- cbind(data.frame(term = x$names), t(ci)) | ||
names(ci) <- c("term", "conf.low", "conf.high") | ||
result <- dplyr::left_join(result, ci, by = "term") | ||
} | ||
|
||
result | ||
} | ||
|
||
#' @importFrom generics glance | ||
#' @export | ||
generics::glance | ||
|
||
#' @method glance netlm | ||
#' @export | ||
glance.netlm <- function(x, ...) { | ||
|
||
mss <- sum((stats::fitted(x) - mean(stats::fitted(x)))^2) | ||
rss <- sum(stats::resid(x)^2) | ||
qn <- NROW(x$qr$qr) | ||
df.int <- x$intercept | ||
rdf <- qn - x$rank | ||
resvar <- rss/rdf | ||
fstatistic <- c(value = (mss/(x$rank - df.int))/resvar, | ||
numdf = x$rank - df.int, | ||
dendf = rdf) | ||
r.squared <- mss/(mss + rss) | ||
adj.r.squared <- 1 - (1 - r.squared) * ((qn - df.int)/rdf) | ||
sigma <- sqrt(resvar) | ||
|
||
dplyr::tibble( | ||
r.squared = r.squared, | ||
adj.r.squared = adj.r.squared, | ||
sigma = sigma, | ||
statistic = fstatistic["value"], | ||
p.value = stats::pf( | ||
fstatistic["value"], | ||
fstatistic["numdf"], | ||
fstatistic["dendf"], | ||
lower.tail = FALSE | ||
), | ||
df = fstatistic["numdf"], | ||
# logLik = as.numeric(stats::logLik(x)), | ||
# AIC = stats::AIC(x), | ||
# BIC = stats::BIC(x), | ||
# deviance = stats::deviance(x), | ||
df.residual = stats::df.residual(x), | ||
nobs = x$n | ||
) | ||
} | ||
|
||
#' @method glance netlogit | ||
#' @export | ||
glance.netlogit <- function(x, ...) { | ||
|
||
# mss <- sum((fitted(x) - mean(fitted(x)))^2) | ||
# rss <- sum(resid(x)^2) | ||
# qn <- NROW(x$qr$qr) | ||
# df.int <- x$intercept | ||
# rdf <- qn - x$rank | ||
# resvar <- rss/rdf | ||
# fstatistic <- c(value = (mss/(x$rank - df.int))/resvar, numdf = x$rank - | ||
# df.int, dendf = rdf) | ||
# r.squared <- mss/(mss + rss) | ||
# adj.r.squared <- 1 - (1 - r.squared) * ((qn - df.int)/rdf) | ||
# sigma <- sqrt(resvar) | ||
|
||
dplyr::tibble( | ||
# r.squared = r.squared, | ||
# adj.r.squared = adj.r.squared, | ||
# sigma = sigma, | ||
# statistic = fstatistic["value"], | ||
# p.value = pf( | ||
# fstatistic["value"], | ||
# fstatistic["numdf"], | ||
# fstatistic["dendf"], | ||
# lower.tail = FALSE | ||
# ), | ||
# df = fstatistic["numdf"], | ||
# logLik = as.numeric(stats::logLik(x)), | ||
pseudo.r.squared = (x$null.deviance - x$deviance)/(x$null.deviance - x$deviance + | ||
x$df.null), | ||
AIC = x$aic, | ||
AICc = x$aic + (2*x$rank^2 + 2*x$rank)/(x$n-x$rank-1), | ||
BIC = x$bic, | ||
chi.squared = 1 - stats::pchisq(x$null.deviance - x$deviance, | ||
df = x$df.null - x$df.residual), | ||
deviance = x$deviance, | ||
null.deviance = x$null.deviance, | ||
df.residual = stats::df.residual(x), | ||
nobs = x$n | ||
) | ||
} | ||
|
||
#' @export | ||
plot.netlm <- function(x, ...){ | ||
distrib <- x$dist | ||
distrib <- as.data.frame(distrib) | ||
names(distrib) <- x$names | ||
distrib$obs <- seq_len(nrow(distrib)) | ||
distrib <- tidyr::pivot_longer(distrib, | ||
cols = 1:(ncol(distrib)-1)) | ||
distrib$coef <- rep(unname(x$coefficients), nrow(x$dist)) | ||
distrib$tstat <- rep(unname(x$tstat), nrow(x$dist)) | ||
distrib$name <- factor(distrib$name, x$names) | ||
ggplot2::ggplot(distrib, ggplot2::aes(.data$value, .data$name)) + | ||
ggplot2::geom_violin(draw_quantiles = c(0.025, 0.975)) + | ||
ggplot2::theme_minimal() + | ||
ylab("") + xlab("Statistic") + | ||
ggplot2::geom_point(aes(x = .data$tstat), size = 2, | ||
colour = "red") + | ||
scale_y_discrete(limits=rev) | ||
} | ||
|
||
#' @export | ||
plot.netlogit <- function(x, ...){ | ||
distrib <- x$dist | ||
distrib <- as.data.frame(distrib) | ||
names(distrib) <- x$names | ||
distrib$obs <- seq_len(nrow(distrib)) | ||
distrib <- tidyr::pivot_longer(distrib, | ||
cols = 1:(ncol(distrib)-1)) | ||
distrib$coef <- rep(unname(x$coefficients), nrow(x$dist)) | ||
distrib$tstat <- rep(unname(x$tstat), nrow(x$dist)) | ||
distrib$name <- factor(distrib$name, x$names) | ||
ggplot2::ggplot(distrib, ggplot2::aes(.data$value, .data$name)) + | ||
ggplot2::geom_violin(draw_quantiles = c(0.025, 0.975)) + | ||
ggplot2::theme_minimal() + | ||
ylab("") + xlab("Statistic") + | ||
ggplot2::geom_point(aes(x = .data$tstat), size = 2, | ||
colour = "red") + | ||
scale_y_discrete(limits=rev) | ||
} | ||
|
||
make_diff_model <- function(out, object) { | ||
class(out) <- c("diff_model", class(out)) | ||
attr(out, "mode") <- node_mode(object) | ||
out | ||
} | ||
|
||
#' @export | ||
print.diff_model <- function(x, ...){ | ||
print(dplyr::tibble(x)) | ||
} | ||
|
||
|
||
#' @export | ||
summary.diff_model <- function(object, ...){ | ||
cum_sum <- NULL | ||
ns <- length(attr(object, "mode")) | ||
dplyr::count(object, t) %>% | ||
mutate(cum_sum = cumsum(n)) %>% | ||
mutate(percent = cum_sum/ns) | ||
} | ||
|
||
#' @export | ||
plot.diff_model <- function(x, ...){ | ||
percent <- NULL | ||
y <- summary(x) | ||
ggplot2::ggplot(y) + | ||
ggplot2::geom_line(aes(x = t, y = percent)) + | ||
ggplot2::theme_minimal() + | ||
ggplot2::ylab("Proportion") + ggplot2::xlab("Time") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' Functions to play games on networks | ||
#' @inheritParams is | ||
#' @param seeds A valid mark vector the length of the | ||
#' number of nodes in the network. | ||
#' @param thresholds A numeric vector indicating the thresholds | ||
#' each node has. By default 1. | ||
#' @param steps The number of steps forward in the diffusion to play. | ||
#' By default the number of nodes in the network. | ||
#' @examples | ||
#' play_diffusion(generate_smallworld(15, 0.025)) | ||
#' @export | ||
play_diffusion <- function(object, | ||
seeds = 1:2, | ||
thresholds = 1, | ||
steps){ | ||
n <- network_nodes(object) | ||
if(missing(steps)) steps <- n | ||
if(length(thresholds)==1) thresholds <- rep(thresholds, n) | ||
|
||
infected <- seeds | ||
t = 0 | ||
events <- data.frame(t = t, nodes = seeds) | ||
|
||
repeat{ | ||
exposed <- unlist(sapply(igraph::neighborhood(object, nodes = infected), | ||
function(x) setdiff(x, infected))) | ||
tabexp <- table(exposed) | ||
new <- as.numeric(names(which(tabexp > thresholds[as.numeric(names(tabexp))]))) | ||
if(length(new)==0) break | ||
infected <- c(infected, new) | ||
t <- t+1 | ||
events <- rbind(events, data.frame(t = t, nodes = new)) | ||
if(length(infected)==n) break | ||
if(t==steps) break | ||
} | ||
make_diff_model(events, object) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.