---
title: 'SOCI424/624: Transmission'
author: '(your name)'
collaborators:
  - 'name'
  - 'name'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# load the 'igraph' package
library(igraph)
```


## Part 1: Get the data

### Adolescent tobacco use
Today you will analyze structural features of tobacco use among a cohort of 129 school children over two years in Glasgow, Scotland. The data is a subset of a larger study commissioned by the Scottish government intended to curb smoking among children. The data you'll be using contains friendship nominations and self-reported use of tobacco, alcohol, and cannabis among the students. You can find more information about the dataset on [Tom Snijder's website](http://www.stats.ox.ac.uk/~snijders/siena/Glasgow_data.htm), and through the [study questionnaire iteself](http://www.stats.ox.ac.uk/~snijders/siena/questionnaire%20Glasgow%20study.pdf).

### Task 1A: Loading the data

The data is available in several files. The friendship nomination data is recorded at two time periods: one in 1995 (<https://soci424.netlify.app/data/glasgow/friendships_t1.txt>) and one two years later in 1997 (<https://soci424.netlify.app/data/glasgow/friendships_t2.txt>). These are both simple adjacency matrices. Information about each of the students is presented in a CSV file (<https://soci424.netlify.app/data/glasgow/student_info.csv>).

-   *Use R's `read.table()` function to load the frienship matrices into two variables named `fr1` and `fr2`. Then use `read.csv()` to download the student data into the variable `students`. Inspect these three objects visually. What vertex attributes are included? What edge attributes?*

```{r 1a_1}
# (your code here)
```

-   *The `graph_from_adjacency_matrix()` function will build native graph objects from adjacency matrices, but it expects the input data to be of a specific type. When you used `read.table()` above, R created `data.frame` objects, which is a flexible way to store tabular data. But `graph_from_adjacency_matrix()` expects a `matrix` object and will throw an error if you try to call it on a `data.frame`. In the space below, build a network for each time period (call them `net1` and `net2`) by (a) converting `fr1` and `fr2` into matrices using the `as.matrix()` function, and (b) calling `graph_from_adjacency_matrix()` on the results.*

```{r 1a_2}
# (your code here)
```

### Task 1B: Visualizing a changing network

-   *Create a 'legible' visualization of _each_ of these networks--something that lets you see their overall structure. You will need to adjust various vertex attributes (e.g. `size` and `label`--you can probably just remove the labels altogether) and edge attributes (e.g. `width`, `arrow.size`, and `color`).*

```{r 1b_1}
# (your code here)
```

When comparing networks with the same nodes but different relations (like these two snapshots of the same cohort of students) it can be useful to use a fixed layout for visualizations. This way, the nodes will be in the same place, it is just the edges that will differ. Normally, igraph re-generates the layout each time you plot a network--this is why when you plot the same network multiple times it will look different each time. But igraph lets you save the results of a layout as a matrix describing the x and y coordinates of each vertex.

-   *The function `layout_nicely()` takes a network as its argument, and returns a 'reasonable' layout for that network. In the block below, use `layout_nicely( net1 %u% net2 )` to generate a layout for the _union_ of both of the networks (the union of two networks is the set of all nodes and every edge that's in either network). Store this layout in a variable named `lyt`. Looking at `lyt`, how many rows and columns does it have? What do the rows and columns represent?*

```{r 1b_2}
# (your code here)
```

-   *Plot both networks, each with the new layout you created in the previous task. To plot using a pre-computed layout you supply the `layout=lyt` argument to the `plot()` function. What does looking at the two networks next to each other tell you about the way the social structure among these students changed over the two years of the study (give a brief narrative description, discussing any patterns, structures, and changes you see)?*

```{r 1b_3}
# (your code here)
```

## Part 2: Causal stories

The original purpose of this study was to understand what leads students to use tobacco. In the following prompts, we will think through some of the potential causal processes displayed in these networks.

-   *Think about a way that adolescents' friendships and social structures could affect their tobacco usage. What are some mechanisms that would lead one's position in a network to cause them to increase or decrease their tobacco usage.*

  * _(your response here)_

-   *Now consider the ways that a student's tobacco usage could affect their friendships and their social relations. How could their behavior and habits affect the formation and dissolution of network ties. Think of some specific mechanisms.*

  * _(your response here)_

-   *Finally, what are some kinds of student characteristics that could be causes of both network structure _and_ tobacco usage? E.g. What (potentially unobserved) aspects of a particular pair of students that would affect both their relationship and their tobacco usage?*

  * _(your response here)_

-   *You've come up with a plausible explanation for (a) how student's network position could affect their tobacco usage, (b) how a student's tobacco usage could affect their network position, and (c) how some other factor could affect a student's network position and their tobocco usage. In general terms, how could you use the kind of network data you have here to distinguish between these accounts? Could more than one of these causal stories be 'right'?*

  * _(your response here)_

## Part 3: Examining tobacco use

-   *Create a vertex attribute called `tobacco` on both networks (`net_1` and `net_2`) that records each student's tobacco usage in the respective wave of the survey. These are stored in the `students` data frame you downloaded in part 1A above, named `tobacco_1` and `tobacco_2`.*

```{r 3_1}
# (your code here)
```

-   *Recreate the visualizations from part 1B (3) above, but color the students to who reported "regular" tobacco use differently from the others.*

```{r 3_2}
# (your code here)
```

-   *Visually inspect these new plot. Do you see any association between tobacco use and who a student is friends with? Do you see any evidence supporting any of the causal stories you described in part 2? Be specific.*

  * _(your response here)_