friend network

library(tidyverse)
── 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.0
✔ 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.3.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.3.3
library(DT)
Warning: package 'DT' was built under R version 4.3.3
library(plotly)
Warning: package 'plotly' was built under R version 4.3.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.3.3
friendships <- read_csv("friendships.csv")
Rows: 6972 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (4): nominator, nominated, score, expected_score

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
friendship_nodes <- read_csv("friendship_nodes.csv")
Rows: 84 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (2): id, name

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
friendships |> 
  group_by(nominator) |> 
  summarize(Mean = round(mean(score), 1)) |> 
  arrange(-Mean) |> 
  datatable(rownames = F,
            caption = 'Average Friendship Score given by each Student')

Average score given by each student to other students rating their frienship.

friendships |> 
  group_by(nominated) |> 
  summarize(Mean = round(mean(score), 1)) |> 
  arrange(-Mean) |> 
  datatable(rownames = F,
            caption = 'Average Friendship Score nominated by each Student')

The average score given to students by other students rating their friendship

friendship_model <- lm(expected_score ~ score, data = friendships)

tidy(friendship_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(friendship_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>
friendships |>
  plot_ly(x = ~score, y = ~expected_score) |>
  add_markers() |>
  add_lines(y = fitted(friendship_model))

Plot showing the scores given by studends and their expected rating.

sqrt(.9108098)
[1] 0.9543636
friends <- friendships |> 
  filter(score > 3) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) |> 
  rename(to = nominated)
friend_network <- graph_from_data_frame(friends, friendship_nodes, directed = T)
friendship_nodes |> 
    mutate(named_friends = degree((friend_network), mode = "out")) |> 
  mutate(named_by_others= degree((friend_network), mode = "in")) |>
  mutate(popularity = named_friends - named_by_others) |>
  select(name, named_friends, named_by_others, popularity) |> 
  datatable(rownames = F)

Data table displaying each student and their score for their frienship with other students, The frienship scores they received and their resulting popularity.

friendship_nodes |>
  mutate(named_friends = degree ((friend_network), mode = "out")) |>
  mutate(named_by_others = degree((friend_network), mode = "in")) |>
  plot_ly(x = ~named_friends) |>
  add_histogram() |>
  layout(title = "Students Scored as friends")

Histogram displaying the scores of the students that named others as friends.

reciprocity(friend_network, mode = "ratio")
[1] 0.5205479

Statistic for how often students name others as friends against how often other students name them as friends

best_friends <- friendships |> 
  filter(score > 4) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) |> 
  rename(to = nominated)
best_friend_network <- graph_from_data_frame(best_friends, friendship_nodes, directed = T)
friendship_nodes |>  
  mutate(value = degree(best_friend_network)) |> 
  mutate(group = membership(infomap.community(best_friend_network))) |>  
  visNetwork(friends, main = "Network of best friendships") |>
  visIgraphLayout(layout = "layout_with_kk") |> 
  visEdges(arrows = "to") |> 
  visOptions(highlightNearest = T, nodesIdSelection = T)
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `group = membership(infomap.community(best_friend_network))`.
Caused by warning:
! `infomap.community()` was deprecated in igraph 2.0.0.
ℹ Please use `cluster_infomap()` instead.

Friend network diagram of participants based on best friend ratings diplayed by size and broke up into groups by color.