# module Statsample::Reliability

### Public Instance Methods

alfa_second_derivative(n,sx,sxy) click to toggle source

Second derivative for alfa Parameters `n`: Number of items `sx`: mean of variances `sxy`: mean of covariances

```# File lib/statsample/reliability.rb, line 106
def alfa_second_derivative(n,sx,sxy)
(2*(sxy**2)*(sxy-sx)).quo(((sxy*(n-1))+sx)**3)
end```
alpha_first_derivative(n,sx,sxy) click to toggle source

First derivative for alfa Parameters `n`: Number of items `sx`: mean of variances `sxy`: mean of covariances

```# File lib/statsample/reliability.rb, line 97
def alpha_first_derivative(n,sx,sxy)
(sxy*(sx-sxy)).quo(((sxy*(n-1))+sx)**2)
end```
cronbach_alpha(ods) click to toggle source

Calculate Chonbach's alpha for a given dataset. only uses tuples without missing data

```# File lib/statsample/reliability.rb, line 6
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

(n_items.quo(n_items-1)) * (1-(s2_items.quo(total.variance)))
end```
cronbach_alpha_from_covariance_matrix(cov) click to toggle source

Get Cronbach's alpha from a covariance matrix

```# File lib/statsample/reliability.rb, line 57
def cronbach_alpha_from_covariance_matrix(cov)
n=cov.row_size
raise "covariance matrix should have at least 2 variables" if n < 2
s2=n.times.inject(0) {|ac,i| ac+cov[i,i]}
(n.quo(n-1))*(1-(s2.quo(cov.total_sum)))
end```
cronbach_alpha_from_n_s2_cov(n,s2,cov) click to toggle source

Get Cronbach alpha from `n` cases, `s2` mean variance and `cov` mean covariance

```# File lib/statsample/reliability.rb, line 53
def cronbach_alpha_from_n_s2_cov(n,s2,cov)
(n.quo(n-1)) * (1-(s2.quo(s2+(n-1)*cov)))
end```
cronbach_alpha_standarized(ods) click to toggle source

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

```# File lib/statsample/reliability.rb, line 21
def cronbach_alpha_standarized(ods)

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```
n_for_desired_alpha(alpha,s2,cov) click to toggle source

Returns n necessary to obtain specific alpha given variance and covariance mean of items

```# File lib/statsample/reliability.rb, line 65
def n_for_desired_alpha(alpha,s2,cov)
min=2
max=1000
n=50
prev_n=0
epsilon=0.0001
dif=1000
c_a=cronbach_alpha_from_n_s2_cov(n,s2,cov)
dif=c_a - alpha
while(dif.abs>epsilon and n!=prev_n)
prev_n=n
if dif<0
min=n
n=(n+(max-min).quo(2)).to_i
else
max=n
n=(n-(max-min).quo(2)).to_i
end
c_a=cronbach_alpha_from_n_s2_cov(n,s2,cov)
dif=c_a - alpha
#puts "#{n} , #{c_a}"

end
n
end```
n_for_desired_reliability(r,r_d,n=1) click to toggle source

Returns the number of items to obtain `r_d` desired reliability from `r` current reliability, achieved with `n` items

```# File lib/statsample/reliability.rb, line 45
def n_for_desired_reliability(r,r_d,n=1)
return nil if r.nil?
(r_d*(1-r)).quo(r*(1-r_d))*n
end```
spearman_brown_prophecy(r,n) click to toggle source

Predicted reliability of a test by replicating `n` times the number of items

```# File lib/statsample/reliability.rb, line 36
def spearman_brown_prophecy(r,n)
(n*r).quo(1+(n-1)*r)
end```