rust 0.9 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror of the factor type in R.
7
+
4
8
  class Factor < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return klass == "factor"
@@ -20,11 +24,17 @@ module Rust
20
24
  Rust._eval("#{variable_name} <- factor(tmp.values, labels=tmp.levels)")
21
25
  end
22
26
 
27
+ ##
28
+ # Creates a new factor given an array of numeric +values+ and symbolic +levels+.
29
+
23
30
  def initialize(values, levels)
24
31
  @levels = levels.map { |v| v.to_sym }
25
32
  @values = values
26
33
  end
27
34
 
35
+ ##
36
+ # Returns the levels of the factor.
37
+
28
38
  def levels
29
39
  @levels
30
40
  end
@@ -35,10 +45,17 @@ module Rust
35
45
  return @levels == other.levels && self.to_a == other.to_a
36
46
  end
37
47
 
48
+ ##
49
+ # Returns the value of the +i+-th element in the factor.
50
+
38
51
  def [](i)
39
52
  FactorValue.new(@values[i], @levels[@values[i] - 1])
40
53
  end
41
54
 
55
+ ##
56
+ # Sets the +value+ of the +i+-th element in the factor. If it is an Integer, the +value+ must be between 1 and
57
+ # the number of levels of the factor. +value+ can be either a FactorValue or a String/Symbol.
58
+
42
59
  def []=(i, value)
43
60
  raise "The given value is outside the factor bounds" if value.is_a?(Integer) && (value < 1 || value > @levels.size)
44
61
 
@@ -57,6 +74,9 @@ module Rust
57
74
  @values[i] = value
58
75
  end
59
76
 
77
+ ##
78
+ # Returns an array of FactorValue for the values in this factor.
79
+
60
80
  def to_a
61
81
  @values.map { |v| FactorValue.new(v, @levels[v - 1]) }
62
82
  end
@@ -76,7 +96,14 @@ module Rust
76
96
  end
77
97
  end
78
98
 
99
+ ##
100
+ # Represents a single value in a factor.
101
+
79
102
  class FactorValue
103
+
104
+ ##
105
+ # Creates a factor with a given +value+ (numeric) and +level+ (symbolic).
106
+
80
107
  def initialize(value, level)
81
108
  @value = value
82
109
  @level = level
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror of the formula type in R.
7
+
4
8
  class Formula < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return klass == "formula" || (klass.is_a?(Array) && klass.include?("formula"))
@@ -24,6 +28,9 @@ module Rust
24
28
  attr_reader :left_part
25
29
  attr_reader :right_part
26
30
 
31
+ ##
32
+ # Creates a new formula with a given +left_part+ (optional) and +right_part+ (as strings).
33
+
27
34
  def initialize(left_part, right_part)
28
35
  raise ArgumentError, "Expected string" if left_part && !left_part.is_a?(String)
29
36
  raise ArgumentError, "Expected string" if !right_part.is_a?(String)
@@ -46,7 +53,10 @@ module Rust
46
53
  return self.to_R.strip
47
54
  end
48
55
  end
49
-
56
+
57
+ ##
58
+ # Mirror of the call type in R.
59
+
50
60
  class Call < RustDatatype
51
61
  def self.can_pull?(type, klass)
52
62
  return klass == "call"
@@ -61,6 +71,9 @@ module Rust
61
71
  Rust._eval("#{variable_name} <- str2lang(call.str)")
62
72
  end
63
73
 
74
+ ##
75
+ # Creates a new call with the given +value+ (String).
76
+
64
77
  def initialize(value)
65
78
  @value = value
66
79
  end
@@ -74,6 +87,9 @@ module Rust
74
87
  end
75
88
  end
76
89
 
90
+ ##
91
+ # Mirror of the environment type in R. Currently not supported.
92
+
77
93
  class Environment < RustDatatype
78
94
  def self.can_pull?(type, klass)
79
95
  return type == "environment" && klass == "environment"
@@ -90,23 +106,36 @@ module Rust
90
106
  end
91
107
  end
92
108
 
109
+ ##
110
+ # Represents a function call in R. After having set up its name (constructor) and, optionally, its arguments
111
+ # and options, it can be used the call method to execute it in the R environment.
112
+
93
113
  class Function
94
114
  attr_reader :name
95
115
  attr_reader :arguments
