lib/statsample/reliability.rb in statsample-0.10.0 vs lib/statsample/reliability.rb in statsample-0.11.0

- old
+ new

@@ -4,14 +4,14 @@ # Calculate Chonbach's alpha for a given dataset. # only uses tuples without missing data def cronbach_alpha(ods) ds=ods.dup_only_valid n_items=ds.fields.size - sum_var_items=ds.vectors.inject(0) {|ac,v| + s2_items=ds.vectors.inject(0) {|ac,v| ac+v[1].variance } total=ds.vector_sum - (n_items.quo(n_items-1)) * (1-(sum_var_items.quo(total.variance))) + (n_items.quo(n_items-1)) * (1-(s2_items.quo(total.variance))) end # Calculate Chonbach's alpha for a given dataset # using standarized values for every vector. # Only uses tuples without missing data @@ -19,10 +19,28 @@ ds=ods.dup_only_valid.fields.inject({}){|a,f| a[f]=ods[f].standarized; a }.to_dataset cronbach_alpha(ds) end + # First derivative for alfa + # Parameters + # <tt>n</tt>: Number of items + # <tt>sx</tt>: mean of variances + # <tt>sxy</tt>: mean of covariances + + def alfa_first_derivative(n,sx,sxy) + (sxy*(sx-sxy)).quo(((sxy*(n-1))+sx)**2) + end + # Second derivative for alfa + # Parameters + # <tt>n</tt>: Number of items + # <tt>sx</tt>: mean of variances + # <tt>sxy</tt>: mean of covariances + + def alfa_second_derivative(n,sx,sxy) + (2*(sxy**2)*(sxy-sx)).quo(((sxy*(n-1))+sx)**3) + end end class ItemCharacteristicCurve attr_reader :totals, :counts, :vector_total def initialize (ds, vector_total=nil) vector_total||=ds.vector_sum @@ -58,26 +76,31 @@ end out end end class ItemAnalysis - attr_reader :mean, :sd,:valid_n, :alpha , :alpha_standarized + attr_reader :mean, :sd,:valid_n, :alpha , :alpha_standarized, :variances_mean, :covariances_mean attr_accessor :name def initialize(ds,opts=Hash.new) @ds=ds.dup_only_valid + @k=@ds.fields.size @total=@ds.vector_sum @item_mean=@ds.vector_mean.mean @mean=@total.mean @median=@total.median @skew=@total.skew @kurtosis=@total.kurtosis @sd = @total.sd + @variance=@total.variance @valid_n = @total.size opts_default={:name=>"Reliability Analisis"} @opts=opts_default.merge(opts) @name=@opts[:name] - + # Mean for covariances and variances + @variances=@ds.fields.map {|f| @ds[f].variance}.to_scale + @variances_mean=@variances.mean + @covariances_mean=(@variance-@variances.sum).quo(@k**2-@k) begin @alpha = Statsample::Reliability.cronbach_alpha(ds) @alpha_standarized = Statsample::Reliability.cronbach_alpha_standarized(ds) rescue => e raise DatasetException.new(@ds,e), "Error calculating alpha" @@ -213,18 +236,22 @@ def report_building(builder) builder.section(:name=>@name) do |s| s.table(:name=>"Summary") do |t| t.row ["Items", @ds.fields.size] t.row ["Total Mean", @mean] + t.row ["Total S.D.", @sd] + t.row ["Total Variance", @variance] t.row ["Item Mean", @item_mean] - t.row ["S.D.", @sd] t.row ["Median", @median] t.row ["Skewness", "%0.4f" % @skew] t.row ["Kurtosis", "%0.4f" % @kurtosis] t.row ["Valid n", @valid_n] t.row ["Cronbach's alpha", "%0.4f" % @alpha] t.row ["Standarized Cronbach's alpha", "%0.4f" % @alpha_standarized] + t.row ["Variances mean", "%g" % @variances_mean] + t.row ["Covariances mean" , "%g" % @covariances_mean] end + itc=item_total_correlation sid=stats_if_deleted is=item_statistics s.table(:name=>"Items report", :header=>["item","mean","sd", "mean if deleted", "var if deleted", "sd if deleted"," item-total correl.", "alpha if deleted"]) do |t|