Sha256: 38e424d14e819e9b703575458609a816052ce911f09e6d69bb83b75d8987c0ea

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Statsample
  module SPSS
    class << self
      # Export a SPSS Matrix with tetrachoric correlations .
      #
      # Use: 
      #   ds=Daru::DataFrame.from_excel("my_data.xls")
      #   puts Statsample::SPSS.tetrachoric_correlation_matrix(ds)
      def tetrachoric_correlation_matrix(ds)
        dsv=ds.reject_values(*Daru::MISSING_VALUES)
        # Delete all vectors doesn't have variation
        dsv.vectors.each { |f|
          if dsv[f].factors.size==1
            dsv.delete_vector(f) 
          else
            dsv[f]=dsv[f].dichotomize
          end
        }

        tcm=Statsample::Bivariate.tetrachoric_correlation_matrix(dsv)
        n=dsv.vectors.to_a.collect {|f|
          sprintf("%d",dsv[f].size)
        }
        meanlist=dsv.vectors.to_a.collect{|f|
          sprintf("%0.3f", dsv[f].mean)
        }
        stddevlist=dsv.vectors.to_a.collect{|f|
          sprintf("%0.3f", dsv[f].sd)
        }
        out=<<-HEREDOC
MATRIX DATA VARIABLES=ROWTYPE_ #{dsv.fields.join(",")}.
BEGIN DATA
N #{n.join(" ")}
MEAN	#{meanlist.join(" ")}
STDDEV #{stddevlist.join(" ")}
HEREDOC
tcm.row_size.times {|i|
  out +="CORR "
  (i+1).times {|j|
    out+=sprintf("%0.3f",tcm[i,j])+" "
  }
  out +="\n"
}
out+="END DATA.\nEXECUTE.\n"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
statsample-ekatena-2.0.2.1 lib/statsample-ekatena/converter/spss.rb
statsample-ekatena-2.0.2 lib/statsample-ekatena/converter/spss.rb
statsample-2.1.0 lib/statsample/converter/spss.rb