R Error: Expecting a single string value: [type=character; extent=5]

Viewed 675

I am working with the R programming language. I am using the "CORELS" library in R, here is an example (CORELS is a statistical model that is similar to a decision tree):

library(corels)

logdir <- tempdir()
rules_file <- system.file("sample_data", "compas_train.out", package="corels")
labels_file <- system.file("sample_data", "compas_train.label", package="corels")
meta_file <- system.file("sample_data", "compas_train.minor", package="corels")

stopifnot(file.exists(rules_file),
          file.exists(labels_file),
          file.exists(meta_file),
          dir.exists(logdir))

corels(rules_file, labels_file, logdir, meta_file,
       verbosity_policy = "silent",
       regularization = 0.015,
       curiosity_policy = 2,   # by lower bound
       map_type = 1)       # permutation map
cat("See ", logdir, " for result file.")

The output can be viewed here:

OPTIMAL RULE LIST
if ({sex:Male,juvenile-crimes:>0}) then ({recidivate-within-two-years:Yes})
else if ({priors:>3}) then ({recidivate-within-two-years:Yes})
else ({recidivate-within-two-years:No})

My Question: I am still a bit confused as to how the syntax works for the above functions. For example, I am trying to use the above functions on the "iris" dataset:

data(iris)
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Now, I tried to apply the "corles" function on the iris data set:

logdir <- tempdir()
rules_file <- iris
labels_file <- colnames(iris)

corels(rules_file, labels_file, logdir, 
       verbosity_policy = "silent",
       regularization = 0.015,
       curiosity_policy = 2,   # by lower bound
       map_type = 1)       # permutation map
cat("See ", logdir, " for result file.")

But this produces the following error:

Error in corels(rules_file, labels_file, logdir, verbosity_policy = "silent",  : 
  Expecting a single string value: [type=character; extent=5].

Can someone please show me how to fix this error?

Thanks

References:

1 Answers

The problem is that you are trying to pass datasets to the corels() function, however it wants you to pass file names. The Data Format section of the github for the corels software has details on how to format your data structures. These should then be saved in files and then passed to corels().

Each row in the data file should contain a rule name surrounded by curly braces, { and }, followed by a 0 or 1 to indicate whether that rule is true for that given row of data.

Here's a simple script to construct some:

library(corels)

# Write a function to take a dataset & expression and format it correctly
make_rule <- function(data,expr){
  rule_name <- deparse1(substitute(expr))
  rule_name <- gsub(" ","",rule_name)
  out <- eval(substitute(expr),data)
  paste0("{",rule_name,"} ",paste0(1*out,collapse=" "))
}

# Create names for our rule/label files

rules_file <- "rule_data"
labels_file <- "label_data"

# Create some example rules (must always be binary operations)
iris_rules <- c(
  make_rule(iris,Sepal.Length < 5.84),
  make_rule(iris,Sepal.Width < 3.05),
  make_rule(iris,Petal.Length < 3.76),
  make_rule(iris,Petal.Width < 1.2)
)

#Label data appropriately. Must be a pair of rules
# where the first is the negative option & the 2nd is the
# positive outcome. Here we want to know how to find when
# the flower is a setosa
iris_labels <- c(
  make_rule(iris,Species != "setosa"),
  make_rule(iris,Species == "setosa")
)

# Save the data in the files
writeLines(iris_rules,rules_file)
writeLines(iris_labels,labels_file)


# reference the files and set verbosity to high so
# we get the full output
corels(rules_file, labels_file, ".",
       verbosity_policy = "loud")
Related