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
@@ -0,0 +1,135 @@
|
|
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
|
+
describe R::Language do
|
27
|
+
|
28
|
+
#========================================================================================
|
29
|
+
context "Working with symbols" do
|
30
|
+
|
31
|
+
it "should convert a Ruby Symbol to an R Symbol with '+'" do
|
32
|
+
a = +:cyl
|
33
|
+
expect(a.is_a? R::RSymbol).to eq true
|
34
|
+
expect a.to_s == "cyl"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should assign to an R symbol and retrieve from it" do
|
38
|
+
R.cyl = 10
|
39
|
+
expect ~:cyl == 10
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should allow calling evaluate on the symbol" do
|
43
|
+
R.cyl = 10
|
44
|
+
expect R.eval(~:cyl) == 10
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
#========================================================================================
|
50
|
+
context "When creating Calls" do
|
51
|
+
|
52
|
+
it "binary operators should apply to symbols" do
|
53
|
+
# this behaviour is a bit different from R's. In R this would raise an error
|
54
|
+
# with cyl not found
|
55
|
+
expect (:cyl + 5).to_s == '.Primitive("+")(cyl, 5L)'
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should properly coerce to language" do
|
59
|
+
expect (5 + 5).to_s == '.Primitive("+")(5L, cyl)'
|
60
|
+
end
|
61
|
+
|
62
|
+
# Formula objects are special and are very similar to quoted expressions, but
|
63
|
+
# they are not really quoted since trying to call R.typeof(<formula>) will
|
64
|
+
# evaluate the formula. We add methods .typeof and .rclass in the Ruby class
|
65
|
+
it "formula have typeof and rclass" do
|
66
|
+
call = :cyl + 5
|
67
|
+
expect call.typeof == "language"
|
68
|
+
expect call.rclass == "call"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "during calls creation, only symbols are unevaluated" do
|
72
|
+
# 5 * 10 evaluate to 50 in the call
|
73
|
+
expect(:cyl + 5 * 10).to_s == '.Primitive("+")(cyl, 50L)'
|
74
|
+
# define a variable x
|
75
|
+
x = 20
|
76
|
+
# x is not quoted in the expression
|
77
|
+
expect(:cyl + 5 * x).to_s == '.Primitive("+")(cyl, 100L)'
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
#========================================================================================
|
83
|
+
|
84
|
+
context "Executing R expressions" do
|
85
|
+
|
86
|
+
it "should eval an R expression in the context of a list" do
|
87
|
+
ct = R.list(a: 10, b: 20, c: 30)
|
88
|
+
exp = :a + :b * :c
|
89
|
+
expect R.eval(exp, ct) == 610
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should eval an R function in the context of a list" do
|
93
|
+
R.x = 5
|
94
|
+
expect R.eval(:x + 10) == 15
|
95
|
+
ct = R.list(x: 20)
|
96
|
+
expect R.eval(:x + 10, ct) == 30
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
#========================================================================================
|
103
|
+
context "When working with Formulas" do
|
104
|
+
|
105
|
+
it "should create a RSymbol from a Ruby Symbol using +" do
|
106
|
+
sym = +:sym
|
107
|
+
expect sym.to_s == "sym"
|
108
|
+
end
|
109
|
+
|
110
|
+
=begin
|
111
|
+
|
112
|
+
it "should create a formula without the lhs" do
|
113
|
+
pending "formulas need to be reimplemented"
|
114
|
+
formula = ~(:cyl + :exp)
|
115
|
+
expect formula.to_s == '~.Primitive("+")(cyl, exp)'
|
116
|
+
expect formula.typeof == 'language'
|
117
|
+
expect formula.rclass == 'formula'
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should create formulas with the '=~' operator" do
|
121
|
+
formula = (:cyl =~ :exp)
|
122
|
+
expect(formula.to_s) == '.Primitive("~")(cyl, exp)'
|
123
|
+
expect formula.typeof == 'language'
|
124
|
+
expect formula.rclass == 'formula'
|
125
|
+
|
126
|
+
formula2 = (:cyl =~ :exp + :exp2 - :exp3)
|
127
|
+
expect formula2.to_s == 'cyl ~ .Primitive("-")(.Primitive("+")(exp, exp2), exp3)'
|
128
|
+
expect formula.typeof == 'language'
|
129
|
+
expect formula.rclass == 'formula'
|
130
|
+
end
|
131
|
+
=end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
@@ -0,0 +1,129 @@
|
|
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
|
+
describe R::List do
|
27
|
+
|
28
|
+
#----------------------------------------------------------------------------------------
|
29
|
+
context "When creating lists" do
|
30
|
+
|
31
|
+
it "should create an empty list" do
|
32
|
+
l = R.list
|
33
|
+
expect(l.length).to eq 0
|
34
|
+
expect(l.class).to eq R::List
|
35
|
+
expect(l.typeof).to eq "list"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return a list when R.list is called" do
|
39
|
+
l = R.list(1, 2)
|
40
|
+
expect(l.length).to eq 2
|
41
|
+
expect(l.class).to eq R::List
|
42
|
+
expect(l.typeof).to eq "list"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should allow creating list of lists" do
|
46
|
+
l = R.list(1, 2, 3, R.list(4, 5, 6))
|
47
|
+
expect(l.length).to eq 4
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should allow mixing different types of objects" do
|
51
|
+
l = R.list(R.c(1, 2, 3), R.c(1.0, 2.0, 3.0), R.list(a: 1, b: 2, c: 3),
|
52
|
+
R.complex(real: R.rnorm(5), imag: R.rnorm(5)))
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should create lists using sequences" do
|
56
|
+
x = R.list(a: (1..10), beta: R.exp(-3..3), logic: R.c(true, false, false, true))
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
#----------------------------------------------------------------------------------------
|
62
|
+
context "When subsetting a list" do
|
63
|
+
|
64
|
+
before(:all) do
|
65
|
+
@l = R.list(1, 2, 3, R.list(4, 5, 6))
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should subset with [] and positve integer. Returns a list" do
|
69
|
+
expect(@l.length).to eq 4
|
70
|
+
# Subsetting a list with [] returns a list
|
71
|
+
expect(@l[1].identical(R.list(1))).to eq true
|
72
|
+
# the 4th element of the list is a list of a list
|
73
|
+
expect(@l[4].identical(R.list(R.list(4, 5, 6)))).to eq true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should subset with [] and negative integer. Returns a list" do
|
77
|
+
expect(@l[-4].identical(R.list(1, 2, 3))).to eq true
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should subset with [] and a vector as index" do
|
81
|
+
expect(@l[R.c(4, 1)].identical(R.list(R.list(4, 5, 6), 1))).to eq true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should subset with [[]] and positive integer. Returns the actual element of the list" do
|
85
|
+
# to extract an element of a list we need double square (dbk) indexing
|
86
|
+
expect(@l[[1]]).to eq 1
|
87
|
+
# dbk indexing to get a list
|
88
|
+
expect(@l[[4]].identical(R.list(4, 5, 6))).to eq true
|
89
|
+
end
|
90
|
+
|
91
|
+
it "can chain subsetting operations" do
|
92
|
+
# 1rst element of 4th vector is a list
|
93
|
+
expect(@l[[4]][1].identical(R.list(4))).to eq true
|
94
|
+
expect(@l[[4]][[1]]).to eq 4
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should subset with [[]] with multiple indexes" do
|
98
|
+
# Note that for a list or other recursive object, the index can be a vector
|
99
|
+
# and each element of the vector is applied in turn to the list, the
|
100
|
+
# selected component, the selected component of that component, and so on.
|
101
|
+
# The result is still a single element.
|
102
|
+
expect(@l[[4, 1]]).to eq 4
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should subset with 'each' returning every element as if subsetting with '[['" do
|
106
|
+
i = 1
|
107
|
+
@l.each do |elmt|
|
108
|
+
if (i < 4)
|
109
|
+
expect elmt == i
|
110
|
+
else
|
111
|
+
expect elmt.identical(R.list(4, 5, 6)) == true
|
112
|
+
end
|
113
|
+
i = i + 1
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should subset with 'each_with_index'" do
|
118
|
+
@l.each_with_index do |elmt, i|
|
119
|
+
if (i < 4)
|
120
|
+
expect elmt == i
|
121
|
+
else
|
122
|
+
expect elmt.identical(R.list(4, 5, 6)) == true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
@@ -0,0 +1,99 @@
|
|
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
|
+
R.require 'stats'
|
26
|
+
|
27
|
+
describe R::List do
|
28
|
+
|
29
|
+
context "The apply family of functions with lists" do
|
30
|
+
|
31
|
+
before(:all) do
|
32
|
+
@x = R.list(a: (1..10), beta: R.exp(-3..3), logic: R.c(true, false, false, true))
|
33
|
+
R.library("stats")
|
34
|
+
# Make @q the function R quantile
|
35
|
+
@q = ~:quantile
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should work with lapply and mean" do
|
39
|
+
# lapply applies a function to each element of a list or vector, returning a list.
|
40
|
+
# compute the list mean for each list element
|
41
|
+
mean = R.lapply(@x, "mean")
|
42
|
+
expect(mean.a).to eq 5.5
|
43
|
+
|
44
|
+
# R.all__equal(x,y) is a utility to compare R objects x and y testing
|
45
|
+
# “near equality”. If they are different, comparison is still made to some extent,
|
46
|
+
# and a report of the differences is returned. Don't use R.all__equal directly
|
47
|
+
# in if expressions—either use isTRUE(all.equal(....)) or identical if appropriate.
|
48
|
+
# We cannot do mean.beta.all__equal in this case, since mean.beta is a Ruby float
|
49
|
+
# and method all__equal is not defined on floats.
|
50
|
+
expect(R.all__equal(mean.beta,
|
51
|
+
4.5351252347,
|
52
|
+
tolerance: (~:".Machine").double__eps ** 0.5)).to eq true
|
53
|
+
|
54
|
+
expect(mean.logic).to eq 0.5
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should allow using lapply with quantile" do
|
58
|
+
# lapply applies a function to each element of a list or vector, returning a list.
|
59
|
+
# median and quartiles for each list element
|
60
|
+
quant = R.lapply(@x, @q)
|
61
|
+
expect(quant.a[1]).to eq 1
|
62
|
+
expect(quant.a["50,00000%"]).to eq 5.50
|
63
|
+
expect(R.all__equal(quant.beta["100,0000%"],
|
64
|
+
20.08553692,
|
65
|
+
tolerance: (~:".Machine").double__eps ** 0.5)).to eq true
|
66
|
+
expect(quant.logic[2]).to eq 0.0
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should allow using method lapply with quantile" do
|
70
|
+
x = R.lapply(@x, @q, R.c(0.25, 0.50, 0.75))
|
71
|
+
expect(x.a[1]).to eq 3.25
|
72
|
+
expect(x.a["50,00000%"]).to eq 5.50
|
73
|
+
expect(R.all__equal(x.beta["75,00000%"],
|
74
|
+
5.0536690,
|
75
|
+
tolerance: (~:".Machine").double__eps ** 0.5)).to eq true
|
76
|
+
expect(x.logic[2]).to eq 0.5
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow using sapply and quantile" do
|
80
|
+
quant = R.sapply(@x, @q)
|
81
|
+
expect quant.rclass == 'matrix'
|
82
|
+
expect quant[:all, 'a'] == R.c(1, 3.25, 5.50, 7.75, 10)
|
83
|
+
expect quant[3, 'beta'] == 1
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should sapply to a sequence" do
|
87
|
+
# sapply isn’t content to always return a list: it attempts to simplify
|
88
|
+
# the results into a non-list vector if possible.
|
89
|
+
i39 = R.sapply((3..9), "seq")
|
90
|
+
expect i39[[1]] == R.c(1, 2, 3)
|
91
|
+
expect i39[[7]] == R.c(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
92
|
+
sap = R.sapply(i39, ~:fivenum)
|
93
|
+
expect sap[1, 1] == 1
|
94
|
+
expect sap[5, 7] == 9
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,83 @@
|
|
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
|
+
describe R do
|
27
|
+
|
28
|
+
#----------------------------------------------------------------------------------------
|
29
|
+
context "Matrices" do
|
30
|
+
|
31
|
+
it "Should create a matrix by column" do
|
32
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2)
|
33
|
+
expect(x[1, 1] == 1).to eq true
|
34
|
+
expect(x[1, 2] == 3).to eq true
|
35
|
+
expect(x.dim == 2).to eq true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "Should create a matrix by row" do
|
39
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2, byrow: true)
|
40
|
+
expect(x[1, 1] == 1).to eq true
|
41
|
+
expect(x[1, 2] == 2).to eq true
|
42
|
+
expect(x.dim == 2).to eq true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "Should add matrices" do
|
46
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2)
|
47
|
+
y = R.matrix(data: R.c(2, 4, 6, 8), nrow: 2, ncol: 2)
|
48
|
+
z = x + y
|
49
|
+
expect(z[1, 1] == 3).to eq true
|
50
|
+
expect(z[2, 2] == 12).to eq true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "Should subtract matrices" do
|
54
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2)
|
55
|
+
y = R.matrix(data: R.c(2, 4, 6, 8), nrow: 2, ncol: 2)
|
56
|
+
z = y - x
|
57
|
+
expect(z[1, 1] == 1).to eq true
|
58
|
+
expect(z[2, 2] == 4).to eq true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should multiply by a scalar" do
|
62
|
+
y = R.matrix(data: R.c(2, 4, 6, 8), nrow: 2, ncol: 2)
|
63
|
+
z = y * 10
|
64
|
+
expect(z[1, 1] == 20).to eq true
|
65
|
+
expect(z[2, 1] == 40).to eq true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should divide a scalar by a matrix" do
|
69
|
+
y = R.matrix(data: R.c(2, 4, 6, 8), nrow: 2, ncol: 2)
|
70
|
+
z = 10/y
|
71
|
+
expect(z == R.c(10/2, 10/4, 10/6, 10/8)).to eq true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should apply a function to all elements of the matrix: ex: sqrt" do
|
75
|
+
x = R.matrix(data: R.c(1, 2, 3, 4), nrow: 2, ncol: 2, byrow: true)
|
76
|
+
z = x.sqrt
|
77
|
+
expect(z[1, 2] == 1.73).to eq true
|
78
|
+
expect(z[2, 1] == 1.41).to eq true
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,99 @@
|
|
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
|
+
describe R::Vector do
|
27
|
+
|
28
|
+
context "When creating vectors" do
|
29
|
+
|
30
|
+
it "should create logical Vectors" do
|
31
|
+
log_vect = R.c(true, false, true, true)
|
32
|
+
expect(log_vect.length).to eq 4
|
33
|
+
expect(log_vect.class).to eq R::Vector
|
34
|
+
expect(log_vect.typeof).to eq "logical"
|
35
|
+
expect(log_vect.mode).to eq "logical"
|
36
|
+
expect(log_vect.storage__mode).to eq "logical"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should create integer Vectors" do
|
40
|
+
# in R, numbers are converted to double. To get an int one needs to add L after the
|
41
|
+
# number 1L is an integer
|
42
|
+
int_vect = R.c(1, 2)
|
43
|
+
expect(int_vect.length).to eq 2
|
44
|
+
expect(int_vect.class).to eq R::Vector
|
45
|
+
expect(int_vect.typeof).to eq "integer"
|
46
|
+
expect(int_vect.mode).to eq "numeric"
|
47
|
+
expect(int_vect.storage__mode).to eq "integer"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should create double Vectors" do
|
51
|
+
float_vect = R.c(3.0, 4, 5)
|
52
|
+
expect(float_vect.length).to eq 3
|
53
|
+
expect(float_vect.class).to eq R::Vector
|
54
|
+
expect(float_vect.typeof).to eq "double"
|
55
|
+
expect(float_vect.mode).to eq "numeric"
|
56
|
+
expect(float_vect.storage__mode).to eq "double"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should create character Vectors" do
|
60
|
+
# Here the vector has 3 elements
|
61
|
+
str_vect = R.c("Hello", "beautiful", "world!")
|
62
|
+
expect(str_vect.length).to eq 3
|
63
|
+
expect(str_vect.class).to eq R::Vector
|
64
|
+
expect(str_vect.typeof).to eq "character"
|
65
|
+
expect(str_vect.mode).to eq "character"
|
66
|
+
expect(str_vect.storage__mode).to eq "character"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should create a complex Vector" do
|
70
|
+
complex_vec = R.complex(real: R.rnorm(100), imag: R.rnorm(100))
|
71
|
+
expect(complex_vec.length).to eq 100
|
72
|
+
expect(complex_vec.typeof).to eq "complex"
|
73
|
+
expect(complex_vec.mode).to eq "complex"
|
74
|
+
expect(complex_vec.storage__mode).to eq "complex"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should allow creation of vectors of vectors" do
|
78
|
+
vect = R.c(1, 2, 3, R.c(4, 5, 6))
|
79
|
+
expect(vect.length).to eq 6
|
80
|
+
expect(vect.class).to eq R::Vector
|
81
|
+
expect(vect[4]).to eq 4
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should create vectors with the use of other R functions" do
|
85
|
+
# R.rep repeats the given vector
|
86
|
+
vec2 = R.rep(R.c("A", "B", "C"), 3)
|
87
|
+
# vec2.pp
|
88
|
+
# expect(vec2.identical(R.c("A", "B", "C", "A", "B", "C", "A", "B", "C"))).to eq true
|
89
|
+
|
90
|
+
# R.table calculates the frequencies of elements
|
91
|
+
vec3 = R.c("A", "B", "C", "A", "A", "A", "A", "B", "B")
|
92
|
+
table = R.table(vec3)
|
93
|
+
# expect(R.c(5, 3, 1).identical table).to eq true
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|