-
Notifications
You must be signed in to change notification settings - Fork 2
/
tutorial1.Rmd
181 lines (147 loc) · 5.28 KB
/
tutorial1.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
---
title: "R Notebook"
output:
html_notebook: default
pdf_document: default
---
## Tutorial 1: Project ACS data wrangling
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing each chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*.
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
In this Rmarkdown presentation, we will explore the 2013 American Community Survey data set.
- `knitr` and Rmarkdown
- The `dplyr` package
- The `googleVis` package
## Load the libraries and data
Here we load the data from a secure dropbox link.
```{r, message=F}
library(dplyr)
library(readr)
library(DT)
```
```{r, include=F}
acs13husa <- read_csv("../data/ss13husa.csv", guess_max = 10000)
```
```{r}
datatable(head(acs13husa,50), options = list(scrollX=T, pageLength = 10))
```
## Basic information about data
```{r,message=F}
dim(acs13husa)
```
## Using survey weights
- In this data set, there are some weight variables.
- This is because American Community Survey is based on a stratified household sample, rather than a simple random sample.
- To adjust for the unequal selection probability of individuals, weights are introduced.
- We should use `WGTP` for estimates.
- And use weight replicates `wgtp1`-`wgtp80` for standard error estimates.
- Reference: https://usa.ipums.org/usa/repwt.shtml
## The `dplyr` package
Dplyr aims to provide a function for each basic verb of data manipulation.
- `filter()`
- `arrange()`
- `select()`
- `distinct()`
- `mutate()`
- `summarise()`
- `sample_n()` and `sample_frac()`
## Add state names and abbreviations
```{r, message=F}
ST.anno=read_csv("../data/statenames.csv")
ST.anno=mutate(ST.anno, STabbr=abbr, STname=name)
acs13husa=mutate(acs13husa, STnum=as.numeric(ST))
acs13husa <- left_join(acs13husa, ST.anno, by = c("STnum" = "code"))
select(sample_n(acs13husa,5), starts_with("ST"))
```
The above codes were contributed by Arnold Chua Lau (Spring 2016).
## Pipeline operator
The same codes above can be re-arranged using the pipeline operator `%>%` to improve readability of your codes.
```{r, message=F}
acs13husa%>%
sample_n(5) %>%
select(starts_with("ST"))
```
## Pipeline basic analysis
Building types (BLD)
01 .Mobile home or trailer
02 .One-family house detached
03 .One-family house attached
04 .2 Apartments
05 .3-4 Apartments
06 .5-9 Apartments
07 .10-19 Apartments
08 .20-49 Apartments
09 .50 or more apartments
10 .Boat, RV, van, etc.
```{r}
table(acs13husa$BLD)
```
```{r, message=F}
mobilehome=
acs13husa %>%
filter(BLD == "01") %>%
group_by(STabbr) %>%
summarize(
AvgPrice = mean(as.numeric(MHP), na.rm=T),
MedianPrice = as.numeric(median(as.numeric(MHP), na.rm=T)),
Count = n()
) %>%
arrange(desc(Count))
```
## Simple plot
```{r, message=FALSE, fig.height=4, fig.width=7}
barplot(mobilehome$Count, names.arg=mobilehome$STabbr,
cex.names=0.9)
```
## Simple plot
```{r, message=FALSE}
plot(c(0,nrow(mobilehome)),
c(min(mobilehome$MedianPrice),
max(mobilehome$MedianPrice)*1.05), type="n",
xlab="States",
ylab="Mobile Home Prices")
points(1:nrow(mobilehome), mobilehome$MedianPrice, col=2, pch=16)
points(1:nrow(mobilehome), mobilehome$AvgPrice, col=4, pch=16)
segments(1:nrow(mobilehome), mobilehome$MedianPrice,
1:nrow(mobilehome), mobilehome$AvgPrice)
text(1:nrow(mobilehome)+0.35,
(mobilehome$MedianPrice+mobilehome$AvgPrice)/2,
mobilehome$STabbr, cex=0.7)
```
## Put together summary data
```{r, message=F}
VALP.sum=summarise(group_by(acs13husa, STabbr),
weighted.mean(as.numeric(VALP),
as.numeric(WGTP),
na.rm=T))
FINCP.sum=summarise(group_by(acs13husa, STabbr),
weighted.mean(as.numeric(FINCP),
as.numeric(WGTP), na.rm=T))
MRGP.sum=summarise(group_by(acs13husa, STabbr),
weighted.mean(as.numeric(MRGP),
as.numeric(WGTP), na.rm=T))
sum.data=data.frame(abbr=ST.abbr,
VALP=unlist(VALP.sum[,2]),
FINCP=unlist(FINCP.sum[,2]),
MRGP=unlist(MRGP.sum[,2]))
sum.data[,-1]=round(sum.data[,-1], digits=2)
```
## Simple Google Visualization
We will use the `googleVis` package to achieve simple interactive visualization (it requires internet connectivity).
```{r, message=F}
library(googleVis)
op <- options(gvis.plot.tag='chart')
Bubble <- gvisBubbleChart(sum.data, idvar="abbr",
xvar="FINCP", yvar="VALP",
colorvar="",
sizevar="MRGP",
options=list(width="1200px",
height="900px",
sizeAxis = '{minValue: 0,
maxSize: 15}'))
```
## Simple Google Visualization
```{r, results='asis', tidy=TRUE, message=FALSE, fig.width=7}
plot(Bubble)
```