Class: Daru::Vector

Inherits:
Object
  • Object
show all
Includes:
Maths::Arithmetic::Vector, Maths::Statistics::Vector, Plotting::Vector, Enumerable
Defined in:
lib/daru/vector.rb,
lib/daru/extensions/rserve.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plotting::Vector

#plot

Methods included from Maths::Statistics::Vector

#average_deviation_population, #box_cox_transformation, #center, #coefficient_of_variation, #count, #dichotomize, #factors, #freqs, #frequencies, #kurtosis, #max, #max_index, #mean, #median, #median_absolute_deviation, #min, #mode, #percentile, #product, #proportion, #proportions, #range, #ranked, #sample_with_replacement, #sample_without_replacement, #skew, #standard_deviation_population, #standard_deviation_sample, #standard_error, #standardize, #sum, #sum_of_squared_deviation, #sum_of_squares, #variance_population, #variance_sample, #vector_centered_compute, #vector_percentile, #vector_standardized_compute

Methods included from Maths::Arithmetic::Vector

#%, #*, #**, #+, #-, #/, #abs, #exp, #round, #sqrt

Constructor Details

#initialize(source, opts = {}) ⇒ Vector

Create a Vector object.

Arguments

Hash. If Array, a numeric index will be created if not supplied in the options. Specifying more index elements than actual values in source will insert nil into the surplus index elements. When a Hash is specified, the keys of the Hash are taken as the index elements and the corresponding values as the values that populate the vector.

Options

  • :name - Name of the vector

  • :index - Index of the vector

  • :dtype - The underlying data type. Can be :array, :nmatrix or :gsl.

Default :array.

  • :nm_dtype - For NMatrix, the data type of the numbers. See the NMatrix docs for

further information on supported data type.

  • :missing_values - An Array of the values that are to be treated as ‘missing’.

nil is the default missing value.

Usage

vecarr = Daru::Vector.new [1,2,3,4], index: [:a, :e, :i, :o]
vechsh = Daru::Vector.new({a: 1, e: 2, i: 3, o: 4})

Parameters:

  • source (Array, Hash)
    • Supply elements in the form of an Array or a



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/daru/vector.rb', line 93

def initialize source, opts={}
  index = nil
  if source.is_a?(Hash)
    index  = source.keys
    source = source.values
  else
    index  = opts[:index]
    source = source || []
  end
  name   = opts[:name]
  set_name name

  @data  = cast_vector_to(opts[:dtype] || :array, source, opts[:nm_dtype])
  @index = create_index(index || @data.size)
  
  if @index.size > @data.size
    cast(dtype: :array) # NM with nils seg faults
    (@index.size - @data.size).times { @data << nil }
  elsif @index.size < @data.size
    raise IndexError, "Expected index size >= vector size. Index size : #{@index.size}, vector size : #{@data.size}"
  end

  @possibly_changed_type = true
  set_missing_values opts[:missing_values]
  set_missing_positions
  set_size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/daru/vector.rb', line 1031

def method_missing(name, *args, &block)
  if name.match(/(.+)\=/)
    self[name] = args[0]
  elsif has_index?(name)
    self[name]
  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#dtypeObject (readonly)

The underlying dtype of the Vector. Can be either :array, :nmatrix or :gsl.



52
53
54
# File 'lib/daru/vector.rb', line 52

def dtype
  @dtype
end

#indexObject (readonly)

The row index. Can be either Daru::Index or Daru::MultiIndex.



48
49
50
# File 'lib/daru/vector.rb', line 48

def index
  @index
end

#labelsObject

Store a hash of labels for values. Supplementary only. Recommend using index for proper usage.



61
62
63
# File 'lib/daru/vector.rb', line 61

def labels
  @labels
end

#missing_positionsObject (readonly)

An Array or the positions in the vector that are being treated as ‘missing’.



58
59
60
# File 'lib/daru/vector.rb', line 58

def missing_positions
  @missing_positions
end

#nameObject (readonly)

The name of the Daru::Vector. String.



46
47
48
# File 'lib/daru/vector.rb', line 46

def name
  @name
end

#nm_dtypeObject (readonly)

If the dtype is :nmatrix, this attribute represents the data type of the underlying NMatrix object. See NMatrix docs for more details on NMatrix data types.



56
57
58
# File 'lib/daru/vector.rb', line 56

def nm_dtype
  @nm_dtype
end

#sizeObject (readonly)

The total number of elements of the vector.



