-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJeff's New Code
203 lines (171 loc) · 7.54 KB
/
Jeff's New Code
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#==============================================================================
# Transparent California
#==============================================================================
# original by Michael Kevane 10/14/2017
# Description: Create tables of descriptive statistics for
# Salary data for public employees in California
# data is from http://transparentcalifornia.com
#==============================================================================
# 1. Settings, packages, and options
#==============================================================================
# Clear the working space
rm(list = ls())
#install.packages("viridis", "raster", "ggmap", "mapproj", "maps", "maptools", "mapdata", "sp", "ggplot2", "dplyr")
#install.packages(c("maps", "mapdata"))
#install.packages("dplyr")
#install.packages("reshape")
#install.packages("ggrepel")
install.packages("noncensus")
# Load packages
library(plyr)
library(sp)
library(raster)
library(viridis)
library(leaflet)
library(doBy)
library(dplyr)
library(foreign)
library(gdata)
library(ggplot2)
library(sandwich)
library(stargazer)
library(tidyr)
library(maps)
library(mapdata)
library(mapproj)
library(maptools)
library(ggmap)
library(reshape2)
library(reshape)
library(ggrepel)
library(noncensus)
# turn off scientific notation except for big numbers
options(scipen = 9)
# function to calculate corrected SEs for regression
cse = function(reg) {
rob = sqrt(diag(vcovHC(reg, type = "HC1")))
return(rob)
}
#==============================================================================
# 2. Data section
#==============================================================================
### Read data
# Data input using read.csv
# Note, albany data available for 2011-2016
# many other cities, counties, agencies available
# Data input using read.csv
sonoma_c <- read.csv("http://transparentcalifornia.com/export/sonoma-county-2016.csv")
napa_c <- read.csv("http://transparentcalifornia.com/export/napa-county-2016.csv")
solano_c <- read.csv("http://transparentcalifornia.com/export/solano-county-2016.csv")
marin_c <- read.csv("http://transparentcalifornia.com/export/marin-county-2016.csv")
contra_costa_c <- read.csv("http://transparentcalifornia.com/export/contra-costa-county-2016.csv")
san_fran_c <- read.csv("https://transparentcalifornia.com/export/san-francisco-2016.csv")
san_mateo_c <- read.csv("http://transparentcalifornia.com/export/san-mateo-county-2016.csv")
santa_cruz_c <- read.csv("http://transparentcalifornia.com/export/santa-cruz-county-2016.csv")
santa_clara_c <- read.csv("http://transparentcalifornia.com/export/santa-clara-county-2016.csv")
alameda_c <- read.csv("http://transparentcalifornia.com/export/alameda-county-2016.csv")
#Average Sonoma County base pay over $25k
sonoma_c$basepay = tolower(sonoma_c$Base.Pay)
son75 <- sonoma_c$basepay[sonoma_c$Base.Pay > 75000]
son75 <- as.numeric(son75)
son75 <- matrix(data = son75, ncol = 1)
son_avg <- mean(son75)
num_son <- nrow(son75)
#Average Napa County base pay over $25k
napa_c$basepay = tolower(napa_c$Base.Pay)
napa75 <- napa_c$basepay[napa_c$Base.Pay >25000]
napa75 <- as.numeric(napa75)
napa75 <- matrix(data = napa75, ncol=1)
napa_avg <- mean(napa75)
num_napa <- nrow(napa75)
#Average Solano County base pay over $25k
solano_c$basepay = tolower(solano_c$Base.Pay)
solano75 <- solano_c$basepay[solano_c$Base.Pay >25000]
solano75 <- as.numeric(solano75)
solano75 <- matrix(data = solano75, ncol = 1)
solano_avg <- mean(solano75)
num_solano <- nrow(solano75
#Average Marin base pay over $25k
marin_c$basepay = tolower(marin_c$Base.Pay)
marin75 <- marin_c$basepay[marin_c$Base.Pay > 25000]
marin75 <- as.numeric(marin75)
marin75 <- matrix(data = marin75, ncol = 1)
marin_avg <- mean(marin75)
num_marin <- nrow(marin75)
#Average Contra Costa County base pay over $25k
contra_costa_c$basepay = tolower(contra_costa_c$Base.Pay)
cc75 <- contra_costa_c$basepay[contra_costa_c$Base.Pay > 25000]
cc75 <- as.numeric(cc75)
cc75 <- matrix(data = cc75, ncol = 1)
cc_avg <- mean(cc75)
num_cc <- nrow(cc75)
#Average San Francisco County base pay over $25k
san_fran_c$basepay = tolower(san_fran_c$Base.Pay)
sf75 <- san_fran_c$basepay[san_fran_c$Base.Pay > 25000]
sf75 <- as.numeric(sf75)
sf75 <- matrix(data = sf75, ncol = 1)
sf_avg <- mean(sf75)
num_sf <- nrow(sf75)
#Average San Mateo County base pay over $25k
san_mateo_c$basepay = tolower(san_mateo_c$Base.Pay)
san_mateo75 <- san_mateo_c$basepay[san_mateo_c$Base.Pay > 25000]
san_mateo75 <- as.numeric(san_mateo75)
san_mateo75 <- matrix(data = san_mateo75, ncol = 1)
san_mateo_avg <- mean(san_mateo75)
num_mateo <- nrow(san_mateo75)
#Average Santa Cruz County base pay over $25k
santa_cruz_c$basepay = tolower(santa_cruz_c$Base.Pay)
cruz75 <- santa_cruz_c$basepay[santa_cruz_c$Base.Pay > 25000]
cruz75 <- as.numeric(cruz75)
cruz75 <- matrix(data = cruz75, ncol = 1)
cruz_avg <- mean(cruz75)
num_cruz <- nrow(cruz75)
#Average Santa Clara County base pay over $25k
santa_clara_c$basepay = tolower(santa_clara_c$Base.Pay)
clara75 <- santa_clara_c$basepay[santa_clara_c$Base.Pay >25000]
clara75 <- as.numeric(clara75)
clara75 <- matrix(data = clara75, ncol = 1)
clara_avg <- mean(clara75)
num_clara <- nrow(clara75)
#Average Alameda County base pay over $25k
alameda_c$basepay = tolower(alameda_c$Base.Pay)
alameda75 <- alameda_c$basepay[alameda_c$Base.Pay > 25000]
alameda75 <- as.numeric(alameda75)
alameda75 <- matrix(data = alameda75, ncol = 1)
alameda_avg <- mean(alameda75)
num_alam <- nrow(alameda75)
son_avg
napa_avg
solano_avg
marin_avg
cc_avg
sf_avg
san_mateo_avg
cruz_avg
clara_avg
alameda_avg
data(counties)
counties
sonoma_pop <-subset(counties, state == "CA" & county_name == "Sonoma County", select = c(county_name, population))
napa_pop <- subset(counties, state == "CA" & county_name == "Napa County", select = c(county_name, population))
solano_pop <-subset(counties, state == "CA" & county_name == "Solano County", select = c(county_name, population))
marin_pop <- subset(counties, state =="CA" & county_name == "Marin County", select = c(county_name, population))
cc_pop <- subset(counties, state =="CA" & county_name == "Contra Costa County", select = c(county_name, population))
sf_pop <- subset(counties, state == "CA" & county_name=="San Francisco County", select = c(county_name, population))
mateo_pop <- subset(counties, state == "CA" & county_name == "San Mateo County", select = c(county_name, population))
cruz_pop <- subset(counties, state =="CA" & county_name == "Santa Cruz County", select = c(county_name, population))
clara_pop <- subset(counties, state =="CA" & county_name == "Santa Clara County", select = c(county_name, population))
alam_pop <- subset(counties, state == "CA" & county_name == "Alameda County", select = c(county_name, population))
cities <- c("Sonoma","Napa", "Solano", "Marin", "Contra Costa", "San Francisco", "San Mateo", "Santa Cruz","Santa Clara","Alameda")
geocode(cities)
#Inserts values into separate Data Frames (each data frame is one column)
city_rats <- c(num_son/sonoma_pop$population, num_napa/napa_pop$population, num_solano/solano_pop$population, num_marin/marin_pop$population, num_cc/cc_pop$population, num_sf/sf_pop$population, num_mateo/mateo_pop$population, num_cruz/cruz_pop$population, num_clara/clara_pop$population, num_alam/alam_pop$population)
city_rats <- as.data.frame(city_rats)
city_names <- as.data.frame(cities)
city_locs <- as.data.frame(geocode(cities))
#Merge data frames horizontally
all_together <- as.data.frame(c(city_names, city_rats, city_locs))
#Rename the columns
colnames(all_together) <- c("CityName", "AveragePay", "Longitude", "Latitude")
all_together <- all_together[, -c(5:7)]
all_together