---
title: "Ego Networks Activity"
author: "Your Name Here"
format: pdf
pdf-engine: pdflatex
---

```{r setup, message=FALSE, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# import the libraries needed
library(igraph)
library(ggraph) # If you get an error run install.packages('ggraph')
library(tidygraph) # If you get an error run install.packages('tidygraph')
library(readxl) # If you get an error run install.packages('tidyverse')
library(tidyverse)
#extrafont::loadfonts() 
set_graph_style(family="sans")
```

We read about the ego networks of Americans. In this exercise, you'll create your own ego network and visualize it, using the skills that we've been working on in R.

First, you should create your network spreadsheets in Excel. Start by making a node attributes file with a row for each person that you have "discussed important matters with" over the last 6 months.

The columns of the spreadsheet should be:
- name
- gender
- education (years of education)
- race
- age
- is.kin (are they related to you?)

For `is.kin`, write TRUE if they are related to you and FALSE if they are not.

Then, make a spreadsheet that's an edgelist with a row for each connection in your network. For each edge, put the edge weight as 0 if they are strangers, 2 if they are especially close, or 1 if they are somewhere in between.

It should look something like this:

from | to | weight
--- | --- | ---
Person 1 | Person 2 | 1
Person 1 | Person 3 | 2
Person 2 | Person 3 | 0
...

Change the code below so that the path points to where you saved the edgelist and node attribute files.

```{r}
edges <- read_excel('/home/jeremy/Teaching/can-s26.jeremydfoote.com/assignments/r_lab_examples/ego_edges.xlsx')
nodes <- read_excel('/home/jeremy/Teaching/can-s26.jeremydfoote.com/assignments/r_lab_examples/ego_atts.xlsx')
```

Code to load the file into R and make it into a network object.

```{r}
G <- graph_from_data_frame(d=edges, vertices = nodes, directed = F) |> as_tbl_graph()
```

Code to filter out the edges that have a weight of 0.

```{r}
G <- G |>
  activate(edges) |>
  filter(!is.na(weight)) |>
  filter(weight != 0)
```

This code will make a plot of the network. You'll learn soon how to do this on your own.

```{r}
G |>
  ggraph(layout = 'stress') +
  geom_edge_fan(aes(width = weight), alpha = .3) +
  geom_node_point(aes(color = is.kin), size = 3) +
  geom_node_text(aes(label = name), repel = TRUE, size = 4) +
  scale_color_viridis_d() +
  scale_edge_width(range = c(1, 3))
```


## Exercises

Calculate the following for your network. Use Google to find the code to do this. I'll give you the first one.

1. The number of nodes

```{r}
G |>
  vcount()
```

2. The number of nodes who are kin (use the `filter` function)
```{r}

## YOUR CODE HERE


```

3. The number of nodes who are non-kin

```{r}

## YOUR CODE HERE

```


4. The density of the network
```{r}

## YOUR CODE HERE

```

5. The standard deviation of age in your network.

As a hint, here's how you would get the standard deviation of education

```{r}

G |>
  activate(nodes) |>
  as_tibble() |>
  summarize(education_sd = sd(education))

```

```{r}

## YOUR CODE HERE

```

6. What proportion of your network has the same race (the most common race)?

HINT: Here's how you would get the proportion of the network that is kin.

```{r}

G |>
  activate(nodes) |>
  as_tibble() |>
  summarize(kin_proportion = sum(is.kin) / n())

```

```{r}

## YOUR CODE HERE

```


7. What proportion of your network has the same gender (the most common gender)?

```{r}

## YOUR CODE HERE

```