96
116
  attr_reader :options
97
117
 
118
+ ##
119
+ # Creates a new function with a given +name+.
120
+
98
121
  def initialize(name)
99
122
  @function = name
100
123
  @arguments = Arguments.new
101
124
  @options = Options.new
102
125
  end
103
126
 
127
+ ##
128
+ # Sets the +options+ (Options type) of the function.
129
+
104
130
  def options=(options)
105
131
  raise TypeError, "Expected Options" unless options.is_a?(Options)
106
132
 
107
133
  @options = options
108
134
  end
109
135
 
136
+ ##
137
+ # Sets the +arguments+ (Arguments type) of the function.
138
+
110
139
  def arguments=(arguments)
111
140
  raise TypeError, "Expected Arguments" unless options.is_a?(Arguments)
112
141
 
@@ -118,23 +147,21 @@ module Rust
118
147
  return "#@function(#{params})"
119
148
  end
120
149
 
150
+ ##
151
+ # Calls the function in the R environment.
152
+
121
153
  def call
122
154
  Rust._eval(self.to_R)
123
155
  end
124
156
  end
125
157
 
126
- class SimpleFormula
127
- def initialize(dependent, independent)
128
- @dependent = dependent
129
- @independent = independent
130
- end
131
-
132
- def to_R
133
- return "#@dependent ~ #@independent"
134
- end
135
- end
158
+ ##
159
+ # Represents an R variable.
136
160
 
137
161
  class Variable
162
+ ##
163
+ # Creates a variable with the given +name+.
164
+
138
165
  def initialize(name)
139
166
  @name = name
140
167
  end
@@ -144,12 +171,18 @@ module Rust
144
171
  end
145
172
  end
146
173
 
174
+ ##
175
+ # Represents the arguments of a function in R. Works as an Array of objects.
176
+
147
177
  class Arguments < Array
148
178
  def to_R
149
179
  return self.map { |v| v.to_R }.join(", ")
150
180
  end
151
181
  end
152
182
 
183
+ ##
184
+ # Represents the options of a function in R. Works as a Hash associating option names to objects.
185
+
153
186
  class Options < Hash
154
187
  def to_R
155
188
  return self.map { |k, v| "#{k}=#{v.to_R}" }.join(", ")
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror of the list type in R.
7
+
4
8
  class List < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return type == "list"
@@ -27,12 +31,18 @@ module Rust
27
31
  end
28
32
  end
29
33
 
34
+ ##
35
+ # Creates an empty list of a given class (+klass+) and the specified +names+.
36
+
30
37
  def initialize(klass, names = [])
31
38
  @data = {}
32
39
  @names = names
33
40
  @klass = klass
34
41
  end
35
42
 
43
+ ##
44
+ # Returns the elements for the name +key+.
45
+
36
46
  def [](key)
37
47
  key = get_key(key)
38
48
 
@@ -40,12 +50,18 @@ module Rust
40
50
  end
41
51
  alias :| :[]
42
52
 
53
+ ##
54
+ # Sets the +value+ for name +key+.
55
+
43
56
  def []=(key, value)
44
57
  key = get_key(key)
45
58
 
46
59
  return @data[key] = value
47
60
  end
48
61
 
62
+ ##
63
+ # Returns the names of the list.
64
+
49
65
  def names
50
66
  @names
51
67
  end
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror of the matrix type in R.
7
+
4
8
  class Matrix < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return klass.is_a?(Array) && klass.include?("matrix")
@@ -27,6 +31,10 @@ module Rust
27
31
  Rust[variable_name] = matrix
28
32
  end
29
33
 
34
+ ##
35
+ # Creates a new matrix with the given +data+ (Ruby Matrix). Optionally, +row_names+ and +column_names+ can
36
+ # be specified.
37
+
30
38
  def initialize(data, row_names = nil, column_names = nil)
31
39
  @data = data.clone
32
40
 
@@ -48,30 +56,45 @@ module Rust
48
56
  end
49
57
  end
50
58
 
59
+ ##
60
+ # Returns the matrix element at row +i+ and column +j+.
61
+
51
62
  def [](i, j)
52
63
  i, j = indices(i, j)
53
64
 
54
65
  return @data[i][j]
55
66
  end
56
67
 
