btb RpackageThis document will show you :
btbFurthermore, it will introduce a way to map your results usingmapsf package and how to save your smoothed spatial datausingsf.
btbbtb is available on CRAN :
To get a bug fix or to use a feature from the development version,you can install the development version of from GitHub :
Spatial smoothing generally reduces individual data disclosure.However, smoothed data can contain individual information. Please remaincautious in any case.
btb package provides several data tables.
For every gas station in metropolitan France, thedfPrix_SP95_2016 table gives :
library(btb)#> Loading required package: dplyr#>#> Attaching package: 'dplyr'#> The following objects are masked from 'package:stats':#>#> filter, lag#> The following objects are masked from 'package:base':#>#> intersect, setdiff, setequal, union#> Loading required package: mapsfdata(dfPrix_SP95_2016)head(dfPrix_SP95_2016)#> SP95 x y#> 2 1.355 588356.7 6221492#> 3 1.469 981997.3 6312078#> 4 1.349 873384.0 6504111#> 7 1.512 1023983.9 6351175#> 8 1.480 377274.3 6682264#> 9 1.403 893440.4 6690013Let’s visualize these stations :
sf package to transform your data.frame asgeometric points.library(sf)#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUEsfPrix_SP95_2016<-st_as_sf(dfPrix_SP95_2016,coords =c("x","y"),crs=2154)plot(sfPrix_SP95_2016$geometry)To figure out your spatial distribution before smoothing you data, itcan be interesting to aggregate your points inside a grid (e.g : numberof gas stations in 20 km pixels grid), because the smoothing is lesstime-consuming
btb provides thebtb_add_centroids and thebtb_ptsToGrid functions to make it easy :
btb_add_centroids).x_centro andy_centro are created in the table.1dfPrix_SP95_2016<-btb_add_centroids(dfPrix_SP95_2016,iCellSize =20000,names_coords =c("x","y"))head(dfPrix_SP95_2016)#> SP95 x y x_centro y_centro#> 2 1.355 588356.7 6221492 590000 6230000#> 3 1.469 981997.3 6312078 990000 6310000#> 4 1.349 873384.0 6504111 870000 6510000#> 7 1.512 1023983.9 6351175 1030000 6350000#> 8 1.480 377274.3 6682264 370000 6690000#> 9 1.403 893440.4 6690013 890000 6690000library(dplyr)centro_values<- dfPrix_SP95_2016%>%group_by(x_centro, y_centro)%>%summarise(pricemean=mean(SP95,rm.na =TRUE))#> `summarise()` has grouped output by 'x_centro'. You can override using the#> `.groups` argument.btb_ptsToGrid)grid_values<-btb_ptsToGrid(centro_values,sEPSG =2154,iCellSize =20000,names_centro =c("x_centro","y_centro"))nrow(grid_values)#> [1] 1328head(grid_values)#> Simple feature collection with 6 features and 3 fields#> Geometry type: POLYGON#> Dimension: XY#> Bounding box: xmin: 120000 ymin: 6760000 xmax: 160000 ymax: 6860000#> Projected CRS: RGF93 v1 / Lambert-93#> # A tibble: 6 × 4#> # Groups: x_centro [2]#> x_centro y_centro pricemean geometry#> <dbl> <dbl> <dbl> <POLYGON [m]>#> 1 130000 6790000 1.33 ((120000 6800000, 140000 6800000, 140000 6780000,…#> 2 130000 6830000 1.32 ((120000 6840000, 140000 6840000, 140000 6820000,…#> 3 130000 6850000 1.36 ((120000 6860000, 140000 6860000, 140000 6840000,…#> 4 150000 6770000 1.35 ((140000 6780000, 160000 6780000, 160000 6760000,…#> 5 150000 6790000 1.37 ((140000 6800000, 160000 6800000, 160000 6780000,…#> 6 150000 6810000 1.30 ((140000 6820000, 160000 6820000, 160000 6800000,…Once you have your polygons and your aggregated data, you can map it.Here, we use themapsf package to do so.
library(mapsf)mapsf::mf_map(x = grid_values,type ="choro",var="pricemean",breaks ="quantile",nbreaks =5,lwd=1,leg_val_rnd =1)This map represents your aggregated (mean price) but is not smoothedyet.
Despite its patchwork aspect, this map is a good first step to betterunderstand your data.
On the example below, we smooth the density of gas stations using5~000 km pixels and a 100 km bandwidth. Note that we need to create anew dummy variable (equals to 1 for every station) to count thestations.
pts_density<- dfPrix_SP95_2016[,c("x","y")]# Create dummypts_density$stations_density<-1Lhead(pts_density)#> x y stations_density#> 2 588356.7 6221492 1#> 3 981997.3 6312078 1#> 4 873384.0 6504111 1#> 7 1023983.9 6351175 1#> 8 377274.3 6682264 1#> 9 893440.4 6690013 1# Smoothingsmooth_density<-btb_smooth(pts = pts_density,sEPSG =2154,iBandwidth =100000,iCellSize =5000)head(smooth_density)#> Simple feature collection with 6 features and 3 fields#> Geometry type: POLYGON#> Dimension: XY#> Bounding box: xmin: 1170000 ymin: 6005000 xmax: 1200000 ymax: 6010000#> Projected CRS: RGF93 v1 / Lambert-93#> x y stations_density geometry#> 1 1172500 6007500 0.008703036 POLYGON ((1170000 6010000, ...#> 2 1177500 6007500 0.010129089 POLYGON ((1175000 6010000, ...#> 3 1182500 6007500 0.011476304 POLYGON ((1180000 6010000, ...#> 4 1187500 6007500 0.012691105 POLYGON ((1185000 6010000, ...#> 5 1192500 6007500 0.013729008 POLYGON ((1190000 6010000, ...#> 6 1197500 6007500 0.014555589 POLYGON ((1195000 6010000, ...# Mapmapsf::mf_map(x = smooth_density,type ="choro",var="stations_density",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)Note thatbtb_smooth isconservative :
pts_density : 5573smooth_density : 5573Smoothing a ratio works almost the same way :
Note that thebtb_smooth function smoothes by defaultall numeric variables in the input points table (parameterpts).
# Prepare your datapts_meanprice<- dfPrix_SP95_2016[,c("x","y","SP95")]pts_meanprice$stations_density<-1Lhead(pts_meanprice)#> x y SP95 stations_density#> 2 588356.7 6221492 1.355 1#> 3 981997.3 6312078 1.469 1#> 4 873384.0 6504111 1.349 1#> 7 1023983.9 6351175 1.512 1#> 8 377274.3 6682264 1.480 1#> 9 893440.4 6690013 1.403 1# Smooth both prices and station densitysmooth_density<-btb_smooth(pts = pts_meanprice,sEPSG =2154,iBandwidth =100000,iCellSize =5000)head(smooth_density)#> Simple feature collection with 6 features and 4 fields#> Geometry type: POLYGON#> Dimension: XY#> Bounding box: xmin: 1170000 ymin: 6005000 xmax: 1200000 ymax: 6010000#> Projected CRS: RGF93 v1 / Lambert-93#> x y SP95 stations_density geometry#> 1 1172500 6007500 0.01320174 0.008703036 POLYGON ((1170000 6010000, ...#> 2 1177500 6007500 0.01536551 0.010129089 POLYGON ((1175000 6010000, ...#> 3 1182500 6007500 0.01740988 0.011476304 POLYGON ((1180000 6010000, ...#> 4 1187500 6007500 0.01925362 0.012691105 POLYGON ((1185000 6010000, ...#> 5 1192500 6007500 0.02082924 0.013729008 POLYGON ((1190000 6010000, ...#> 6 1197500 6007500 0.02208448 0.014555589 POLYGON ((1195000 6010000, ...# Calculate the smoothed mean (from smoothed nominator and denominator)smooth_density<- smooth_density%>%mutate(meanprice=SP95/stations_density)mapsf::mf_map(x = smooth_density,type ="choro",var="meanprice",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)Quantile smoothing is a different methodology.
Its major benefits are :
pts_quantiles<- dfPrix_SP95_2016[,c("x","y","SP95")]head(pts_quantiles)#> x y SP95#> 2 588356.7 6221492 1.355#> 3 981997.3 6312078 1.469#> 4 873384.0 6504111 1.349#> 7 1023983.9 6351175 1.512#> 8 377274.3 6682264 1.480#> 9 893440.4 6690013 1.403smooth_quantiles<-btb_smooth(pts = pts_quantiles,sEPSG =2154,iBandwidth =100000,iCellSize =5000,vQuantiles =c(0.5,0.9))head(smooth_quantiles)#> Simple feature collection with 6 features and 5 fields#> Geometry type: POLYGON#> Dimension: XY#> Bounding box: xmin: 1170000 ymin: 6005000 xmax: 1200000 ymax: 6010000#> Projected CRS: RGF93 v1 / Lambert-93#> nbObs SP95_05 SP95_09 x y geometry#> 1 26 1.513 1.53 1172500 6007500 POLYGON ((1170000 6010000, ...#> 2 27 1.513 1.53 1177500 6007500 POLYGON ((1175000 6010000, ...#> 3 27 1.513 1.53 1182500 6007500 POLYGON ((1180000 6010000, ...#> 4 26 1.513 1.53 1187500 6007500 POLYGON ((1185000 6010000, ...#> 5 26 1.513 1.53 1192500 6007500 POLYGON ((1190000 6010000, ...#> 6 26 1.513 1.53 1197500 6007500 POLYGON ((1195000 6010000, ...# Median smoothing :mapsf::mf_map(x = smooth_quantiles,type ="choro",var="SP95_05",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)# Smooth the 9th decile :mapsf::mf_map(x = smooth_quantiles,type ="choro",var="SP95_09",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)Here, we use data which indicates the number of poor households insquared data (200 meters) of an island called “La Réunion”.
Each point is the centroid of the grid used to publish aggregateddata (1 000 meters pixels).
Let’s smooth the proportion of poors among households with anautomatic grid (iNeighbor parameter absentinbtb_smooth function).
In the following example, note that thebtb_smoothfunction accepts sf points in input (also the case withbtb_ptsToGrid).
# Load datadata("reunion")head(reunion)#> x y houhold phouhold#> 1 359500 7634300 5.069307 2.3762376#> 2 359500 7634500 26.930693 12.6237624#> 3 355900 7634500 15.000000 4.0000000#> 4 356100 7634500 39.000000 20.0000000#> 5 356300 7634500 41.642857 15.1428571#> 6 356500 7634500 2.357143 0.8571429# Optional : transform as sf pointssfreunion<- sf::st_as_sf(reunion,coords=c("x","y"),crs =3727)plot(sfreunion$geometry)# btb_smooth with an automatic gridsmooth_reunion<-btb_smooth(sfreunion,iCellSize =500,iBandwidth =5000)# Calculate the ratiosmooth_reunion<- smooth_reunion%>%mutate(prop_poors =100* phouhold/ houhold)# mapmapsf::mf_map(x = smooth_reunion,type ="choro",var="prop_poors",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)Now, let’s smooth the same ratio, with the same smoothingspecifications (iBandwidth andiCellSize) butwithiNeighbor = 0. In this case, the automatic grid onlyuses pixels that contain at least one data point (here, at least onehousehold). The result is quite different.
smooth_reunion<-btb_smooth(sfreunion,iCellSize =500,iBandwidth =5000,iNeighbor =0)smooth_reunion<- smooth_reunion%>%mutate(prop_poors =100* phouhold/ houhold)mapsf::mf_map(x = smooth_reunion,type ="choro",var="prop_poors",breaks ="quantile",nbreaks =5,border =NA,leg_val_rnd =1)Using theInspirenorm,btb_smooth andbtb_ptsToGrid allowyou to name your pixels in a proper international way. It could beuseful for reuse purpose, merge operations, etc.
You just need to useinspire = TRUE :
smooth_reunion<-btb_smooth(sfreunion,iCellSize =500,iBandwidth =2000,iNeighbor =0,inspire =TRUE)smooth_reunion<- smooth_reunion%>%mutate(prop_poors =100* phouhold/ houhold)head(smooth_reunion)#> Simple feature collection with 6 features and 6 fields#> Geometry type: POLYGON#> Dimension: XY#> Bounding box: xmin: 355500 ymin: 7634000 xmax: 360000 ymax: 7635000#> Projected CRS: Reunion 1947 / TM Reunion#> x y houhold phouhold idInspire#> 1 359750 7634250 58.60575 24.13523 CRS3727RES500mN7634000E359500#> 2 355750 7634750 158.31143 69.21210 CRS3727RES500mN7634500E355500#> 3 356250 7634750 191.80652 87.96571 CRS3727RES500mN7634500E356000#> 4 356750 7634750 195.59692 90.96524 CRS3727RES500mN7634500E356500#> 5 357750 7634750 125.03327 53.53656 CRS3727RES500mN7634500E357500#> 6 358750 7634750 89.45056 34.01019 CRS3727RES500mN7634500E358500#> geometry prop_poors#> 1 POLYGON ((359500 7634500, 3... 41.18236#> 2 POLYGON ((355500 7635000, 3... 43.71895#> 3 POLYGON ((356000 7635000, 3... 45.86169#> 4 POLYGON ((356500 7635000, 3... 46.50648#> 5 POLYGON ((357500 7635000, 3... 42.81785#> 6 POLYGON ((358500 7635000, 3... 38.02121Then, to export your geometric data, you can use thesf::write_sf function.
By default, the grid is centered on the geographicalreferential origin. If you want to shift this origin, use theoffset parameter.↩︎