## R Functions

Below are several R functions that I (and others) have programmed. I have found these very useful.

Sensitivity Analysis for Latent HomophilySensitivity Analysis for Latent Homophily

Studies of social influence often estimate the effect of peers by regressing some outcome of interest (e.g. delinquency) on a measure of peer behavior (e.g. average peer delinquency in the network). However,

*latent homophily*, an unmeasured factor which influences friendship selection and some outcome of interest, can create artificial statistical dependence between an outcome of interest for two individuals in the absence of any causal relationship (Shalizi and Thomas 2011). This program examines the sensitivity of estimates from a regression model of some outcome on a directly measured outcome of peers. VanderWeele and Arah (2011) and VanderWeele (2011) have developed a means to examine the sensitivity of an estimate to latent homophily. Basically, the approach examines the hypothetical conditions under which the effect of social influence would be explained by latent homophily.

The syntax below provides the sensitivity analyses conducted by Young (2014) on two prominent studies of peer delinquency and social influence (i.e. Haynie 2001; Meldrum, Young, and Weerman 2009).

__Syntax__

Sensitivity Analysis for Haynie (2001) in .R format

Sensitivity Analysis for Meldrum, Young, and Weerman (2009) in .R format

**Add Pie**This function was written by Christopher Steven Marcum and David R. Schaefer. The function

**add.pie**adds pie charts to network visualizations that have been called using the gplot function in the sna package. A discussion of the function is provided in the article Save Room for Pie: Adding Pie Charts to Network Visualizations in R with statnet and plotrix.

__Syntax__

The add.pie function in .R format

**Roster to Adjacency**This function takes network data in roster format and creates an adjacency matrix from the data.

__Syntax__

The roster.to.adjacency function in .R format

**Egocentric Stats**These functions take an adjacency matrix and a variable and creates the egocentric means and standard deviation for the variable. The functions require the adjacency matrix ("adj") and a variable ("var"). There are three types of networks to call: send, receive, and send and receive. So, there are six functions (3 for mean, 3 for sd). The end of the function gives an example of how this works.

__Syntax__

The egocentric.stats function in .R format