── 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 networklibrary(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)
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.
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(nominated, nominator) |>datatable(rownames = F,caption ='Friendship ratings given and expected by each student')
In looking at this data table, it seems that this data set follows the trend of rating and expected rating being the same.
student_model <-lm(expected_score ~ score, data = friendships)
friendships %>%plot_ly(x =~score, y =~expected_score) %>%add_markers() %>%add_lines(y =fitted(student_model))
The regression line is pretty straight, supporting my observation above.
It looks like student 11 had a bigger difference in named (41) and returned friendships (15). They however had the second most named by others ratings than anyone else. Student 1 had 19 named by others and named 14 themselves.
friendship_nodes %>%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 students named")
It looks like in this data set it was most common to name about 6 friends.
reciprocity(student_network, mode ="ratio")
[1] 0.5205479
It looks like the reciprocity ratio was about half the named friendships.
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `group = membership(infomap.community(student_networkbf))`.
Caused by warning:
! `infomap.community()` was deprecated in igraph 2.0.0.
ℹ Please use `cluster_infomap()` instead.
The best friends out of this data set cut the numbers in about half. Still a lot of students. Based on the degree function 11 has the most reciprocal best friend ratings. You can see the colored nodes with no connections named no one as their best friend and no one named them as theirs. I tried to add communities just for the best friends network, so I am not sure why it gave the outliars colors.