--- title: Programming with Radiant author: "Vincent R. Nijs, Rady School of Management (UCSD)" date: "`r Sys.Date()`" output: rmarkdown::html_vignette: pandoc_args: ["--css", "https://use.fontawesome.com/releases/v5.15.3/css/all.css"] vignette: > %\VignetteIndexEntry{Programming with Radiant} %\VignetteEngine{knitr::rmarkdown} \usepackage[utf8]{inputenc} --- ```{r r_setup, include = FALSE} 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](https://rmarkdown.rstudio.com/) 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: ```r 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. ```{r single_mean_price, fig.height = 3, fig.width = 5} 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` ```{r scatter, fig.height = 4, fig.width = 5} 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. ```{r single_mean_mpg, fig.height = 3, fig.width = 5} 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: ```{r compare_means_diamonds, fig.height = 5, fig.width = 4} 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)`. ```{r eval = FALSE} ## 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: ```{r compare_means_salary, fig.height = 3, fig.width = 4} 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: ```{r} 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) ``` ```{r regress_coeff, fig.width = 6, fig.height = 4} plot(result, plots = "coef") ``` ```{r regress_dashboard, fig.width = 5, fig.height = 7} 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. ```{r hclus, fig.width = 4, fig.height = 5} ## 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) ``` ```{r dendro, fig.width = 4, fig.height = 5} ## show the dendrogram with cutoff at 0.05 plot(result, plots = "dendro", cutoff = 0.05) ``` ```{r kclus, fig.width = 5, fig.height = 6} ## 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. ```{r child = "_footer.md"} ```