I've just updated and uploaded a note on bivariate regression from a sampling perspective (based on GOV 2000 material):
Note on Bivariate Regression: Connecting Practice and Theory
7/10/2013
Plotting regression coefficients with confidence intervals in ggplot2
A graphical approach to displaying regression coefficients / effect sizes across multiple specifications can often be significantly more powerful and intuitive than presenting a regression table. Moreover, we can easily express uncertainty in the form of confidence intervals around our estimates. As a quick example, suppose that we wanted to compare the effect of British colonial status upon country-level corruption across multiple specifications and two methods (OLS and WLS) from the following paper: Treisman, Daniel. 2000. "The causes of corruption: a cross-national study," Journal of Public Economics 76: 399-457.
Specifically, the dependent variable is TI98, the perceived corruption score calculated by Transparency International for 1998. The variable whose effect we seek is an indicator that equals 1 if the country is a former British colony or the UK, and 0 otherwise. I took the coefficients and associated standard errors on British colonial status from Tables 2 and 3 across the 5 different specifications where TI98 is the dependent variable. I then entered them into a data frame with the following structure:
Specifically, the dependent variable is TI98, the perceived corruption score calculated by Transparency International for 1998. The variable whose effect we seek is an indicator that equals 1 if the country is a former British colony or the UK, and 0 otherwise. I took the coefficients and associated standard errors on British colonial status from Tables 2 and 3 across the 5 different specifications where TI98 is the dependent variable. I then entered them into a data frame with the following structure:
coef se method specification lb ub
1 -1.99 1.01 WLS 1 -3.969564 -0.01043638
2 -1.56 0.59 WLS 2 -2.716379 -0.40362125
3 -1.25 0.52 WLS 3 -2.269181 -0.23081873
4 -1.20 0.54 WLS 4 -2.258381 -0.14161945
5 -1.04 0.79 WLS 5 -2.588372 0.50837155
6 -1.25 0.81 OLS 1 -2.837571 0.33757083
7 -1.08 0.54 OLS 2 -2.138381 -0.02161945
8 -0.98 0.53 OLS 3 -2.018781 0.05878091
9 -0.82 0.58 OLS 4 -1.956779 0.31677911
10 -1.06 0.96 OLS 5 -2.941565 0.82156543
Note that I calculated the upper bound (ub) and lower bound (lb) of the 95% confidence interval using the standard errors provided in the table (I assumed normality holds due to the Central Limit Theorem, which may be questionable in some specifications given small sample sizes).
I then generated the following plot:
Here is the code for making this plot in ggplot2 from the dataframe I provided above:
pd <- position_dodge(width=0.2,height=NULL) ggplot(treisman, aes(specification,coef, color=method)) + geom_point(aes(shape=method),size=4, position=pd) + scale_color_manual(name="Method",values=c("coral","steelblue")) + scale_shape_manual(name="Method",values=c(17,19)) + theme_bw() + scale_x_continuous("Specification", breaks=1:length(specification), labels=specification) + scale_y_continuous("Estimated effect of being a former British colony or the UK on TI98") + geom_errorbar(aes(ymin=lb,ymax=ub),width=0.1,position=pd)
The
geom_errorbar()
function plots the confidence intervals. Note that I use the position_dodge()
function to horizontally shift the coefficients and confidence intervals for the same specifications for clarity. The height=NULL
option can be omitted. The color and shape for the legend is controlled manually.
What would happen if I just set
name="Method"
for the scale_color_manual
command, but left out the scale_shape_manual
command, letting it be automatically determined:ggplot(treisman, aes(specification,coef, color=method)) + geom_point(aes(shape=method),size=4, position=pd) + scale_color_manual(name="Method",values=c("coral","steelblue")) + theme_bw() + scale_x_continuous("Specification", breaks=1:length(specification), labels=specification) + scale_y_continuous("Estimated effect of being a former British colony or the UK on TI98") + geom_errorbar(aes(ymin=lb,ymax=ub),width=0.1,position=pd)
This would be the plot:
This happens because I also set the shape of the points to be determined by the
method
variable, just as for color. I thus I need to manually give the same name to both scales, or else otherwise they are automatically broken up into two legends, one manual titled "Method" and one automatic title "method".
What if I wanted to reorder the ordering of the methods in the plot; that is, if we wanted WLS to be plotted first, then OLS?
This can be achieved with the following command before running the first block of code on this page.
df$method <- reorder(df$method,rep(1:2,each=5))
The result is the following:
Finally, suppose that we wanted to customize the x-axis labels by tilting them diagonally and changing them to a dark grey. Adding the following extra piece of code to the blocks of code above would accomplish that:
+ theme(axis.text.x=element_text(angle=45,color="darkgray"))
6/20/2013
Drawing DAGs: R Solution
Following up on the previous post, another way to construct DAGs is using R. I think the igraph package is one of the customizable ways to do so. This is a powerful package designed for the visualization and analysis of networks and offers much more functionality than you will use for DAGs.
The R equivalent to the instrumental variables DAG constructed in TeX in the preceding post is the following:
To construct this in R, first you want to create an igraph object (with directed edges) as follows:
g1 <- graph.formula(Z--+T, T--+Y,U--+T, U--+Y, U--+Z, Z--+Y)If you just view the g1 object (by running
g1
), you will see the following:
IGRAPH DN-- 4 6 -- + attr: name (v/c)This denotes that this is a directed named (DN) network with 4 vertices (nodes) and 6 edges. The names of the vertices are stored as an attribute (and correspond to the variable names: Z, T, U, Y). Note that we can access the vertex name attribute as
V(g1)$name
. Running this will help us identify the order in which the vertices were entered into the igraph object:
> V(g1)$name [1] "Z" "A" "Y" "U"To give the vertices custom attributes:
V(g1)$label <- V(g1)$name V(g1)$color <- c(rep("black",3),"white") V(g1)$size <- 7 V(g1)$label.cex <- 1.5 V(g1)$label.dist <- 2 V(g1)$label.color <- "black"Now the edges (first see the edge sequence by running
E(g1)
):
E(g1)$color <- "black" E(g1)$color[2] <- E(g1)$color[4] <- "red" E(g1)$lty <- c(1,2,1,2,1,1) E(g1)$label <- c(NA,"X",NA,"X",NA,NA) E(g1)$label.color <- "red" E(g1)$label.cex <- 1.5If you run
plot(g1)
now, you will note that the igraph library automatically arranges the vertices. To manually override this arrangement, run the following command:
tkplot(g1)This opens an interactive graphing screen that allows you to manually move around the vertices and arrange them as you want. Now, while leaving the popup window open, extract the new coordinates for the vertices as:
coord.g1 <- tkplot.getcoords(1)(Note that the 1 refers to the number of the interactive window -- this one was the first I opened in this R session). Now, pass these coordinates to the plotting of the igraph object (along with moving around the label location relative to the vertices):
plot(g1, layout=coord.g1,vertex.label.degree=c(pi,pi/2, 0, -pi/2)
Drawing DAGs: LaTeX Solution
Directed acyclic graphs (DAGs) are commonly used to represent causal relationships between variables across a wide variety of disciplines. For an excellent (and quite accessible) textbook on the topic, please see the book Causal Inference by Miguel Hernan and Jamie Robins.
In this post, I briefly explore how you can draw DAGs in LaTeX. In the subsequent post, I will show how to draw DAGs using R.
The first example of DAG is the common instrumental variable (IV) setup:
We seek to identify the effect of treatment T on outcome Y. However, this is confounded by an unmeasured variable U. The IV is denoted as Z. Technically, we do not need to put in the crossed out red edges between U and Z and Z and Y - absence of edges encodes independence relations. However, I included them to reinforce (and make explicit) the assumptions needed for identification of causal effects using IVs:
In this post, I briefly explore how you can draw DAGs in LaTeX. In the subsequent post, I will show how to draw DAGs using R.
The first example of DAG is the common instrumental variable (IV) setup:
We seek to identify the effect of treatment T on outcome Y. However, this is confounded by an unmeasured variable U. The IV is denoted as Z. Technically, we do not need to put in the crossed out red edges between U and Z and Z and Y - absence of edges encodes independence relations. However, I included them to reinforce (and make explicit) the assumptions needed for identification of causal effects using IVs:
- (Quasi)-exogeneity of the instrument (no path from U to Z)
- Exclusion restriction (no direct path from Z to Y)
This was made using the TikZ package in LaTeX. I used the
\usepackage{pgf,tikz}
command at the beginning of my document.
The code to create the DAG above is:
\begin{tikzpicture} % nodes % \node[text centered] (z) {$Z$}; \node[right = 1.5 of z, text centered] (t) {$T$}; \node[right=1.5 of t, text centered] (y) {$Y$}; \node[draw, rectangle, dashed, above = 1 of t, text centered] (u) {$U$}; % edges % \draw[->, line width= 1] (z) -- (t); \draw [->, line width= 1] (t) -- (y); \draw[->,red, line width= 1,dashed] (u) --node {X} (z); \draw[->,line width= 1] (u) --(t); \draw[->,line width= 1] (u) -- (y); \draw[->, red, line width=1,dashed] (z) to [out=270,in=270, looseness=0.5] node{X} (y); \end{tikzpicture}
Note that I first create the nodes (corresponding to variables in the DAG), and then draw the directed edges between the nodes.
Another example of a DAG is a simple structural equation model where we want each edge to be marked with the parameter signifying the causal effect. For example:
In LaTeX:
\begin{tikzpicture} % nodes % \node[text centered] (t) {$T$}; \node[right = 1.5 of t, text centered] (m) {$M$}; \node[right=1.5 of m, text centered] (y) {$Y$}; % edges % \draw[->, line width= 1] (t) -- node[above,font=\footnotesize]{$\beta$} (m); \draw [->, line width= 1] (m) -- node[above,font=\footnotesize]{$\gamma$} (y); \draw[->, line width=1] (t) to [out=270,in=270, looseness=0.5] node[below,font=\footnotesize]{$\delta$} (y); \end{tikzpicture}
Subscribe to:
Posts (Atom)