|
| 1 | +library(tidyverse) |
| 2 | +library(magrittr) |
| 3 | +library(lubridate) |
| 4 | +library(sf) |
| 5 | +library(dplyr) |
| 6 | +library(ggplot2) |
| 7 | +sf_use_s2(FALSE) |
| 8 | + |
| 9 | +# Load grid |
| 10 | +grid_10km = read_sf("../data/input/local_data/10km_grid_wgs84/10km_grid_wgs84.shp")%>% |
| 11 | + rename( |
| 12 | + grid_id = ID |
| 13 | + ) |
| 14 | + |
| 15 | +# Load smokePM predictions on smoke days |
| 16 | +preds = read_csv("/net/rcstorenfs02/ifs/rc_labs/dominici_lab/lab/data/exposures/smoke/smokePM2pt5_predictions_daily_10km_20060101-20201231.csv") %>% |
| 17 | + mutate( |
| 18 | + date = ymd(date) |
| 19 | + ) %>% |
| 20 | + rename( |
| 21 | + smoke = smokePM_pred, |
| 22 | + grid_id = grid_id_10km |
| 23 | + ) |
| 24 | + |
| 25 | +# Create output data.frame |
| 26 | +zip_smoke_df = data.frame(zip = character(), |
| 27 | + date = Date(), |
| 28 | + smoke = double(), |
| 29 | + stringsAsFactors = FALSE) |
| 30 | + |
| 31 | +for(y_ in 2006:2007){ |
| 32 | + y_ <- 2006 |
| 33 | + # Load full set of dates |
| 34 | + dates = seq.Date(ymd(paste0(y_, "0101")), |
| 35 | + ymd(paste0(y_, "1231")), |
| 36 | + by = "day") |
| 37 | + |
| 38 | + # Get full combination of grid cell-days |
| 39 | + out = expand.grid(grid_id = grid_10km$grid_id, date = dates) |
| 40 | + |
| 41 | + # Match smokePM predictions on smoke days to grid cell-days |
| 42 | + out = left_join(out, preds, by = c("grid_id", "date")) |
| 43 | + |
| 44 | + # Predict 0 for remaining grid cell-days, which are non-smoke days |
| 45 | + out = mutate(out, smoke = replace_na(smoke, 0)) |
| 46 | + |
| 47 | + |
| 48 | + # Load area-based weights |
| 49 | + zip_to_grid = read.csv(paste0("../data/output/zip_weights_df_", as.character(y_), ".csv"))%>% |
| 50 | + mutate( |
| 51 | + zip = sprintf("%05d", zip_id) |
| 52 | + ) |
| 53 | + |
| 54 | + # Merge gridded smoke values with weights |
| 55 | + zip_grid_smoke = merge(out, zip_to_grid) |
| 56 | + |
| 57 | + # Compute zip-code level smoke values |
| 58 | + zip_smoke_df_y = zip_grid_smoke %>% |
| 59 | + group_by(zip, date) %>% |
| 60 | + summarise(smoke = weighted.mean(smoke, w=w)) |
| 61 | + |
| 62 | + zip_smoke_df = rbind(zip_smoke_df, zip_smoke_df_y) |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +zip_sf = read_rds("../data/intermediate/scratch/zip_sf_list.rds") |
| 67 | + |
| 68 | +save(zip_smoke_df, file = "../data/output/smoke/daily_zip_test.RData") |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +# Load required libraries |
| 73 | +library(tidyverse) |
| 74 | +library(sf) |
| 75 | + |
| 76 | +# Read in the shapefile for zipcodes |
| 77 | +zip_sf <- read_sf("../data/input/local_data/Zipcode_Info/polygon/ESRI06USZIP5_POLY_WGS84.shp") |
| 78 | + |
| 79 | +zip_smoke_df$date <- as.Date(zip_smoke_df$date) |
| 80 | + |
| 81 | +# Subset the data for the given date |
| 82 | +zip_smoke_subset <- zip_smoke_df %>% filter(date == "2006-01-01") |
| 83 | + |
| 84 | +# Join the data with the shapefile based on the zipcode |
| 85 | +zip_sf <- left_join(zip_sf, zip_smoke_subset, by = c("ZIP" = "zip")) |
| 86 | + |
| 87 | +# Drop rows with NULL values in smoke column |
| 88 | +zip_sf <- zip_sf %>% drop_na(smoke) |
| 89 | + |
| 90 | + |
| 91 | +# Create a map of smoke in every zipcode with thinner line width |
| 92 | +ggplot() + |
| 93 | + geom_sf(data = zip_sf, aes(fill = smoke), lwd = 0.1) + |
| 94 | + scale_fill_gradient(low = "yellow", high = "red") + |
| 95 | + theme_void() |
| 96 | + |
0 commit comments