-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rCh10.html
124 lines (108 loc) · 6.27 KB
/
rCh10.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
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(<span class="dt">list=</span><span class="kw">ls</span>())
<span class="kw">source</span>(<span class="st">'runDir.R'</span>)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">'ggplot2'</span>)
<span class="kw">runDir</span>(<span class="st">'../CodeExamples/c10_Documentation_and_deployment'</span>,
<span class="st">'../Buzz'</span>,<span class="dt">last=</span><span class="dv">221</span>)</code></pre></div>
<pre><code>[1] "############################### start 204 Fri Jun 17 10:43:36 2016"
[1] "##### running /Users/johnmount/Documents/work/PracticalDataScienceWithR/zmPDSwR/RunExamples/replacements/00204_informalexample_10.1_of_section_10.2.1.R"
[1] "##### in directory ../Buzz"
> # can't "knit" inside a knitr document
[1] "############################### end 204 Fri Jun 17 10:43:36 2016"
[1] "############################### start 211 Fri Jun 17 10:43:36 2016"
[1] "##### running ../CodeExamples/c10_Documentation_and_deployment/00211_example_10.7_of_section_10.3.1.R"
[1] "##### in directory ../Buzz"
> # example 10.7 of section 10.3.1
> # (example 10.7 of section 10.3.1) : Documentation and deployment : Using comments and version control for running documentation : Writing effective comments
> # Title: Example code comment
>
> # Return the pseudo logarithm of x, which is close to
> # sign(x)*log10(abs(x)) for x such that abs(x) is large
> # and doesn't "blow up" near zero. Useful
> # for transforming wide-range variables that may be negative
> # (like profit/loss).
> # See: http://www.win-vector.com/blog
> # /2012/03/modeling-trick-the-signed-pseudo-logarithm/
> # NB: This transform has the undesirable property of making most
> # signed distributions appear bimodal around the origin, no matter
> # what the underlying distribution really looks like.
> # The argument x is assumed be numeric and can be a vector.
> pseudoLog10 <- function(x) { asinh(x/2)/log(10) }
[1] "############################### end 211 Fri Jun 17 10:43:36 2016"
[1] "############################### start 212 Fri Jun 17 10:43:36 2016"
[1] "##### running ../CodeExamples/c10_Documentation_and_deployment/00212_example_10.8_of_section_10.3.1.R"
[1] "##### in directory ../Buzz"
> # example 10.8 of section 10.3.1
> # (example 10.8 of section 10.3.1) : Documentation and deployment : Using comments and version control for running documentation : Writing effective comments
> # Title: Useless comment
>
> #######################################
> # Function: addone
> # Author: John Mount
> # Version: 1.3.11
> # Location: RSource/helperFns/addone.R
> # Date: 10/31/13
> # Arguments: x
> # Purpose: Adds one
> #######################################
> addone <- function(x) { x + 1 }
[1] "############################### end 212 Fri Jun 17 10:43:36 2016"
[1] "############################### start 213 Fri Jun 17 10:43:36 2016"
[1] "##### running ../CodeExamples/c10_Documentation_and_deployment/00213_example_10.9_of_section_10.3.1.R"
[1] "##### in directory ../Buzz"
> # example 10.9 of section 10.3.1
> # (example 10.9 of section 10.3.1) : Documentation and deployment : Using comments and version control for running documentation : Writing effective comments
> # Title: Worse than useless comment
>
> # adds one
> addtwo <- function(x) { x + 2 }
[1] "############################### end 213 Fri Jun 17 10:43:36 2016"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(<span class="dt">list=</span><span class="kw">ls</span>())
<span class="kw">source</span>(<span class="st">'runDir.R'</span>)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">'ggplot2'</span>)
<span class="kw">runDir</span>(<span class="st">'../CodeExamples/c10_Documentation_and_deployment'</span>,
<span class="st">'../Buzz'</span>,<span class="dt">first=</span><span class="dv">225</span>)</code></pre></div>
<pre><code>[1] "############################### start 225 Fri Jun 17 10:43:36 2016"
[1] "##### running ../CodeExamples/c10_Documentation_and_deployment/00225_example_10.18_of_section_10.4.2.R"
[1] "##### in directory ../Buzz"
> # example 10.18 of section 10.4.2
> # (example 10.18 of section 10.4.2) : Documentation and deployment : Deploying models : Deploying models by export
> # Title: Exporting the random forest model
>
> load('thRS500.Rdata') # Note: 1
> library(randomForest) # Note: 2
randomForest 4.6-12
Type rfNews() to see new features/changes/bug fixes.
Attaching package: 'randomForest'
The following object is masked from 'package:ggplot2':
margin
> extractTrees <- function(rfModel) { # Note: 3
ei <- function(i) {
ti <- getTree(rfModel,k=i,labelVar=T)
ti$nodeid <- 1:dim(ti)[[1]]
ti$treeid <- i
ti
}
nTrees <- rfModel$ntree
do.call('rbind',sapply(1:nTrees,ei,simplify=F))
}
> write.table(extractTrees(fmodel), # Note: 4
file='rfmodel.tsv',row.names=F,sep='\t',quote=F)
> # Note 1:
> # Load the saved buzz workspace (includes the
> # random forest model).
>
> # Note 2:
> # Load the random forest library (loading the
> # workspace doesn’t load the library).
>
> # Note 3:
> # Define a function that joins the tree tables
> # from the random forest getTree() method into one
> # large table of trees.
>
> # Note 4:
> # Write the table of trees as a tab-separated
> # values table (easy for other software to
> # read).
>
[1] "############################### end 225 Fri Jun 17 10:43:37 2016"</code></pre>