inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
73 stars 21 forks source link

ZIP/ZAP vignette: a fix for minor errors #225

Closed tmeeha closed 3 months ago

tmeeha commented 3 months ago

I found a few minor bugs in the ZIP/ZAP vignette. Here are the fixes.

# rename gives an error. 'sum' needs to be changed to 'lyr1'
counts_df <- crds(counts_rstr, df = TRUE, na.rm = TRUE) %>%
  bind_cols(values(counts_rstr, mat = TRUE, na.rm = TRUE)) %>%
  rename(count = lyr1) %>% # 'sum' should become 'lyr1'
  mutate(present = count > 0) %>%
  st_as_sf(coords = c("x", "y"), crs = st_crs(nests))

# '$vegetation' should be removed
names(gcov_vegetation) <- gcov_lvls[[1]]# '$vegetation' should be removed
gcov_vegetation %>% plot()
finnlindgren commented 3 months ago

Hi, can you provide some more information, i.e. sessionInfo() and the full error messages you get with the original code. I don't get any errors with the current code, and looking at the data contents before the rename(count = sum) line, the data.frame does contain a variable called sum (which isn't ideal, but does seem to work as intended). I'm not sure where your lyr1 variable is meant to come from? Similarly for the second part; I don't see a problem on my end, and based on the contents of the data, the vegetation variable does need to be extracted:

> gcov_lvls[[1]]
  value vegetation
1     1 Colonising
2     2  Disturbed
3     3  Grassland
4     4    Primary
5     5  Secondary
6     6 Transition

I have the latest terra version, 1.7-71. I wonder if you might have an outdated terra version, as both inlabru and terra were updated recently (December/January) to fix a previous terra interface issue and its interaction with how inlabru needed to use it. Not sure if that particular issue is involved here, but it's the best guess I have.

tmeeha commented 3 months ago

I did not have the latest versions of terra or inlabru. Now that I have updated it works fine. Sorry for the diversion. Here are the old errors in case this is helpful:

counts_df <- crds(counts_rstr, df = TRUE, na.rm = TRUE) %>%

  • bind_cols(values(counts_rstr, mat = TRUE, na.rm = TRUE)) %>%
  • rename(count = sum) %>%
  • mutate(present = count > 0) %>%
  • st_as_sf(coords = c("x", "y"), crs = st_crs(nests)) Error in rename(): ! Can't rename columns that don't exist. ✖ Column sum doesn't exist. Run rlang::last_trace() to see where the error occurred. rlang::last_trace() <error/vctrs_error_subscript_oob> Error in rename(): ! Can't rename columns that don't exist. ✖ Column sum doesn't exist.

    Backtrace: ▆

    1. ├─... %>% st_as_sf(coords = c("x", "y"), crs = st_crs(nests))
    2. ├─sf::st_as_sf(., coords = c("x", "y"), crs = st_crs(nests))
    3. ├─dplyr::mutate(., present = count > 0)
    4. ├─dplyr::rename(., count = sum)
    5. └─dplyr:::rename.data.frame(., count = sum) Run rlang::last_trace(drop = FALSE) to see 19 hidden frames. Warning message: In class(object) <- "environment" : Setting class(x) to "environment" sets attribute to NULL; result will no longer be an S4 object

names(gcov_vegetation) <- gcov_lvls[[1]]$vegetation Error in gcov_lvls[[1]]$vegetation : $ operator is invalid for atomic vectors

finnlindgren commented 3 months ago

Thanks!