Class: Statsample::FormulaWrapper
- Defined in:
- lib/statsample/formula/formula.rb
Overview
This class recognizes what terms are numeric and accordingly forms groups which are fed to Formula Once they are parsed with Formula, they are combined back
Instance Attribute Summary collapse
-
#canonical_tokens ⇒ Object
readonly
Returns the value of attribute canonical_tokens.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
-
#canonical_to_s ⇒ String
Returns canonical tokens in a readable form.
-
#initialize(formula, df) ⇒ FormulaWrapper
constructor
Initializes formula wrapper object to parse a given formula into some tokens which do not overlap one another.
-
#non_redundant_tokens ⇒ Array
Returns tokens to produce non-redundant design matrix.
Constructor Details
#initialize(formula, df) ⇒ FormulaWrapper
Specify 0 as a term in the formula if you do not want constant to be included in the parsed formula
Initializes formula wrapper object to parse a given formula into some tokens which do not overlap one another.
21 22 23 24 25 26 27 28 29 |
# File 'lib/statsample/formula/formula.rb', line 21 def initialize(formula, df) @df = df # @y store the LHS term that is name of vector to be predicted # @tokens store the RHS terms of the formula @y, *@tokens = split_to_tokens(formula) @tokens = @tokens.uniq.sort manage_constant_term @canonical_tokens = non_redundant_tokens end |
Instance Attribute Details
#canonical_tokens ⇒ Object (readonly)
Returns the value of attribute canonical_tokens.
6 7 8 |
# File 'lib/statsample/formula/formula.rb', line 6 def canonical_tokens @canonical_tokens end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
6 7 8 |
# File 'lib/statsample/formula/formula.rb', line 6 def tokens @tokens end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
6 7 8 |
# File 'lib/statsample/formula/formula.rb', line 6 def y @y end |
Instance Method Details
#canonical_to_s ⇒ String
‘y~a+b(-)’ means ‘a’ exist in full rank expansion and ‘b(-)’ exist in reduced rank expansion
Returns canonical tokens in a readable form.
41 42 43 |
# File 'lib/statsample/formula/formula.rb', line 41 def canonical_to_s canonical_tokens.join '+' end |
#non_redundant_tokens ⇒ Array
Returns tokens to produce non-redundant design matrix
47 48 49 50 51 52 53 54 |
# File 'lib/statsample/formula/formula.rb', line 47 def non_redundant_tokens groups = split_to_groups # TODO: An enhancement # Right now x:c appears as c:x groups.each { |k, v| groups[k] = strip_numeric v, k } groups.each { |k, v| groups[k] = Formula.new(v).canonical_tokens } groups.flat_map { |k, v| add_numeric v, k } end |