0% found this document useful (0 votes)
10 views4 pages

Political Analysis with R Guide

This document is a table of contents for a book on political analysis using R. It outlines 8 chapters that cover topics like obtaining and installing R, loading and cleaning data, descriptive statistics, linear models, and generalized linear models. Each chapter is further broken down into sections that provide more detailed explanations of analytical techniques in R.

Uploaded by

linda.modolo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Political Analysis with R Guide

This document is a table of contents for a book on political analysis using R. It outlines 8 chapters that cover topics like obtaining and installing R, loading and cleaning data, descriptive statistics, linear models, and generalized linear models. Each chapter is further broken down into sections that provide more detailed explanations of analytical techniques in R.

Uploaded by

linda.modolo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

James E.

Monogan III

Political Analysis Using R

123
Contents

1 Obtaining R and Downloading Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


1.1 Background and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Where Can I Get R? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Getting Started: A First Session in R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Saving Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Work Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.6 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Loading and Manipulating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1 Reading in Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.1 Reading Data from Other Programs . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.2 Data Frames in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.3 Writing Out Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Viewing Attributes of the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 Logical Statements and Variable Generation . . . . . . . . . . . . . . . . . . . . . . . 20
2.4 Cleaning Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4.1 Subsetting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.4.2 Recoding Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5 Merging and Reshaping Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.6 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3 Visualizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1 Univariate Graphs in the base Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.1 Bar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.2 The plot Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.2.1 Line Graphs with plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.2.2 Figure Construction with plot: Additional Details. . . . . . 45
3.2.3 Add-On Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3 Using lattice Graphics in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.4 Graphic Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.5 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

xi
xii Contents

4 Descriptive Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.1 Measures of Central Tendency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.1 Frequency Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.2 Measures of Dispersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.2.1 Quantiles and Percentiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.3 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5 Basic Inferences and Bivariate Association . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.1 Significance Tests for Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.1.1 Two-Sample Difference of Means Test,
Independent Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.1.2 Comparing Means with Dependent Samples . . . . . . . . . . . . . . 69
5.2 Cross-Tabulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.3 Correlation Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
5.4 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6 Linear Models and Regression Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6.1 Estimation with Ordinary Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.2 Regression Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.2.1 Functional Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.2.2 Heteroscedasticity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.2.3 Normality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
6.2.4 Multicollinearity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.2.5 Outliers, Leverage, and Influential Data Points . . . . . . . . . . . 94
6.3 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7 Generalized Linear Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.1 Binary Outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
7.1.1 Logit Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.1.2 Probit Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.1.3 Interpreting Logit and Probit Results . . . . . . . . . . . . . . . . . . . . . . 105
7.2 Ordinal Outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.3 Event Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.3.1 Poisson Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7.3.2 Negative Binomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
7.3.3 Plotting Predicted Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.4 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
8 Using Packages to Apply Advanced Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.1 Multilevel Models with lme4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.1.1 Multilevel Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.1.2 Multilevel Logistic Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
8.2 Bayesian Methods Using MCMCpack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
8.2.1 Bayesian Linear Regression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
8.2.2 Bayesian Logistic Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Contents xiii

8.3 Causal Inference with cem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140