68
+ ##
69
+ # Sets the matrix element at row +i+ and column +j+ with +value+.
70
+
71
+ def []=(i, j, value)
72
+ i, j = indices(i, j)
73
+
74
+ @data[i][j] = value
75
+ end
76
+
77
+ ##
78
+ # Returns the number of rows.
79
+
57
80
  def rows
58
81
  @data.size
59
82
  end
60
83
 
84
+ ##
85
+ # Returns the number of columns.
86
+
61
87
  def cols
62
88
  @data[0].size
63
89
  end
64
90
 
91
+ ##
92
+ # Returns a flattened version of the matrix (Array).
93
+
65
94
  def flatten
66
95
  return @data.flatten
67
96
  end
68
97
 
69
- def []=(i, j, value)
70
- i, j = indices(i, j)
71
-
72
- @data[i][j] = value
73
- end
74
-
75
98
  def inspect
76
99
  row_names = @row_names || (0...self.rows).to_a.map { |v| v.to_s }
77
100
  column_names = @column_names || (0...self.cols).to_a.map { |v| v.to_s }
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror for the S4 class in R.
7
+
4
8
  class S4Class < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return type == "S4"
@@ -20,6 +24,9 @@ module Rust
20
24
  "immutable"
21
25
  end
22
26
 
27
+ ##
28
+ # Creates a new S4 element, given its +variable_name+, class name (+klass+), and +slots+.
29
+
23
30
  def initialize(variable_name, klass, slots)
24
31
  @klass = klass
25
32
  @slots = slots
@@ -27,6 +34,9 @@ module Rust
27
34
  self.r_mirror_to(variable_name)
28
35
  end
29
36
 
37
+ ##
38
+ # Returns the slot +key+ for the class name (+klass+).
39
+
30
40
  def [](key)
31
41
  raise ArgumentError, "Unknown slot `#{key}` for class `#@klass`" unless @slots.include?(key)
32
42
 
@@ -36,6 +46,9 @@ module Rust
36
46
  end
37
47
  alias :| :[]
38
48
 
49
+ ##
50
+ # Returns the slot +key+ for the class name (+klass+) with +value+.
51
+
39
52
  def []=(key, value)
40
53
  raise ArgumentError, "Unknown slot `#{key}` for class `#@klass`" unless @slots.include?(key)
41
54
 
@@ -44,10 +57,16 @@ module Rust
44
57
  end
45
58
  end
46
59
 
60
+ ##
61
+ # Returns the slots.
62
+
47
63
  def slots
48
64
  @slots
49
65
  end
50
66
 
67
+ ##
68
+ # Returns the class name.
69
+
51
70
  def class_name
52
71
  @klass
53
72
  end
@@ -1,6 +1,10 @@
1
1
  require_relative 'datatype'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Represents a sequence of values in R (through a call to the seq function).
7
+
4
8
  class Sequence < RustDatatype
5
9
  attr_reader :min
6
10
  attr_reader :max
@@ -9,15 +13,24 @@ module Rust
9
13
  return false
10
14
  end
11
15
 
16
+ ##
17
+ # Creates a new sequence from +min+ to +max+ with a given +step+ (default = 1).
18
+
12
19
  def initialize(min, max, step=1)
13
20
  @min = min
14
21
  @max = max
15
22
  @step = step
16
23
  end
17
24
 
18
- def step(step)
25
+ ##
26
+ # Sets the step to +step+.
27
+
28
+ def step=(step)
19
29
  @step = step
30
+
31
+ return self
20
32
  end
33
+ alias :step :step=
21
34
 
22
35
  def each
23
36
  (@min..@max).step(@step) do |v|
@@ -1,6 +1,10 @@
1
1
  require_relative '../core'
2
2
 
3
3
  module Rust
4
+
5
+ ##
6
+ # Mirror for an ANOVA model type in R. To create a new ANOVA model (aov), call the #generate method.
7
+
4
8
  class ANOVAModel < RustDatatype
5
9
  def self.can_pull?(type, klass)
6
10
  return type == "list" && [klass].flatten.include?("aov")
@@ -16,6 +20,10 @@ module Rust
16
20
  @model.load_in_r_as(variable_name)
17
21
  end
18
22
 
23
+ ##
24
+ # Generates a new ANOVA model with a given +formula+, +data+. +options+ can be specified and directly passed
25
+ # to the aov function in R.
26
+
19
27
  def self.generate(formula, data, **options)
