scicom 0.2.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +674 -0
  3. data/README.md +66 -0
  4. data/README.md~ +290 -0
  5. data/Rakefile +51 -0
  6. data/config.rb +163 -0
  7. data/doc/PypeR.pdf +0 -0
  8. data/doc/Stat 133 Class Notes (Phil Spector).pdf +29905 -45
  9. data/doc/The R interface.docx +0 -0
  10. data/lib/JRubyR/as_mdarray.rb +60 -0
  11. data/lib/JRubyR/attributes.rb +74 -0
  12. data/lib/JRubyR/dataframe.rb +35 -0
  13. data/lib/JRubyR/environment.rb +60 -0
  14. data/lib/JRubyR/function.rb +61 -0
  15. data/lib/JRubyR/index.rb +278 -0
  16. data/lib/JRubyR/list.rb +56 -0
  17. data/lib/JRubyR/list_orig.rb +111 -0
  18. data/lib/JRubyR/logical_value.rb +56 -0
  19. data/lib/JRubyR/rbsexp.rb +386 -0
  20. data/lib/JRubyR/renjin.rb +431 -0
  21. data/lib/JRubyR/ruby_classes.rb +58 -0
  22. data/lib/JRubyR/sequence.rb +56 -0
  23. data/lib/JRubyR/vector.rb +493 -0
  24. data/lib/env.rb +12 -0
  25. data/lib/rinruby.rb +795 -0
  26. data/lib/scicom.rb +29 -0
  27. data/target/helper.jar +0 -0
  28. data/test/baseball.csv +1 -0
  29. data/test/env.rb +7 -0
  30. data/test/test_R_interface.rb +165 -0
  31. data/test/test_array.rb +191 -0
  32. data/test/test_attributes.rb +261 -0
  33. data/test/test_basic.rb +156 -0
  34. data/test/test_column-major.rb +114 -0
  35. data/test/test_complete.rb +49 -0
  36. data/test/test_creation.rb +299 -0
  37. data/test/test_dataframe.rb +248 -0
  38. data/test/test_distribution.rb +320 -0
  39. data/test/test_double_assign.rb +240 -0
  40. data/test/test_double_receive.rb +106 -0
  41. data/test/test_environment.rb +57 -0
  42. data/test/test_factor.rb +285 -0
  43. data/test/test_functions.rb +67 -0
  44. data/test/test_linear_model.rb +64 -0
  45. data/test/test_list.rb +220 -0
  46. data/test/test_matrix.rb +205 -0
  47. data/test/test_mdarray.rb +258 -0
  48. data/test/test_operators.rb +227 -0
  49. data/test/test_sequence.rb +63 -0
  50. data/test/test_subsetting.rb +67 -0
  51. data/test/test_tmp.rb +67 -0
  52. data/test/test_vector.rb +227 -0
  53. data/vendor/Renjin.pdf +0 -0
  54. data/vendor/renjin-script-engine-0.7.0-RC7-SNAPSHOT-jar-with-dependencies.jar +0 -0
  55. data/version.rb +2 -0
  56. metadata +196 -0
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'env'
27
+ require 'scicom'
28
+
29
+
30
+ class SciComTest < Test::Unit::TestCase
31
+
32
+ context "R environment" do
33
+
34
+ #--------------------------------------------------------------------------------------
35
+ #
36
+ #--------------------------------------------------------------------------------------
37
+
38
+ setup do
39
+
40
+ end
41
+
42
+ #--------------------------------------------------------------------------------------
43
+ #
44
+ #--------------------------------------------------------------------------------------
45
+
46
+ should "keep reference and call R functions" do
47
+
48
+ var = R.c((0..10), 50)
49
+ R.mean(var).pp
50
+ R.mean(var, trim: 0.10).pp
51
+
52
+ f = R.eval("mean")
53
+ f.call(R.c(2, 3)).pp
54
+ f.call(var, trim: 0.10).pp
55
+
56
+ ff = R.d("mean")
57
+ ff.call(var, trim: 0.10).pp
58
+
59
+ sum = R.eval("sum")
60
+ dgamma = R.eval("dgamma")
61
+ p dgamma
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,64 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'env'
27
+ require 'scicom'
28
+
29
+ class SciComTest < Test::Unit::TestCase
30
+
31
+ context "R environment" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ setup do
38
+
39
+ end
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ should "Read file and apply linear model to dataset" do
46
+
47
+ # This dataset comes from Baseball-Reference.com.
48
+ baseball = R.read__csv("baseball.csv")
49
+ # Lets look at the data available for Momeyball.
50
+ # (baseball.Year < R.d(2002)).pp
51
+ moneyball = R.subset(baseball, baseball.Year < 2002)
52
+
53
+ # Let's see if we can predict the number of wins, by looking at
54
+ # runs allowed (RA) and runs scored (RS). RD is the runs difference.
55
+ # We are making a linear model for predicting wins (W) based on RD
56
+ moneyball.RD = moneyball.RS - moneyball.RA
57
+ wins_reg = R.lm("W ~ RD", data: moneyball)
58
+ R.summary(wins_reg).pp
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
data/test/test_list.rb ADDED
@@ -0,0 +1,220 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'env'
27
+ require 'scicom'
28
+
29
+ class SciComTest < Test::Unit::TestCase
30
+
31
+ context "R environment" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ setup do
38
+
39
+ # create a list with named elements
40
+ @x = R.list(first: (1..10), second: R.c("yes","no"), third: R.c(TRUE,FALSE),
41
+ fourth: R.gl(2,3))
42
+
43
+ @seq = R.c((1..10))
44
+ @seq2 = R.c((1...10))
45
+ @str_vec = R.c("yes", "no")
46
+ @str_vec2 = R.c("yes", "yes")
47
+ @trth_vec = R.c(TRUE, FALSE)
48
+ @trth_vec2 = R.c(FALSE, FALSE)
49
+ @gl = R.gl(2, 3)
50
+
51
+ end
52
+
53
+ #--------------------------------------------------------------------------------------
54
+ #
55
+ #--------------------------------------------------------------------------------------
56
+
57
+ should "access lists elements with indexing [] and [[]]" do
58
+
59
+ # get the first element of the list, usign indexing.
60
+ # Both [] and [[]] indexing can be used with the same R rules.
61
+ # Indexing with [] returns a list
62
+ assert_equal("list", @x[1].typeof.gz)
63
+
64
+ # Indexing with [[]] return the sequence type wich is "integer" is this case
65
+ assert_equal("integer", @x[[1]].typeof.gz)
66
+
67
+ # Multiple indexing is OK.
68
+ assert_equal("integer", @x[1][[1]].typeof.gz)
69
+ assert_equal("integer", @x[1][[1]][1].typeof.gz)
70
+
71
+ # Getting the Ruby value of a vector is done with method get. Method gz is equivalent
72
+ # to get(0)
73
+ assert_equal(1, @x[1][[1]][1].gz)
74
+ assert_equal(1, @x[[1]][1].gz)
75
+ assert_equal(3, @x[[1]][3].gz)
76
+ assert_equal(10, @x[[1]][10].gz)
77
+
78
+ # to get the class of an RBSexp we need to call method rclass. We cannot call method
79
+ # class on it as it will return the Ruby class ('class' is a Ruby method).
80
+ assert_equal("list", @x.rclass.gz)
81
+
82
+ end
83
+
84
+ #--------------------------------------------------------------------------------------
85
+ #
86
+ #--------------------------------------------------------------------------------------
87
+
88
+ should "acess list elements by named item" do
89
+
90
+ # should also access element of the list by name. Every element of the lists is a
91
+ # list
92
+ assert_equal("list", @x["second"].typeof.gz)
93
+ assert_equal("character", @x[["second"]].typeof.gz)
94
+ assert_equal("yes", @x[["second"]][1].gz)
95
+ assert_equal(2, @x[["fourth"]][4].gz)
96
+
97
+ end
98
+
99
+ #--------------------------------------------------------------------------------------
100
+ #
101
+ #--------------------------------------------------------------------------------------
102
+
103
+ should "assign to lists elements" do
104
+
105
+ # assign to the list
106
+ @x[1] = "new list element"
107
+ assert_equal("new list element", @x[[1]].gz)
108
+
109
+ end
110
+
111
+ #--------------------------------------------------------------------------------------
112
+ #
113
+ #--------------------------------------------------------------------------------------
114
+
115
+ should "access individual lists elements with [[]] notation" do
116
+
117
+ assert_equal(true, R.all(@seq == @x[[1]]).gt)
118
+ assert_equal(false, R.all(@seq2 == @x[[1]]).gt)
119
+
120
+ assert_equal(true, R.all(@str_vec == @x[[2]]).gt)
121
+ assert_equal(false, R.all(@str_vec2 == @x[[2]]).gt)
122
+
123
+ assert_equal(true, R.all(@trth_vec == @x[[3]]).gt)
124
+ assert_equal(false, R.all(@trth_vec2 == @x[[3]]).gt)
125
+
126
+ assert_equal(true, R.all(@gl == @x[[4]]).gt)
127
+
128
+ # accessing with the [[<name>]] notation
129
+ assert_equal(true, R.all(@seq == @x[["first"]]).gt)
130
+ assert_equal(false, R.all(@seq2 == @x[["first"]]).gt)
131
+
132
+ assert_equal(true, R.all(@str_vec == @x[["second"]]).gt)
133
+ assert_equal(false, R.all(@str_vec2 == @x[["second"]]).gt)
134
+
135
+ assert_equal(true, R.all(@trth_vec == @x[["third"]]).gt)
136
+ assert_equal(false, R.all(@trth_vec2 == @x[["third"]]).gt)
137
+
138
+ assert_equal(true, R.all(@gl == @x[["fourth"]]).gt)
139
+
140
+ end
141
+
142
+ #--------------------------------------------------------------------------------------
143
+ #
144
+ #--------------------------------------------------------------------------------------
145
+
146
+ should "allow Ruby chaining" do
147
+
148
+ assert_equal(true, R.all(@seq == @x.first).gt)
149
+ assert_equal(false, R.all(@seq2 == @x.first).gt)
150
+
151
+ assert_equal(true, R.all(@str_vec == @x.second).gt)
152
+ assert_equal(false, R.all(@str_vec2 == @x.second).gt)
153
+
154
+ assert_equal(true, R.all(@trth_vec == @x.third).gt)
155
+ assert_equal(false, R.all(@trth_vec2 == @x.third).gt)
156
+
157
+ assert_equal(true, R.all(@gl == @x.fourth).gt)
158
+
159
+ # multiple indexing
160
+ assert_equal(1, @x.first[1].gz)
161
+ assert_equal(1, @x.first[[1]].gz)
162
+
163
+ end
164
+
165
+ #--------------------------------------------------------------------------------------
166
+ #
167
+ #--------------------------------------------------------------------------------------
168
+
169
+ should "be able to call functions on lists" do
170
+
171
+ p "trying method %in%"
172
+ (@x._ :in, @x).pp
173
+
174
+ end
175
+
176
+ #--------------------------------------------------------------------------------------
177
+ #
178
+ #--------------------------------------------------------------------------------------
179
+
180
+ should "be able to assign a Ruby array to R (not really recommended)" do
181
+
182
+ # names is a Ruby array
183
+ names = ["Lisa", "Teasha", "Aaron", "Thomas"]
184
+
185
+ # Ruby Arrays can be used as arguments to R functions. An Ruby Array will be
186
+ # converted to an R list.
187
+ people = R.identity(names)
188
+ people.pp
189
+
190
+ # If a Ruby Array is assigned to an R variable, this R variable is a list. Note
191
+ # that variable 'people' above and variable 'R.people' are two different variables.
192
+ # While the first is defined in the Ruby environment, the second is defined in
193
+ # R environment.
194
+ R.people = names
195
+ R.people.pp
196
+
197
+ R.lst = [1, 2, 3, 4, 5, 6]
198
+ R.lst.pp
199
+
200
+ lst = R.identity([1, 2, 3])
201
+ lst.pp
202
+
203
+ # Using hash inside an Array does not work properly! This list will have 5
204
+ # elements and the fifth is empty.
205
+ mv = ["Lisa", 1, "John", 2, mary: 3, john: 4]
206
+ print mv
207
+
208
+ mix_vec = R.identity(["Lisa", 1, "John", 2, mary: 3, john: 4])
209
+ mix_vec.pp
210
+
211
+ # this gives an error in Renjin about Unmatched positional argument. Renjin bug already
212
+ # reported
213
+ # R.str(R.lst)
214
+
215
+ end
216
+
217
+
218
+ end
219
+
220
+ end
@@ -0,0 +1,205 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'env'
27
+ require 'scicom'
28
+
29
+ class SciComTest < Test::Unit::TestCase
30
+
31
+ context "R environment" do
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ # R code in this file are extracted from:
35
+ #
36
+ # Matrix Algebra in R
37
+ # William Revelle
38
+ # Northwestern University
39
+ # January 24, 2007
40
+ # http://personality-project.org/r/sem.appendix.1.pdf
41
+ #--------------------------------------------------------------------------------------
42
+
43
+ setup do
44
+
45
+ end
46
+
47
+ #--------------------------------------------------------------------------------------
48
+ #
49
+ #--------------------------------------------------------------------------------------
50
+
51
+ should "create matrix with the matrix function" do
52
+
53
+ # R provides numeric row and column names (e.g., [1,] is the first row, [,4] is the
54
+ # fourth column, but it is useful to label the rows and columns to make the rows
55
+ # (subjects) and columns (variables) distinction more obvious.
56
+ xij = R.matrix(R.seq(1..40), ncol: 4)
57
+
58
+ # method fassign is used whenever in R there would be a function assignment such as,
59
+ # for example: rownames(x) <- c("v1", "v2", "v3")
60
+ # R.fassign(xij, :rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
61
+ # R.fassign(xij, :colnames, R.paste("V", R.seq(1, xij.attr.dim[2]), sep: ""))
62
+
63
+ # this can also be done by calling fassing on the object directly
64
+ xij.fassign(:rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
65
+ xij.fassign(:colnames, R.paste("V", R.seq(1, xij.attr.dim[2]), sep: ""))
66
+ xij.pp
67
+
68
+ # if an index can be passed to the R function, then fassign needs to be a bit more
69
+ # complex. For instance, callling dimnames(x)[[1]] <- "name" is done by the following
70
+ # call: x.fassign({f: :dimnames, index:[[1]]}, "name"
71
+ # Changing rownames by using function :dimnames and index [[1]]
72
+ xij.fassign({f: :dimnames, index: [[1]]},
73
+ R.paste("DS", R.seq(1, xij.attr.dim[1]), sep: ""))
74
+
75
+ # Changing colnames by using function :dimnames and index [[2]]
76
+ xij.fassign({f: :dimnames, index: [[2]]},
77
+ R.paste("DV", R.seq(1, xij.attr.dim[2]), sep: ""))
78
+
79
+ xij.pp
80
+
81
+ # Just as the transpose of a vector makes a column vector into a row vector, so
82
+ # does the transpose of a matrix swap the rows for the columns. Note that now the
83
+ # subjects are columns and the variables are the rows.
84
+ xij.t.pp
85
+
86
+ end
87
+
88
+ #--------------------------------------------------------------------------------------
89
+ #
90
+ #--------------------------------------------------------------------------------------
91
+
92
+ should "add matrices" do
93
+
94
+ # The previous matrix is rather uninteresting, in that all the columns are simple
95
+ # products of the first column. A more typical matrix might be formed by sampling
96
+ # from the digits 0-9. For the purpose of this demonstration, we will set the random
97
+ # number seed to a memorable number so that it will yield the same answer each time.
98
+ R.set__seed(42)
99
+ xij = R.matrix(R.sample(R.seq(0, 9), 40, replace: TRUE), ncol: 4)
100
+
101
+ xij.fassign(:rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
102
+ xij.fassign(:colnames, R.paste("V", R.seq(1, xij.attr.dim[2]), sep: ""))
103
+ xij.pp
104
+
105
+ # Just as we could with vectors, we can add, subtract, muliply or divide the matrix
106
+ # by a scaler (a number with out a dimension)
107
+ (xij + 4).pp
108
+
109
+ ((xij + 4) / 3).round(2).pp
110
+
111
+
112
+ # We can also multiply each row (or column, depending upon order) by a vector.
113
+ v = R.seq(10)
114
+ v.pp
115
+
116
+ (xij * v).pp
117
+
118
+ end
119
+
120
+ #--------------------------------------------------------------------------------------
121
+ #
122
+ #--------------------------------------------------------------------------------------
123
+
124
+ should "multiply matrices" do
125
+
126
+ R.set__seed(42)
127
+ xij = R.matrix(R.sample(R.seq(0, 9), 40, replace: TRUE), ncol: 4)
128
+ xij.fassign(:rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
129
+ xij.fassign(:colnames, R.paste("V", R.seq(1, xij.attr.dim[2]), sep: ""))
130
+
131
+ # Consider our matrix Xij with 10 rows of 4 columns. Call an individual element in
132
+ # this matrix xij . We can find the sums for each column of the matrix by multiplying
133
+ # the matrix by our “one” vector with Xij. That is, we can find PN i=1 Xij for the j
134
+ # columns, and then divide by the number (n) of rows. (Note that we can get the same
135
+ # result by finding colMeans(Xij). We can use the dim function to find out how many
136
+ # cases (the number of rows) or the number of variables (number of columns). dim has
137
+ # two elements: dim(Xij)[1] = number of rows, dim(Xij)[2] is the number of columns.
138
+ xij.dim.pp
139
+
140
+ n = xij.dim[1]
141
+ n.pp
142
+ one = R.rep(1, n)
143
+ one.pp
144
+ x_means = one.t._ :*, xij/n
145
+ x_means.pp
146
+
147
+ # A built in function to find the means of the columns is colMeans. (See rowMeans
148
+ # for the equivalent for rows.)
149
+ xij.colMeans.pp
150
+
151
+ # x_means and xij.colMeans have the same results
152
+ assert_equal(true, (x_means == xij.colMeans).all.gt)
153
+
154
+ # Variances and covariances are measures of dispersion around the mean. We find
155
+ # these by first subtracting the means from all the observations. This means centered
156
+ # matrix is the original matrix minus a matrix of means. To make them have the same
157
+ # dimensions we premultiply the means vector by a vector of ones and subtract this from
158
+ # the data matrix.
159
+ x_diff = xij - (one._ :*, x_means)
160
+ x_diff.pp
161
+
162
+ # To find the variance/covariance matrix, we can first find the the inner product of
163
+ # the means centered matrix X.diff = Xij - X.means t(Xij-X.means) with itself and divide
164
+ # by n-1. We can compare this result to the result of the cov function (the normal way to
165
+ # find covariance
166
+ x_cov = (x_diff.t._ :*, x_diff/(n - 1)).round(2)
167
+ x_cov.pp
168
+
169
+ # calling function cov
170
+ xij.cov.round(2).pp
171
+
172
+ # both solutions are equal
173
+ assert_equal(true, (x_cov == xij.cov.round(2)).all.gt)
174
+
175
+ # Some operations need to find just the diagonal. For instance, the diagonal of the
176
+ # matrix x_cov (found above) contains the variances of the items. To extract just the
177
+ # diagonal, or create a matrix with a particular diagonal we use the diag command. We
178
+ # can convert the covariance matrix x_cov to a correlation matrix x_cor by pre and post
179
+ # multiplying the covariance matrix with a diagonal matrix containing the reciprocal of
180
+ # the standard deviations (square roots of the variances). Remember that the correlation,
181
+ # r_xy, is merely the covariance_xy/sqrt(VxVy). Compare this to the standard command for
182
+ # finding correlations cor.
183
+ x_cov.diag.round(2).pp
184
+
185
+ sdi = (1 / x_cov.diag.sqrt).diag
186
+ sdi.fassign(:rownames, x_cov.rownames)
187
+ sdi.fassign(:colnames, x_cov.colnames)
188
+ sdi.round(2).pp
189
+
190
+ x_cor = (sdi._ :*, x_cov)._ :*, sdi
191
+ x_cor.fassign(:rownames, x_cov.rownames)
192
+ x_cor.fassign(:colnames, x_cov.colnames)
193
+ x_cor.round(2).pp
194
+
195
+ # use the cor function to find the correlation
196
+ xij.cor.round(2).pp
197
+
198
+ assert_equal(true, (x_cor.round(2) == xij.cor.round(2)).all.gt)
199
+
200
+ end
201
+
202
+
203
+ end
204
+
205
+ end