gof test: error in UseMethod("droplevels")

951 views
Skip to first unread message

Vivien Louppe

unread,
May 26, 2020, 10:22:12 AM5/26/20
to unmarked
Hi everyone,

I am trying to perform a gof test on a single species single season model using the function "mb.gof.test".
I am using a script which was working well a few month ago on (almost) the same camera trap data. I am trying to use it on a subset of these data, but now it gives me an error message:

> y <- ua
> scov <- scov
> ufo <- unmarkedFrameOccu(y = y,
+                          siteCovs = scov)

> ufo@siteCovs$elevation <- scale(ufo@siteCovs$elevation)
> ufo@siteCovs$clc <- as.factor(ufo@siteCovs$clc)
> ufo@siteCovs$precipitation <- scale(ufo@siteCovs$precipitation)

> full <- occu(formula = ~ 1  # detection formula
+              ~ clc + elevation + precipitation, # occupancy formula
+              data = ufo)

> mb.gof.test(mod = full,
+             nsim = 1000,
+             plot.hist = TRUE,
+             parallel = TRUE)
Error in UseMethod("droplevels") :
  no applicable method for 'droplevels' applied to an object of class "character"

I checked the class of the model and the data, and none of them is an object of class "character".
It is most probably a very obvious mistake from my part but I really can't identify it.

I really would appreciate your help.

Many thanks,

Vivien



Ken Kellner

unread,
May 26, 2020, 10:53:12 AM5/26/20
to unmarked
Hi Vivien,

This  is related to the new R 4.0 changing the default way character strings are handled in data frames. Setting your global option back to the way it used to be, e.g.

options(stringsAsFactors = TRUE)

should work as a temporary workaround until package AICcmodavg is updated to reflect the new default.

Ken

Vivien Louppe

unread,
May 27, 2020, 8:39:16 AM5/27/20
to unmarked
Hi Ken,

Thank you very much for your message.
I tried to change the global option as you suggested, but unfortunately it didn't worked. Actually, R doesn't want me to change the global options...

> options(stringsAsFactors = TRUE)
Warning message:
In options(stringsAsFactors = TRUE) :
  'options(stringsAsFactors = TRUE)' is deprecated and will be disabled

I tried to do the gof test anyway, but as expected, I got an error message reporting the same problem as before (in a slightly different formulation):
> mb.gof.test(mod = full,
+             nsim = 1000, # dans l'idéal, mettre 10000

+             plot.hist = TRUE,
+             parallel = TRUE)
Error in checkForRemoteErrors(val) :
  5 nodes produced errors; first error: pas de méthode pour 'droplevels' applicable pour un objet de classe "character"

In the documentation of the base functions of R, it is said that:
default.stringsAsFactors is a utility that takes getOption("stringsAsFactors") and ensures the result is TRUE or FALSE (or throws an error if the value is not NULL). This function is deprecated now and will no longer be available in the future.

For a non-expert of R like me, a simple solution would probably be to use a old version of R...
But if you have any idea of another possible solution, please let me know!

Many thanks,

Vivien

Marc J. Mazerolle

unread,
May 27, 2020, 8:45:27 AM5/27/20
to unmarked

Hi Vivien,


The new version of AICcmodavg is not on CRAN yet. I'll send you a temporary fix to take care of this in an email offlist.


Best,


Marc


____________________
Marc J. Mazerolle
Département des sciences du bois et de la forêt
2405 rue de la Terrasse
Université Laval
Québec, Québec G1V OA6, Canada
Tel: (418) 656-2131 ext. 407120
Email: marc.ma...@sbf.ulaval.ca

De : unma...@googlegroups.com <unma...@googlegroups.com> de la part de Vivien Louppe <vivien...@gmail.com>
Envoyé : 27 mai 2020 08:39
À : unmarked
Objet : [unmarked] Re: gof test: error in UseMethod("droplevels")
 
[Externe UL*]
--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/unmarked/0838e921-0ae5-4f20-a838-a617b2d97cbb%40googlegroups.com.
*ATTENTION : L’émetteur de ce courriel est externe à l’Université Laval.
Évitez de cliquer sur un hyperlien, d’ouvrir une pièce jointe ou de transmettre des informations si vous ne connaissez pas l’expéditeur du courriel. En cas de doute, contactez l’équipe de soutien informatique de votre unité ou hamec...@ulaval.ca.


Ken Kellner

unread,
May 27, 2020, 9:04:36 AM5/27/20
to unmarked
Marc's solution will be the best option, but in case that doesn't work for some reason - 

As you note the  `options(stringsAsFactors = TRUE)` command is deprecated, but for now, it still does actually work. You can double check:

options()$stringsAsFactors #will be FALSE
options(stringsAsFactors = TRUE)
options()$stringsAsFactors #will now be TRUE

The reason for the error is that you would have to set this option in every parallel core, since you are running mb.gof.fit in parallel, but the code I sent does not do that.
If you set parallel=FALSE I think it will work (but of course it will be slower).

