Differences between R and Julia
The IAI R interface matches the Julia API very closely, so you can refer to the Julia documentation for information on most tasks. On this page we note the main differences between the R and Julia interfaces.
Conversion of Julia data types to R
In order to figure out the types to pass to an IAI function from the R interface, you can refer to the equivalent function in the Julia API and translate the types to their R equivalent. Most literal data types convert in a straighforward manner, for example:
Int
tointeger
(can also pass as a round-numberdouble
, e.g.,1.0
)Float64
todouble
String
tocharacter
Dict
tolist
The following Julia types can be passed as follows:
nothing
can be passed usingNULL
- a
Symbol
can be passed as acharacter
- a
Vector
can be passed as an atomicvector
- a
Matrix
can be passed as amatrix
- a
DataFrame
can be passed as adata.frame
Specifying Feature Set in R
We list the R input types for specifying set of features in a dataframe as learner parameters. Refer to IAI.FeatureSet
for the Julia equivalence:
Input Type | Description | Examples |
---|---|---|
All | Use all columns | list(All = c()) |
Integer or a vector of Integer s | Specify indices of columns to use | 1 , c(1, 3, 4) |
String or a vector of String s | Specify names of columns to use | "x1" , c("x1", "x3") |
Not | Specify columns not to use | list(Not = 1) , list(Not = c("x2", "x4")) |
Between | Specify range of columns to use | list(Between = c("x1", "x4")) |
Interactive Visualizations
The write_html
and show_in_browser
functions work the same in R as in Julia for saving visualizations to file or displaying in an external browser, respectively. Additionally, visualizations will be automatically shown in the viewer pane when using RStudio, similar to how visualizations are automatically displayed in Jupyter notebooks.
To include data in the tree visualization, the data
keyword argument should be passed as an unnamed list
:
iai::write_html("tree.html", lnr, data = list(X, y))
Below is an example that shows the equivalent R code for the advanced visualization examples in Julia. In these examples we work with the following tree learner:
We can rename the features with a list
that maps from the original names to more descriptive names:
vis_renamed_features <- iai::tree_plot(lnr, feature_renames = list(
"disp" = "Displacement",
"hp" = "Horsepower",
"wt" = "Weight"
))
We can also have a finer-grained control of what is displayed for each node, such as adding summary statistics. We create a list
of list
s with the parameters controlling what you want to show in each node and pass this as extra_content
:
node_inds <- iai::apply_nodes(lnr, X)
extras <- lapply(node_inds, function(inds) {
list(node_details_extra = paste0("<b>Mean horsepower in node:</b> ",
round(mean(X[inds, "hp"]), digits = 2)))
})
vis_extra_text <- iai::tree_plot(lnr, extra_content = extras)
Finally, we can combine multiple learners into a single visualization as described in the Julia documentation. In R, a question is a single-entry named list
of the form list(question = responses)
, where question
is the string for the question and responses
is itself a list
of possible responses:
questions <- list("Use learner with" = list(
"renamed features" = vis_renamed_features,
"extra text output" = vis_extra_text
))
iai::multi_tree_plot(questions)