20
28
  mapped = ""
21
29
  if options.size > 0
@@ -32,14 +40,23 @@ module Rust
32
40
  end
33
41
  end
34
42
 
43
+ ##
44
+ # Creates a new +model+.
45
+
35
46
  def initialize(model)
36
47
  @model = model
37
48
  end
38
49
 
50
+ ##
51
+ # Returns the model.
52
+
39
53
  def model
40
54
  @model
41
55
  end
42
56
 
57
+ ##
58
+ # Returns a summary of the ANOVA model through the summary function in R.
59
+
43
60
  def summary
44
61
  unless @summary
45
62
  Rust.exclusive do
@@ -5,7 +5,14 @@ require_relative '../stats/correlation'
5
5
  module Rust::Models
6
6
  end
7
7
 
8
+ ##
9
+ # Contains classes that allow to run regression models.
10
+
8
11
  module Rust::Models::Regression
12
+
13
+ ##
14
+ # Generic regression model in R.
15
+
9
16
  class RegressionModel < Rust::RustDatatype
10
17
  def self.can_pull?(type, klass)
11
18
  # Can only pull specific sub-types
@@ -16,6 +23,11 @@ module Rust::Models::Regression
16
23
  @model.load_in_r_as(variable_name)
17
24
  end
18
25
 
26
+ ##
27
+ # Generates a new regression model. +object_type+ is the Ruby class of the model object; +model_type+ represents
28
+ # the type of model at hand; +dependent_variable+ and +independent_variables+ are directly used as part of the
29
+ # model formula. +data+ represents the dataset to be used. +options+ can be specified and directly passed to the
30
+ # model.
19
31
 
20
32
  def self.generate(object_type, model_type, dependent_variable, independent_variables, data, **options)
21
33
  mapped = ""
@@ -36,6 +48,9 @@ module Rust::Models::Regression
36
48
  return result
37
49
  end
38
50
  end
51
+
52
+ ##
53
+ # Creates a new +model+.
39
54
 
40
55
  def initialize(model)
41
56
  raise StandardError if model.is_a?(RegressionModel)
@@ -46,6 +61,9 @@ module Rust::Models::Regression
46
61
  @model
47
62
  end
48
63
 
64
+ ##
65
+ # Returns the residuals of the model.
66
+
49
67
  def residuals
50
68
  Rust.exclusive do
51
69
  @residuals = Rust["residuals(#{self.r_mirror})"] unless @residuals
@@ -54,6 +72,9 @@ module Rust::Models::Regression
54
72
  return @residuals
55
73
  end
56
74
 
75
+ ##
76
+ # Returns the fitted values of the model.
77
+
57
78
  def fitted
58
79
  Rust.exclusive do
59
80
  @fitted = Rust["fitted(#{self.r_mirror})"] unless @fitted
@@ -62,22 +83,37 @@ module Rust::Models::Regression
62
83
  return @fitted
63
84
  end
64
85
 
86
+ ##
87
+ # Returns the actual values in the dataset.
88
+
65
89
  def actuals
66
90
  return self.fitted.zip(self.residuals).map { |couple| couple.sum }
67
91
  end
68
92
 
93
+ ##
94
+ # Returns the r-squared of the model.
95
+
69
96
  def r_2
70
97
  return self.summary|"r.squared"
71
98
  end
72
99
 
100
+ ##
101
+ # Returns the adjusted r-squared of the model.
102
+
73
103
  def r_2_adjusted
74
104
  return self.summary|"adj.r.squared"
75
105
  end
76
106
 
107
+ ##
108
+ # Returns the mean squared error of the model.
109
+
77
110
  def mse
78
111
  Rust::Descriptive.variance(self.residuals)
79
112
  end
80
113
 
114
+ ##
115
+ # Returns the coefficients of the model.
116
+
81
117
  def coefficients
82
118
  a = self.summary|"coefficients"
83
119
  end
@@ -86,6 +122,9 @@ module Rust::Models::Regression
86
122
  return model|name.to_s
87
123
  end
88
124
 
125
+ ##
126
+ # Returns a summary for the model using the summary function in R.
127
+
89
128
  def summary
90
129
  unless @summary
91
130
  Rust.exclusive do
@@ -101,6 +140,9 @@ module Rust::Models::Regression
101
140
  end
