Module: Statsample::Mx

Defined in:
lib/statsample/converters.rb

Class Method Summary collapse

Class Method Details

.write(dataset, filename, type = :covariance) ⇒ Object


86
87
88
89
90
91
92
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
# File 'lib/statsample/converters.rb', line 86

def write(dataset,filename,type=:covariance)
  puts "Writing MX File"
  File.open(filename,"w") do |fp|
    fp.puts "! #{filename}"
    fp.puts "! Output generated by Statsample"
    fp.puts "Data Ninput=#{dataset.fields.size} Nobservations=#{dataset.cases}"
    fp.puts "Labels " + dataset.vectors.to_a.join(" ")
    case type
      when :raw
      fp.puts "Rectangular"
      dataset.each do |row|
        out=dataset.vectors.to_a.collect do |f|
          if dataset[f].is_valid? row[f]
            row[f]
          else
            "."
          end
        end
        fp.puts out.join("\t")
      end
      fp.puts "End Rectangular"
    when :covariance
      fp.puts " CMatrix Full"
      cm=Statsample::Bivariate.covariance_matrix(dataset)
      d=(0...(cm.row_size)).collect {|row|
        (0...(cm.column_size)).collect{|col|
          cm[row,col].nil? ? "." : sprintf("%0.3f", cm[row,col])
        }.join(" ")
      }.join("\n")
      fp.puts d
    end
  end
end