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

Error when a marginal has a single string category #2

Closed
dkyleward opened this issue Sep 9, 2019 · 0 comments
Closed

Error when a marginal has a single string category #2

dkyleward opened this issue Sep 9, 2019 · 0 comments

Comments

@dkyleward
Copy link
Owner

While explaining how to resolve issue #1, I realized that the following code will generate an error:

survey <- tibble(
  size = c(1, 2, 1, 1),
  autos = c("any", "any", "any", "any"),
  weight = 1
)
targets <- list()
targets$size <- tibble(
  `1` = 75,
  `2` = 25
)
targets$autos <- tibble(
  any = 100
)
result <- ipu(survey, targets)

This is because I'm using a single string "any" as the marginal category. The error message:

 Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels 

This is an error in the stats::contrasts() function. The code:

function (x, how.many, value) 
{
  if (is.logical(x)) 
    x <- factor(x, levels = c(FALSE, TRUE))
  if (!is.factor(x)) 
    stop("contrasts apply only to factors")
  if (nlevels(x) < 2L) 
    stop("contrasts can be applied only to factors with 2 or more levels")
# ...

Oddly enough, using all ones instead of "any" works. My package relies on the mlr::createDummyFeatures() function. Perhaps I should write my own version of the function that handles this issue (and also remove a package dependency in the process).

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

1 participant