102
141
  end
103
142
 
143
+ ##
144
+ # Represents a linear regression model in R.
145
+
104
146
  class LinearRegressionModel < RegressionModel
105
147
  def self.can_pull?(type, klass)
106
148
  return type == "list" && klass == "lm"
@@ -112,6 +154,10 @@ module Rust::Models::Regression
112
154
  return LinearRegressionModel.new(model)
113
155
  end
114
156
 
157
+ ##
158
+ # Generates a linear regression model, given its +dependent_variable+ and +independent_variables+ and its +data+.
159
+ # +options+ can be specified and directly passed to the model.
160
+
115
161
  def self.generate(dependent_variable, independent_variables, data, **options)
116
162
  RegressionModel.generate(
117
163
  LinearRegressionModel,
@@ -124,6 +170,9 @@ module Rust::Models::Regression
124
170
  end
125
171
  end
126
172
 
173
+ ##
174
+ # Represents a linear mixed effects model in R.
175
+
127
176
  class LinearMixedEffectsModel < RegressionModel
128
177
  def self.can_pull?(type, klass)
129
178
  return type == "S4" && klass == "lmerModLmerTest"
@@ -152,6 +201,10 @@ module Rust::Models::Regression
152
201
  return @summary
153
202
  end
154
203
 
204
+ ##
205
+ # Generates a linear mixed effects model, given its +dependent_variable+ and +independent_variables+ and its +data+.
206
+ # +options+ can be specified and directly passed to the model.
207
+
155
208
  def self.generate(dependent_variable, fixed_effects, random_effects, data, **options)
156
209
  Rust.prerequisite("lmerTest")
157
210
  Rust.prerequisite("rsq")
@@ -169,7 +222,7 @@ module Rust::Models::Regression
169
222
  end
170
223
 
171
224
  def r_2
172
- Rust.exclusive do
225
+ Rust.exclusive do
173
226
  Rust._eval("tmp.rsq <- rsq(#{self.r_mirror}, adj=F)")
174
227
  return Rust['tmp.rsq']
175
228
  end
@@ -1,7 +1,16 @@
1
1
  require_relative 'core'
2
2
 
3
3
  module Rust::Plots
4
+
5
+ ##
6
+ # Allows to create one or many scatter plots.
7
+
4
8
  class ScatterPlot < BasePlot
9
+
10
+ ##
11
+ # Creates a new scatter plot, given two arrays of values +x+ and +y+ for the respective axes (optional).
12
+ # +options+ can be specified and directly passed to the plot function in R.
13
+
5
14
  def initialize(x = nil, y = nil, **options)
6
15
  super()
7
16
  @series = []
@@ -10,30 +19,46 @@ module Rust::Plots
10
19
  end
11
20
  end
12
21
 
22
+ ##
23
+ # Adds a new data series, given the values for the +x+ and +y+ axes.
24
+ # +options+ can be specified and directly passed to the plot function in R.
25
+
13
26
  def series(x, y, **options)
14
27
  @series << [x, y, options]
15
28
 
16
29
  return self
17
30
  end
18
31
 
32
+ ##
33
+ # Sets the thickness of the plot lines.
34
+
19
35
  def thickness(t)
20
36
  self['lwd'] = t
21
37
 
22
38
  return self
23
39
  end
24
40
 
41
+ ##
42
+ # Changes the plot type to lines.
43
+
25
44
  def lines()
26
45
  self['type'] = "l"
27
46
 
28
47
  return self
29
48
  end
30
49
 
50
+ ##
51
+ # Changes the plot type to points.
52
+
31
53
  def points()
32
54
  self['type'] = "p"
33
55
 
34
56
  return self
35
57
  end
36
58
 
59
+ ##
60
+ # Changes the plot type to lines and points.
61
+
37
62
  def lines_and_points()
38
63
  self['type'] = "b"
39
64
 
@@ -86,7 +111,14 @@ module Rust::Plots
86
111
  end
87
112
  end
88
113
 
114
+ ##
115
+ # Represents a bar plot in R.
116
+
89
117
  class BarPlot < BasePlot
118
+
119
+ ##
120
+ # Creates a new bar plot with the given +bars+ values.
121
+
90
122
  def initialize(bars)
91
123
  super()
92
124
  @bars = bars