rrudolph / fox-secr

secr analysis of fox capture data
0 stars 1 forks source link

Why did this purrr and mutate suddenly stop working? #1

Closed rrudolph closed 5 years ago

rrudolph commented 5 years ago

https://github.com/rrudolph/fox-secr/blob/f5cd0c935729c32f2fff76cc2265cc8e4951a17f/3_All_Models_AIC_Adults.R#L51-L57

I just can't figure it out. I get this error

Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "list"

It was working perfectly for a while, where it would mutate on the predict formula and then subset males and females from that predict list. The rest of the script worked as intended, getting output tables of the density, g0, and sigma for each sex.

I suddenly got the error after I refactored the code a bit to include a functions.R script and tightened up the paths with the here() function. I tried to revert back to how it was before refactoring but still got the same error. I also get the same error on two other machines, one linux, another on windows. I have absolutely no idea what went wrong but suspect it has something to do with the allModels list of lists. The strange part is that it was fine until it wasn't.

Some background: A contractor helped us with fox population modeling using secr. The original script had lots of repeated code (1000+ lines) and used a separate excel spreadsheet to generate a lot of the code. I wanted to refactor it so it was concise and compact. The first scripts generate the output files. This third script takes those models and crunches the numbers to get a population size estimate.

rrudolph commented 5 years ago

Update: Ok so apparently you can't perform mutate on a list of lists, only a data frame. So weird that it used to work then stopped working. So I need to figure out how to perform that purr::map() function on all the models is some other way.

rrudolph commented 5 years ago

Ok I think I fixed it with help from the UCSB slack channel.

I have no idea why it was working before, but it decided that you can't mutate on a list. So now I removed the mutates and just use one map function like so.

allModels_predict <- purrr::map(allModels,
 ~predict(.x, newdata = NULL, type = c('response','link'), se.fit = TRUE, alpha = .2))

And then from there I can reference the males/females/adults/pups from within that list, rbind them, sort them, etc.