1 Create small network

Create a directed network shown on the figure below. Mind the labels of the nodes.

g <- igraph::make_graph( ~  A --+ B:C, D --+ E:F, A +--+ D, G +--+ A:D  )
plot(g)

2 Mutual ties

Transform the network created above to an undirected network such that an undirected tie is present if and only if there are reciprocated ties in the original network. Visualize the network and check the correctness of the result.

gg <- igraph::as.undirected(g, mode="mutual")
plot(gg)

# Verify number of edges
igraph::ecount(gg)
## [1] 3

3 Histograms of degree distributions

Compute the degree distributions (in-, out- and total) for the classroom network and visualize it with a barchart.

barplot(igraph::degree_distribution(classroom), 
        xlab="Total degree", ylab="Frequency")

barplot(igraph::degree_distribution(classroom, mode="in"), 
        xlab="In-degree", ylab="Frequency")

barplot(igraph::degree_distribution(classroom, mode="out"), 
        xlab="Out-degree", ylab="Frequency")

4 Sociality and popularity

Check whether popularity in the classroom is associated with sociality. We will measure popularity with in-degree and sociality with out-degree. Answer the question by

# Data frame with two variables containing in-degrees and out-degrees for each
# vertex
degs <- data.frame(
  indegree=igraph::degree(classroom, mode="in"),
  outdegree = igraph::degree(classroom, mode="out")
)
# Scatterplot
plot(indegree ~ outdegree, data=degs,
     xlab="Sociality", ylab="Popularity",
     col = igraph::V(classroom)$female + 1 )

# Correlation coef
cor(degs)
##            indegree outdegree
## indegree  1.0000000 0.3498949
## outdegree 0.3498949 1.0000000

5 Other centrality measures

Select two other centrality measures. Are they highly correlated as well? What two measures would have the lowest correlation?

# Data frame with two variables containing in-degrees and out-degrees for each
# vertex
cents <- data.frame(
  bet=igraph::betweenness(classroom),
  ev=igraph::eigen_centrality(classroom)$vector
)

# Scatterplot
plot(bet ~ ev, data=cents,
     xlab="Betweenness", ylab="Eigenvector",
     col = igraph::V(classroom)$female + 1 )

# Correlation coef
cor(cents)
##           bet        ev
## bet 1.0000000 0.7119318
## ev  0.7119318 1.0000000
# The plot using 'network' package
classroom_network <- intergraph::asNetwork(classroom)

plot(
  classroom_network,
  vertex.cex=cents$bet/25+1,
  vertex.col=rgb(0,0,cents$ev/max(cents$ev))
)

6 Popularity in Faux Mesa High

Look at the school liking” network faux.mesa.high. Who is the most popular boy? and girl? What grades are they in?

load("introToSNAinR.Rdata")
order(sna::degree(faux.mesa.high,cmode="indegree"))
##   [1]   3   4   6   7  10  12  20  24  26  28  35  37  39  41  42  45  46  48
##  [19]  49  50  62  67  69  72  73  80  84  85  94  95 106 107 113 116 118 119
##  [37] 120 126 130 135 143 145 152 154 159 162 163 168 169 171 172 175 177 184
##  [55] 188 197 203   5  14  17  19  23  32  33  38  40  57  68  71  78  81  82
##  [73]  86  90  91  93  97  98 101 111 112 117 121 122 125 128 133 141 144 146
##  [91] 147 148 155 156 166 167 170 174 176 180 181 186 193 198 199 200 202 205
## [109]  11  13  16  27  29  31  34  53  56  58  60  63  65  75  76  77  83  88
## [127]  89 103 105 109 131 132 151 182 191 192 196 201   8  21  36  43  44  51
## [145]  54  59  61  70  99 108 114 115 124 129 134 138 142 149 153 161 164 178
## [163] 183 194 195 204   2   9  15  18  30  52  66  92 102 137 150 157 158 165
## [181] 173 179 187 190  64  74  79 100 104 110 127 136 140 185  22 139  25  96
## [199] 123 189 160  47  55  87   1
network::get.vertex.attribute(faux.mesa.high,"Sex")[1] #Female
## [1] "F"
network::get.vertex.attribute(faux.mesa.high,"Grade")[1]
## [1] 7
network::get.vertex.attribute(faux.mesa.high,"Sex")[87] #Female
## [1] "F"
network::get.vertex.attribute(faux.mesa.high,"Sex")[55] #Male
## [1] "M"
network::get.vertex.attribute(faux.mesa.high,"Grade")[55]
## [1] 8
# Network visualization
sexColor<-ifelse(network::get.vertex.attribute(faux.mesa.high,"Sex")=="F",4,50)
network::plot.network(
  faux.mesa.high,
  vertex.col = rainbow(6)[network::get.vertex.attribute(faux.mesa.high,"Grade")-6],
  vertex.cex=sna::degree(faux.mesa.high,cmode="indegree")/2,
  vertex.sides=sexColor
)