-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriendshipEDA.Rmd
92 lines (73 loc) · 2.31 KB
/
friendshipEDA.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: "friendshipEDA.Rmd"
author: "Noah Johnson"
date: "April 22, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(install.load)
install_load('tidyverse')
install_load('ggplot2')
install_load('networkD3')
```
## Simple Network Example
```{r}
# Simple example
Source <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
Target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
NetworkData <- data.frame(Source, Target)
# Create graph
#simpleNetwork(NetworkData)
simpleNetwork(NetworkData, fontFamily = "sans-serif")
```
## Force Network example
```{r}
data("MisLinks")
data("MisNodes")
```
```{r}
str(MisLinks)
str(MisNodes)
```
```{r}
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", Target = "target", Value = "value", NodeID = "name",
Nodesize = "size", radiusCalculation = "Math.sqrt(d.nodesize)+6", Group = "group", opacity = 1, opacityNoHover = TRUE, width = 1000, height = 700, fontSize = 12)
```
```{r}
load(file = "data/friendsmatrix.Rda")
load(file = "data/networkIDmapping.Rda")
load(file = "data/groups.Rda")
load(file = "data/proximityEvents.Rda")
str(friends)
str(mapped_ids)
str(groups)
str(proximity.events)
```
```{r}
n <- nrow(friends)
sources <- c()
targets <- c()
values <- c()
# Generate links
k <- 1
for (i in 1:N) {
for (j in 1:N) {
if (!is.na(friends[i,j])) {
sources[k] <- i-1 # forceNetwork requires 0 indexing
targets[k] <- j-1
values[k] <- log(proximity.events[i,j] + proximity.events[j,i] + 1) + 1 # pad by 1 so there's no zero-width links
k <- k + 1
}
}
}
# Count number of friends for every subject in the network
sizes <- sapply(1:N, function(s) return (exp(sum(targets + 1 == s))), USE.NAMES = FALSE)
# Create df for nodes (1 per subject)
friendNodes <- data.frame(subjectID = mapped_ids, size = sizes, group = groups)
# Create df for links
friendLinks <- data.frame(source = sources, target = targets, value = values)
```
```{r}
forceNetwork(Links = friendLinks, Nodes = friendNodes, Source = "source", Target = "target", Value = "value", NodeID = "subjectID", Nodesize = "size", Group = "group", linkWidth = JS("function(d) { return Math.sqrt(d.value); }"), arrows = TRUE, legend = TRUE, zoom = T, opacity = 1, opacityNoHover = 1, charge = -100, width = 1000, height = 700, fontSize = 12)
```