Network Analysis_Brittanee Fisher_Assignment

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

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
library(DT)
library(plotly)

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
college_students <- 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.
college_links <- 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.
college_links |> 
  group_by(nominator) |> 
  summarize(Mean = round(mean(score), 1)) |> 
  arrange(-Mean) |> 
  datatable(rownames = F,
            caption = 'Average friendship scores given by each college student')

This table shows the average rating that each college student gave to their classmates.

college_links |> 
  group_by(nominated) |> 
  summarize(Mean = round(mean(score), 1)) |> 
  arrange(-Mean) |> 
  datatable(rownames = F,
            caption = 'Average friendship scores given to each college student')

This table shows the average rating each student recieved from their classmates.

college_model <- lm(expected_score ~ score, data = college_links)

tidy(college_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(college_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>
college_links |>
  plot_ly(x = ~score, y = ~expected_score) |>
  add_markers() |>
  add_lines(y = fitted(college_model))

This graph shows the correlation between the college students’ score of other classmates and their expected score from other classmates.

collegefriends <- college_links |> 
  filter(score > 2) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) |> 
  rename(to = nominated)
college_network <- graph_from_data_frame(collegefriends, college_students, directed = T)
college_students |> 
    mutate(named_friends = degree((college_network), mode = "out")) |> 
  mutate(named_by_others= degree((college_network), mode = "in")) |> 
  mutate(popularity = named_by_others - named_friends) |>
  select(name, named_friends, named_by_others, popularity) |> 
  datatable(rownames = F)

This table shows the number of people that each student named as a friend as well as the number of times each student was named as a friend. The final collumn in this table displays the popularity of each college student.

college_students |> 
    mutate(named_friends = degree((college_network), mode = "out")) |> 
  mutate(named_by_others= degree((college_network), mode = "in")) |>
  plot_ly(x = ~named_friends) |>
  add_histogram() |>
  layout(title = "How many friends college students named")

This graph shows the number of friends that the college students named.

reciprocity(college_network, mode = "ratio")
[1] 0.527088

The reciprocity analysis shows that 52% of the people that were named friends considered their nominator a friend as well.

collegebestfriends <- college_links |> 
  filter(score > 4) |> 
  select(nominator, nominated) |> 
  rename(from = nominator) |> 
  rename(to = nominated)
bestfriend_network <- graph_from_data_frame(collegebestfriends, college_students, directed = T)
college_students |> 
  mutate(group = membership(infomap.community(bestfriend_network))) |>
  mutate(value = degree(bestfriend_network)) |>
  visNetwork(collegebestfriends, main = "Network of College Bestfriends") |>
  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(bestfriend_network))`.
Caused by warning:
! `infomap.community()` was deprecated in igraph 2.0.0.
ℹ Please use `cluster_infomap()` instead.

This graphic shows the connection between college students who consider other classmates as best friends. The size of the dot determines popularity, and the colors represent the bestfriend communities among the college students.