-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Hi,
First of all thanks for creating such a straightforward-to-use package! I have an issue with staggregate_bin() and was hoping you could help me solve it.
Description of the task: I have daily temperature data and want to split it into bins. For simplicity only two bins: below and above 30 degree celcius. I have polygons in the form of California Counties and want to aggregate to this spatial level, weighted by population (secondary_raster). The time is either kept at daily level or aggregated to monthly.
Problem: In the dataframe returned by staggregate_bin() I would assume that the bins sum to 1 for the daily level and the number of days in a month when aggregated to the monthly level. However, the bin variables ("bin_ninf_to_30" & "bin_30_to_inf") sum below 1 (or 28/30/31 depending on the month) when on the daily (monthly) level. I attached a picture from of the staggregate_bin() output. Do you have an idea what could cause this?
Troubleshooting: I already checked that there are no missing values in the rasters in the areas corresponding to the polygons. The weights also sum to one for each polygon in the overlay_weights, secondary_weights & overlay_secondary_weights.
Any help would be greatly appreciated :)
Best,
Jonas
target_geometry <- counties(state = "CA", class = "sf") %>% st_transform(crs = 4326)
environmental_raster_imputed <- focal(environmental_raster, 31, "modal", na.policy="only", expand = T)
secondary_raster_imputed <- subst(secondary_raster_resampled, NA, 0)
# Compute the overlay weights for the target_geometry and raster grid
overlay_weights <- overlay_weights(polygons = target_geometry, # Input raster data (temperature)
polygon_id_col = "spatial_ID", # Column in target_geometry representing unique ID
grid = environmental_raster_imputed) # Environmental raster (imputed)
# Compute secondary weights (population weights) by aligning the secondary raster with the temperature raster
secondary_weights <- secondary_weights(secondary_raster = secondary_raster_imputed, # Imputed secondary raster
grid = environmental_raster_imputed, # Environmental raster to align with
extent = "full") # Use the full extent of the Environmental raster
# Compute the overlay weights for the polygons and the raster grid, including the population weights
overlay_secondary_weights <- overlay_weights(polygons = target_geometry, # Polygon data for California counties
polygon_id_col = "spatial_ID", # Column in target_geometry representing unique ID
grid = environmental_raster_imputed, # Temperature raster (imputed)
secondary_weights = secondary_weights) # Use the secondary (population) weights
# Aggregate the data by creating a bin for temperatures above 30 degrees Celsius
binned_output_daily <- staggregate_bin(data = environmental_raster_imputed, # Input raster data (temperature)
overlay_weights = overlay_secondary_weights, # Population-weighted overlay weights
time_interval = "1 day", # Daily interval for binning
daily_agg = "none", # No daily aggregation, keep daily values
time_agg = "day", # Aggregate results for each day
bin_breaks = c(30)) # Bin for temperatures above 30°C