50
51
52
# File 'lib/daru/vector.rb', line 50

def size
  @size
end

Class Method Details

.[](*args) ⇒ Object

Create a vector using (almost) any object

  • Array: flattened

  • Range: transformed using to_a

  • Daru::Vector

  • Numeric and string values

Description

The ‘Vector.[]` class method creates a vector from almost any object that has a `#to_a` method defined on it. It is similar to R’s ‘c` method.

Usage

a = Daru::Vector[1,2,3,4,6..10]
#=>
# <Daru::Vector:99448510 @name = nil @size = 9 >
#   nil
# 0   1
# 1   2
# 2   3
# 3   4
# 4   6
# 5   7
# 6   8
# 7   9
# 8  10


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/daru/vector.rb', line 174

def self.[](*args)
  values = []
  args.each do |a|
    case a
    when Array
      values.concat a.flatten
    when Daru::Vector
      values.concat a.to_a
    when Range
      values.concat  a.to_a
    else
      values << a
    end
  end
  Daru::Vector.new(values)
end

._load(data) ⇒ Object

:nodoc:



1019
1020
1021
1022
1023
# File 'lib/daru/vector.rb', line 1019

def self._load(data) # :nodoc:
  h = Marshal.load(data)
  Daru::Vector.new(h[:data], index: h[:index], 
    name: h[:name], dtype: h[:dtype], missing_values: h[:missing_values])
end

.new_with_size(n, opts = {}, &block) ⇒ Object

Create a new vector by specifying the size and an optional value and block to generate values.

Description

The new_with_size class method lets you create a Daru::Vector by specifying the size as the argument. The optional block, if supplied, is run once for populating each element in the Vector.

The result of each run of the block is the value that is ultimately assigned to that position in the Vector.

Options

:value All the rest like .new



136
137
138
139
140
141
142
143
144
145
# File 'lib/daru/vector.rb', line 136

def self.new_with_size n, opts={}, &block
  value = opts[:value]
  opts.delete :value
  if block
    vector = Daru::Vector.new n.times.map { |i| block.call(i) }, opts
  else
    vector = Daru::Vector.new n.times.map { value }, opts
  end
  vector
end

Instance Method Details

#==(other) ⇒ Object

Two vectors are equal if the have the exact same index values corresponding with the exact same elements. Name is ignored.



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/daru/vector.rb', line 329

def == other
  case other
  when Daru::Vector
    @index == other.index and @size == other.size and
    @index.all? do |index|
      self[index] == other[index]
    end
  else
    # TODO: Compare against some other obj (string, number, etc.)
  end
end

#[](*indexes) ⇒ Object

Get one or more elements with specified index or a range.

Usage

# For vectors employing single layer Index

v[:one, :two] # => Daru::Vector with indexes :one and :two
v[:one]       # => Single element
v[:one..:three] # => Daru::Vector with indexes :one, :two and :three

# For vectors employing hierarchial multi index


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/daru/vector.rb', line 202

def [](*indexes)
  indexes.map! { |e| e.respond_to?(:to_sym) ? e.to_sym : e }
  location = indexes[0]
  if @index.is_a?(MultiIndex)
    result = 
    if location.is_a?(Integer)
      element_from_numeric_index(location)
    elsif location.is_a?(Range)
      arry = location.inject([]) do |memo, num|
        memo << element_from_numeric_index(num)
        memo
      end

      new_index = Daru::MultiIndex.new(@index.to_a[location])
      Daru::Vector.new(arry, index: new_index, name: @name, dtype: dtype)
    else
      sub_index = @index[indexes]

      if sub_index.is_a?(Integer)
        element_from_numeric_index(sub_index)
      else
        elements = sub_index.map do |tuple|
          @data[@index[(indexes + tuple)]]
        end
        Daru::Vector.new(elements, index: Daru::MultiIndex.new(sub_index.to_a),
          name: @name, dtype: @dtype)
      end
    end

    return result
  else
    unless indexes[1]
      case location
      when Range
        range = 
        if location.first.is_a?(Numeric)
          location
        else
          first = location.first
          last  = location.last

          (first..last)
        end
        indexes = @index[range]
      else
        return element_from_numeric_index(location)
      end
    end

    Daru::Vector.new indexes.map { |loc| @data[index_for(loc)] }, name: @name, 
      index: indexes.map { |e| named_index_for(e) }, dtype: @dtype
  end
end

#[]=(*location, value) ⇒ Object

