lib/statsample/reliability.rb in statsample-0.18.0 vs lib/statsample/reliability.rb in statsample-1.0.0
- old
+ new
@@ -3,10 +3,11 @@
class << self
# Calculate Chonbach's alpha for a given dataset.
# only uses tuples without missing data
def cronbach_alpha(ods)
ds=ods.dup_only_valid
+ return nil if ds.vectors.any? {|k,v| v.variance==0}
n_items=ds.fields.size
return nil if n_items<=1
s2_items=ds.vectors.inject(0) {|ac,v|
ac+v[1].variance }
total=ds.vector_sum
@@ -14,14 +15,21 @@
(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
-
+ # Return nil if one or more vectors has 0 variance
def cronbach_alpha_standarized(ods)
- ds=ods.dup_only_valid.fields.inject({}){|a,f|
- a[f]=ods[f].standarized; a
+
+ ds=ods.dup_only_valid
+
+ return nil if ds.vectors.any? {|k,v| v.variance==0}
+
+ ds=ds.fields.inject({}){|a,f|
+ a[f]=ods[f].standarized;
+ a
}.to_dataset
+
cronbach_alpha(ds)
end
# Predicted reliability of a test by replicating
# +n+ times the number of items
def spearman_brown_prophecy(r,n)
\ No newline at end of file