-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rcode_regression_NEI_WLS.R
150 lines (99 loc) · 4.9 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
147
148
149
150
rm(list=ls())
`%notin%` <- Negate(`%in%`)
library(tidyverse)
library(Hmisc)
library(usmap)
library(car)
library(data.table)
########
# read data frames
########
cat("load pm25\n")
df_change_pm25 <- read.csv("df_change_pm25.csv")
df_change_pm25$diff <- df_change_pm25$before - df_change_pm25$after
##
cat("load NO2\n")
df_change_no2<- read.csv("df_change_no2.csv")
df_change_no2$diff <- df_change_no2$before - df_change_no2$after
cat("load pop density\n")
df_pop <- read.csv('pop_density_census2010.csv', encoding="us-ascii")
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'))
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
#############
### interaction model
df_no2 <- df_all_scaled %>% filter(no2_change!="")
model_no2_I <- lm(no2_change ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_no2)
df_no2$resids.ols.no2.I <- model_no2_I$residuals
fit.SDfunc2 <- lm(abs(resids.ols.no2.I) ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_no2)
fitted.SDs2 <- fit.SDfunc2$fitted.values #Use fitted standard deviation estimates
weights.d.2 <- 1/fitted.SDs2^2 #Weights are inverse variances
fit.WLS.I.no2 <- lm(no2_change ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_no2, weights = weights.d.2)
summary(fit.WLS.I.no2)
## pm2.5
### Interaction Model
df_pm25 <- df_all_scaled %>% filter(pm25_change!="")
model_pm25_I <- lm(pm25_change ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_pm25)
df_pm25$resids.ols.pm25.I <- model_pm25_I$residuals
fit.SDfunc2pm25.2 <- lm(abs(resids.ols.pm25.I) ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_pm25)
fitted.SDs2pm25.2 <- fit.SDfunc2pm25.2$fitted.values #Use fitted standard deviation estimates
weights.d.pm25.2 <- 1/fitted.SDs2pm25.2^2 #Weights are inverse variances
fit.WLS.I.pm25 <- lm(pm25_change ~ (Fire.Sources+Mobile.Sources+
Stationary.Sources+
pop_densitypermile2 + as.factor(region))^2, data=df_pm25, weights = weights.d.pm25.2)
summary(fit.WLS.I.pm25)