Just like in Hashes, you can specify the index label of the Daru::Vector and assign an element an that place in the Daru::Vector.

Usage

v = Daru::Vector.new([1,2,3], index: [:a, :b, :c])
v[:a] = 999
#=> 
##<Daru::Vector:90257920 @name = nil @size = 3 >
#    nil
#  a 999
#  b   2
#  c   3


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/daru/vector.rb', line 269

def []=(*location, value)
  cast(dtype: :array) if value.nil? and dtype != :array

  @possibly_changed_type = true if @type == :object  and (value.nil? or 
    value.is_a?(Numeric))
  @possibly_changed_type = true if @type == :numeric and (!value.is_a?(Numeric) and
    !value.nil?)

  pos =
  if @index.is_a?(MultiIndex) and !location[0].is_a?(Integer)
    index_for location
  else
    index_for location[0]
  end

  if pos.is_a?(MultiIndex)
    pos.each do |sub_tuple|
      self[*(location + sub_tuple)] = value
    end
  else
    @data[pos] = value
  end

  set_size
  set_missing_positions unless Daru.lazy_update
end

#_dump(depth) ⇒ Object

:nodoc:



1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/daru/vector.rb', line 1010

def _dump(depth) # :nodoc:
  Marshal.dump({
    data:  @data.to_a, 
    dtype: @dtype, 
    name:  @name, 
    index: @index,
    missing_values: @missing_values})
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


455
456
457
# File 'lib/daru/vector.rb', line 455

def all? &block
  @data.data.all?(&block)
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


451
452
453
# File 'lib/daru/vector.rb', line 451

def any? &block
  @data.data.any?(&block)
end

#bootstrap(estimators, nr, s = nil) ⇒ Object

Bootstrap

Generate nr resamples (with replacement) of size s from vector, computing each estimate from estimators over each resample. estimators could be a) Hash with variable names as keys and lambdas as values

a.bootstrap(:log_s2=>lambda {|v| Math.log(v.variance)},1000)

b) Array with names of method to bootstrap

a.bootstrap([:mean, :sd],1000)

c) A single method to bootstrap

a.jacknife(:mean, 1000)

If s is nil, is set to vector size by default.

Returns a DataFrame where each vector is a vector of length nr containing the computed resample estimates.



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/daru/vector.rb', line 870

def bootstrap(estimators, nr, s=nil)
  s ||= size
  h_est, es, bss = prepare_bootstrap(estimators)

  nr.times do |i|
    bs = sample_with_replacement(s)
    es.each do |estimator|
      bss[estimator].push(h_est[estimator].call(bs))
    end
  end

  es.each do |est|
    bss[est] = Daru::Vector.new bss[est]
  end

  Daru::DataFrame.new bss
end

#cast(opts = {}) ⇒ Object

Cast a vector to a new data type.

Options

  • :dtype - :array for Ruby Array. :nmatrix for NMatrix.

Raises:

  • (ArgumentError)


382
383
384
385
386
387
388
# File 'lib/daru/vector.rb', line 382

def cast opts={}
  dt = opts[:dtype]
  raise ArgumentError, "Unsupported dtype #{opts[:dtype]}" unless 
    dt == :array or dt == :nmatrix or dt == :gsl

  @data = cast_vector_to dt unless @dtype == dt
end

#clone_structureObject

Copies the structure of the vector (i.e the index, size, etc.) and fills all all values with nils.



997
998
999
# File 'lib/daru/vector.rb', line 997

def clone_structure
  Daru::Vector.new(([nil]*@size), name: @name, index: @index.dup)
end

#concat(element, index = nil) ⇒ Object Also known as: push, <<

Append an element to the vector by specifying the element and index

Raises:

  • (IndexError)


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/daru/vector.rb', line 357

def concat element, index=nil
  raise IndexError, "Expected new unique index" if @index.include? index

  if index.nil? and @index.index_class == Integer
    @index = create_index(@size + 1)
    index  = @size
  else
    begin
      @index = create_index(@index + index)
    rescue StandardError => e
      raise e, "Expected valid index."
    end
  end
  @data[@index[index]] = element
  set_size
  set_missing_positions unless Daru.lazy_update
end

#daru_vector(*name) ⇒ Object Also known as: dv



1025
1026
1027
# File 'lib/daru/vector.rb', line 1025

def daru_vector *name
  self
end

#db_type(dbs = :mysql) ⇒ Object

Returns the database type for the vector, according to its content



982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/daru/vector.rb', line 982

