-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathREADME.Rmd
188 lines (133 loc) · 5.99 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
---
output: md_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE}
knitr::opts_knit$set(
stop_on_error = 2L
)
knitr::opts_chunk$set(
fig.path="man/figures/"
)
```
[](https://zeligproject.org/)
<!--- Badges ----->
**Release:**
[](https://CRAN.R-project.org/package=Zelig)


**Development:**
[](https://www.repostatus.org/)
[](https://travis-ci.org/IQSS/Zelig)
[](https://ci.appveyor.com/project/IQSS/Zelig)
[](https://codecov.io/gh/IQSS/Zelig)
[Dev-Blog](https://medium.com/zelig-dev)
## Zelig workflow overview
All models in Zelig can be estimated and results explored presented using four simple functions:
1. `zelig` to estimate the parameters,
2. `setx` to set fitted values for which we want to find quantities of interest,
3. `sim` to simulate the quantities of interest,
4. `plot` to plot the simulation results.
#### Zelig 5 reference classes
Zelig 5 introduced [reference classes](http://adv-r.had.co.nz/R5.html). These
enable a different way of working with Zelig that is detailed in [a separate vignette](http://docs.zeligproject.org/articles/zelig5_vs_zelig4.html). Directly using the reference class architecture is optional. They are not used in the examples
below.
## Zelig Quickstart Guide
Let’s walk through an example. This example uses the swiss dataset. It contains
data on fertility and socioeconomic factors in Switzerland’s 47 French-speaking
provinces in 1888 (Mosteller and Tukey, 1977, 549-551). We will model the effect
of education on fertility, where education is measured as the percent of
draftees with education beyond primary school and fertility is measured using
the common standardized fertility measure (see Muehlenbein (2010, 80-81) for
details).
## Installing and Loading Zelig
If you haven't already done so, open your R console and install Zelig. We
recommend installing Zelig with the zeligverse package. This installs core
Zelig and ancillary packages at once.
```{r eval=FALSE}
install.packages('zeligverse')
```
Alternatively you can install the development version of Zelig with:
```{r eval=FALSE}
devtools::install_github('IQSS/Zelig')
```
Once Zelig is installed, load it:
```{r message=FALSE}
library(zeligverse)
```
## Building Models
Let’s assume we want to estimate the effect of education on fertility. Since
fertility is a continuous variable, least squares (`ls`) is an appropriate model
choice. To estimate our model, we call the `zelig()` function with three two
arguments: equation, model type, and data:
```{r}
# load data
data(swiss)
# estimate ls model
z5_1 <- zelig(Fertility ~ Education, model = "ls", data = swiss, cite = FALSE)
# model summary
summary(z5_1)
```
```{r include=FALSE}
edu_coef <- round(coef(z5_1)[[2]], digits = 2)
if (is.na(edu_coef)) stop()
```
The `r edu_coef` coefficient on education suggests a negative relationship
between the education of a province and its fertility rate. More precisely,
for every one percent increase in draftees educated beyond primary school, the
fertility rate of the province decreases `r edu_coef * -1` units. To help us
better interpret this finding, we may want other quantities of interest, such as
expected values or first differences. Zelig makes this simple by automating the
translation of model estimates into interpretable quantities of interest using
Monte Carlo simulation methods (see King, Tomz, and Wittenberg (2000) for more
information). For example, let’s say we want to examine the effect of increasing
the percent of draftees educated from 5 to 15. To do so, we set our predictor
value using the `setx()` and `setx1()` functions:
```{r}
# set education to 5 and 15
z5_1 <- setx(z5_1, Education = 5)
z5_1 <- setx1(z5_1, Education = 15)
# model summary
summary(z5_1)
```
After setting our predictor value, we simulate using the `sim()` method:
```{r}
# run simulations and estimate quantities of interest
z5_1 <- sim(z5_1)
# model summary
summary(z5_1)
```
At this point, we’ve estimated a model, set the predictor value, and estimated
easily interpretable quantities of interest. The `summary()` method shows us our
quantities of interest, namely, our expected and predicted values at each level
of education, as well as our first differences–the difference in expected values
at the set levels of education.
# Visualizations
Zelig’s `plot()` function plots the estimated quantities of interest:
```{r example_plot_graph, fig.height=11, fig.width=7}
plot(z5_1)
```
We can also simulate and plot simulations from ranges of simulated values:
```{r}
z5_2 <- zelig(Fertility ~ Education, model = "ls", data = swiss, cite = FALSE)
# set Education to range from 5 to 15 at single integer increments
z5_2 <- setx(z5_2, Education = 5:15)
# run simulations and estimate quantities of interest
z5_2 <- sim(z5_2)
```
Then use the `plot()` function as before:
```{r example_plot_ci_plot, fig.height=5, fig.width=5}
z5_2 <- plot(z5_2)
```
# Getting help
The primary documentation for Zelig is available at: <http://docs.zeligproject.org/articles/>.
Within R, you can access function help using the normal `?` function, e.g.:
```{r, eval=FALSE}
?setx
```
If you are looking for details on particular estimation model methods, you can
also use the `?` function. Simply place a `z` before the model name. For
example, to access details about the `logit` model use:
```{r, eval=FALSE}
?zlogit
```