Ken


On Wednesday, May 27, 2020 at 8:45:27 AM UTC-4, Marc J. Mazerolle wrote:

Hi Vivien,


The new version of AICcmodavg is not on CRAN yet. I'll send you a temporary fix to take care of this in an email offlist.


Best,


Marc


____________________
Marc J. Mazerolle
Département des sciences du bois et de la forêt
2405 rue de la Terrasse
Université Laval
Québec, Québec G1V OA6, Canada
Tel: (418) 656-2131 ext. 407120
[Externe UL*]
To unsubscribe from this group and stop receiving emails from it, send an email to unma...@googlegroups.com.

Vivien Louppe

unread,
May 27, 2020, 12:20:59 PM5/27/20
to unmarked
Thank you Ken and Marc for your repsonse, both your solutions worked well.

Cheers,

Vivien

Rachel Moseley

unread,
Mar 17, 2021, 9:21:06 AM3/17/21
to unmarked
Hi,
I am having the same issue when I run a goodness of fit test on multiple season model data. I have a covariate labeled "cycle" that is meant to be a factor. I have used the following code and while forcing the covariate to be a factor in the current dataframe worked, I noticed that cycle is converted to a character after I create the .umf.


dat<- read.csv("HHB_occ2020.csv",stringsAsFactors=TRUE)
'data.frame': 4410 obs. of  32 variables:
 $ loc                         : int  11 11 11 11 11 11 11 11 11 11 ...
 $ site                        : int  1 1 1 1 1 1 1 1 1 1 ...
 $ occasion                    : int  1 2 3 4 5 6 7 1 2 3 ...
 $ season                      : int  1 1 1 1 1 1 1 2 2 2 ...
 $ julian                      : int  242 243 237 238 239 240 241 292 293 287 ...
 $ jDate                       : num  0.997 0.995 1 1 1 ...
 $ det                         : int  1 1 0 0 0 1 1 1 1 0 ...
 $ year                        : int  1 1 1 1 1 1 1 1 1 1 ...
 $ region                      : int  1 1 1 1 1 1 1 1 1 1 ...
 $ treehab                     : int  1 1 1 1 1 1 1 1 1 1 ...
 $ cycle                       : Factor w/ 4 levels "lac","mate","preg",..: 1 1 1 1 1 1 1 2 2 2 ...

Bat.umf <- unmarkedMultFrame(y = y, numPrimary = 14, siteCovs = sitecovs,
                             obsCovs = list(temp=temp, wind=wind, rain=rain, month=month, seas=seas, julian=julian,jdate=jdate,humidity=humidity),
                             yearlySiteCovs = list(cycle = cycle, myt = myt, myr = myr, myw = myw, year=year,
                                                  mmStudyt=mmStudyt,mmStudyr=mmStudyr,mmStudyw=mmStudyw))

str(Bat.umf)
Formal class 'unmarkedMultFrame' [package "unmarked"] with 7 slots
  ..@ numPrimary    : num 14
  ..@ yearlySiteCovs:'data.frame': 630 obs. of  8 variables:
  .. ..$ cycle   : chr [1:630] "lac" "mate" "prepreg" "preg" ...
  .. ..$ myt     : num [1:630] 10.7 10.7 10.7 10.7 11.5 ...
  .. ..$ myr     : num [1:630] 22.91 22.91 22.91 22.91 6.43 ...
  .. ..$ myw     : num [1:630] 4.17 4.17 4.17 4.17 3.8 ...
  .. ..$ year    : int [1:630] 1 1 1 1 2 2 2 2 3 3 ...
  .. ..$ mmStudyt: num [1:630] 13.4 12.4 9.2 10.8 13.1 ...
  .. ..$ mmStudyr: num [1:630] 9.29 15.06 14.13 6.37 8.57 ...
  .. ..$ mmStudyw: num [1:630] 3.77 3.73 4.44 2.79 3.93 ...
  ..@ y             : int [1:45, 1:98] 1 0 0 1 1 NA 1 1 1 1 ...


You can see that "cycle" is now a character instead of a factor. It seems in my case that this is an issue with unmarked rather than AICcmodavg, though I do obviously get the same message when I try to run the goodness of fit test: Error in UseMethod("droplevels") :
  no applicable method for 'droplevels' applied to an object of class "character". Any ideas or workarounds? I know that the .umf is an S4 object and I'm not having luck converting the class after the fact. Similarly this code worked on previous versions of R but I am currently using 4.0.0. My Unmarked version is 0.13-2.
Cheers,
Rachel

Ken Kellner

unread,
Mar 17, 2021, 9:25:52 AM3/17/21
to unmarked
Hi Rachel,

Can you upgrade to the latest unmarked (1.0.1) and try again? This should be fixed in the current CRAN version.

Alternatively you should also still be able to set

options(stringsAsFactors = TRUE)

but I don't recommend that as it will stop working in a future version of R.

Ken

Reply all
Reply to author
Forward
0 new messages