def db_type(dbs=:mysql)
  # first, detect any character not number
  if @data.find {|v| v.to_s=~/\d{2,2}-\d{2,2}-\d{4,4}/} or @data.find {|v| v.to_s=~/\d{4,4}-\d{2,2}-\d{2,2}/}
    return "DATE"
  elsif @data.find {|v|  v.to_s=~/[^0-9e.-]/ }
    return "VARCHAR (255)"
  elsif @data.find {|v| v.to_s=~/\./}
    return "DOUBLE"
  else
    return "INTEGER"
  end
end

#delete(element) ⇒ Object

Delete an element by value



391
392
393
# File 'lib/daru/vector.rb', line 391

def delete element
  self.delete_at index_of(element)      
end

#delete_at(index) ⇒ Object

Delete element by index



396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/daru/vector.rb', line 396

def delete_at index
  idx = named_index_for index
  @data.delete_at @index[idx]

  if @index.index_class == Integer
    @index = Daru::Index.new @size-1
  else
    @index = Daru::Index.new (@index.to_a - [idx])
  end

  set_size
  set_missing_positions unless Daru.lazy_update
end

#delete_if(&block) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/daru/vector.rb', line 518

def delete_if &block
  return to_enum(:delete_if) unless block_given?

  keep_e = []
  keep_i = []
  each_with_index do |n, i|
    if yield(n)
      keep_e << n
      keep_i << i
    end
  end

  @data = cast_vector_to @dtype, keep_e
  @index = @index.is_a?(MultiIndex) ? MultiIndex.new(keep_i) : Index.new(keep_i)
  set_missing_positions unless Daru.lazy_update
  set_size

  self
end

#detach_indexObject



667
668
669
670
671
672
# File 'lib/daru/vector.rb', line 667

def detach_index
  Daru::DataFrame.new({
    index: @index.to_a.map(&:to_s),
    vector: @data.to_a
  })
end

#dupObject

Duplicate elements and indexes



851
852
853
# File 'lib/daru/vector.rb', line 851

def dup 
  Daru::Vector.new @data.dup, name: @name, index: @index.dup
end

#each(&block) ⇒ Object



17
18
19
20
21
22
# File 'lib/daru/vector.rb', line 17

def each(&block)
  return to_enum(:each) unless block_given?
  
  @data.each(&block)
  self
end

#each_index(&block) ⇒ Object



24
25
26
27
28
29
# File 'lib/daru/vector.rb', line 24

def each_index(&block)
  return to_enum(:each_index) unless block_given?

  @index.each(&block)
  self
end

#each_with_index(&block) ⇒ Object



31
32
33
34
35
36
# File 'lib/daru/vector.rb', line 31

def each_with_index(&block)
  return to_enum(:each_with_index) unless block_given?

  @index.each { |i|  yield(self[i], i) }
  self
end

#exists?(value) ⇒ Boolean

Returns true if the value passed is actually exists or is not marked as a *missing value*.

Returns:

  • (Boolean)


498
499
500
# File 'lib/daru/vector.rb', line 498

def exists? value
  !@missing_values.has_key?(self[index_of(value)])
end

#has_index?(index) ⇒ Boolean

Returns true if an index exists

Returns:

  • (Boolean)


685
686
687
# File 'lib/daru/vector.rb', line 685

def has_index? index
  @index.include? index
end

#has_missing_data?Boolean Also known as: flawed?

Reports whether missing data is present in the Vector.

Returns:

  • (Boolean)


350
351
352
# File 'lib/daru/vector.rb', line 350

def has_missing_data?
  !missing_positions.empty?
end

#head(q = 10) ⇒ Object



341
342
343
# File 'lib/daru/vector.rb', line 341

def head q=10
  self[0..(q-1)]
end

#index_of(element) ⇒ Object

Get index of element



436
437
438
# File 'lib/daru/vector.rb', line 436

def index_of element
  @index.key @data.index(element)
end

#inspect(spacing = 20, threshold = 15) ⇒ Object

Over rides original inspect for pretty printing in irb



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/daru/vector.rb', line 804

