Skip to content

Commit

Permalink
sna and magrittr namespace bug fix + update description fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellevy committed Jul 8, 2016
1 parent 42532a9 commit d2d7094
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
Package: gwdegree
Title: A Shiny App to Aid Interpretation of Geometrically-Weighted Degree Estimates in Exponential
Random Graph Models
Version: 0.1.0.9000
Version: 0.1.1
Authors@R: person("Michael", "Levy", email = "[email protected]", role = c("aut", "cre"))
License: MIT + file LICENSE
Description: This is a Shiny application intended to provide better understanding of how geometrically-weighted degree terms function in exponential random graph models of networks. It contains just one user function, gwdegree(), which launches the Shiny application.
URL: https://github.com/michaellevy/gwdegree, https://michaellevy.shinyapps.io/gwdegree/
BugReports: https://github.com/michaellevy/gwdegree/issues
Depends:
R (>= 3.2.3),
ergm,
ggplot2,
magrittr,
shiny,
shinydashboard
Imports:
dplyr,
gtools,
magrittr,
network,
scales,
sna,
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import(dplyr)
import(ergm)
import(ggplot2)
import(gtools)
import(magrittr)
import(scales)
import(shiny)
import(shinydashboard)
import(sna)
import(tidyr)
importFrom(grDevices,topo.colors)
importFrom(graphics,plot)
importFrom(magrittr,"%>%")
importFrom(network,network)
importFrom(network,network.size)
importFrom(stats,rbinom)
importFrom(tidyr,gather)
35 changes: 21 additions & 14 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ utils::globalVariables(".") # For the magrittr pipe to circumvent cran-check no
#' @return NULL Called for side-effect of launching shiny application
#' @export
#'
#' @import ergm sna ggplot2 scales dplyr tidyr gtools shiny shinydashboard
#' @import ergm sna ggplot2 magrittr scales dplyr gtools shiny shinydashboard
#' @importFrom grDevices topo.colors
#' @importFrom graphics plot
#' @importFrom network network.size
#' @importFrom network network
#' @importFrom magrittr "%>%"
#' @importFrom stats rbinom
#' @importFrom tidyr gather
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -66,18 +66,25 @@ simCCCent = function(gwdRange = c(-2, 2), gwespRange = c(-.5, .5),

N = makeNetwork(netSize, density)

lapply(1:nrow(dfForSim), function(i) {
n = simulate.formula(N ~ gwdegree(theta_s, TRUE) + gwesp(theta_t, TRUE),
coef = unlist(dfForSim[i, ]),
constraints = ~ edges,
nsim = nsim)
data.frame(
Centralization = mean(sna::centralization(n, 'degree', mode = 'graph')),
ClusteringCoef = mean(clusteringCoef(n))
)
}) %>%
do.call(rbind, .) %>%
cbind(dfForSim, .)
cbind(dfForSim,
do.call(rbind,
lapply(1:nrow(dfForSim), function(i) {
n = simulate.formula(N ~ gwdegree(theta_s, TRUE) + gwesp(theta_t, TRUE),
coef = unlist(dfForSim[i, ]),
constraints = ~ edges,
nsim = nsim)
data.frame(
Centralization = mean(degCent(n)),
ClusteringCoef = mean(clusteringCoef(n))
)
})
)
)
}

degCent = function(n) {
degs = sna::degree(n)
sum(abs(max(degs) - degs)) / sna::degree(n, tmaxdev = TRUE)
}

clusteringCoef = function(net)
Expand Down
21 changes: 5 additions & 16 deletions inst/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,8 @@ ui = dashboardPage(header, sidebar, body)
server =
shinyServer(function(input, output, session) {

# lapply(c('ergm', 'network', 'sna', 'ggplot2', 'dplyr', 'scales', 'magrittr'),
# require, char = TRUE)
# source("../R/functions.R")
theme_set(theme_bw(base_size = 20))


### Change-statistic plot ####
deltaGWDdf = reactive({
gwdegree:::makeDeltaGWDdF(kmin = input$degreeRange[1],
Expand Down Expand Up @@ -262,7 +258,7 @@ server =
isolate({
networks = vector('list', 2L)
networks[[1]] = replicate(input$reps,
gwdegree:::makeNetwork(input$netSize, density = input$density),
gwdegree:::makeNetwork(input$netSize, input$density),
simplify = FALSE)
networks[[2]] =
lapply(networks[[1]], function(basis) { # To keep number edges same across types
Expand All @@ -279,7 +275,7 @@ server =
# type is stack of simulated graphs of a particular type
# Split into each simulation, tabulate degrees, and use `smartbind` to match on names (ie, degree)
deg =
degree(networks[[type]], g = 1:length(networks[[type]]), gmode = 'graph') %>%
sna::degree(networks[[type]], g = 1:length(networks[[type]]), gmode = 'graph') %>%
split(., 1:ncol(.)) %>%
lapply(table) %>%
do.call(gtools::smartbind, .) %>%
Expand All @@ -294,15 +290,10 @@ server =
deg[is.na(deg)] = 0
deg$type = names(networks[type])

list(deg = deg,
var = data.frame(
type = names(networks)[type],
var = mean(sapply(networks[[type]], function(n) var(degree(n, gmode = 'graph'))))
))
deg
})

vari = do.call(rbind, lapply(dd, "[[", "var"))
deg = do.call(rbind, lapply(dd, "[[", "deg"))
deg = do.call(rbind, dd)

# Fill in the degrees-never-found to keep box-widths constant
filled = expand.grid(
Expand All @@ -316,15 +307,13 @@ server =
filled$type = factor(filled$type, levels = sort(unique(filled$type), decreasing = TRUE))
})

list(dd = filled, variance = vari, random = networks[[1]][[1]], gwd = networks[[2]][[1]])
list(dd = filled, random = networks[[1]][[1]], gwd = networks[[2]][[1]])


})

#### Plot degree distributions ####

# Pretty the x-axis labels and get outlier points colored
# And add average variance
output$degDistPlot = renderPlot({

ggplot(degDists()[["dd"]], aes(x = factor(degree), y = count, fill = type)) +
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test_degCent.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library(sna)
context("clustering coefficient function")

networks = list(
nEmpty = gwdegree:::makeNetwork(100, 0),
nFull = gwdegree:::makeNetwork(100, 1),
nOther = gwdegree:::makeNetwork(100, .2)
)

test_that("Degree centralization is the same for my function as package:sna",
expect_equal(
sapply(networks, gwdegree:::degCent),
sapply(networks, sna::centralization, sna::degree, "graph")
))

0 comments on commit d2d7094

Please sign in to comment.