Programming with Radiant

library(radiant)
knitr::opts_chunk$set(
  comment = NA, 
  cache = FALSE, 
  message = FALSE, 
  warning = FALSE,
  dpi = 96
)
options(width = 150)

Radiant's goal is to provide access to the power of R for business analytics and data science. Although Radiant's web-interface can handle many data and analysis tasks, you may prefer to write your own code. Radiant provides a bridge to programming in R(studio) by exporting the functions used for analysis. For example, you can run your analyses in Radiant and output the relevant function calls to an R or Rmarkdown document. Most pages in the app have an icon on the bottom left of your screen that you can click to generate a (reproducible) report with your analysis in the Report > Rmd (or Report > R) tab. As an alternative to clicking the icon you can also press ALT-enter on your keyboard. Click the Knit report button on the Report > Rmd page to render the report to HTML or press the Save report button to produce a Notebook, HTML, PDF, Word, or Rmarkdown file. Radiant's function manuals can be viewed using the commands below:

help(package = radiant.data)
help(package = radiant.design)
help(package = radiant.basics)
help(package = radiant.model)
help(package = radiant.multivariate)

You can also use Rstudio to render and edit Rmarkdown documents generated in Radiant. When you install and load Radiant it exports functions that can be called from R-code and/or an Rmarkdown document. For example, you can paste the commands below into the command console to get the same output as in the browser interface.

library(radiant)
data(diamonds, envir = environment())
result <- single_mean(diamonds, "price")
summary(result)
plot(result)

You can also call functions for visualization (see below) and access help from the console using ?visualize

visualize(
  diamonds, 
  xvar = "carat", 
  yvar = "price", 
  type = "scatter",
  facet_row = "clarity", 
  color = "clarity", 
  labs = labs(title = "Diamond Prices ($)"),
  custom = FALSE
) 

Use library(radiant) to load the library. To see the index of functions currently available in, for example, Radiant's Model menu use the help(package = "radiant.model") command

Lets start by comparing the mean of a variable to a (population) value using R's built-in mtcars dataset. This functionality is in the Radiant menu Basics > Means > Single mean. The analysis is conducted in function single_mean. Calling the summary method on the result object will show tabular output. Calling plot on the same result object will produce relevant plots.

result <- single_mean(
  mtcars, 
  var = "mpg", 
  comp_value = 20, 
  alternative = "greater"
)
summary(result)
plot(result, plots = "hist")

To compare the mean price of diamonds across different levels of clarity we can call the compare_means function:

result <- compare_means(
  diamonds, 
  var1 = "clarity", 
  var2 = "price", 
  adjust = "bonf"
)
summary(result)
plot(result, plots = c("bar", "density"))

These datasets are available after loading the radiant library by using the data function. We can also load data through Radiant's browser interface and then access the data from the console after closing the app. Start radiant using the command below and then click select Examples from the Load data of type dropdown in the Data > Manage tab. Then close the app by clicking the icon in the navbar and then clicking Stop. The datasets loaded through the web-interface are now available in the r_data environment as well. To use them directly in your code use attach(r_data).

## start radiant in Rstudio, load the example data, then click the power 
## icon in the navigation bar and click on Stop
radiant::radiant()

Because we already loaded the radiant library we already have access to all the data we need here. Lets use the compare_means function to evaluate salary data for professors of different ranks using:

result <- compare_means(salary, var1 = "rank", var2 = "salary")
summary(result)
plot(result)

We can also run regressions and get output in a format that would require quite a few lines of code to produce from scratch:

result <- regress(diamonds, rvar = "price", evar = c("carat","clarity"))
summary(result, sum_check = "confint")
pred <- predict(result, pred_cmd = "carat = 1:10")
print(pred, n = 10)
plot(result, plots = "coef")
plot(result, plots = "dashboard", lines = "line", nrobs = 100)

As another example, imagine that you want to segment a sample of respondents based on their toothpaste attitudes. Below is the required code to produce results using functions from the Radiant package. For help on the commands and options for cluster analysis use ?hclus, ?plot.hclus, and ?klus. See also the Radiant function manuals linked above.

## run hierarchical cluster analysis on the shopping data, variables v1 through v6
result <- hclus(shopping, "v1:v6")

## summary - not much here - plots are more important
summary(result)

## check the help file on how to plot results from hierarchical cluster
## analysis default plots
## it looks like there is a big jump in overall within-cluster
## heterogeneity in the step from 3 to 2 segments
plot(result)
## show the dendrogram with cutoff at 0.05
plot(result, plots = "dendro", cutoff = 0.05)
## plots created above suggest 3 clusters may be  most appropriate
## use kclus to create the clusters
## generate output and store cluster membership
result <- kclus(shopping, vars = "v1:v6", nr_clus = 3)
summary(result)
plot(result, plots = c("density", "bar"))
shopping <- store(shopping, result, name = "clus")

## was the data really changed?
head(as.data.frame(shopping))

See if you can reproduce this output in the radiant browser interface. Start Radiant from the Addins dropdown in Rstudio.




Try the radiant package in your browser

Any scripts or data that you put into this service are public.

radiant documentation built on Sept. 11, 2023, 5:10 p.m.