def inspect spacing=20, threshold=15
  longest = [@name.to_s.size,
             (@index.to_a.map(&:to_s).map(&:size).max || 0), 
             (@data    .map(&:to_s).map(&:size).max || 0),
             'nil'.size].max

  content   = ""
  longest   = spacing if longest > spacing
  name      = @name || 'nil'
  formatter = "\n%#{longest}.#{longest}s %#{longest}.#{longest}s"
  content  += "\n#<" + self.class.to_s + ":" + self.object_id.to_s + " @name = " + name.to_s + " @size = " + size.to_s + " >"

  content += sprintf formatter, "", name
  @index.each_with_index do |index, num|
    content += sprintf formatter, index.to_s, (self[*index] || 'nil').to_s
    if num > threshold
      content += sprintf formatter, '...', '...'
      break
    end
  end
  content += "\n"

  content
end

#is_nil?Boolean

Returns a vector which has true in the position where the element in self is nil, and false otherwise.

Usage

v = Daru::Vector.new([1,2,4,nil])
v.is_nil?
# => 
#<Daru::Vector:89421000 @name = nil @size = 4 >
#      nil
#  0  false
#  1  false
#  2  false
#  3  true

Returns:

  • (Boolean)


634
635
636
637
638
639
640
641
# File 'lib/daru/vector.rb', line 634

def is_nil?
  nil_truth_vector = clone_structure
  @index.each do |idx|
    nil_truth_vector[idx] = self[idx].nil? ? true : false
  end

  nil_truth_vector
end

#jackknife(estimators, k = 1) ⇒ Object

Jacknife

Returns a dataset with jacknife delete-k estimators estimators could be: a) Hash with variable names as keys and lambdas as values

a.jacknife(:log_s2=>lambda {|v| Math.log(v.variance)})

b) Array with method names to jacknife

a.jacknife([:mean, :sd])

c) A single method to jacknife

a.jacknife(:mean)

k represent the block size for block jacknife. By default is set to 1, for classic delete-one jacknife.

Returns a dataset where each vector is an vector of length cases/k containing the computed jacknife estimates.

Reference:

  • Sawyer, S. (2005). Resampling Data: Using a Statistical Jacknife.



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/daru/vector.rb', line 905

def jackknife(estimators, k=1)
  raise "n should be divisible by k:#{k}" unless size % k==0

  nb = (size / k).to_i
  h_est, es, ps = prepare_bootstrap(estimators)

  est_n = es.inject({}) do |h,v|
    h[v] = h_est[v].call(self)
    h
  end

  nb.times do |i|
    other = @data.dup
    other.slice!(i*k, k)
    other = Daru::Vector.new other

    es.each do |estimator|
      # Add pseudovalue
      ps[estimator].push(
        nb * est_n[estimator] - (nb-1) * h_est[estimator].call(other))
    end
  end

  es.each do |est|
    ps[est] = Daru::Vector.new ps[est]
  end
  Daru::DataFrame.new ps
end

#map!(&block) ⇒ Object



38
39
40
41
42
43
# File 'lib/daru/vector.rb', line 38

def map!(&block)
  return to_enum(:map!) unless block_given?
  @data.map!(&block)
  update
  self
end

#missing_valuesObject

The values to be treated as ‘missing’. nil is the default missing type. To set missing values see the missing_values= method.



298
299
300
# File 'lib/daru/vector.rb', line 298

def missing_values
  @missing_values.keys  
end

#missing_values=(values) ⇒ Object

Assign an Array to treat certain values as ‘missing’.

Usage

v = Daru::Vector.new [1,2,3,4,5]
v.missing_values = [3]
v.update
v.missing_positions 
#=> [2]


311
312
313
314
# File 'lib/daru/vector.rb', line 311

def missing_values= values
  set_missing_values values
  set_missing_positions unless Daru.lazy_update
end

#n_validObject

number of non-missing elements



680
681
682
# File 'lib/daru/vector.rb', line 680

def n_valid
  @size - missing_positions.size
end

#not_nil?Boolean

Opposite of #is_nil?

Returns:

  • (Boolean)


644
645
646
647
648
649
650
651
# File 'lib/daru/vector.rb', line 644

def not_nil?
  nil_truth_vector = clone_structure
  @index.each do |idx|
    nil_truth_vector[idx] = self[idx].nil? ? false : true
  end

  nil_truth_vector
end

#only_missing(as_a = :vector) ⇒ Object

Returns a Vector containing only missing data (preserves indexes).



961
962
963
964
965
966
967
# File 'lib/daru/vector.rb', line 961

def only_missing as_a=:vector
  if as_a == :vector
    self[*missing_positions]
  elsif as_a == :array
    self[*missing_positions].to_a
  end
end

#only_numericsObject

Returns a Vector with only numerical data. Missing data is included but non-Numeric objects are excluded. Preserves index.



