* Next we will loop through every student to see if that student is not depressed if that student becomes depressed
forv i=1(1)`Num_students' {
* If the student is depressed skip that student
if depression[`i']==0 forv j=1(1)`Num_students' {
* This checks if j is friends with i qui if fri_`j'[`i'] == 1 & depression[`j']==1 {
local depression_spread=rbinomial(1,`beta') if `depression_spread' == 1 { replace depression2=1 if _n==`i'noi di "Student `i' becomes depressed as a result of `j''s depression"break }}
}
}

* Can we plot one of those awesome social network graphs in Stata?
gl two_list

* Person
forv i=1(1)`Num_students' {
local x_pos = x[`i']
local y_pos = y[`i']
qui gen conct_x_`i' = `x_pos' if _n==1
qui gen conct_y_`i' = `y_pos' if _n==1
* This creates two variables for every person in the network.
* It allows in effect a separate network for each person.

local count_position = 1

forv j=1(1)`i' {
* This checks if j is friends with iqui if fri_`j'[`i'] == 1 {
replace conct_x_`i' = x[`j'] if _n==`count_position'+1
replace conct_y_`i' = y[`j'] if _n==`count_position'+1
replace conct_x_`i' = `x_pos' if _n==`count_position'+2
replace conct_y_`i' = `y_pos' if _n==`count_position'+2 * This draws a connection from the person i to the person j before returning to person i.
local count_position = `count_position'+2 * This moves the position up 2 spaces to make space for the next expansion of the set.
}
gl two_list ${two_list} (line conct_y_`i' conct_x_`i', mcolor(gs8))
* This adds a entry in the list of graphs to be graphed for each person.
}
}

* A typical way of storing network data is as edgelists (see: http://en.wikipedia.org/wiki/Doubly_connected_edge_list).

* We can create an edge list from our data with the following code:

gen edge1 = .
gen edge2 = .

local count_position = 1

* This will loop through all of the students and add to the edge list one observation for every connection in each direction
forv i=1(1)`Num_students' {
forv j=1(1)`i' {
* This checks if j is friends with iqui if fri_`j'[`i'] == 1 {
replace edge1 = `i' if _n==`count_position'
replace edge2 = `j' if _n==`count_position'
local count_position = `count_position'+1 noi di "edge1 edge2"* This will makge sure we do not run out of observations in the data set to record our edges if `count_position'==_N set obs `=`count_position'+1'}
}
}

* netplot is a use written program that allows social network plots to be easily drawn from edgelist data.
* It works much faster than my network mapping code generated above.
cap netplot edge?
* If you do not have netplot installed the following code should install it.
if _rc!=0 {
ssc install netplot
netplot edge*

}

* This option allows network plots to be drawn as a circle.
cap netplot edge*, type(circle)