-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprogramming.html
671 lines (576 loc) · 29.4 KB
/
programming.html
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Programming with Radiant</title>
<script src="libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.5/css/united.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="libs/navigation-1.1/tabsets.js"></script>
<link href="libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="libs/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 50px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h2 {
padding-top: 55px;
margin-top: -55px;
}
.section h3 {
padding-top: 55px;
margin-top: -55px;
}
/* don't use link color in navbar */
.dropdown-menu>li>a {
display: inline;
color: black;
padding-top: 0;
padding-bottom: 0;
}
.dropdown-menu .divider {
margin-top: 2px;
margin-bottom: 2px;
}
img {
max-width: 85% !important;
}
/*
img.center {
display: block;
margin: 0 auto;
}
*/
</style>
<link rel="stylesheet" href="libs/font-awesome-4.1.0/css/font-awesome.min.css"/>
<link rel="shortcut icon" type="image/png" href="/docs/images/radiant_red.png"/>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="https://github.com/radiant-rstats/docs">Radiant</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/docs/index.html">Home</a></li>
<li><a href="/docs/news.html">News</a></li>
<li><a href="/docs/install.html">Install</a></li>
<li class="dropdown">
<a href="/docs/data/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Data<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="/docs/data/manage.html">Manage</a></li>
<li><a href="/docs/data/view.html">View</a></li>
<li><a href="/docs/data/visualize.html">Visualize</a></li>
<li><a href="/docs/data/pivotr.html">Pivot</a></li>
<li><a href="/docs/data/explore.html">Explore</a></li>
<li><a href="/docs/data/transform.html">Transform</a></li>
<li><a href="/docs/data/combine.html">Combine</a></li>
</ul>
</li>
<li class="dropdown">
<a href="/docs/design/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Design<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">DOE</li>
<li><a href="/docs/design/doe.html">Design of Experiments (DOE)</a></li>
<li class="divider"></li>
<li class="dropdown-header">Sample</li>
<li><a href="/docs/design/sampling.html">Random sampling</a></li>
<li><a href="/docs/design/sample_size.html">Sample size (single)</a></li>
<li><a href="/docs/design/sample_size_comp.html">Sample size (compare)</a></li>
</ul>
</li>
<li class="dropdown">
<a href="/docs/basics/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Basics<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Probability</li>
<li><a href="/docs/basics/prob_calc.html">Probability calculator</a></li>
<li><a href="/docs/basics/clt.html">Central Limit Theorem</a></li>
<li class="divider"></li>
<li class="dropdown-header">Means</li>
<li><a href="/docs/basics/single_mean.html">Single mean</a></li>
<li><a href="/docs/basics/compare_means.html">Compare means</a></li>
<li class="divider"></li>
<li class="dropdown-header">Proportions</li>
<li><a href="/docs/basics/single_prop.html">Single proportion</a></li>
<li><a href="/docs/basics/compare_props.html">Compare proportions</a></li>
<li class="divider"></li>
<li class="dropdown-header">Tables</li>
<li><a href="/docs/basics/goodness.html">Goodness of fit</a></li>
<li><a href="/docs/basics/cross_tabs.html">Cross-tabs</a></li>
<li><a href="/docs/basics/correlation.html">Correlation</a></li>
</ul>
</li>
<li class="dropdown">
<a href="/docs/model/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Model<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Estimate</li>
<li><a href="/docs/model/regress.html">Linear regression (OLS)</a></li>
<li><a href="/docs/model/logistic.html">Logistic regression (GLM)</a></li>
<li><a href="/docs/model/nn.html">Neural Network</a></li>
<li><a href="/docs/model/nb.html">Naive Bayes</a></li>
<li><a href="/docs/model/crtree.html">Classification and regression trees</a></li>
<li class="divider"></li>
<li class="dropdown-header">Evaluate</li>
<li><a href="/docs/model/evalreg.html">Evaluate regression</a></li>
<li><a href="/docs/model/evalbin.html">Evaluate classification</a></li>
<li class="divider"></li>
<li class="dropdown-header">Recommend</li>
<li><a href="/docs/model/crs.html">Collaborative Filtering</a></li>
<li class="divider"></li>
<li class="dropdown-header">Decide</li>
<li><a href="/docs/model/dtree.html">Decision analysis</a></li>
<li><a href="/docs/model/simulater.html">Simulate</a></li>
</ul>
</li>
<li class="dropdown">
<a href="/docs/multivariate/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Multivariate<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Maps</li>
<li><a href="/docs/multivariate/mds.html">(Dis)similarity</a></li>
<li><a href="/docs/multivariate/prmap.html">Attributes</a></li>
<li class="divider"></li>
<li class="dropdown-header">Factor</li>
<li><a href="/docs/multivariate/pre_factor.html">Pre-factor</a></li>
<li><a href="/docs/multivariate/full_factor.html">Factor</a></li>
<li class="divider"></li>
<li class="dropdown-header">Cluster</li>
<li><a href="/docs/multivariate/hclus.html">Hierarchical</a></li>
<li><a href="/docs/multivariate/kclus.html">K-clustering</a></li>
<li class="divider"></li>
<li class="dropdown-header">Conjoint</li>
<li><a href="/docs/multivariate/conjoint.html">Conjoint</a></li>
</ul>
</li>
<li class="dropdown">
<a href="/docs/R/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">R<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="/docs/data/report_rmd.html">Report > Rmd</a></li>
<li><a href="/docs/data/report_r.html">Report > R</a></li>
<li><a href="/docs/programming.html">Programming</a></li>
<li><a href="/docs/resources.html">Resources</a></li>
</ul>
</li>
<li><a href="/docs/tutorials.html">Tutorials</a></li>
<li><a href="/docs/about.html">About</a></li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Programming with Radiant</h1>
</div>
<p>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 <a href="http://rmarkdown.rstudio.com/">Rmarkdown</a> 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 <em>Report > Rmd</em> (or <em>Report > R</em>) tab. As an alternative to clicking the <i title='Report results' class='fa fa-edit'></i> icon you can also press <code>ALT-enter</code> on your keyboard. Click the <code>Knit report</code> button on the <em>Report > Rmd</em> page to render the report to HTML or press the <code>Save report</code> button to produce a Notebook, HTML, PDF, Word, or Rmarkdown file. Radiant’s function manuals can be viewed using the commands below:</p>
<pre class="r"><code>help(package = radiant.data)
help(package = radiant.design)
help(package = radiant.basics)
help(package = radiant.model)
help(package = radiant.multivariate)</code></pre>
<p>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.</p>
<pre class="r"><code>library(radiant)
data(diamonds)
result <- single_mean(diamonds, "price")
summary(result)</code></pre>
<pre><code>Single mean test
Data : diamonds
Variable : price
Confidence: 0.95
Null hyp. : the mean of price = 0
Alt. hyp. : the mean of price is not equal to 0
mean sd n n_missing
3,907.186 3,956.915 3,000 0
diff se t.value p.value df 2.5% 97.5%
3907.186 72.243 54.084 < .001 2999 3765.535 4048.837 ***
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<pre class="r"><code>plot(result)</code></pre>
<p><img src="programming_files/figure-html/unnamed-chunk-1-1.png" width="1008" /></p>
<p>You can also call functions for visualization (see below) and access help from the console using <code>?visualize</code></p>
<pre class="r"><code>visualize(
diamonds,
xvar = "carat",
yvar = "price",
type = "scatter",
facet_row = "clarity",
color = "clarity",
custom = TRUE
) +
labs(title = "Diamond Prices ($)")</code></pre>
<p><img src="programming_files/figure-html/scatter-1.png" width="720" /></p>
<p>As an example, you can render the <a href="https://radiant-rstats.github.io/docs/examples/single_mean.Rmd"><code>single_mean.Rmd</code></a> file into html (or PDF or Word if you prefer) in Rstudio. Try the code in <a href="https://radiant-rstats.github.io/docs/examples/radiant_rcode.R"><code>radiant_rcode.R</code></a> for a more extensive example.</p>
<p>To install Radiant with complete documentation for offline access, open R(studio) and copy-and-paste the commands below:</p>
<pre class="r"><code>install.packages("radiant", repos = "https://radiant-rstats.github.io/minicran/")
library(radiant)</code></pre>
<p>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 <code>help(package = "radiant.model")</code> command</p>
<p>Lets start by comparing the mean of a variable to a (population) value using R’s built-in <code>mtcars</code> dataset. This functionality is in the Radiant menu <em>Basics > Means > Single mean</em>. The analysis is conducted in function <code>single_mean</code>. Calling the <code>summary</code> method on the result object will show tabular output. Calling <code>plot</code> on the same result object will produce relevant plots.</p>
<pre class="r"><code>result <- single_mean(
mtcars,
var = "mpg",
comp_value = 20,
alternative = "greater"
)
summary(result)</code></pre>
<pre><code>Single mean test
Data : mtcars
Variable : mpg
Confidence: 0.95
Null hyp. : the mean of mpg = 20
Alt. hyp. : the mean of mpg is > 20
mean sd n n_missing
20.091 6.027 32 0
diff se t.value p.value df 5% 100%
0.091 1.065 0.085 0.466 31 18.284 Inf
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<pre class="r"><code>plot(result, plots = c("hist", "simulate"))</code></pre>
<p><img src="programming_files/figure-html/single_mean_mpg-1.png" width="720" /></p>
<p>To compare the mean price of diamonds across different levels of clarity we can call the <code>compare_means</code> function:</p>
<pre class="r"><code>result <- compare_means(
diamonds,
var1 = "clarity",
var2 = "price",
adjust = "bonf"
)
summary(result)</code></pre>
<pre><code>Pairwise mean comparisons (t-test)
Data : diamonds
Variables : clarity, price
Samples : independent
Confidence: 0.95
Adjustment: Bonferroni
clarity mean n sd se ci
I1 4,194.775 40 2,519.678 398.396 805.832
SI2 5,100.189 529 4,406.982 191.608 376.407
SI1 3,998.577 721 3,813.190 142.011 278.804
VS2 3,822.967 661 3,917.484 152.372 299.193
VS1 3,789.181 442 3,880.907 184.596 362.797
VVS2 3,337.820 284 3,932.706 233.363 459.348
VVS1 2,608.460 224 3,201.282 213.895 421.513
IF 2,411.697 99 3,330.560 334.734 664.268
Null hyp. Alt. hyp. diff p.value
I1 = SI2 I1 not equal to SI2 -905.414 1
I1 = SI1 I1 not equal to SI1 196.198 1
I1 = VS2 I1 not equal to VS2 371.808 1
I1 = VS1 I1 not equal to VS1 405.594 1
I1 = VVS2 I1 not equal to VVS2 856.955 1
I1 = VVS1 I1 not equal to VVS1 1586.315 0.023
I1 = IF I1 not equal to IF 1783.078 0.025
SI2 = SI1 SI2 not equal to SI1 1101.612 < .001
SI2 = VS2 SI2 not equal to VS2 1277.222 < .001
SI2 = VS1 SI2 not equal to VS1 1311.008 < .001
SI2 = VVS2 SI2 not equal to VVS2 1762.369 < .001
SI2 = VVS1 SI2 not equal to VVS1 2491.729 < .001
SI2 = IF SI2 not equal to IF 2688.492 < .001
SI1 = VS2 SI1 not equal to VS2 175.610 1
SI1 = VS1 SI1 not equal to VS1 209.396 1
SI1 = VVS2 SI1 not equal to VVS2 660.757 0.446
SI1 = VVS1 SI1 not equal to VVS1 1390.117 < .001
SI1 = IF SI1 not equal to IF 1586.880 0.001
VS2 = VS1 VS2 not equal to VS1 33.786 1
VS2 = VVS2 VS2 not equal to VVS2 485.146 1
VS2 = VVS1 VS2 not equal to VVS1 1214.507 < .001
VS2 = IF VS2 not equal to IF 1411.270 0.005
VS1 = VVS2 VS1 not equal to VVS2 451.361 1
VS1 = VVS1 VS1 not equal to VVS1 1180.721 0.001
VS1 = IF VS1 not equal to IF 1377.484 0.012
VVS2 = VVS1 VVS2 not equal to VVS1 729.361 0.606
VVS2 = IF VVS2 not equal to IF 926.123 0.68
VVS1 = IF VVS1 not equal to IF 196.763 1
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<pre class="r"><code>plot(result, plots = c("bar", "density"))</code></pre>
<p><img src="programming_files/figure-html/compare_means_diamonds-1.png" width="576" /></p>
<p>To get help for the <code>single_mean</code> and <code>compare_means</code> functions use <code>?single_mean</code> and <code>?compare_means</code>. These help files also have links to information about the <code>summary</code> and <code>plot</code> methods for each class. See also <code>?summary.single_mean</code> and <code>?plot.compare_means</code></p>
<p>These datasets are available after loading the radiant library by using the <code>data</code> 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 <code>Examples</code> from the <code>Load data of type</code> dropdown in the <em>Data > Manage</em> 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 <code>Stop</code>. The datasets loaded through the web-interface are now available in the <code>r_data</code> environment as well. To use them directly in your code use <code>attach(r_data)</code>.</p>
<pre class="r"><code>## start radiant in Rstudio, load the example data, then click the power icon and Stop
radiant::radiant()</code></pre>
<p>Because we already loaded the radiant library we already have access to all the data we need here. Lets use the <code>compare_means</code> function to evaluate salary data for professors of different ranks using:</p>
<pre class="r"><code>result <- compare_means(salary, var1 = "rank", var2 = "salary")
summary(result)</code></pre>
<pre><code>Pairwise mean comparisons (t-test)
Data : salary
Variables : rank, salary
Samples : independent
Confidence: 0.95
Adjustment: None
rank mean n sd se ci
AsstProf 80,775.985 67 8,174.113 998.627 1,993.823
AssocProf 93,876.438 64 13,831.700 1,728.962 3,455.056
Prof 126,772.109 266 27,718.675 1,699.541 3,346.322
Null hyp. Alt. hyp. diff
AsstProf = AssocProf AsstProf not equal to AssocProf -13100.45
AsstProf = Prof AsstProf not equal to Prof -45996.12
AssocProf = Prof AssocProf not equal to Prof -32895.67
p.value
< .001
< .001
< .001
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<pre class="r"><code>plot(result)</code></pre>
<p><img src="programming_files/figure-html/compare_means_salary-1.png" width="576" /></p>
<p>An alternative way to write this code is to use <code>piping</code> (see <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/introduction.html">dplyr</a> and <a href="https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html">magrittr</a> vignettes):</p>
<pre class="r"><code>salary %>%
compare_means("rank", "salary") %>%
{summary(.); plot(.)}</code></pre>
<pre><code>Pairwise mean comparisons (t-test)
Data : .
Variables : rank, salary
Samples : independent
Confidence: 0.95
Adjustment: None
rank mean n sd se ci
AsstProf 80,775.985 67 8,174.113 998.627 1,993.823
AssocProf 93,876.438 64 13,831.700 1,728.962 3,455.056
Prof 126,772.109 266 27,718.675 1,699.541 3,346.322
Null hyp. Alt. hyp. diff
AsstProf = AssocProf AsstProf not equal to AssocProf -13100.45
AsstProf = Prof AsstProf not equal to Prof -45996.12
AssocProf = Prof AssocProf not equal to Prof -32895.67
p.value
< .001
< .001
< .001
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
<p><img src="programming_files/figure-html/compare_means_salary_piped-1.png" width="576" /></p>
<p>We can also run regressions and get output in a format that would require many lines of code to produce from scratch:</p>
<pre class="r"><code>result <- regress(diamonds, rvar = "price", evar = c("carat", "clarity"))
summary(result, sum_check = "confint")</code></pre>
<pre><code>Linear regression (OLS)
Data : diamonds
Response variable : price
Explanatory variables: carat, clarity
Null hyp.: the effect of x on price is zero
Alt. hyp.: the effect of x on price is not zero
coefficient std.error t.value p.value
(Intercept) -6780.993 204.952 -33.086 < .001 ***
carat 8438.030 51.101 165.125 < .001 ***
clarity|SI2 2790.760 201.395 13.857 < .001 ***
clarity|SI1 3608.531 200.508 17.997 < .001 ***
clarity|VS2 4249.906 201.607 21.080 < .001 ***
clarity|VS1 4461.956 204.592 21.809 < .001 ***
clarity|VVS2 5109.476 210.207 24.307 < .001 ***
clarity|VVS1 5027.669 214.251 23.466 < .001 ***
clarity|IF 5265.170 233.658 22.534 < .001 ***
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
R-squared: 0.904, Adjusted R-squared: 0.904
F-statistic: 3530.024 df(8,2991), p.value < .001
Nr obs: 3,000
coefficient 2.5% 97.5% +/-
(Intercept) -6780.993 -7182.855 -6379.131 401.862
carat 8438.030 8337.834 8538.227 100.196
clarity|SI2 2790.760 2395.873 3185.646 394.886
clarity|SI1 3608.531 3215.384 4001.679 393.148
clarity|VS2 4249.906 3854.604 4645.208 395.302
clarity|VS1 4461.956 4060.801 4863.111 401.155
clarity|VVS2 5109.476 4697.311 5521.640 412.165
clarity|VVS1 5027.669 4607.574 5447.764 420.095
clarity|IF 5265.170 4807.024 5723.317 458.147</code></pre>
<pre class="r"><code>pred <- predict(result, pred_cmd = "carat = 1:10")
print(pred, n = 10)</code></pre>
<pre><code>Linear regression (OLS)
Data : diamonds
Response variable : price
Explanatory variables: carat, clarity
Interval : confidence
Prediction command : carat = 1:10
clarity carat Prediction 2.5% 97.5% +/-
SI1 1 5265.569 5174.776 5356.362 90.793
SI1 2 13703.599 13557.662 13849.536 145.937
SI1 3 22141.629 21908.326 22374.933 233.303
SI1 4 30579.660 30251.571 30907.748 328.088
SI1 5 39017.690 38592.329 39443.051 425.361
SI1 6 47455.720 46931.983 47979.458 523.738
SI1 7 55893.751 55271.056 56516.445 622.695
SI1 8 64331.781 63609.787 65053.775 721.994
SI1 9 72769.811 71948.301 73591.322 821.511
SI1 10 81207.842 80286.667 82129.017 921.175</code></pre>
<pre class="r"><code>plot(result, plots = "coef")</code></pre>
<p><img src="programming_files/figure-html/regress_coeff-1.png" width="864" /></p>
<pre class="r"><code>plot(result, plots = "dashboard", lines = "line")</code></pre>
<p><img src="programming_files/figure-html/regress_dashboard-1.png" width="720" /></p>
<p>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 <code>?hclus</code>, <code>?plot.hclus</code>, and <code>?klus</code>. See also the Radiant function manuals linked above.</p>
<pre class="r"><code>## 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)</code></pre>
<pre><code>Hierarchical cluster analysis
Data : shopping
Variables : v1:v6
Method : ward.D
Distance : sq.euclidian
Observations: 20 </code></pre>
<pre class="r"><code>## 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)</code></pre>
<p><img src="programming_files/figure-html/hclus-1.png" width="576" /></p>
<pre class="r"><code>## show the dendrogram with cutoff at 0.05
plot(result, plots = "dendro", cutoff = 0.05)</code></pre>
<p><img src="programming_files/figure-html/dendro-1.png" width="576" /></p>
<pre class="r"><code>## 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)</code></pre>
<pre><code>K-means cluster analysis
Data : shopping
Variables : v1:v6
Clustering by: K-means
HC method : ward.D
HC distance : sq.euclidian
Observations : 20
Generated : 3 clusters of sizes 8 | 6 | 6
Cluster means:
v1 v2 v3 v4 v5 v6
Cluster 1 5.75 3.62 6.00 3.12 1.88 3.88
Cluster 2 1.67 3.00 1.83 3.50 5.50 3.33
Cluster 3 3.50 5.83 3.33 6.00 3.50 6.00
Percentage of within cluster heterogeneity accounted for by each cluster:
Cluster 1 40.31%
Cluster 2 25.98%
Cluster 3 33.71%
Between cluster heterogeneity accounts for 73.93% of the
total heterogeneity in the data (higher is better).</code></pre>
<pre class="r"><code>plot(result, plots = c("density","bar"))</code></pre>
<p><img src="programming_files/figure-html/kclus-1.png" width="720" /></p>
<pre class="r"><code>shopping <- store(shopping, result, name = "clus")
## was the data really changed?
head(shopping)</code></pre>
<pre><code> id v1 v2 v3 v4 v5 v6 clus
1 1 6 4 7 3 2 3 1
2 2 2 3 1 4 5 4 2
3 3 7 2 6 4 1 3 1
4 4 4 6 4 5 3 6 3
5 5 1 3 2 2 6 4 2
6 6 6 4 6 3 3 4 1</code></pre>
<p>See if you can reproduce this output in the radiant web-interface. Start <code>Radiant</code> from the <code>Addins</code> dropdown in Rstudio. You can also run code inside the Radiant app in the <em>Report > R</em> tab. See <a href="https://radiant-rstats.github.io/docs/data/code.html" target="_blank">Code</a> page for details.</p>
© Vincent Nijs (2018) <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank"><img alt="Creative Commons License" style="border-width: 0" src="/docs/images/by-nc-sa.png"/></a>
<!-- some extra javascript for older browsers -->
<script type="text/javascript" src="libs/polyfill.js"></script>
<!-- mathjax -->
<!-- <script type='text/javascript' src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> -->
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
$('a[href="' + href + '"]').parent().addClass('active');
// manage active menu header
if (href.startsWith('authoring_'))
$('a[href="' + 'authoring' + '"]').parent().addClass('active');
else if (href.endsWith('_format.html'))
$('a[href="' + 'formats' + '"]').parent().addClass('active');
else if (href.startsWith('developer_'))
$('a[href="' + 'developer' + '"]').parent().addClass('active');
});
// Google analytics
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-61296577-2', 'auto');
ga('send', 'pageview');
</script>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>