971
972
973
974
975
976
977
978
979
# File 'lib/daru/vector.rb', line 971

def only_numerics
  numeric_indexes = []

  each_with_index do |v, i|
    numeric_indexes << i if(v.kind_of?(Numeric) or @missing_values.has_key?(v))
  end

  self[*numeric_indexes]
end

#only_valid(as_a = :vector, duplicate = true) ⇒ Object

Creates a new vector consisting only of non-nil data

Arguments

as an Array. Otherwise will return a Daru::Vector.

vector, setting this to false will return the same vector. Otherwise, a duplicate will be returned irrespective of presence of missing data.



945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/daru/vector.rb', line 945

def only_valid as_a=:vector, duplicate=true
  return self.dup if !has_missing_data? and as_a == :vector and duplicate
  return self if !has_missing_data? and as_a == :vector and !duplicate
  return self.to_a if !has_missing_data? and as_a != :vector

  new_index = @index.to_a - missing_positions
  new_vector = new_index.map do |idx|
    self[idx]
  end

  return new_vector if as_a != :vector
  
  Daru::Vector.new new_vector, index: new_index, name: @name, dtype: dtype
end

#recode(dt = nil, &block) ⇒ Object

Like map, but returns a Daru::Vector with the returned values.



503
504
505
506
507
# File 'lib/daru/vector.rb', line 503

def recode dt=nil, &block
  return to_enum(:recode) unless block_given?

  dup.recode! dt, &block
end

#recode!(dt = nil, &block) ⇒ Object

Destructive version of recode!



510
511
512
513
514
515
516
# File 'lib/daru/vector.rb', line 510

def recode! dt=nil, &block
  return to_enum(:recode!) unless block_given?

  @data.map!(&block).data
  @data = cast_vector_to(dt || @dtype)
  self
end

#reindex(new_index) ⇒ Object

Create a new vector with a different index.

Parameters:

  • new_index (Symbol, Array, Daru::Index)

    The new index. Passing :seq will reindex with sequential numbers from 0 to (n-1).



833
834
835
836
# File 'lib/daru/vector.rb', line 833

def reindex new_index
  index = create_index(new_index == :seq ? @size : new_index)
  Daru::Vector.new @data.to_a, index: index, name: name, dtype: @dtype
end

#rename(new_name) ⇒ Object

Give the vector a new name

Parameters:

  • new_name (Symbol)

    The new name.



841
842
843
844
845
846
847
848
# File 'lib/daru/vector.rb', line 841

def rename new_name
  if new_name.is_a?(Numeric)
    @name = new_name 
    return
  end
  
  @name = new_name.to_sym
end

#replace_nils(replacement) ⇒ Object

Non-destructive version of #replace_nils!



675
676
677
# File 'lib/daru/vector.rb', line 675

def replace_nils replacement
  self.dup.replace_nils!(replacement)
end

#replace_nils!(replacement) ⇒ Object

Replace all nils in the vector with the value passed as an argument. Destructive. See #replace_nils for non-destructive version

Arguments

  • replacement - The value which should replace all nils



659
660
661
662
663
664
665
# File 'lib/daru/vector.rb', line 659

def replace_nils! replacement
  missing_positions.each do |idx|
    self[idx] = replacement
  end

  self
end

#report_building(b) ⇒ Object



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/daru/vector.rb', line 774

def report_building b
  b.section(:name => name) do |s|
    s.text "n :#{size}"
    s.text "n valid:#{n_valid}"
    if @type == :object
      s.text  "factors: #{factors.to_a.join(',')}"
      s.text  "mode: #{mode}"

      s.table(:name => "Distribution") do |t|
        frequencies.sort_by { |a| a.to_s }.each do |k,v|
          key = @index.include?(k) ? @index[k] : k
          t.row [key, v , ("%0.2f%%" % (v.quo(n_valid)*100))]
        end
      end
    end

    s.text "median: #{median.to_s}" if (@type==:numeric or @type==:numeric)
    if @type==:numeric
      s.text "mean: %0.4f" % mean
      if sd
        s.text "std.dev.: %0.4f" % sd
        s.text "std.err.: %0.4f" % se
        s.text "skew: %0.4f" % skew
        s.text "kurtosis: %0.4f" % kurtosis
      end
    end
  end
end

#reset_index!Object



615
616
617
618
# File 'lib/daru/vector.rb', line 615

def reset_index!
  @index = Daru::Index.new(Array.new(size) { |i| i })
  self
end

#save(filename) ⇒ Object

