Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dummyVars: contr.ltfr not found if caret not attached #380

Closed
saviola777 opened this issue Feb 25, 2016 · 2 comments
Closed

dummyVars: contr.ltfr not found if caret not attached #380

saviola777 opened this issue Feb 25, 2016 · 2 comments

Comments

@saviola777
Copy link

Hello,

I'm using caret for optional pre-processing in another package. This means I have caret in the suggests field of the DESCRIPTION file, and do not directly import from it, but call its functions using the namespace prefix caret::. In this context, there is a small bug with the dummyVars function, which registers the contr.ltfr function in the options, assuming that it is available in the global environment. Here is a minimal example to reproduce the error (note that I do not call library(caret) intentionally):

data(iris)
d <- caret::dummyVars(~., iris)
irisD <- predict(d, newdata=iris)
# Error in get(ctr, mode = "function", envir = parent.frame()) : 
# object 'contr.ltfr' of mode 'function' was not found

I work around this by exporting my own version of contr.ltfr which calls caret::contr.ltfr, so it is not a big deal, and maybe using caret without library'ing or importing it completely is not a scenario you want to support, that would be fine as well. I just wanted to let you know, maybe there is an easy solution or fix.

Something else I have thought about: You do not seem to reset the "contrasts" option, so a call to dummyVars creates side-effects (any function using the "contrasts" option after this may behave differently), is this intentional?

Thanks, keep up the good work on this great package.

@topepo
Copy link
Owner

topepo commented Mar 6, 2016

do not directly import from it

Out of curiosity, why not?

I don't know of an easy method. You could always just copy the code from this package and do a proper attribution. That's not pretty but I don't know of any cleaner way than this or importing it.

Something else I have thought about: You do not seem to reset the "contrasts" option, so a call to dummyVars creates side-effects (any function using the "contrasts" option after this may behave differently), is this intentional?

I don't see this...

> library(caret)
Loading required package: lattice
Loading required package: ggplot2
options("contrasts") 
nope <- capture.output(example("dummyVars"))
options("contrasts") 
> options("contrasts") 
$contrasts
        unordered           ordered 
"contr.treatment"      "contr.poly" 

> nope <- capture.output(example("dummyVars"))
> options("contrasts") 
$contrasts
        unordered           ordered 
"contr.treatment"      "contr.poly" 

The code has

if(!object$fullRank)
{
  oldContr <- options("contrasts")$contrasts
  newContr <- oldContr
  newContr["unordered"] <- "contr.ltfr"
  options(contrasts = newContr)
}
<snip>
if(!object$fullRank) options(contrasts = oldContr)

@saviola777
Copy link
Author

Out of curiosity, why not?

I wanted to keep caret an optional dependecy, so any import would have to be conditional (depending on whether caret is installed). At runtime, this is possible via require(), but CRAN does not like this, so I use requireNamespace() (as seen here), which does not attach the package.

I don't see this...

Ah, yes, my bad. This only happens if the caret package is not attached becuse then an error occurs between setting and resetting of the contrasts and they are never reset.

I guess I will just make caret an Imports dependency, since I agree that with the way that contrasts can only be defined as character, there is no easy fix for this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants