class Daru::Vector

Public Instance Methods

to_indicator_cols_df(name:, for_model_without_intercept: false) click to toggle source

Auxiliary function which is useful for fitting of linear models. Transforms a Daru::Vector, whose entries are assumed to represent levels of a categorical variable, into a Daru::DataFrame with a column of zeros and ones for each category. If full is set to false, then the first category is discarded, which is useful to generate a design matrix for linear regression, when a intercept term is present in the model.

Arguments

  • name - used for the naming of the columns of the returned data frame

  • for_model_without_intercept - if false (which is the default), then the first column of the produced data frame will be excluded

Usage

a # => <Daru::Vector:70083983735480 @name = nil @size = 5 >

  nil
0 1.0
1 2.0
2 3.0
3 1.0
4 1.0

a.to_indicator_cols_df(name: 'MyVar', for_model_without_intercept: true) # =>

#<Daru::DataFrame:70083988870200 @name = 08de5ef9-5c59-4acf-9853-04289d1a4ba5 @size = 5>
            MyVar.1.0  MyVar.2.0  MyVar.3.0 
         0        1.0        0.0        0.0 
         1        0.0        1.0        0.0 
         2        0.0        0.0        1.0 
         3        1.0        0.0        0.0 
         4        1.0        0.0        0.0
# File lib/mixed_models/daru_methods.rb, line 123
def to_indicator_cols_df(name:, for_model_without_intercept: false)
  levels = self.to_a.uniq
  names = levels.map { |e| "#{name}_lvl_#{e}".to_sym }
  unless for_model_without_intercept 
    levels.shift
    names.shift
  end

  cols_array = Array.new
  levels.each do |l|
    col = Array.new
    self.each { |e| e==l ? col.push(1.0) : col.push(0.0) }
    cols_array.push(col)
  end

  return Daru::DataFrame.new(cols_array, order: names)
end
to_nm(dtype: :float64, stype: :dense) click to toggle source

Transform a Daru::Vector into a NMatrix

Arguments

  • dtype - the dtype of the returned NMatrix; defaults to float64

  • stype - the stype of the returned NMatrix; defaults to dense

# File lib/mixed_models/daru_methods.rb, line 86
def to_nm(dtype: :float64, stype: :dense)
  n = self.size
  return NMatrix.new([n,1], self.to_a, dtype: dtype, stype: stype)
end