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

levels of categorical variables not preserved ? #42

Open
markusloecher opened this issue Feb 17, 2025 · 1 comment
Open

levels of categorical variables not preserved ? #42

markusloecher opened this issue Feb 17, 2025 · 1 comment

Comments

@markusloecher
Copy link

It seems that the original ordering of the levels of categorical variables can change randomly, which might catch the unsuspecting user by surprise.

Here is a self.contained example using the categorical features of the titanic data:

library(titanic)

data(titanic_train)
titanic_train = titanic_train[,c("Survived", "Pclass", "Sex")]
for (j in 1:3) titanic_train[,j] = factor(titanic_train[,j])
str(titanic_train)
GenSynData = function(x = titanic_train){
  # Train the ARF
  arf <- adversarial_rf(x, verbose = 0)
  # Estimate distribution parameters
  psi <- forde(arf, x)
  #generate:
  x_syn = forge(psi, nrow(x)) 
  LevChanged = 0
  for (j in 1:ncol(x)){
    if (any(levels(x[,j]) != levels(x_syn[,j]))) {
      LevChanged = LevChanged + 1
      cat("new levels for ",colnames(x)[j] , levels(x_syn[,j]), "\n")
    }
  }
  cat(LevChanged, " categorical levels changed overall!\n")
}
set.seed(123)
for (k in 1:5) GenSynData()

'data.frame': 891 obs. of 3 variables:
$ Survived: Factor w/ 2 levels "0","1": 1 2 2 2 1 1 1 1 2 2 ...
$ Pclass : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 3 1 3 3 2 ...
$ Sex : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
new levels for Survived 1 0
new levels for Pclass 3 1 2
new levels for Sex male female
3 categorical levels changed overall!
new levels for Pclass 3 2 1
new levels for Sex male female
2 categorical levels changed overall!
new levels for Survived 1 0
new levels for Pclass 3 2 1
2 categorical levels changed overall!
new levels for Pclass 2 3 1
1 categorical levels changed overall!
new levels for Survived 1 0
new levels for Pclass 1 3 2
new levels for Sex male female
3 categorical levels changed overall!

@mnwright
Copy link
Member

I think this is fixed already on Github (but not on CRAN). Another reason for a new CRAN release!

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