galaaz 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +32 -0
- data/Rakefile +177 -0
- data/bin/galaaz +8 -0
- data/examples/50Plots_MasterList/scatter_plot.rb +51 -0
- data/examples/baseball.csv +1 -0
- data/examples/baseball.rb +16 -0
- data/examples/ggplot.rb +178 -0
- data/examples/islr/Figure.jpg +0 -0
- data/examples/islr/all.rb +32 -0
- data/examples/islr/ch2.spec.rb +148 -0
- data/examples/islr/ch3.spec.rb +28 -0
- data/examples/islr/ch3_boston.rb +77 -0
- data/examples/islr/ch3_multiple_regression.rb +36 -0
- data/examples/islr/ch6.spec.rb +64 -0
- data/examples/paper/paper.rb +36 -0
- data/examples/sthda_ggplot/README.md +38 -0
- data/examples/sthda_ggplot/all.rb +68 -0
- data/examples/sthda_ggplot/one_variable_continuous/density_gg.rb +52 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_area.rb +61 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_density.rb +77 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_dotplot.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_freqpoly.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_histogram.rb +62 -0
- data/examples/sthda_ggplot/one_variable_continuous/histogram_density.rb +55 -0
- data/examples/sthda_ggplot/one_variable_continuous/stat.rb +62 -0
- data/examples/sthda_ggplot/one_variable_discrete/bar.rb +54 -0
- data/examples/sthda_ggplot/qplots/box_violin_dot.rb +57 -0
- data/examples/sthda_ggplot/qplots/scatter_plots.rb +67 -0
- data/examples/sthda_ggplot/scatter_gg.rb +60 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_bin2d.rb +49 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_density2d.rb +64 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +52 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_point.rb +65 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_smooth.rb +66 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +83 -0
- data/examples/sthda_ggplot/two_variables_cont_function/geom_area.rb +63 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +85 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_boxplot.rb +62 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_dotplot.rb +75 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_jitter.rb +74 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_line.rb +55 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_violin.rb +70 -0
- data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +40 -0
- data/examples/sthda_ggplot/two_variables_error/geom_crossbar.rb +108 -0
- data/examples/subsetting.rb +372 -0
- data/lib/expression.rb +45 -0
- data/lib/galaaz.rb +27 -0
- data/lib/r.rb +118 -0
- data/lib/r_methods.rb +89 -0
- data/lib/rbinary_operators.rb +226 -0
- data/lib/rclosure.rb +34 -0
- data/lib/rdata_frame.rb +63 -0
- data/lib/renvironment.rb +34 -0
- data/lib/rexpression.rb +34 -0
- data/lib/rindexed_object.rb +68 -0
- data/lib/rlanguage.rb +64 -0
- data/lib/rlist.rb +72 -0
- data/lib/rmatrix.rb +38 -0
- data/lib/rmd_indexed_object.rb +43 -0
- data/lib/robject.rb +297 -0
- data/lib/rpkg.rb +53 -0
- data/lib/rsupport.rb +292 -0
- data/lib/rsupport_scope.rb +77 -0
- data/lib/rsymbol.rb +57 -0
- data/lib/ruby_callback.rb +83 -0
- data/lib/ruby_extensions.rb +74 -0
- data/lib/runary_operators.rb +58 -0
- data/lib/rvector.rb +117 -0
- data/r_requires/ggplot.rb +31 -0
- data/specs/all.rb +45 -0
- data/specs/r_dataframe.spec.rb +181 -0
- data/specs/r_eval.spec.rb +164 -0
- data/specs/r_function.spec.rb +105 -0
- data/specs/r_language.spec.rb +135 -0
- data/specs/r_list.spec.rb +129 -0
- data/specs/r_list_apply.spec.rb +99 -0
- data/specs/r_matrix.spec.rb +83 -0
- data/specs/r_vector_creation.spec.rb +99 -0
- data/specs/r_vector_functions.spec.rb +59 -0
- data/specs/r_vector_object.spec.rb +94 -0
- data/specs/r_vector_operators.spec.rb +174 -0
- data/specs/r_vector_subsetting.spec.rb +136 -0
- data/specs/tmp.rb +134 -0
- data/version.rb +2 -0
- metadata +198 -0
Binary file
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
# load ISLR and MASS Libraries
|
28
|
+
R.install_and_loads('ISLR', 'MASS')
|
29
|
+
|
30
|
+
require_relative 'ch2.spec'
|
31
|
+
require_relative 'ch3.spec'
|
32
|
+
require_relative 'ch6.spec'
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
context "ISLR" do
|
28
|
+
|
29
|
+
context "Chapter 2 - Basic Commands" do
|
30
|
+
|
31
|
+
it "Creates a vector" do
|
32
|
+
x = R.c(1, 3, 2, 5)
|
33
|
+
expect(x[1] == 1).to eq true
|
34
|
+
expect(x[3] == 2).to eq true
|
35
|
+
expect(x.length == 4).to eq true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should add vectors" do
|
39
|
+
x = R.c(1, 6, 2)
|
40
|
+
y = R.c(1, 4, 3)
|
41
|
+
expect(x.length == 3).to eq true
|
42
|
+
z = x + y
|
43
|
+
expect(z[1] == 2).to eq true
|
44
|
+
expect(z[3] == 5).to eq true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should create matrix by column" do
|
48
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2)
|
49
|
+
expect(x.rclass == "matrix").to eq true
|
50
|
+
expect(x[1, 1] == 1).to eq true
|
51
|
+
expect(x[1, 2] == 3).to eq true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should create matrix by row" do
|
55
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2, byrow: true)
|
56
|
+
expect(x.rclass == "matrix").to eq true
|
57
|
+
expect(x[1, 1] == 1).to eq true
|
58
|
+
expect(x[1, 2] == 2).to eq true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should apply a function to all elements of the matrix: ex: sqrt" do
|
62
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2, byrow: true)
|
63
|
+
z = x.sqrt
|
64
|
+
expect(z[1, 2] == 1.73).to eq true
|
65
|
+
expect(z[2, 1] == 1.41).to eq true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should generate a vector of random normal variables using rnorm" do
|
69
|
+
x = R.rnorm(50)
|
70
|
+
y = x + R.rnorm(50, mean: 40, sd: 0.1)
|
71
|
+
expect(R.cor(x, y) == 0.995).to eq true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should allow to setting the seed" do
|
75
|
+
R.set__seed(1303)
|
76
|
+
x = R.rnorm(50)
|
77
|
+
expect(x[1] == -1.1440).to eq true
|
78
|
+
expect(x[2] == 1.3421).to eq true
|
79
|
+
expect(x[4] == 0.5364).to eq true
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should calculate the mean" do
|
83
|
+
R.set__seed(3)
|
84
|
+
y = R.rnorm(100)
|
85
|
+
expect(y.mean == 0.0110).to eq true
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should calculate the variance" do
|
89
|
+
R.set__seed(3)
|
90
|
+
y = R.rnorm(100)
|
91
|
+
expect(y.var == 0.7329).to eq true
|
92
|
+
expect(y.var.sqrt == 0.8561).to eq true
|
93
|
+
expect(y.sd == 0.8561).to eq true
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
context "Chapter 2 - Graphics" do
|
99
|
+
|
100
|
+
it "should plot graphics" do
|
101
|
+
# To see the graphic we need to set the device to awt
|
102
|
+
R.awt
|
103
|
+
x = R.rnorm(100)
|
104
|
+
y = R.rnorm(100)
|
105
|
+
# plot commands do not work. Need to work with ggplot or grid
|
106
|
+
# qplot uses a similar interface as plot
|
107
|
+
# Need to call print at the end of the plot
|
108
|
+
R.qplot(x, y,
|
109
|
+
xlab: "this is the x-axis",
|
110
|
+
ylab: "this is the y-axis",
|
111
|
+
main: "Plot of X vs Y")
|
112
|
+
.print
|
113
|
+
# the graphics dies when the script ends... waiting 3 secs
|
114
|
+
# so that the graphic can be seen
|
115
|
+
sleep(3)
|
116
|
+
end
|
117
|
+
|
118
|
+
# does not print anything... open issue!
|
119
|
+
it "should create a jpeg file" do
|
120
|
+
R.jpeg("/home/rbotafogo/desenv/galaaz/examples/islr/Figure.jpg")
|
121
|
+
x = R.rnorm(100)
|
122
|
+
y = R.rnorm(100)
|
123
|
+
plot = R.qplot(x, y, col: "green")
|
124
|
+
R.dev__off
|
125
|
+
end
|
126
|
+
|
127
|
+
it "creates sequences with 'seq'" do
|
128
|
+
x = R.seq(1, 10)
|
129
|
+
expect(x[5] == 5).to eq true
|
130
|
+
y = R.seq(-~:pi, ~:pi, length: 50)
|
131
|
+
expect(y[1] == -3.14159265).to eq true
|
132
|
+
expect(y[7] == -2.37222302).to eq true
|
133
|
+
expect(y[26] == 0.06411414).to eq true
|
134
|
+
end
|
135
|
+
=begin
|
136
|
+
it "creates contour plots with ggplot" do
|
137
|
+
x = y = R.seq(-R.pi, R.pi, length: 50)
|
138
|
+
df = R.data__frame(x: x, y: y)
|
139
|
+
f = E.outer(:x, :y, lambda { |x, y| R.cos(y) / (1 + x**2) })
|
140
|
+
|
141
|
+
R.awt
|
142
|
+
print R.ggplot(df, E.aes(:x, :y, z: f))
|
143
|
+
end
|
144
|
+
=end
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
require_relative 'ch3_boston'
|
28
|
+
require_relative 'ch3_multiple_regression'
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
##########################################################################################
|
3
|
+
# @author Rodrigo Botafogo
|
4
|
+
#
|
5
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
6
|
+
# and distribute this software and its documentation, without fee and without a signed
|
7
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
8
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
9
|
+
# distributions.
|
10
|
+
#
|
11
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
12
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
13
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
14
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
15
|
+
#
|
16
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
17
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
18
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
19
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
20
|
+
# OR MODIFICATIONS.
|
21
|
+
##########################################################################################
|
22
|
+
|
23
|
+
require 'galaaz'
|
24
|
+
require 'ggplot'
|
25
|
+
|
26
|
+
R.install_and_loads('ISLR', 'MASS')
|
27
|
+
|
28
|
+
# Simple linear regression from ISLR book. Chapter 3 Lab
|
29
|
+
|
30
|
+
# load boston data frame on variable boston
|
31
|
+
boston = ~:Boston
|
32
|
+
|
33
|
+
puts boston.names
|
34
|
+
|
35
|
+
boston_lm = R.lm(+:medv =~ +:lstat, data: :Boston)
|
36
|
+
# puts boston_lm.str
|
37
|
+
# puts boston_lm.summary
|
38
|
+
puts boston_lm.names
|
39
|
+
puts boston_lm.coef
|
40
|
+
puts boston_lm.confint
|
41
|
+
conf = R.predict(boston_lm, R.data__frame(lstat: (R.c(5, 10, 15))), interval: "confidence")
|
42
|
+
puts conf
|
43
|
+
pred = R.predict(boston_lm, R.data__frame(lstat: (R.c(5, 10, 15))), interval: "prediction")
|
44
|
+
puts pred
|
45
|
+
|
46
|
+
R.awt
|
47
|
+
|
48
|
+
puts R.qplot(boston.lstat, boston.medv, col: "red") +
|
49
|
+
R.geom_abline(intercept: boston_lm.coef[1],
|
50
|
+
slope: boston_lm.coef[2],
|
51
|
+
color: "blue",
|
52
|
+
linetype: "dashed",
|
53
|
+
size: 1.5)
|
54
|
+
|
55
|
+
# uncomment if needed to pause while analysing graph. reads input from user
|
56
|
+
# a = gets.chomp
|
57
|
+
|
58
|
+
# sleep two seconds so that the graph shows up
|
59
|
+
sleep(2)
|
60
|
+
R.grid__newpage
|
61
|
+
|
62
|
+
puts R.qplot(R.predict(boston_lm), R.residuals(boston_lm))
|
63
|
+
|
64
|
+
sleep(2)
|
65
|
+
R.grid__newpage
|
66
|
+
|
67
|
+
puts R.qplot(R.predict(boston_lm), R.rstudent(boston_lm))
|
68
|
+
|
69
|
+
sleep(2)
|
70
|
+
R.grid__newpage
|
71
|
+
|
72
|
+
vals = R.hatvalues(boston_lm)
|
73
|
+
# method size returns a Numeric... size is equivalent to 'length << 0'
|
74
|
+
puts R.qplot((1..vals.size), vals)
|
75
|
+
|
76
|
+
sleep(2)
|
77
|
+
R.grid__newpage
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
R.install_and_loads('ISLR', 'MASS')
|
28
|
+
|
29
|
+
# Multiple linear regression from ISLR book. Chapter 3 Lab, pg 113
|
30
|
+
lm_fit = R.lm(+:medv =~ +:lstat + +:age, data: :Boston)
|
31
|
+
puts lm_fit.summary
|
32
|
+
|
33
|
+
# Non-linear Transformations of the Predictors
|
34
|
+
# Creating a more complex formula requires the use of R.formula
|
35
|
+
lm_fit5 = R.lm(R.formula("medv ~ poly(lstat, 5)"), data: :Boston)
|
36
|
+
puts lm_fit5.summary
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
|
26
|
+
R.install_and_loads('ISLR', 'MASS')
|
27
|
+
|
28
|
+
context "ISLR" do
|
29
|
+
|
30
|
+
context "Chapter 6 - Subset Selection Methods - page 244" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
@hitters = ~:Hitters
|
34
|
+
end
|
35
|
+
|
36
|
+
it "Should access the names of the dataset" do
|
37
|
+
expect(@hitters.names[1] == "AtBat").to eq true
|
38
|
+
expect(@hitters.names[5] == "RBI").to eq true
|
39
|
+
expect(@hitters.names[11] == "CRuns").to eq true
|
40
|
+
expect(@hitters.dim == R.c(322, 20)).to eq true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "Should count na's using 'R.sum'" do
|
44
|
+
expect(R.sum(@hitters.Salary.is__na) == 59).to eq true
|
45
|
+
expect(R.sum(@hitters.Salary.is__na)).to eq 59
|
46
|
+
end
|
47
|
+
|
48
|
+
it "Should count na's using Ruby Enumerable 'sum'" do
|
49
|
+
# method 'sum' is a Ruby Enumerable method. To count na's we can loop through
|
50
|
+
# every element and check if they are na or not. Note that the return os
|
51
|
+
# is__na is an R::Vector, so we need to 'pop' the value to a Ruby value in
|
52
|
+
# ordet to apply the '?' method
|
53
|
+
expect(@hitters.Salary.sum { |e| ((e.is__na == true).pop) ? 1 : 0 }).to eq 59
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should remove missing values with na__omit" do
|
57
|
+
@hitters = @hitters.na__omit
|
58
|
+
expect(@hitters.dim == R.c(263, 20)).to eq true
|
59
|
+
expect(R.sum(@hitters.is__na)).to eq 0
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'galaaz'
|
4
|
+
require 'ggplot'
|
5
|
+
|
6
|
+
faithful = ~:faithful
|
7
|
+
|
8
|
+
# sp is the basic plot having in the x axis the eruption data and in y the waiting
|
9
|
+
|
10
|
+
# Note that function 'aes' is called with E.aes. This prevents the execution of the aes function and
|
11
|
+
|
12
|
+
# passes it “as-is” to function ggplot. Note that :eruption and :waiting are column names for the
|
13
|
+
|
14
|
+
# faithful data frame and do not exist in the scope of the aes method. Calling aes with R.aes will fail.
|
15
|
+
|
16
|
+
sp = faithful.ggplot(E.aes(x: :eruptions, y: :waiting))
|
17
|
+
|
18
|
+
# create the output device for plotting
|
19
|
+
|
20
|
+
R.awt
|
21
|
+
|
22
|
+
# Use stat_density_2d with geom = "polygon"
|
23
|
+
|
24
|
+
# Note that in order to call a method without any arguments we pass an empty argument list.
|
25
|
+
|
26
|
+
# in Ruby, parenthesis are unnecessary, however in R, writing the function name without
|
27
|
+
|
28
|
+
# parenthesis returns the actual function and not a call to it.
|
29
|
+
|
30
|
+
print sp + R.geom_point +
|
31
|
+
R.stat_density_2d(E.aes_string(fill: '..level..'),
|
32
|
+
geom: "polygon")
|
33
|
+
|
34
|
+
|
35
|
+
a = gets.chomp
|
36
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
The examples presented in this directory are extracted from:
|
2
|
+
|
3
|
+
http://www.sthda.com/english/wiki/be-awesome-in-ggplot2-a-practical-guide-to-be-highly-effective-r-software-and-data-visualization
|
4
|
+
|
5
|
+
# Basics
|
6
|
+
|
7
|
+
ggplot2 is a powerful and a flexible R package, implemented by Hadley Wickham, for
|
8
|
+
producing elegant graphics. The gg in ggplot2 means Grammar of Graphics, a graphic concept
|
9
|
+
which describes plots by using a “grammar”.
|
10
|
+
|
11
|
+
According to ggplot2 concept, a plot can be divided into different fundamental parts:
|
12
|
+
Plot = data + Aesthetics + Geometry.
|
13
|
+
|
14
|
+
The principal components of every plot can be defined as follow:
|
15
|
+
|
16
|
+
* data is a data frame
|
17
|
+
* Aesthetics is used to indicate x and y variables. It can also be used to control the
|
18
|
+
color, the size or the shape of points, the height of bars, etc…..
|
19
|
+
* Geometry corresponds to the type of graphics (histogram, box plot, line plot,
|
20
|
+
density plot, dot plot, ….)
|
21
|
+
|
22
|
+
Two main functions, for creating plots, are available in ggplot2 package : a qplot() and
|
23
|
+
ggplot() functions.
|
24
|
+
|
25
|
+
* qplot() is a quick plot function which is easy to use for simple plots.
|
26
|
+
* The ggplot() function is more flexible and robust than qplot for building a plot piece by piece.
|
27
|
+
|
28
|
+
The generated plot can be kept as a variable and then printed at any time using the function
|
29
|
+
print().
|
30
|
+
|
31
|
+
After creating plots, two other important functions are:
|
32
|
+
|
33
|
+
* last_plot(), which returns the last plot to be modified
|
34
|
+
* ggsave(“plot.png”, width = 5, height = 5), which saves the last plot in the current
|
35
|
+
working directory.
|
36
|
+
|
37
|
+
This document describes how to create and customize different types of graphs using ggplot2.
|
38
|
+
Many examples of code and graphics are provided.
|