Module: Statsample::Mx

Defined in:
lib/statsample/converters.rb

Class Method Summary collapse

Class Method Details

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/statsample/converters.rb', line 246

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.fields.join(" ")
    case type
      when :raw
      fp.puts "Rectangular"
      dataset.each do |row|
        out=dataset.fields.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