Module: Statsample::Mx

Defined in:
lib/statsample/converters.rb

Class Method Summary collapse

Class Method Details

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



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
255
256
257
258
259
260
261
# File 'lib/statsample/converters.rb', line 229

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