Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Justin extensions #34

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
20 changes: 18 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
doc.yaml
*.swp
*.rbc
coverage
Expand All @@ -10,4 +9,21 @@ doc
examples/images/*
examples/*.html
web/upload_task.rb
.idea

# From standard gemfile
*.gem
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
19 changes: 2 additions & 17 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
source "https://www.rubygems.org"
gem 'minitest'
gem 'rdoc'
gem 'mocha', '0.14.0' #:require=>'mocha/setup'
gem 'shoulda','3.5.0'
gem 'shoulda-matchers','2.2.0'
gem 'hoe'
#gem 'bio-statsample-timeseries'
gem 'reportbuilder'
gem 'dirty-memoize'
gem 'distribution'
gem 'extendmatrix'
gem 'minimization'
gem 'rserve-client'
gem 'rubyvis'
gem 'spreadsheet'
gem 'rb-gsl'
gem 'awesome_print'

gemspec
81 changes: 0 additions & 81 deletions Gemfile.lock

This file was deleted.

1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require 'rubygems'
require 'statsample'
require 'hoe'
require 'rdoc'
require "bundler/gem_tasks"

Hoe.plugin :git
Hoe.plugin :doofus
Expand Down
6 changes: 6 additions & 0 deletions examples/correlation_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
'd'=>rnorm(samples))
cm=cor(ds)
summary(cm)

cp = corp(ds)
summary(cp)

cov = cov(ds)
summary(cov)
end

if __FILE__==$0
Expand Down
12 changes: 11 additions & 1 deletion lib/statsample/bivariate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def t_pearson(v1,v2)
# giving r and vector size
# Source : http://faculty.chass.ncsu.edu/garson/PA765/correl.htm
def t_r(r,size)
raise "In computing value for t test for a pearson correlation, invalid size of series: #{size}" if size <= 2
raise "In computing value for t test for a pearson correlation, invalid value of r: #{r}" if r >= 1.1
if r >= 1
puts "StatSample::Bivariate#t_r: got an R value > 1 (#{r}), so substituting 0.999999"
r = 0.999999
end
r * Math::sqrt(((size)-2).to_f / (1 - r**2))
end
# Retrieves the probability value (a la SPSS)
Expand Down Expand Up @@ -251,7 +257,11 @@ def correlation_probability_matrix(ds, tails=:both)
(row==col or ds[row].type!=:scale or ds[col].type!=:scale) ? nil : prop_pearson(t_pearson(ds[row],ds[col]), v1a.size, tails)
end
end
Matrix.rows(rows)
m = Matrix.rows(rows)
m.extend(Statsample::CovariateMatrix)
m.fields=ds.fields
m.name = "Correlation Probability"
m
end

# Spearman ranked correlation coefficient (rho) between 2 vectors
Expand Down
68 changes: 36 additions & 32 deletions lib/statsample/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def to_dataset
if defined? :eigenpairs
alias_method :eigenpairs_ruby, :eigenpairs
end

if Statsample.has_gsl?
# Optimize eigenpairs of extendmatrix module using gsl
def eigenpairs
to_gsl.eigenpairs
end
end

def eigenvalues
eigenpairs.collect {|v| v[0]}
end
Expand All @@ -44,11 +44,11 @@ def eigenvectors
def eigenvectors_matrix
Matrix.columns(eigenvectors)
end









def to_gsl
out=[]
self.row_size.times{|i|
Expand Down Expand Up @@ -76,7 +76,7 @@ class Matrix
def to_gsl
self
end

def to_dataset
f = (self.respond_to? :fields_y) ? fields_y : column_size.times.map {|i| _("VAR_%d") % (i+1) }
ds=Statsample::Dataset.new(f)
Expand All @@ -91,7 +91,7 @@ def to_dataset
ds.name=self.name if self.respond_to? :name
ds
end

def row_size
size1
end
Expand All @@ -110,18 +110,18 @@ def eigenvalues
def eigenvectors
eigenpairs.collect {|v| v[1]}
end

# Matrix sum of squares
def mssq
sum=0
to_v.each {|i| sum+=i**2}
sum
end

def eigenvectors_matrix
eigval, eigvec= GSL::Eigen.symmv(self)
GSL::Eigen::symmv_sort(eigval, eigvec, GSL::Eigen::SORT_VAL_DESC)
eigvec
eigvec
end
def eigenpairs
eigval, eigvec= GSL::Eigen.symmv(self)
Expand All @@ -130,7 +130,7 @@ def eigenpairs
[eigval[i],eigvec.get_col(i)]
}
end

#def eigenpairs_ruby
# self.to_matrix.eigenpairs_ruby
#end
Expand Down Expand Up @@ -158,7 +158,7 @@ def total_sum
module Statsample
# Module to add names to X and Y fields
module NamedMatrix
include Summarizable
include Summarizable

def fields
raise "Should be square" if !square?
Expand All @@ -178,10 +178,10 @@ def fields_y=(v)
@fields_y=v
end
def fields_x
@fields_x||=row_size.times.collect {|i| _("X%d") % i}
@fields_x||=row_size.times.collect {|i| _("X%d") % i}
end
def fields_y
@fields_y||=column_size.times.collect {|i| _("Y%d") % i}
@fields_y||=column_size.times.collect {|i| _("Y%d") % i}
end

def name
Expand All @@ -195,7 +195,7 @@ def get_new_name
@@named_matrix+=1
_("Matrix %d") % @@named_matrix
end

end
# Module to add method for variance/covariance and correlation matrices
# == Usage
Expand All @@ -209,15 +209,19 @@ module CovariateMatrix
# Get type of covariate matrix. Could be :covariance or :correlation
def _type
if row_size==column_size
if row_size.times.find {|i| self[i,i]!=1.0}
:covariance
else
:correlation
@type ||= begin
if row_size.times.find { |i| self[i, i] == 1.0 }
:correlation
elsif row_size.times.find { |i| self[i, i].nil? }
:correlation_probability
else
:covariance
end
end
else
@type
end

end
def _type=(t)
@type=t
Expand All @@ -233,7 +237,7 @@ def correlation
end
}
})
matrix.extend CovariateMatrix
matrix.extend CovariateMatrix
matrix.fields_x=fields_x
matrix.fields_y=fields_y
matrix._type=:correlation
Expand All @@ -242,19 +246,19 @@ def correlation
self
end
end


# Get variance for field k
#
def variance(k)
submatrix([k])[0,0]
end

def get_new_name
@@covariatematrix+=1
_("Covariate matrix %d") % @@covariatematrix
end

# Select a submatrix of factors. If you have a correlation matrix
# with a, b and c, you could obtain a submatrix of correlations of
# a and b, b and c or a and b
Expand All @@ -276,24 +280,24 @@ def submatrix(rows,columns=nil)
raise ArgumentError, "rows shouldn't be empty" if rows.respond_to? :size and rows.size==0
columns||=rows
# Convert all fields on index
row_index=rows.collect {|v|
row_index=rows.collect {|v|
r=v.is_a?(Numeric) ? v : fields_x.index(v)
raise "Index #{v} doesn't exists on matrix" if r.nil?
r
}
column_index=columns.collect {|v|
column_index=columns.collect {|v|
r=v.is_a?(Numeric) ? v : fields_y.index(v)
raise "Index #{v} doesn't exists on matrix" if r.nil?
r
}


fx=row_index.collect {|v| fields_x[v]}
fy=column_index.collect {|v| fields_y[v]}

matrix= Matrix.rows(row_index.collect {|i|
row=column_index.collect {|j| self[i,j]}})
matrix.extend CovariateMatrix
matrix.extend CovariateMatrix
matrix.fields_x=fx
matrix.fields_y=fy
matrix._type=_type
Expand Down
8 changes: 7 additions & 1 deletion lib/statsample/shorthand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ def names(ds)
def cor(ds)
Statsample::Bivariate.correlation_matrix(ds)
end

# Create a correlation probability matrix from a dataset
def corp(ds)
Statsample::Bivariate.correlation_probability_matrix(ds)
end

# Create a variance/covariance matrix from a dataset
def cov(ds)
Statsample::Bivariate.covariate_matrix(ds)
Statsample::Bivariate.covariance_matrix(ds)
end
# Create a Statsample::Vector
# Analog to R's c
Expand Down
Loading