Class: Statsample::Token
Overview
To encapsulate interaction as well as non-interaction terms
Instance Attribute Summary collapse
-
#full ⇒ Object
readonly
Returns the value of attribute full.
-
#interact_terms ⇒ Object
readonly
Returns the value of attribute interact_terms.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #add(other) ⇒ Object
- #expand ⇒ Object
- #hash ⇒ Object
-
#initialize(value, full = true) ⇒ Token
constructor
A new instance of Token.
- #size ⇒ Object
- #to_df(df) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value, full = true) ⇒ Token
Returns a new instance of Token.
164 165 166 167 |
# File 'lib/statsample/formula/formula.rb', line 164 def initialize(value, full = true) @interact_terms = value.include?(':') ? value.split(':') : [value] @full = coerce_full full end |
Instance Attribute Details
#full ⇒ Object (readonly)
Returns the value of attribute full.
162 163 164 |
# File 'lib/statsample/formula/formula.rb', line 162 def full @full end |
#interact_terms ⇒ Object (readonly)
Returns the value of attribute interact_terms.
162 163 164 |
# File 'lib/statsample/formula/formula.rb', line 162 def interact_terms @interact_terms end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
162 163 164 |
# File 'lib/statsample/formula/formula.rb', line 162 def value @value end |
Instance Method Details
#<=>(other) ⇒ Object
223 224 225 |
# File 'lib/statsample/formula/formula.rb', line 223 def <=>(other) size <=> other.size end |
#==(other) ⇒ Object Also known as: eql?
212 213 214 215 |
# File 'lib/statsample/formula/formula.rb', line 212 def ==(other) value == other.value && full == other.full end |
#add(other) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/statsample/formula/formula.rb', line 180 def add(other) # ANYTHING + FACTOR- : ANYTHING = FACTOR : ANYTHING # ANYTHING + ANYTHING : FACTOR- = ANYTHING : FACTOR if size > other.size other.add self elsif other.size == 2 && size == 1 && other.interact_terms.last == value && other.full.last == full.first && other.full.first == false Token.new( "#{other.interact_terms.first}:#{value}", [true, other.full.last] ) elsif other.size == 2 && size == 1 && other.interact_terms.first == value && other.full.first == full.first && other.full.last == false Token.new( "#{value}:#{other.interact_terms.last}", [other.full.first, true] ) elsif value == '1' && other.size == 1 Token.new(other.value, true) end end |
#expand ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/statsample/formula/formula.rb', line 234 def case size when 0 [self] when 1 [Token.new('1'), Token.new(value, false)] when 2 a, b = interact_terms [Token.new('1'), Token.new(a, false), Token.new(b, false), Token.new(a + ':' + b, [false, false])] end end |
#hash ⇒ Object
219 220 221 |
# File 'lib/statsample/formula/formula.rb', line 219 def hash value.hash ^ full.hash end |
#size ⇒ Object
173 174 175 176 177 178 |
# File 'lib/statsample/formula/formula.rb', line 173 def size # TODO: Return size 1 for value '1' also # CAn't do this at the moment because have to make # changes in sorting first value == '1' ? 0 : interact_terms.size end |
#to_df(df) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/statsample/formula/formula.rb', line 247 def to_df(df) case size when 1 if df[value].category? df[value].contrast_code full: full.first else Daru::DataFrame.new value => df[value].to_a end when 2 to_df_when_interaction(df) end end |
#to_s ⇒ Object
227 228 229 230 231 232 |
# File 'lib/statsample/formula/formula.rb', line 227 def to_s interact_terms .zip(full) .map { |t, f| f ? t : t + '(-)' } .join ':' end |