BiologicalRecordsCentre / sparta

Species Presence/Absence R Trends Analyses
http://biologicalrecordscentre.github.io/sparta/index.html
MIT License
21 stars 24 forks source link

occDetModel function falls over at point 7 below. #253

Closed julianflowers closed 1 month ago

julianflowers commented 1 year ago

Hi

I am trying to use your excellent package to calculate occupancy but keep getting an error

Error in x$Version : $ operator is invalid for atomic vectors which seems to relate to the line of code in the function below. Traceback included FYI. Grateful for any help / advice

Julian Flowers

packages <- c(sapply(session.info[7][[1]], function(x) x$Version), sapply(session.info[8][[1]], function(x) x$Version))

9. FUN(X[[i]], ...) 8. lapply(X = X, FUN = FUN, ...) 7. sapply(session.info[7][[1]], function(x) x$Version) 6. occDetFunc(taxa_name = taxa_name, n_iterations = 200, burnin = 15, occDetdata = formattedOccData$occDetdata, spp_vis = formattedOccData$spp_vis, write_results = TRUE, seed = 123) 5. FUN(X[[i]], ...) 4. lapply(x, fun, ...) 3. sfLapply(x[sIndex:eIndex], fun, ...) 2. sfClusterApplySR("Abdera biflexuosa", occ_mod_function) 1. system.time({ para_out_1 <- sfClusterApplySR("Abdera biflexuosa", occ_mod_function) })

larswesterberg commented 7 months ago

I suggest changing this code in occDetFunc()

packages <- c(sapply(session.info[7][[1]], function(x) x$Version),
+               sapply(session.info[8][[1]], function(x) x$Version))

... to ...

packages <- c(sapply(session.info[9][[1]], function(x) x$Version),
+               sapply(session.info[10][[1]], function(x) x$Version))

I used trace("occDetFunc",edit=TRUE) to edit and the function run without warning. However, I don't recommend editing code in packages with trace()

DylanCarbone commented 5 months ago

Hi @julianflowers,

@AugustT, The error can be reproduced by running the function example

# Create data
set.seed(125)
n <- 15000 #size of dataset
nyr <- 20 # number of years in data
nSamples <- 100 # set number of dates
nSites <- 50 # set number of sites

# Create somes dates
first <- as.Date(strptime("1980/01/01", "%Y/%m/%d")) 
last <- as.Date(strptime(paste(1980+(nyr-1),"/12/31", sep=''), "%Y/%m/%d")) 
dt <- last-first 
rDates <- first + (runif(nSamples)*dt)

# taxa are set as random letters
taxa <- sample(letters, size = n, TRUE)

# three sites are visited randomly
site <- sample(paste('A', 1:nSites, sep=''), size = n, TRUE)

# the date of visit is selected at random from those created earlier
survey <- sample(rDates, size = n, TRUE)

# run the model with these data for one species
# using defaults
results <- occDetModel(taxa = taxa,
                       site = site,
                       survey = survey,
                       species_list = 'a',
                       write_results = FALSE,
                       n_iterations = 1000,
                       burnin = 10,
                       thinning = 2)

As @larswesterberg shows, you can fix it by calling other and loaded packages correctly from the system info. I think it would be best to name the list elements directly.

    packages <- c(sapply(session.info$otherPkgs, function(x) x$Version),
                  sapply(session.info$loadedOnly, function(x) x$Version))

I'll make a pull request.

Thanks

AugustT commented 5 months ago

@DylanCarbone @larswesterberg thanks for this. Agreed better to use the names rather than index, happy to review the PR