-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathprogramming.Rmd
179 lines (144 loc) · 8.11 KB
/
programming.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
title: Programming with Radiant
---
```{r setup, include = FALSE}
library(radiant)
options(
width = 250,
scipen = 100,
max.print = 5000,
stringsAsFactors = FALSE
)
knitr::opts_chunk$set(
echo = TRUE,
comment = NA,
cache = FALSE,
message = FALSE,
warning = FALSE,
dpi = 96
)
set.seed(1234)
```
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 <i title='Report results' class='fa fa-edit'></i> 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 <i title='Report results' class='fa fa-edit'></i> 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.
To review the functions used in radiant see the [pkgdown](https://github.com/r-lib/pkgdown) documentation sites:
* http://radiant-rstats.github.io/radiant
* http://radiant-rstats.github.io/radiant.data
* http://radiant-rstats.github.io/radiant.design
* http://radiant-rstats.github.io/radiant.basics
* http://radiant-rstats.github.io/radiant.model
* http://radiant-rstats.github.io/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}
library(radiant)
data(diamonds, package = "radiant.data", 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 = 6, fig.width = 5}
visualize(
diamonds,
xvar = "carat",
yvar = "price",
type = "scatter",
facet_row = "clarity",
color = "clarity",
labs = list(title = "Diamond Prices ($)"),
custom = FALSE
)
```
As an example, you can render the [`single_mean.Rmd`](https://radiant-rstats.github.io/docs/examples/single_mean.Rmd) file into html (or PDF or Word if you prefer) in Rstudio. Try the code in [`radiant_rcode.R`](https://radiant-rstats.github.io/docs/examples/radiant_rcode.R) for a more extensive example.
To install Radiant with complete documentation for offline access, open R(studio) and copy-and-paste the commands below:
```r
options(repos = c(RSM = "https://radiant-rstats.github.io/minicran", CRAN = "https://cloud.r-project.org"))
install.packages("radiant")
library(radiant)
```
This will install and load the library and the required packages. 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 = 6, fig.width = 5}
result <- single_mean(
mtcars,
var = "mpg",
comp_value = 20,
alternative = "greater"
)
summary(result)
plot(result, plots = c("hist", "simulate"))
```
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"))
```
To get help for the `single_mean` and `compare_means` functions use `?single_mean` and `?compare_means`. These help files also have links to information about the `summary` and `plot` methods for each class. See also `?summary.single_mean` and `?plot.compare_means`
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 <i title='Power off' class='fa fa-power-off'></i> 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 and 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)
```
An alternative way to write this code is to use `piping` (see [dplyr](https://cran.r-project.org/web/packages/dplyr/vignettes/introduction.html) and [magrittr](https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html) vignettes):
```{r compare_means_salary_piped, fig.height = 3, fig.width = 4}
salary %>%
compare_means("rank", "salary") %>%
{
summary(.)
plot(.)
}
```
We can also run regressions and get output in a format that would require many 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")
```
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 = 6}
## 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"))
```
```{r}
shopping <- store(shopping, result, name = "clus")
## was the data really changed?
shopping
```
See if you can reproduce this output in the radiant web-interface. Start `Radiant` from the `Addins` dropdown in Rstudio. You can also run code inside the Radiant app in the _Report > R_ tab. See <a href="https://radiant-rstats.github.io/docs/data/code.html" target="_blank">Code</a> page for details.