# = statsample.rb - # Statsample - Statistic package for Ruby # Copyright (C) 2008-2009 Claudio Bustos # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # $:.unshift(File.dirname(__FILE__)) $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext")) require 'delegate' require 'matrix' class Numeric def square ; self * self ; end end def create_test(*args,&proc) description=args.shift fields=args [description, fields, Proc.new] end begin require 'rbgsl' HAS_GSL=true rescue LoadError HAS_GSL=false end begin require 'alglib' HAS_ALGIB=true rescue LoadError HAS_ALGIB=false end begin require 'statsample/optimization' rescue LoadError module Statsample OPTIMIZED=false end end # # Modules for statistical analysis # See first: # * Converter : several modules to import and export data # * Vector: The base class of all analysis # * Dataset: An union of vectors. # module Statsample VERSION = '0.3.2' SPLIT_TOKEN = "," autoload(:Database, 'statsample/converters') autoload(:Anova, 'statsample/anova') autoload(:CSV, 'statsample/converters') autoload(:Excel, 'statsample/converters') autoload(:GGobi, 'statsample/converters') autoload(:DominanceAnalysis, 'statsample/dominanceanalysis') autoload(:HtmlReport, 'statsample/htmlreport') autoload(:Mx, 'statsample/converters') autoload(:Resample, 'statsample/resample') autoload(:SRS, 'statsample/srs') autoload(:Codification, 'statsample/codification') autoload(:Reliability, 'statsample/reliability') autoload(:Bivariate, 'statsample/bivariate') autoload(:Multivariate, 'statsample/multivariate') autoload(:Regression, 'statsample/regression') autoload(:Test, 'statsample/test') def self.load(filename) fp=File.open(filename,"r") o=Marshal.load(fp) fp.close o end module Util # Reference: http://www.itl.nist.gov/div898/handbook/eda/section3/normprpl.htm def normal_order_statistic_medians(i,n) if i==1 u= 1.0 - normal_order_statistic_medians(n,n) elsif i==n u=0.5**(1 / n.to_f) else u= (i - 0.3175) / (n + 0.365) end u end end module Writable def save(filename) fp=File.open(filename,"w") Marshal.dump(self,fp) fp.close end end module HtmlSummary def add_line(n=nil) self << "
" end def nl self << "
" end def add(text) self << ("

"+text.gsub("\n","
")+"

") end def parse_table(table) self << table.parse_html end end module ConsoleSummary def add_line(n=80) self << "-"*n+"\n" end def nl self << "\n" end def add(text) self << text end def parse_table(table) self << table.parse_console end end class ReportTable attr_reader :header def initialize(h=[]) @rows=[] @max_cols=[] self.header=(h) end def add_row(row) row.each_index{|i| @max_cols[i]=row[i].to_s.size if @max_cols[i].nil? or row[i].to_s.size > @max_cols[i] } @rows.push(row) end def add_horizontal_line @rows.push(:hr) end def header=(h) h.each_index{|i| @max_cols[i]=h[i].to_s.size if @max_cols[i].nil? or h[i].to_s.size>@max_cols[i] } @header=h end def parse_console_row(row) out="| " @max_cols.each_index{|i| if row[i].nil? out << " "*(@max_cols[i]+2)+"|" else t=row[i].to_s out << " "+t+" "*(@max_cols[i]-t.size+1)+"|" end } out << "\n" out end def parse_console_hr "-"*(@max_cols.inject(0){|a,v|a+v.size+3}+2)+"\n" end def parse_console out="\n" out << parse_console_hr out << parse_console_row(header) out << parse_console_hr @rows.each{|row| if row==:hr out << parse_console_hr else out << parse_console_row(row) end } out << parse_console_hr out end def parse_html out="\n" if header.size>0 out << "" end out << "\n" row_with_line=false @rows.each{|row| if row==:hr row_with_line=true else out << "" out << "\n" row_with_line=false end } out << "
"+header.join("")+"
" out << row.join("") +"
\n" out end end end require 'statsample/vector' require 'statsample/dataset' require 'statsample/crosstab'