8.3.1 Covariate Imbalance, Implementing CEM,
and the ATT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
8.3.2 Exploring Different CEM Solutions . . . . . . . . . . . . . . . . . . . . . . . 145
8.4 Legislative Roll Call Analysis with wnominate . . . . . . . . . . . . . . . . . . 147
8.5 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9 Time Series Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.1 The Box–Jenkins Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.1.1 Transfer Functions Versus Static Models . . . . . . . . . . . . . . . . . . 163
9.2 Extensions to Least Squares Linear Regression Models . . . . . . . . . . . 167
9.3 Vector Autoregression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
9.4 Further Reading About Time Series Analysis . . . . . . . . . . . . . . . . . . . . . . 181
9.5 Alternative Time Series Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
9.6 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
10 Linear Algebra with Programming Applications . . . . . . . . . . . . . . . . . . . . . . . 187
10.1 Creating Vectors and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
10.1.1 Creating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
10.1.2 Converting Matrices and Data Frames. . . . . . . . . . . . . . . . . . . . . 192
10.1.3 Subscripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
10.2 Vector and Matrix Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
10.2.1 Matrix Algebra. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
10.3 Applied Example: Programming OLS Regression . . . . . . . . . . . . . . . . . 198
10.3.1 Calculating OLS by Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
10.3.2 Writing An OLS Estimator in R . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10.3.3 Other Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
10.4 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
11 Additional Programming Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.1 Probability Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
11.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
11.3 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.4 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
11.5 Optimization and Maximum Likelihood Estimation . . . . . . . . . . . . . . . 215
11.6 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
11.6.1 Simulating a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
11.7 Monte Carlo Analysis: An Applied Example . . . . . . . . . . . . . . . . . . . . . . . 223
11.7.1 Strategic Deterrence Log-Likelihood Function . . . . . . . . . . . 225
11.7.2 Evaluating the Estimator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
11.8 Practice Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

Common questions

Powered by AI

Univariate graphs in R are typically crafted using basic package functions like bar graphs and histograms, focusing on single-variable representation . In contrast, lattice graphics provide a framework for constructing conditioned plots, which allows for more sophisticated and multi-dimensional data visualization. This transition involves more complex syntax and offers enhanced customization for visualizing relationships between variables .

Conducting linear regression analysis in R involves estimating coefficients using the lm() function, which fits a linear model using ordinary least squares . Diagnostics involves checking assumptions through tests for functional form, heteroscedasticity, normality, multicollinearity, along with identifying outliers, leverage, and influential data points, ensuring the integrity and validity of the regression results .

Generalized linear models extend linear models to accommodate binary outcomes with the use of logit and probit models, allowing for non-normal data distributions . They also handle ordinal outcomes by modeling ordered data and predicting probabilities of membership in various categories, providing flexibility in treating different types of response variables in regression analysis .

R offers techniques for causal inference such as using the cem package for Coarsened Exact Matching, which helps to balance covariates across treatment and control groups to improve causal estimates . Covariate imbalance can lead to biased estimates, thus aligning data through methods like CEM is crucial for assessing the true effect of the treatment .

Time series analysis in R uses models like ARIMA to forecast and examine data over time, with the Box-Jenkins method providing a systematic approach to identifying, estimating, and checking models . The advantage of the Box-Jenkins method lies in its ability to model complex patterns with input series, allowing for precise control over model specifications through autoregressive, differencing, and moving average components .

Multilevel models in R are implemented using the lme4 package, which allows for multilevel linear and logistic regression, accommodating hierarchical data structures . These models can incorporate both fixed and random effects, capturing relationships within nested data hierarchies, such as students within schools or repeated measures within subjects .

The primary method for reading data in R involves using functions such as read.table() or read.csv() to load data from files, while data can be written to files using write.table() or write.csv() functions . Data frames play a crucial role in data manipulation in R as they are used to store tabular data in a two-dimensional structure, allowing for column-wise heterogeneous data, which makes reading from different file formats and writing out data flexible .

R can be installed by obtaining the software from the CRAN (Comprehensive R Archive Network) website . After installing, setting up a first session involves familiarizing oneself with R's environment and interface, such as using scripts to input commands, saving inputs and outputs, and learning session management techniques .

R handles measures of central tendency, such as mean, median, and mode, through various built-in functions including mean(), median(), and custom tables for modes . Measures of dispersion like variance and standard deviation can be calculated using var() and sd() functions, respectively. These tools help summarize and understand the spread and central values of data .

The primary commands for matrix operations in R include matrix() for creating matrices, and functions like %*% for matrix multiplication, solve() for inversion, and t() for transpose . These operations underpin numerous applications in linear algebra, including solving systems of equations and performing transformations, providing a foundation for more complex analytical tasks such as regression analysis .

You might also like