Save the vector to a file

Arguments

  • filename - Path of file where the vector is to be saved



1006
1007
1008
# File 'lib/daru/vector.rb', line 1006

def save filename
  Daru::IO.save self, filename
end

#sort(opts = {}, &block) ⇒ Object

Sorts a vector according to its values. If a block is specified, the contents will be evaluated and data will be swapped whenever the block evaluates to true. Defaults to ascending order sorting. Any missing values will be put at the end of the vector. Preserves indexing. Default sort algorithm is quick sort.

Options

  • :ascending - if false, will sort in descending order. Defaults to true.

  • :type - Specify the sorting algorithm. Only supports quick_sort for now.

Usage

v = Daru::Vector.new ["My first guitar", "jazz", "guitar"]
# Say you want to sort these strings by length.
v.sort(ascending: false) { |a,b| a.length <=> b.length }


475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/daru/vector.rb', line 475

def sort opts={}, &block
  opts = {
    ascending: true,
    type: :quick_sort
  }.merge(opts)

  block = lambda { |a,b| a <=> b } unless block

  order = opts[:ascending] ? :ascending : :descending
  vector, index = send(opts[:type], @data.to_a.dup, @index.to_a, order, &block)
  index = @index.is_a?(MultiIndex) ? Daru::MultiIndex.new(index) : index

  Daru::Vector.new(vector, index: create_index(index), name: @name, dtype: @dtype)
end

#sorted_data(&block) ⇒ Object

Just sort the data and get an Array in return using Enumerable#sort. Non-destructive.



492
493
494
# File 'lib/daru/vector.rb', line 492

def sorted_data &block
  @data.to_a.sort(&block)
end

#split_by_separator(sep = ",") ⇒ Object

Returns a hash of Vectors, defined by the different values defined on the fields Example:

a=Daru::Vector.new(["a,b","c,d","a,b"])
a.split_by_separator
=>  {"a"=>#<Daru::Vector:0x7f2dbcc09d88
      @data=[1, 0, 1]>,
     "b"=>#<Daru::Vector:0x7f2dbcc09c48
      @data=[1, 1, 0]>,
    "c"=>#<Daru::Vector:0x7f2dbcc09b08
      @data=[0, 1, 1]>}


581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/daru/vector.rb', line 581

def split_by_separator sep=","
  split_data = splitted sep
  factors = split_data.flatten.uniq.compact

  out = factors.inject({}) do |h,x|
    h[x] = []
    h
  end

  split_data.each do |r|
    if r.nil?
      factors.each do |f|
        out[f].push(nil)
      end
    else
      factors.each do |f|
        out[f].push(r.include?(f) ? 1:0)
      end
    end
  end

  out.inject({}) do |s,v|
    s[v[0]] = Daru::Vector.new v[1]
    s
  end
end

#split_by_separator_freq(sep = ",") ⇒ Object



608
609
610
611
612
613
# File 'lib/daru/vector.rb', line 608

def split_by_separator_freq(sep=",")
  split_by_separator(sep).inject({}) do |a,v|
    a[v[0]] = v[1].inject { |s,x| s+x.to_i }
    a
  end
end

#splitted(sep = ",") ⇒ Object

Return an Array with the data splitted by a separator.

a=Daru::Vector.new(["a,b","c,d","a,b","d"])
a.splitted
  =>
[["a","b"],["c","d"],["a","b"],["d"]]


556
557
558
559
560
561
562
563
564
565
566
# File 'lib/daru/vector.rb', line 556

def splitted sep=","
  @data.map do |s|
    if s.nil?
      nil
    elsif s.respond_to? :split
      s.split sep
    else
      [s]
    end
  end
end

#summary(method = :to_text) ⇒ Object

Create a summary of the Vector using Report Builder.



770
771
772
# File 'lib/daru/vector.rb', line 770

def summary(method = :to_text)
  ReportBuilder.new(no_title: true).add(self).send(method)
end

#tail(q = 10) ⇒ Object



345
346
347
# File 'lib/daru/vector.rb', line 345

def tail q=10
  self[(@size - q - 1)..(@size-1)]
end

#to_aObject

Return an array



727
728
729
# File 'lib/daru/vector.rb', line 727

def to_a
  @data.to_a
end

#to_gslObject

If dtype != gsl, will convert data to GSL::Vector with to_a. Otherwise returns the stored GSL::Vector object.



706
707
708
709
710
711
712
713
714
715
716
# File 'lib/daru/vector.rb', line 706

