-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rcode_regression_NEI_WLS.R
146 lines (93 loc) · 4.5 KB
/
Rcode_regression_NEI_WLS.R
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
rm(list=ls())
`%notin%` <- Negate(`%in%`)
library(tidyverse)
library(Hmisc)
library(usmap)
########
# read data frames
########
df_change_pm25 <- read.csv("../df_change_pm25.csv")
df_change_pm25$diff <- df_change_pm25$before - df_change_pm25$after
##
df_change_no2<- read.csv("../df_change_no2.csv")
df_change_no2$diff <- df_change_no2$before - df_change_no2$after
df_pop<- read.csv('../pop_density_census2010.csv',fileEncoding="UTF-8-BOM")
df_pop$state <- state.abb[match(df_pop$state, state.name)]
df_pop <- df_pop %>% drop_na()
df_regions <- read.csv('df_regions.csv')
df_regions$state <- trimws(df_regions$ï..state, which = c("both"))
df_regions$state <- state.abb[match(df_regions$state, state.name)]
nei <- read.csv('NEI_sector_report.txt')
## select only NO2 and PM2.5
neif <- nei %>% select(c('STATE','MAJOR_SOURCE_TYPE','EMISSION_TONS','POLLUTANT')) %>%
filter(POLLUTANT %in% c('PM2.5','Nitrogen Oxides')) %>%
filter(STATE %notin% c('Puerto Rico', 'Virgin Islands','Tribal Land','District Of Columbia'))
## calc tot emission by state
nei_grp_source <- neif %>% group_by(STATE, MAJOR_SOURCE_TYPE) %>%
summarise(tot_emission = sum(EMISSION_TONS)) %>% arrange(STATE,tot_emission) %>%
spread(MAJOR_SOURCE_TYPE, tot_emission) %>%
replace(is.na(.), 0)
## change state name to abbr
nei_grp_source$state <- state.abb[match(nei_grp_source$STATE, state.name)]
nei_grp_source_perc <- neif %>% group_by(STATE, MAJOR_SOURCE_TYPE) %>%
summarise(tot_emission = sum(EMISSION_TONS)) %>% arrange(STATE,tot_emission) %>%
mutate(emission_perc = 100*tot_emission/sum(tot_emission)) %>%
# filter(MAJOR_SOURCE_TYPE %in% c("Mobile Sources", 'Stationary Sources')) %>%
select(-tot_emission)%>%
spread(MAJOR_SOURCE_TYPE, emission_perc) %>%
replace(is.na(.), 0)
#
nei_grp_source_perc$state <- state.abb[match(nei_grp_source_perc$STATE, state.name)]
## merge with change in pollutants
df_all <- nei_grp_source_perc%>%
left_join(df_change_no2, by='state')%>% rename( no2_change = diff) %>%
select(-c('before', 'after')) %>%
left_join( df_change_pm25, by='state')%>% rename( pm25_change = diff) %>%
select(-c('before', 'after', 'STATE', 'X.x','X.y'))
names(df_all) <- sub(" ", ".", names(df_all))
###############################
## ADD CONFOUNDERS: pop density,
## landmass and lat and longitude
###############################
df_all<- df_all %>%
left_join(df_pop , by='state') %>%
left_join(df_regions, by='state')
################################
# SCALED PREDICTORS
################################
cols_to_scale <- c('Fire.Sources','Mobile.Sources',"Stationary.Sources")
df_all_scaled <- df_all
df_all_scaled[,cols_to_scale] <- scale(df_all[, cols_to_scale])
#############
## WLS
#############
library(car) # for qqPlot
df_no2 <- df_all_scaled %>% filter(no2_change!="")
model_no2 <- lm(no2_change ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_no2)
df_no2$resids.ols.no2 <- model_no2$residuals
fit.SDfunc <- lm(abs(resids.ols.no2) ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_no2)
fitted.SDs <- fit.SDfunc$fitted.values #Use fitted standard deviation estimates
weights.d <- 1/fitted.SDs^2 #Weights are inverse variances
fit.WLS.no2 <- lm(no2_change ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_no2, weights = weights.d)
summary(fit.WLS.no2)
## pm2.5
df_pm25 <- df_all_scaled %>% filter(pm25_change!="")
model_pm25 <- lm(pm25_change ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_pm25)
df_pm25$resids.ols.pm25 <- model_pm25$residuals
fit.SDfuncpm25 <- lm(abs(resids.ols.pm25) ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_pm25)
fitted.SDspm25 <- fit.SDfuncpm25$fitted.values #Use fitted standard deviation estimates
weights.d.pm25 <- 1/fitted.SDspm25^2 #Weights are inverse variances
fit.WLS.pm25 <- lm(pm25_change ~ Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region), data=df_pm25, weights = weights.d.pm25)
summary(fit.WLS.pm25)