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.
- checksums.yaml +7 -0
- data/LICENSE.txt +674 -0
- data/README.md +66 -0
- data/README.md~ +290 -0
- data/Rakefile +51 -0
- data/config.rb +163 -0
- data/doc/PypeR.pdf +0 -0
- data/doc/Stat 133 Class Notes (Phil Spector).pdf +29905 -45
- data/doc/The R interface.docx +0 -0
- data/lib/JRubyR/as_mdarray.rb +60 -0
- data/lib/JRubyR/attributes.rb +74 -0
- data/lib/JRubyR/dataframe.rb +35 -0
- data/lib/JRubyR/environment.rb +60 -0
- data/lib/JRubyR/function.rb +61 -0
- data/lib/JRubyR/index.rb +278 -0
- data/lib/JRubyR/list.rb +56 -0
- data/lib/JRubyR/list_orig.rb +111 -0
- data/lib/JRubyR/logical_value.rb +56 -0
- data/lib/JRubyR/rbsexp.rb +386 -0
- data/lib/JRubyR/renjin.rb +431 -0
- data/lib/JRubyR/ruby_classes.rb +58 -0
- data/lib/JRubyR/sequence.rb +56 -0
- data/lib/JRubyR/vector.rb +493 -0
- data/lib/env.rb +12 -0
- data/lib/rinruby.rb +795 -0
- data/lib/scicom.rb +29 -0
- data/target/helper.jar +0 -0
- data/test/baseball.csv +1 -0
- data/test/env.rb +7 -0
- data/test/test_R_interface.rb +165 -0
- data/test/test_array.rb +191 -0
- data/test/test_attributes.rb +261 -0
- data/test/test_basic.rb +156 -0
- data/test/test_column-major.rb +114 -0
- data/test/test_complete.rb +49 -0
- data/test/test_creation.rb +299 -0
- data/test/test_dataframe.rb +248 -0
- data/test/test_distribution.rb +320 -0
- data/test/test_double_assign.rb +240 -0
- data/test/test_double_receive.rb +106 -0
- data/test/test_environment.rb +57 -0
- data/test/test_factor.rb +285 -0
- data/test/test_functions.rb +67 -0
- data/test/test_linear_model.rb +64 -0
- data/test/test_list.rb +220 -0
- data/test/test_matrix.rb +205 -0
- data/test/test_mdarray.rb +258 -0
- data/test/test_operators.rb +227 -0
- data/test/test_sequence.rb +63 -0
- data/test/test_subsetting.rb +67 -0
- data/test/test_tmp.rb +67 -0
- data/test/test_vector.rb +227 -0
- data/vendor/Renjin.pdf +0 -0
- data/vendor/renjin-script-engine-0.7.0-RC7-SNAPSHOT-jar-with-dependencies.jar +0 -0
- data/version.rb +2 -0
- metadata +196 -0
@@ -0,0 +1,248 @@
|
|
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
|
+
=begin
|
46
|
+
should "create data-frame from a single vector" do
|
47
|
+
|
48
|
+
vec = R.seq(20)
|
49
|
+
vec.attr.dim = R.c(4, 5)
|
50
|
+
df = R.as__data__frame(vec)
|
51
|
+
df.pp
|
52
|
+
assert_equal(4, df.nrow.gz)
|
53
|
+
assert_equal(5, df.ncol.gz)
|
54
|
+
|
55
|
+
df[0].pp
|
56
|
+
df[1].pp
|
57
|
+
df["V2"].pp
|
58
|
+
df["V4"].pp
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
#--------------------------------------------------------------------------------------
|
63
|
+
#
|
64
|
+
#--------------------------------------------------------------------------------------
|
65
|
+
|
66
|
+
should "work with build-in data-frames" do
|
67
|
+
|
68
|
+
# We use built-in data frames in R for our tutorials. For example, here is a built-in
|
69
|
+
# data frame in R, called mtcars.
|
70
|
+
|
71
|
+
# to access a build-in data-frame, use method R.d with the data-frame's name
|
72
|
+
mtcars = R.d("mtcars")
|
73
|
+
|
74
|
+
p "mtcars build-in data-frame"
|
75
|
+
mtcars.pp
|
76
|
+
|
77
|
+
# Here is the cell value from the first row, second column of mtcars.
|
78
|
+
assert_equal(6, mtcars[1, 2].gz)
|
79
|
+
|
80
|
+
# Moreover, we can use the row and column names instead of the numeric coordinates.
|
81
|
+
assert_equal(6, mtcars["Mazda RX4", "cyl"].gz)
|
82
|
+
|
83
|
+
# Lastly, the number of data rows in the data frame is given by the nrow function.
|
84
|
+
assert_equal(32, mtcars.nrow.gz) # number of data rows
|
85
|
+
|
86
|
+
# And the number of columns of a data frame is given by the ncol function.
|
87
|
+
assert_equal(11, mtcars.ncol.gz) # number of columns
|
88
|
+
|
89
|
+
p "mtcars head"
|
90
|
+
mtcars.head.pp
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
#--------------------------------------------------------------------------------------
|
95
|
+
#
|
96
|
+
#--------------------------------------------------------------------------------------
|
97
|
+
|
98
|
+
should "access data-frames by column vector" do
|
99
|
+
|
100
|
+
mtcars = R.d("mtcars")
|
101
|
+
|
102
|
+
# We reference a data frame column with the double square bracket "[[]]" operator.
|
103
|
+
# For example, to retrieve the ninth column vector of the built-in data set mtcars,
|
104
|
+
# we write mtcars[[9]].
|
105
|
+
mtcars[[9]].pp
|
106
|
+
|
107
|
+
# We can retrieve the same column vector by its name.
|
108
|
+
mtcars[["am"]].pp
|
109
|
+
|
110
|
+
# We can also retrieve with the "." operator in lieu of the double square
|
111
|
+
# bracket operator.
|
112
|
+
mtcars.am.pp
|
113
|
+
|
114
|
+
# Yet another way to retrieve the same column vector is to use the single square
|
115
|
+
# bracket "[]" operator. We prepend the column name with 'nil', which signals a
|
116
|
+
# wildcard match for the row position.
|
117
|
+
mtcars[nil, "am"].pp
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
#--------------------------------------------------------------------------------------
|
122
|
+
#
|
123
|
+
#--------------------------------------------------------------------------------------
|
124
|
+
|
125
|
+
should "access data-frames by column slice" do
|
126
|
+
|
127
|
+
mtcars = R.d("mtcars")
|
128
|
+
|
129
|
+
# We retrieve a data frame column slice with the single square bracket "[]" operator.
|
130
|
+
|
131
|
+
# Numeric Indexing
|
132
|
+
# The following is a slice containing the first column of the built-in data set
|
133
|
+
# mtcars.
|
134
|
+
mtcars[1].pp
|
135
|
+
|
136
|
+
# Name Indexing
|
137
|
+
# We can retrieve the same column slice by its name.
|
138
|
+
mtcars["mpg"].pp
|
139
|
+
|
140
|
+
# To retrieve a data frame slice with the two columns mpg and hp, we pack the
|
141
|
+
# column names in an index vector inside the single square bracket operator.
|
142
|
+
mtcars[R.c("mpg", "hp")].pp
|
143
|
+
|
144
|
+
end
|
145
|
+
=end
|
146
|
+
|
147
|
+
#--------------------------------------------------------------------------------------
|
148
|
+
#
|
149
|
+
#--------------------------------------------------------------------------------------
|
150
|
+
|
151
|
+
should "access data-frames by row slice" do
|
152
|
+
|
153
|
+
mtcars = R.d("mtcars")
|
154
|
+
|
155
|
+
# We retrieve rows from a data frame with the single square bracket operator, just
|
156
|
+
# like what we did with columns. However, in additional to an index vector of row
|
157
|
+
# positions, we append an nil. This is important, as the nil signals a wildcard match
|
158
|
+
# for the second coordinate for column positions.
|
159
|
+
|
160
|
+
# Numeric Indexing
|
161
|
+
# For example, the following retrieves a row record of the built-in data set mtcars.
|
162
|
+
# Please notice the nil in the square bracket operator. It states that the 1974 Camaro
|
163
|
+
# Z28 has a gas mileage of 13.3 miles per gallon, and an eight cylinder 245 horse power
|
164
|
+
# engine, ..., etc.
|
165
|
+
mtcars[24, nil].pp
|
166
|
+
|
167
|
+
# To retrieve more than one rows, we use a numeric index vector.
|
168
|
+
mtcars[R.c(3, 24), nil].pp
|
169
|
+
|
170
|
+
# Name Indexing
|
171
|
+
# We can retrieve a row by its name.
|
172
|
+
mtcars["Camaro Z28", nil].pp
|
173
|
+
|
174
|
+
# And we can pack the row names in an index vector in order to retrieve multiple
|
175
|
+
# rows.
|
176
|
+
mtcars[R.c("Datsun 710", "Camaro Z28"), nil].pp
|
177
|
+
|
178
|
+
# Logical Indexing
|
179
|
+
# Lastly, we can retrieve rows with a logical index vector. In the following
|
180
|
+
# vector L, the member value is TRUE if the car has automatic transmission, and
|
181
|
+
# FALSE if otherwise.
|
182
|
+
auto = mtcars.am == 0
|
183
|
+
auto.pp
|
184
|
+
|
185
|
+
# Here is the list of vehicles with automatic transmission.
|
186
|
+
mtcars[auto, nil].pp
|
187
|
+
|
188
|
+
# And here is the gas mileage data for automatic transmission.
|
189
|
+
mtcars[auto, nil].mpg.pp
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
#--------------------------------------------------------------------------------------
|
194
|
+
#
|
195
|
+
#--------------------------------------------------------------------------------------
|
196
|
+
|
197
|
+
should "create data-frame from multiple vectors" do
|
198
|
+
=begin
|
199
|
+
|
200
|
+
# name age hgt wgt race year SAT
|
201
|
+
# Bob 21 70 180 Cauc Jr 1080
|
202
|
+
# Fred 18 67 156 Af.Am Fr 1210
|
203
|
+
# Barb 18 64 128 Af.Am Fr 840
|
204
|
+
# Sue 24 66 118 Cauc Sr 1340
|
205
|
+
# Jeff 20 72 202 Asian So 880
|
206
|
+
|
207
|
+
name = R.c("Bob", "Fred", "Barb", "Sue", "Jeff")
|
208
|
+
age = R.c(21, 18, 18, 24, 20)
|
209
|
+
hgt = R.c(70, 67, 64, 66, 72)
|
210
|
+
wgt = R.c(180, 156, 128, 118, 202)
|
211
|
+
race = R.c("Cauc", "Af. Am", "Af. Am", "Cauc", "Asian")
|
212
|
+
sat = R.c(1080, 1210, 840, 1340, 880)
|
213
|
+
|
214
|
+
df = R.data__frame(name, age, hgt, wgt, race, sat)
|
215
|
+
df.colnames.pp
|
216
|
+
df.colnames(prefix: "sc").pp
|
217
|
+
|
218
|
+
|
219
|
+
# Renjin allows changes to variable properties
|
220
|
+
R.eval("colnames(#{df.r}) = c('name', 'age', 'height', 'weigth', 'race', 'SAT')")
|
221
|
+
R.eval("print(colnames(#{df.r}))")
|
222
|
+
|
223
|
+
rbvec = R.eval("vec = c(1, 2, 3, 4, 5)")
|
224
|
+
# this is a new vector with the same name. Assigning a new value to a large
|
225
|
+
# vector can then be very costly as every assignment does copy the old data.
|
226
|
+
R.eval("vec[1] = 10")
|
227
|
+
R.eval("print(vec)")
|
228
|
+
# this proves that vec is actually a new vec. We have kept the old vector in
|
229
|
+
# variable rbvec.
|
230
|
+
rbvec.print
|
231
|
+
|
232
|
+
|
233
|
+
# R.colnames(df) = R.c("name", "age", "height", "weigth", "race", "SAT")
|
234
|
+
df.print
|
235
|
+
summ = R.summary(df.r)
|
236
|
+
p summ
|
237
|
+
summ.print
|
238
|
+
|
239
|
+
R.eval("print(colnames(#{df.r}))")
|
240
|
+
col = R.colnames(:df)
|
241
|
+
col.print
|
242
|
+
=end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
@@ -0,0 +1,320 @@
|
|
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
|
+
# creating a new instance of Renjin
|
40
|
+
@r1 = R.new
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
#======================================================================================
|
45
|
+
#
|
46
|
+
#======================================================================================
|
47
|
+
|
48
|
+
should "be able to call built-in R numeric functions" do
|
49
|
+
|
50
|
+
# All R numeric functions are available to be called directly from a Ruby script.
|
51
|
+
# Note that a numeric function in R always returns a vector (MDArray), in that case,
|
52
|
+
# of size 1, so we need to index the result with [0].
|
53
|
+
assert_equal(20.5, R.abs(-20.5))
|
54
|
+
assert_equal(Math.sqrt(84), R.sqrt(84)[0])
|
55
|
+
assert_equal(4, R.ceiling(3.475)[0])
|
56
|
+
assert_equal(3, R.floor(3.475)[0])
|
57
|
+
assert_equal(5, R.trunc(5.99)[0])
|
58
|
+
assert_equal(3.46, R.round(3.457, digits: 2)[0])
|
59
|
+
assert_equal(3.5, R.signif(3.475, digits: 2)[0])
|
60
|
+
assert_equal(Math.cos(10), R.cos(10)[0])
|
61
|
+
assert_equal(Math.sin(0.53), R.sin(0.53)[0])
|
62
|
+
assert_equal(Math.tan(0.53), R.tan(0.53)[0])
|
63
|
+
assert_equal(Math.acos(0.53), R.acos(0.53)[0])
|
64
|
+
assert_equal(Math.cosh(0.53), R.cosh(0.53)[0])
|
65
|
+
assert_equal(Math.acosh(1), R.acosh(1)[0])
|
66
|
+
assert_equal(Math.log(25.45), R.log(25.45)[0])
|
67
|
+
# Math.log10 = 1.4056877866727773
|
68
|
+
# R.log10 = 1.4056877866727775
|
69
|
+
# assert_equal(Math.log10(25.45), R.log10(25.45)[0])
|
70
|
+
assert_equal(Math.exp(2.43), R.exp(2.43)[0])
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
#======================================================================================
|
75
|
+
#
|
76
|
+
#======================================================================================
|
77
|
+
|
78
|
+
should "be able to call built-in R character functions" do
|
79
|
+
|
80
|
+
# Extract or replace substrings in a character vector.
|
81
|
+
x = "abcdef"
|
82
|
+
assert_equal("bcd", R.substr(x, 2, 4)[0])
|
83
|
+
|
84
|
+
# Returns a logical array vector
|
85
|
+
vec = R.c(TRUE, TRUE, FALSE)
|
86
|
+
vec = R.c(true, true, false)
|
87
|
+
|
88
|
+
# returns a DoubleMDArray. NA in MDArray is NaN. There is no difference
|
89
|
+
vec = R.c(NaN, NA, EPSILON)
|
90
|
+
vec.print
|
91
|
+
|
92
|
+
# grep(pattern, x, ignore.case=FALSE, fixed=FALSE). Search for pattern in x.
|
93
|
+
# If fixed = FALSE then pattern is a regular expression. If fixed = TRUE then pattern
|
94
|
+
# is a text string. Returns matching indices.
|
95
|
+
res = R.grep("A", R.c("b","A","c"), fixed: TRUE)
|
96
|
+
assert_equal(2, res[0])
|
97
|
+
|
98
|
+
# Split the elements of character vector x at split.
|
99
|
+
# Returns a ListVector
|
100
|
+
split = R.strsplit(x, "")
|
101
|
+
|
102
|
+
# returns c("x1","x2" "x3")
|
103
|
+
# Method in R is called as paste("x", 1:3, sep = "")
|
104
|
+
str = R.paste("x", (1..3), sep: "")
|
105
|
+
str.print
|
106
|
+
|
107
|
+
# returns c("xM1","xM2" "xM3")
|
108
|
+
# Method in R is called as paste("x", 1:3, sep = "M")
|
109
|
+
str = R.paste("x", (1..3), sep: "M")
|
110
|
+
str.print
|
111
|
+
|
112
|
+
# date is a Closure
|
113
|
+
date = R.date()
|
114
|
+
|
115
|
+
# str = R.paste("Today is", R.date())
|
116
|
+
str.print
|
117
|
+
|
118
|
+
str = R.toupper("this is a string")
|
119
|
+
assert_equal("THIS IS A STRING", str[0])
|
120
|
+
|
121
|
+
str = R.tolower("THIS IS ALSO A STRING")
|
122
|
+
assert_equal("this is also a string", str[0])
|
123
|
+
|
124
|
+
# R.sub(pattern, replacement, x, ignore.case = FALSE, fixed = FALSE)
|
125
|
+
# Find pattern in x and replace with replacement text. If fixed=FALSE then pattern is a
|
126
|
+
# regular expression. If fixed = TRUE then pattern is a text string.
|
127
|
+
# returns "Hello.There"
|
128
|
+
str = R.sub("\\\\s",".","Hello There")
|
129
|
+
assert_equal("Hello.There", str[0])
|
130
|
+
|
131
|
+
=begin
|
132
|
+
x <- "abcdef"
|
133
|
+
substr(x, 2, 4) <- "22222" is "a222ef"
|
134
|
+
=end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
#======================================================================================
|
139
|
+
#
|
140
|
+
#======================================================================================
|
141
|
+
|
142
|
+
should "be able to call built-in R statistical probability functions" do
|
143
|
+
|
144
|
+
# By prefixing a "d" to the function name in the table above, you can get probability
|
145
|
+
# density values (pdf). By prefixing a "p", you can get cumulative probabilities (cdf).
|
146
|
+
# By prefixing a "q", you can get quantile values. By prefixing an "r", you can get
|
147
|
+
# random numbers from the distribution. I will demonstrate using the normal distribution.
|
148
|
+
|
149
|
+
# cumulative normal probability for q (area under the normal curve to the right of q)
|
150
|
+
assert_equal(0.975, R.pnorm(1.96))
|
151
|
+
|
152
|
+
# The dnorm( ) function returns the height of the normal curve at some value along the
|
153
|
+
# x-axis.
|
154
|
+
assert_equal(0.24197072451914337, R.dnorm(1)[0])
|
155
|
+
|
156
|
+
# The pnorm( ) function is the cumulative density function or cdf. It returns the area
|
157
|
+
# below the given value of "x",
|
158
|
+
assert_equal(0.841344746068543, R.pnorm(1)[0])
|
159
|
+
|
160
|
+
# Once again, the defaults for mean and sd are 0 and 1 respectively. These can be set
|
161
|
+
# to other values as in the case of dnorm( ). To find the area above the cutoff x-value,
|
162
|
+
# either subtract from 1, or set the "lower.tail=" option to FALSE...
|
163
|
+
assert_equal(0.15865525393145696, 1 - R.pnorm(1)[0])
|
164
|
+
assert_equal(0.15865525393145696, R.pnorm(1, "lower.tail" => FALSE))
|
165
|
+
|
166
|
+
# To get quantiles or "critical values", you can use the qnorm( ) function as in the
|
167
|
+
# following examples...
|
168
|
+
# p = .05, one-tailed (upper)
|
169
|
+
assert_equal(1.644854, R.qnorm(0.95))
|
170
|
+
|
171
|
+
# p = .05, two-tailed
|
172
|
+
R.qnorm(R.c(0.025,0.975)).print
|
173
|
+
|
174
|
+
# deciles from the unit normal dist.
|
175
|
+
R.qnorm(R.seq(0.1,0.9,0.1)).print
|
176
|
+
|
177
|
+
# area below t = 2.101, df = 8
|
178
|
+
assert_equal(0.9655848143495498, R.pt(2.101, df: 8)[0])
|
179
|
+
|
180
|
+
# critical value of chi square, df = 1
|
181
|
+
assert_equal(3.8414588206939566, R.qchisq(0.95, df: 1)[0])
|
182
|
+
|
183
|
+
R.qf(R.c(0.025,0.975), df1: 3, df2: 12).print
|
184
|
+
|
185
|
+
# a discrete binomial probability
|
186
|
+
assert_equal(0.010843866711637968, R.dbinom(60, size: 100, prob: 0.5)[0])
|
187
|
+
|
188
|
+
# Random numbers are generated from a given distribution like this...
|
189
|
+
|
190
|
+
# 9 uniformly distributed random nos.
|
191
|
+
R.runif(9).print
|
192
|
+
|
193
|
+
# 9 normally distributed random nos.
|
194
|
+
R.rnorm(9).print
|
195
|
+
|
196
|
+
# 9 t-distributed random nos.
|
197
|
+
R.rt(9, df: 10).print
|
198
|
+
|
199
|
+
R.eval("print(quantile(rivers))")
|
200
|
+
quant = R.quantile(:rivers)
|
201
|
+
quant.print
|
202
|
+
summary = R.summary(:rivers)
|
203
|
+
summary.print
|
204
|
+
|
205
|
+
# quintiles
|
206
|
+
quint = R.quantile(:rivers, probs: R.seq(0.2,0.8,0.2))
|
207
|
+
quint.print
|
208
|
+
|
209
|
+
# deciles
|
210
|
+
dec = R.quantile(:rivers, probs: R.seq(0.1,0.9,0.1))
|
211
|
+
dec.print
|
212
|
+
|
213
|
+
# And then there is the "type=" option. It turns out there is some disagreement among
|
214
|
+
# different sources as to just how quantiles should be calculated from an empirical
|
215
|
+
# distribution. R doesn't take sides. It gives you nine different methods! Pick the
|
216
|
+
# one you like best by setting the "type=" option to a number between 1 and 9. Here
|
217
|
+
# are some details (and more are available on the help page): type=2 will give the results
|
218
|
+
# most people are taught to calculate in an intro stats course, type=3 is the SAS
|
219
|
+
# definition, type=6 is the Minitab and SPSS definition, type=7 is the default and the
|
220
|
+
# S definition and seems to work well when the variable is continuous.
|
221
|
+
|
222
|
+
# deciles - Don't see any difference, shoud there be?
|
223
|
+
dec = R.quantile(:rivers, probs: R.seq(0.1,0.9,0.1), type: 1)
|
224
|
+
dec.print
|
225
|
+
|
226
|
+
dec = R.quantile(:rivers, probs: R.seq(0.1,0.9,0.1), type: 2)
|
227
|
+
dec.print
|
228
|
+
|
229
|
+
dec = R.quantile(:rivers, probs: R.seq(0.1,0.9,0.1), type: 7o)
|
230
|
+
dec.print
|
231
|
+
|
232
|
+
=begin
|
233
|
+
|
234
|
+
|
235
|
+
dnorm(x) normal density function (by default m=0 sd=1)
|
236
|
+
# plot standard normal curve
|
237
|
+
x <- pretty(c(-3,3), 30)
|
238
|
+
y <- dnorm(x)
|
239
|
+
plot(x, y, type='l', xlab="Normal Deviate", ylab="Density", yaxs="i")
|
240
|
+
qnorm(p) normal quantile.
|
241
|
+
value at the p percentile of normal distribution
|
242
|
+
qnorm(.9) is 1.28 # 90th percentile
|
243
|
+
rnorm(n, m=0,sd=1) n random normal deviates with mean m
|
244
|
+
and standard deviation sd.
|
245
|
+
#50 random normal variates with mean=50, sd=10
|
246
|
+
x <- rnorm(50, m=50, sd=10)
|
247
|
+
dbinom(x, size, prob)
|
248
|
+
pbinom(q, size, prob)
|
249
|
+
qbinom(p, size, prob)
|
250
|
+
rbinom(n, size, prob) binomial distribution where size is the sample size
|
251
|
+
and prob is the probability of a heads (pi)
|
252
|
+
# prob of 0 to 5 heads of fair coin out of 10 flips
|
253
|
+
dbinom(0:5, 10, .5)
|
254
|
+
# prob of 5 or less heads of fair coin out of 10 flips
|
255
|
+
pbinom(5, 10, .5)
|
256
|
+
dpois(x, lamda)
|
257
|
+
ppois(q, lamda)
|
258
|
+
qpois(p, lamda)
|
259
|
+
rpois(n, lamda) poisson distribution with m=std=lamda
|
260
|
+
#probability of 0,1, or 2 events with lamda=4
|
261
|
+
dpois(0:2, 4)
|
262
|
+
# probability of at least 3 events with lamda=4
|
263
|
+
1- ppois(2,4)
|
264
|
+
dunif(x, min=0, max=1)
|
265
|
+
punif(q, min=0, max=1)
|
266
|
+
qunif(p, min=0, max=1)
|
267
|
+
runif(n, min=0, max=1) uniform distribution, follows the same pattern
|
268
|
+
as the normal distribution above.
|
269
|
+
#10 uniform random variates
|
270
|
+
x <- runif(10)
|
271
|
+
=end
|
272
|
+
|
273
|
+
end
|
274
|
+
=begin
|
275
|
+
#======================================================================================
|
276
|
+
#
|
277
|
+
#======================================================================================
|
278
|
+
|
279
|
+
should "integrate Ruby sequence with R sequence" do
|
280
|
+
|
281
|
+
seq = R.seq(2, 10)
|
282
|
+
|
283
|
+
res = R.eval <<EOF
|
284
|
+
print(#{seq.r});
|
285
|
+
print(#{seq.r});
|
286
|
+
print(ls());
|
287
|
+
EOF
|
288
|
+
|
289
|
+
# remove the variable from R
|
290
|
+
seq.destroy
|
291
|
+
|
292
|
+
R.eval("print(ls())")
|
293
|
+
|
294
|
+
end
|
295
|
+
|
296
|
+
#======================================================================================
|
297
|
+
#
|
298
|
+
#======================================================================================
|
299
|
+
|
300
|
+
should "integrate MDArray with R vector" do
|
301
|
+
|
302
|
+
# typed_arange does the same as arange but for arrays of other type
|
303
|
+
arr = MDArray.typed_arange(:double, 60)
|
304
|
+
# MDArray is stored in row-major order
|
305
|
+
arr.reshape!([5, 3, 4])
|
306
|
+
# arr.print
|
307
|
+
|
308
|
+
R.eval <<EOF
|
309
|
+
print(#{arr.r});
|
310
|
+
vec = #{arr.r};
|
311
|
+
print(vec);
|
312
|
+
print(vec[1, 1, 1]);
|
313
|
+
|
314
|
+
EOF
|
315
|
+
|
316
|
+
end
|
317
|
+
=end
|
318
|
+
end
|
319
|
+
|
320
|
+
end
|