CornellLabofOrnithology / ebird-best-practices

Best Practices for Using eBird Data
https://CornellLabOfOrnithology.github.io/ebird-best-practices/
Other
31 stars 12 forks source link

Extracting lc values from all years #9

Closed lime-n closed 3 years ago

lime-n commented 3 years ago

I have arrived onto this code:

lc_extract_pred <- landcover[[paste0("y", max_lc_year)]] %>% 
  exact_extract(r_cells, progress = FALSE) %>% 
  map(~ count(., landcover = value)) %>% 
  tibble(id = r_cells$id, data = .) %>% 
  unnest(data)

It works well, however, as mentioned in the example, this only extracts landcover values within neighbourhoods for the latest year, 2019. How can I extract these values across all years?

I have thought of using

 max_lc_year <- names(landcover) %>% 
+     str_extract("[0-9]{4}") %>% 
+     as.integer() %>% 
+     all()

To extract all years, however, it returns this error:

Error: Problem with `mutate()` input `landcover`.
x object 'value' not found
i Input `landcover` is `value`.

Do I have to change an earlier step to make this work?

mstrimas commented 3 years ago

Essentially you would probably loop, either with map or a for loop, over the years and bind together the resulting data frames. Unfortunately, I'm super busy at the moment, and don't have time to answer questions like this in detail since it's really more of a general R question than a question about eBird data and analysis. I'd suggest general R resources like those suggested in the intro

lime-n commented 3 years ago

I have used this code:

 all_lc_year <- names(landcover) %>% 
     str_extract("[0-9]{4}") %>% 
     as.integer()

to collect the values for all years, and then took the code in part:

lc_extract_ext <- landcover[[paste0("y", all_lc_year)]] %>% 
  exact_extract(r_cells, progress = FALSE)
lc_extract_cnt <- map(lc_extract_ext, ~ count(., landcover = value)) %>% 
  tibble(id = r_cells$id, data = .)
lc_extract_pred <- unnest(lc_extract_cnt, data)

And find that the error appears in:

lc_extract_cnt <- map(lc_extract_ext, ~ count(., landcover = value)) %>% 
  tibble(id = r_cells$id, data = .)

I cannot seem to get around this error, have you figured it out?

lime-n commented 3 years ago

I found that doing it by each individual year and then combining the rows by columns worked.

The next step is trying to plot several different maps as shown in the code but for each individual year.

mstrimas commented 3 years ago

Yes, this code is all designed to produce predictions for a single year. By looping you can of course make predictions for multiple years, however, caution is warranted because you can't necessarily compare these predictions between years. For example, there may be systematic differences in the eBird effort between years that can result in apparent year-to-year differences that are not driven by anything ecological. This is part of the reason we only produce estimates for a single year in the best practices book. So, just be careful when making predictions for multiple years.