def to_gsl
  if Daru.has_gsl?
    if dtype == :gsl
      return @data.data
    else
      GSL::Vector.alloc only_valid(:array).to_a
    end
  else
    raise NoMethodError, "Install gsl-nmatrix for access to this functionality."
  end
end

#to_hashObject

Convert to hash. Hash keys are indexes and values are the correspoding elements



719
720
721
722
723
724
# File 'lib/daru/vector.rb', line 719

def to_hash
  @index.inject({}) do |hsh, index|
    hsh[index] = self[index]
    hsh
  end
end

#to_html(threshold = 30) ⇒ Object

Convert to html for iruby



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/daru/vector.rb', line 737

def to_html threshold=30
  name = @name || 'nil'
  html = "<table>" + 
    "<tr>" +
      "<th colspan=\"2\">" + 
        "Daru::Vector:#{self.object_id} " + " size: #{size}" + 
      "</th>" +
    "</tr>"
  html += '<tr><th> </th><th>' + name.to_s + '</th></tr>'
  @index.each_with_index do |index, num|
    html += '<tr><td>' + index.to_s + '</td>' + '<td>' + self[index].to_s + '</td></tr>'

    if num > threshold
      html += '<tr><td>...</td><td>...</td></tr>'

      last_index = @index.to_a.last
      html += '<tr>' + 
                '<td>' + last_index.to_s       + '</td>' +
                '<td>' + self[last_index].to_s + '</td>' +
              '</tr>'
      break
    end
  end
  html += '</table>'

  html
end

#to_json(*args) ⇒ Object

Convert the hash from to_hash to json



732
733
734
# File 'lib/daru/vector.rb', line 732

def to_json *args 
  self.to_hash.to_json
end

#to_matrix(axis = :horizontal) ⇒ Object

Convert Vector to a horizontal or vertical Ruby Matrix.

Arguments

  • axis - Specify whether you want a :horizontal or a :vertical matrix.



694
695
696
697
698
699
700
701
702
# File 'lib/daru/vector.rb', line 694

def to_matrix axis=:horizontal
  if axis == :horizontal
    Matrix[to_a]
  elsif axis == :vertical
    Matrix.columns([to_a])
  else
    raise ArgumentError, "axis should be either :horizontal or :vertical, not #{axis}"
  end
end

#to_REXPObject



17
18
19
# File 'lib/daru/extensions/rserve.rb', line 17

def to_REXP
  Rserve::REXP::Wrapper.wrap(self.to_a)
end

#to_sObject



765
766
767
# File 'lib/daru/vector.rb', line 765

def to_s
  to_html
end

#typeObject

The type of data contained in the vector. Can be :object or :numeric. If the underlying dtype is an NMatrix, this method will return the data type of the NMatrix object.

Running through the data to figure out the kind of data is delayed to the last possible moment.



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/daru/vector.rb', line 416

def type
  return @data.nm_dtype if dtype == :nmatrix

  if @type.nil? or @possibly_changed_type
    @type = :numeric
    self.each do |e|
      unless e.nil?
        unless e.is_a?(Numeric)
          @type = :object
          break
        end
      end
    end
    @possibly_changed_type = false
  end

  @type
end

#uniqObject

Keep only unique elements of the vector alongwith their indexes.



441
442
443
444
445
446
447
448
449
# File 'lib/daru/vector.rb', line 441

def uniq
  uniq_vector = @data.uniq
  new_index   = uniq_vector.inject([]) do |acc, element|  
    acc << index_of(element) 
    acc
  end

  Daru::Vector.new uniq_vector, name: @name, index: new_index, dtype: @dtype
end

#updateObject

Method for updating the metadata (i.e. missing value positions) of the after assingment/deletion etc. are complete. This is provided so that time is not wasted in creating the metadata for the vector each time assignment/deletion of elements is done. Updating data this way is called lazy loading. To set or unset lazy loading, see the .lazy_update= method.



321
322
323
324
325
# File 'lib/daru/vector.rb', line 321

def update
  if Daru.lazy_update
    set_missing_positions
  end
end

#verify(&block) ⇒ Object

Reports all values that doesn’t comply with a condition. Returns a hash with the index of data and the invalid data.



540
541
542
543
544
545
546
547
548
549
# File 'lib/daru/vector.rb', line 540

def verify &block
  h = {}
  (0...size).each do |i|
    if !(yield @data[i])
      h[i] = @data[i]
    end
  end

  h
end