class: center, middle, inverse, title-slide .title[ # Phase II: Using Our Toolbox ] .subtitle[ ## Module 6: Spatial Awareness ] .author[ ### Dr. Christopher Kenaley ] .institute[ ### Boston College ] .date[ ### 2026/11/11 ] --- class: top # In class today <!-- Add icon library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"> .pull-left[ Today we'll .... - Look at how to assemble our data - Tune an RF model ] .pull-right[ <img src="https://cdn.britannica.com/40/109040-050-62EEDEA6/Male-white-tailed-deer.jpg" width="450"> ] --- # What are we up against? .pull-left[ - Deer data + NH_deer.txt (.csv) - Shape file + New_Hampshire_Political_Boundaries.shp + `sf` object - Land use: + NLCD_2021_Land_Cover_L48_20230630_qtPZ6zMaTAibmibqfd37.tiff + raster -> `stars` object ] .pull-right[ <img src="https://cdn.britannica.com/40/109040-050-62EEDEA6/Male-white-tailed-deer.jpg" width="450"> ] --- # What are we up against? .pull-left[ - Create an object named `deer`: + `read_csv()` + modify values removing "_" + group by `TOWN` + summarize with mean of `HSM` - Create an object named `nh`: + `read_sf()` + rename the political unit (`pbpNAME`) to match the same in `deer` - Create an object named `lu` + `stars::read_stars()` ] .pull-right[ <img src="https://cdn.britannica.com/40/109040-050-62EEDEA6/Male-white-tailed-deer.jpg" width="450"> ] ] --- # What are we up against? .pull-left[ - Create `nh_dat`: + relative proportion of land use + HSM for each town (Hint: `dplyr::group_by(..., .drop = FALSE)` and `dplyr::count()` + establishes counts, including 0’s, on a per-Town basis - Create `rf_dat`: + Based on `nh_dat` + contains only the data needed to run an RF model + predicts HSM based on land-use proportions. + arcsine transform your proportions data + `arc_sin <- function(x) asin(sqrt(x))` ] .pull-right[ <img src="https://cdn.britannica.com/40/109040-050-62EEDEA6/Male-white-tailed-deer.jpg" width="450"> ] --- # Tuning a RF model .pull-left[ ``` r bermuda_house <- st_read("bermuda_shape/bermuda_dat.shp") ``` ``` ## Reading layer `bermuda_dat' from data source ## `/Users/Chris/Dropbox/Documents/bc (1)/3140.f26/lect/bermuda_shape/bermuda_dat.shp' ## using driver `ESRI Shapefile' ## Simple feature collection with 348 features and 10 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -64.88471 ymin: 32.24808 xmax: -64.64763 ymax: 32.38866 ## Geodetic CRS: WGS 84 ``` ``` r rf_dat <- bermuda_house %>% ungroup() %>% st_drop_geometry() %>% na.omit() %>% select(-name, -pred_col, -subparish) head(rf_dat) ``` ``` ## tot_pop blue green orange pink purple white ## 1 1243 0.04278075 0.3101604 0.20320856 0.05347594 0.25668449 0.13368984 ## 2 1348 0.31443299 0.3659794 0.08247423 0.07216495 0.08762887 0.07731959 ## 3 1208 0.25294118 0.3529412 0.07058824 0.09411765 0.21764706 0.01176471 ## 4 1212 0.08484848 0.2545455 0.06666667 0.20606061 0.14545455 0.24242424 ## 5 1271 0.15646259 0.2517007 0.08163265 0.24489796 0.16326531 0.10204082 ## 6 208 0.03846154 0.2307692 0.34615385 0.07692308 0.11538462 0.19230769 ``` ] .pull-right[ <img src="https://images.pexels.com/photos/38006677/pexels-photo-38006677.jpeg " width="400"> ] --- .pull-left[ ``` r tuneRF(x = rf_dat %>% dplyr::select(-tot_pop), y = rf_dat$tot_pop, stepFactor = 0.5, ntreeTry = 1000, trace = F, mtryStart = 1, improve = 0.01, ) ``` ``` ## -0.03464865 0.01 ``` ``` ## Warning in randomForest.default(x, y, mtry = mtryCur, ntree = ntreeTry, : ## invalid mtry: reset to within valid range ``` ``` ## -0.002257232 0.01 ``` ``` ## Warning in xy.coords(x, y, xlabel, ylabel, log): 1 x value <= 0 omitted from ## logarithmic plot ``` <!-- --> ``` ## mtry OOBError ## 0 0 22906.88 ## 1 1 22855.29 ## 2 2 23647.20 ``` ] .pull-right[ <img src="https://images.pexels.com/photos/38006677/pexels-photo-38006677.jpeg " width="400"> ]