Egocentric network analysis with R
Chapter 1 Introduction
This book is a companion to my workshop on egocentric network analysis with R. I have taught this workshop for the past several years at different conferences and summer schools on social network analysis, personal networks, and network science. The book is a work in progress and I’ll keep updating it as I continue to teach the workshop and related courses.
A Spanish translation of part of this book has appeared in the journal REDES - Revista hispana para el análisis de redes sociales. My colleagues and I provide a more in-depth discussion of personal network analysis and the methods covered in this workshop in our textbook on personal network research (McCarty et al. 2019). See the bibliography at the end of the book for a list of other useful references on egocentric or personal network analysis.
Feel free to contact me to know more about this workshop and how to take it, or to give me feedback or report any issue about this book.
1.1 Workshop materials
The materials for this workshop consist of this book and the workshop folder. The workshop folder can be downloaded here and includes:
Scriptssubfolder: all code shown in this book.
Datasubfolder: all data we’re going to use.
Slidessubfolder: the workshop slides.
Egocentric_R.Rproj: the workshop R project file.
Scripts subfolder you’ll see seven script (.R) files. You can access and run the R code in each script by opening the .R file in RStudio. Each script corresponds to one of the following chapters (see the table of contents):
1.2 Workshop setup
To take this workshop you need to:
- Download the workshop folder here
- Click on the link > Click on Download in the top-right > Click on Direct download
- Then save the folder to your computer
- Download the last version of R here (select a location near you)
- Follow instructions to install R in your computer
- Download RStudio (free version) here
- Follow instructions to install RStudio in your computer
- Install the R packages listed below
- Open RStudio and go to
Top menu > Tools > Install packages...
- Install each package in the list
- Open RStudio and go to
- Bring your laptop to the workshop
Once in class, go to the workshop folder (point 1 above) and double-click on the workshop R project file (
Egocentric_R.Rproj) in it. That will open RStudio.
NOTE: It’s very important that you save the workshop folder as downloaded to a location in your computer, and open
Egocentric_R.Rproj within that folder. By doing so, you will be opening RStudio and setting the workshop folder as your R working directory. All our R scripts assume that working directory. In particular, they assume that the
Data subfolder is in your R working directory. You can type
getwd() in your R console to see the path to your R working directory and make sure that it’s correctly pointed to the location of the workshop folder in your computer.
1.2.1 Required R packages
- For descriptive statistics
- For network data, measures, visualization
- To fit multilevel statistical models and view their results
1.2.2 RStudio settings
RStudio gives you the ability to select and change various settings and features of its interface: see the
Preferences... menu option. These are some of the settings you should pay attention to:
Preferences... > Code > Editing > Soft-wrap R source file. Here you can decide whether or not to wrap long code lines in the editor. When code lines in a script are not wrapped, be aware that some code will be hidden if script lines are longer than your editor window’s width (you’ll have to scroll right to see the rest of the code). With a script (
.R) file open in the editor, try both options (checked and unchecked) to see what you’re more comfortable with.
Preferences... > Code > Display > Highlight R function calls. This allows you to highlight all pieces of code that call an R function (“command”). I find function highlights very helpful to navigate a script and suggest that you check this option.
This workshop uses real-world data collected in 2012 with a personal network survey among 107 Sri Lankan immigrants in Milan, Italy. Out of the 107 respondents, 102 reported their personal network. All data are in the
The data files include ego-level data (gender, age, educational level, etc. for each Sri Lankan respondent), alter attributes (alter’s nationality, country of residence, closeness to ego etc.), and information on alter-alter ties. Each personal network has a fixed size of 45 alters. Information about data variables and categories is available in
All data objects are saved as R objects in the R data file
data.rda. Data objects are the following:
ego.df: A data frame with ego-level attributes for all respondents (egos).
alter.attr.all: A data frame with alter-level attributes for all alters from all respondents.
gr.list: A list. Each list element is one ego-network stored as an
alter.attr.28: A data frame with alter-level attributes only for alters nominated by ego ID 28.
gr.28: The ego-network of ego ID 28 stored as an
gr.ego.28: The same as
gr.28, but with the node of the ego included in the
The R data objects above were imported from raw csv data files. All csv files are in the
ego_data.csv: A csv file with ego-level data for all the egos.
alter_attributes.csv: A single csv file including attributes of all alters from all egos.
alter_ties_028.csv: The edge list for ego ID 28’s egocentric network.
alter_attributes_028.csv: The alter attributes in ego ID 28’s egocentric network.
adj_028.csv: The adjacency matrix for ego ID 28’s egocentric network.
alter_ties.csv: A single csv file with the edge list for all alters from all egos.
For most of the workshop we will directly use R data objects in
data.rda. We will not focus on importing ego-network data from outside sources (for example, csv files). However, Section 7.1 shows you how the data objects in
data.rda were created by importing csv files with the
egor package. Section 8.2 covers importing ego-network data using just