R network analysis

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(igraph)                    # This is the package to analyze the network
Warning: package 'igraph' was built under R version 4.2.3

Attaching package: 'igraph'

The following objects are masked from 'package:lubridate':

    %--%, union

The following objects are masked from 'package:dplyr':

    as_data_frame, groups, union

The following objects are masked from 'package:purrr':

    compose, simplify

The following object is masked from 'package:tidyr':

    crossing

The following object is masked from 'package:tibble':

    as_data_frame

The following objects are masked from 'package:stats':

    decompose, spectrum

The following object is masked from 'package:base':

    union
library(visNetwork)                # Creates visualizations of the network
Warning: package 'visNetwork' was built under R version 4.2.3
library(DT)
Warning: package 'DT' was built under R version 4.2.3
library(plotly)
Warning: package 'plotly' was built under R version 4.2.3

Attaching package: 'plotly'

The following object is masked from 'package:igraph':

    groups

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(broom)                     # includes some of our regression functions
Warning: package 'broom' was built under R version 4.2.3
students <- read.csv("C:/Users/katri/Downloads/R class/friendship_nodes.csv")
student_links <- read.csv("C:/Users/katri/Downloads/R class/friendships.csv")
student_links |> 
  group_by(nominator, nominated) |> 
  summarize(Mean = round(mean(score), 1)) |> 
  arrange(-Mean) |> 
  datatable(rownames = F,
            caption = 'Friendship ratings given by and to each student')
`summarise()` has grouped output by 'nominator'. You can override using the
`.groups` argument.
  1. This table shows on average, how students would rate others and puts them from highest to lowest. Participants 11 and 56 were tied in rating others the highest. Students 1, 11, and 63 were tied for being ranked the highest. This means that they were perceived by others as being a closer friend.
friends <- student_links |> 
  filter(score > 2) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) %>% 
  rename(to = nominated)
student_model <- lm(expected_score ~ score, data = student_links)

tidy(student_model)
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   0.0987   0.00739      13.4 3.22e-40
2 score         0.940    0.00352     267.  0       
glance(student_model)
# A tibble: 1 × 12
  r.squared adj.r.squared sigma statistic p.value    df logLik   AIC   BIC
      <dbl>         <dbl> <dbl>     <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>
1     0.911         0.911 0.339    71178.       0     1 -2353. 4713. 4733.
# ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
student_links |>
  plot_ly(x = ~score, y = ~expected_score) |>
  add_markers() |>
  add_lines(y = fitted(student_model))
  1. Scatter plot of the scores and expected scores between students with a regression line.
student_network <- graph_from_data_frame(friends, students, directed = T)
students |> 
    mutate(named_friends = degree((student_network), mode = "out")) |> 
  mutate(named_by_others= degree((student_network), mode = "in")) |> 
  mutate(popularity = named_by_others - named_friends) |>
  select(name, named_friends, named_by_others, popularity) |> 
  datatable(rownames = F)
  1. Table showing how frequently subjects were named as a friend by others, how frequently they named others as a friend, and how popular each subject was.
students |> 
    mutate(named_friends = degree((student_network), mode = "out")) |> 
  mutate(named_by_others= degree((student_network), mode = "in")) |> 
  plot_ly(x = ~named_friends) |>
  add_histogram() |>
  layout(title = "How many friends participant named")
  1. Histogram showing how many friends each participant named.
reciprocity(student_network, mode = "ratio")
[1] 0.527088
  1. The reciprocity for this study was approximately 53%.
besties <- student_links |> 
  filter(score > 4) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) |>
  rename(to = nominated)

New network with just best friends

students |> 
    mutate(named_friends = degree((student_network), mode = "out")) |> 
  mutate(named_by_others= degree((student_network), mode = "in")) |> 
  mutate(popularity = named_by_others - named_friends) |>
  select(name, named_friends, named_by_others, popularity) 
   name named_friends named_by_others popularity
1     1            26              32          6
2     2             5               4         -1
3     3            20              19         -1
4     4            13              16          3
5     5            10              12          2
6     6            31              20        -11
7     7            15              15          0
8     8             7              12          5
9     9            16              23          7
10   10            16              12         -4
11   11            52              26        -26
12   12            14              14          0
13   13            19               9        -10
14   14             6              12          6
15   15            19              11         -8
16   16            22              14         -8
17   17            11              11          0
18   18            18              12         -6
19   19            13              19          6
20   20            30              13        -17
21   21            15               9         -6
22   22            18              23          5
23   23             9               3         -6
24   24            30              25         -5
25   25            14              28         14
26   26            10              12          2
27   27             2               5          3
28   28             7              16          9
29   29            13              13          0
30   30            33              27         -6
31   31            18              25          7
32   32             9              13          4
33   33             4               7          3
34   34             8              14          6
35   35            16              12         -4
36   36            22              20         -2
37   37             6               3         -3
38   38            38              24        -14
39   39            29              20         -9
40   40            20              26          6
41   41            22              20         -2
42   42            27              13        -14
43   43            14              19          5
44   44            18              20          2
45   45            15              21          6
46   46            10              15          5
47   47             3              18         15
48   48            24              14        -10
49   49            11               5         -6
50   50            24              18         -6
51   51            24              21         -3
52   52            22              28          6
53   53            10              13          3
54   54            27              29          2
55   55             2               2          0
56   56            55              23        -32
57   57             3               6          3
58   58            15              14         -1
59   59            17              24          7
60   60            11              17          6
61   61            13              16          3
62   62             9              11          2
63   63            11              30         19
64   64             3               9          6
65   65            16              16          0
66   66            11              21         10
67   67            13              18          5
68   68            16              22          6
69   69            17              16         -1
70   70            19              10         -9
71   71            26              19         -7
72   72            31              28         -3
73   73             1               1          0
74   74             8              15          7
75   75            20              33         13
76   76            15              10         -5
77   77             7              19         12
78   78            14               4        -10
79   79             5              14          9
80   80            10              16          6
81   81             0               0          0
82   82            23              16         -7
83   83            14              23          9
84   84            13              15          2
besties_network <- graph_from_data_frame(besties, students, directed = T)

Code to make a new network

students |> 
  mutate(value = degree(besties_network)) |>
  mutate(group = membership(infomap.community(besties_network))) |>
visNetwork(besties, main = "Network of participant best friends") |>
  visIgraphLayout(layout = "layout_nicely") |>
 visEdges(arrows = "to") |>
  visOptions(highlightNearest = T, nodesIdSelection = T) 
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `group = membership(infomap.community(besties_network))`.
Caused by warning:
! `infomap.community()` was deprecated in igraph 2.0.0.
ℹ Please use `cluster_infomap()` instead.
  1. Visual of the network of only best friends color coordinated.