Closed zpmdal closed 4 weeks ago
It looks like there is mismatch in dates between your presence tibble and the raster. If you could create a small reprex (cut down your raster to a smaller area and only have a few presences, so that you can share small-ish files, we can help you fix the problem).
Thanks for your quick response. I saved the SpatRastDataset as an RDS file (zipped before uploading) without any cutting down, as the area is not very big (Nova Scotian Shelf).
This is the presence dataset.
Ok, so there were two issues. First, the time units of the time axis of the raster were not defined (which caused the function to crash). Second, when converting dates from the presence tibble, you need to use a different function, as you are not working with time BP (but time CE). Here is a little sample code that works on my laptop:
nscotia<-readRDS("~/Downloads/tidysdm_bug/SSTSpatRastDataset.Rds")
ncscotia <- terra::unwrap (nscotia)
time(nscotia[1], tstep="years")<-time(nscotia[1])
time(nscotia[2], tstep="years")<-time(nscotia[2])
time(nscotia[3], tstep="years")<-time(nscotia[3])
time(nscotia[4], tstep="years")<-time(nscotia[4])
pres <- read.csv("~/Downloads/tidysdm_bug/ASA_Contemp.csv")
foo <- sample_pseudoabs_time(pres,
n_per_presence = 3,
raster = nscotia,
time_col = "Year",
lubridate_fun = lubridate::date_decimal,
method = c("dist_min", km2m(5)))
Note the use of lubridate::date_decimal
to convert your time in years CE to a date object.
I have also modified tidysdm
to be a bit more helpful. So, if you run the dev
version of sample_pseudoabs_time()
on your raster without setting the time units, you would now get:
Error in sample_pseudoabs_time(pres, n_per_presence = 3, raster = nscotia, :
the units of the time axis of the raster are not defined;
when using terra::time() use either POSIXct or Dates, or set tstep to 'years'
which should be a bit more informative than the ugly crash that you experienced. Let me know if this fixes your problem and we can close this issue.
Thanks! You solved my problem. Really appreciated your great work!
This is not really about bug, but about data format issue. I don't have problem following the example of "Application with palaeodata". But when I suppled my own data with similar format I ran into the error below. I guess the problem is with my raster = SSTSpatRastDataset.
When I print the object print(SSTSpatRastDataset), I got the following output: class : SpatRasterDataset subdatasets : 4 dimensions : 1104, 2203 (nrow, ncol) nlyr : 4, 4, 4, 4 resolution : 0.01, 0.01 (x, y) extent : -73.025, -50.995, 40.975, 52.015 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : crop_Spr20_av20i.tif, crop_Spr21_av21i.tif, crop_Spr22_av22i.tif, crop_Spr23_av23i.tif, crop_sss_m_2020.tif, crop_sss_m_2021.tif, ... names : SprSST, sssM, yrMaxSST, angws
I have 4 time points: time(SSTSpatRastDataset):
$sssM [1] 2000 2021 2022 2023
$yrMaxSST [1] 2020 2021 2022 2023
$angws [1] 2020 2021 2022 2023
When I did the same for the example raster (climate_full), I got the following: class : SpatRasterDataset subdatasets : 3 dimensions : 49, 87 (nrow, ncol) nlyr : 5, 5, 5 resolution : 1, 1 (x, y) extent : -27, 60, 33, 82 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) source(s) : memory names : bio01, bio10, bio12
and time(climate_full): $bio01 [1] -18050 -13050 -8050 -3050 1950
$bio10 [1] -18050 -13050 -8050 -3050 1950
$bio12 [1] -18050 -13050 -8050 -3050 1950
So, my question is what is the correct way to build the SpatRasterDataset? Here is how I built the SpatRasterDataset: var1_2020 = rast(var1_2020.tif); time(var1_2020)=2020 var1_2021 = rast(var1_2021.tif); time(var1_2021) = 2021 ... var1 = c(var1_2020, va1_2021...var1_2023) var2 = c(var2_2020, var2_2021...var2_2023)
SSTsRastDataset = sds(var1, var2...)