Class: Statsample::Reliability::ICC

Inherits:
Object
  • Object
show all
Includes:
Summarizable
Defined in:
lib/statsample/reliability/icc.rb

Overview

Intra-class correlation

According to Shrout & Fleiss (1979, p.422): “ICC is the correlation between one measurement (either a single rating or a mean of several ratings) on a target and another measurement obtained on that target”

Usage

require 'statsample'
size = 1000
a = Daru::Vector.new(size.times.map {rand(10)})
b = a.recode{|i|i+rand(4)-2}
c = a.recode{|i|i+rand(4)-2}
d = a.recode{|i|i+rand(4)-2}
ds = Daru::DataFrame.new({:a => a,:b => b,:c => c,:d => d})
# Use :type attribute to set type to summarize
icc=Statsample::Reliability::ICC.new(ds, :type=>:icc_1_k)
puts icc.summary

Reference

  • Shrout,P. & Fleiss, J. (1979). Intraclass Correlation: Uses in assessing rater reliability. Psychological Bulletin, 86(2), 420-428

  • McGraw, K. & Wong, S.P. (1996). Forming Inferences About Some Intraclass Correlation Coefficients. Psychological methods, 1(1), 30-46.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

#summary

Constructor Details

#initialize(ds, opts = Hash.new) ⇒ ICC

Returns a new instance of ICC.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/statsample/reliability/icc.rb', line 98

def initialize(ds, opts=Hash.new)
  @ds=ds.reject_values(*Daru::MISSING_VALUES)
  @vectors=@ds.map { |e| e }
  @n=@ds.nrows
  @k=@ds.ncols
  compute
  @g_rho=0
  @alpha=0.05
  @icc_name=nil
  opts_default={:name=>"Intra-class correlation", :type=>:icc_1}
  @opts=opts_default.merge(opts)
  @opts.each{|k,v| self.send("#{k}=",v) if self.respond_to? k }
end

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



96
97
98
# File 'lib/statsample/reliability/icc.rb', line 96

def alpha
  @alpha
end

#df_bjObject (readonly)

Returns the value of attribute df_bj.



33
34
35
# File 'lib/statsample/reliability/icc.rb', line 33

def df_bj
  @df_bj
end

#df_btObject (readonly)

Create a ICC analysis for a given dataset Each vector is a different measurement. Only uses complete data (listwise deletion).



31
32
33
# File 'lib/statsample/reliability/icc.rb', line 31

def df_bt
  @df_bt
end

#df_residualObject (readonly)

Returns the value of attribute df_residual.



34
35
36
# File 'lib/statsample/reliability/icc.rb', line 34

def df_residual
  @df_residual
end

#df_wtObject (readonly)

Returns the value of attribute df_wt.



32
33
34
# File 'lib/statsample/reliability/icc.rb', line 32

def df_wt
  @df_wt
end

#fObject (readonly)

Returns the value of attribute f.



91
92
93
# File 'lib/statsample/reliability/icc.rb', line 91

def f
  @f
end

#g_rhoObject

Returns the value of attribute g_rho.



95
96
97
# File 'lib/statsample/reliability/icc.rb', line 95

def g_rho
  @g_rho
end

#icc_1Object (readonly)

:section: McGraw and Wong ICC denominations



61
62
63
# File 'lib/statsample/reliability/icc.rb', line 61

def icc_1
  @icc_1
end

#icc_1_1Object (readonly)

:section: Shrout and Fleiss ICC denominations



52
53
54
# File 'lib/statsample/reliability/icc.rb', line 52

def icc_1_1
  @icc_1_1
end

#icc_1_kObject (readonly)

Returns the value of attribute icc_1_k.



55
56
57
# File 'lib/statsample/reliability/icc.rb', line 55

def icc_1_k
  @icc_1_k
end

#icc_2_1Object (readonly)

Returns the value of attribute icc_2_1.



53
54
55
# File 'lib/statsample/reliability/icc.rb', line 53

def icc_2_1
  @icc_2_1
end

#icc_2_kObject (readonly)

Returns the value of attribute icc_2_k.



56
57
58
# File 'lib/statsample/reliability/icc.rb', line 56

def icc_2_k
  @icc_2_k
end

#icc_3_1Object (readonly)

Returns the value of attribute icc_3_1.



54
55
56
# File 'lib/statsample/reliability/icc.rb', line 54

def icc_3_1
  @icc_3_1
end

#icc_3_kObject (readonly)

Returns the value of attribute icc_3_k.



57
58
59
# File 'lib/statsample/reliability/icc.rb', line 57

def icc_3_k
  @icc_3_k
end

#icc_a_1Object (readonly)

Returns the value of attribute icc_a_1.



63
64
65
# File 'lib/statsample/reliability/icc.rb', line 63

def icc_a_1
  @icc_a_1
end

#icc_a_kObject (readonly)

Returns the value of attribute icc_a_k.



66
67
68
# File 'lib/statsample/reliability/icc.rb', line 66

def icc_a_k
  @icc_a_k
end

#icc_c_1Object (readonly)

Returns the value of attribute icc_c_1.



62
63
64
# File 'lib/statsample/reliability/icc.rb', line 62

def icc_c_1
  @icc_c_1
end

#icc_c_kObject (readonly)

Returns the value of attribute icc_c_k.



65
66
67
# File 'lib/statsample/reliability/icc.rb', line 65

def icc_c_k
  @icc_c_k
end

#icc_kObject (readonly)

Returns the value of attribute icc_k.



64
65
66
# File 'lib/statsample/reliability/icc.rb', line 64

def icc_k
  @icc_k
end

#kObject (readonly)

Returns the value of attribute k.



69
70
71
# File 'lib/statsample/reliability/icc.rb', line 69

def k
  @k
end

#lboundObject (readonly)

Returns the value of attribute lbound.



92
93
94
# File 'lib/statsample/reliability/icc.rb', line 92

def lbound
  @lbound
end

#ms_bjObject (readonly) Also known as: jms, msc

Returns the value of attribute ms_bj.



38
39
40
# File 'lib/statsample/reliability/icc.rb', line 38

def ms_bj
  @ms_bj
end

#ms_btObject (readonly) Also known as: bms, msr

Returns the value of attribute ms_bt.



36
37
38
# File 'lib/statsample/reliability/icc.rb', line 36

def ms_bt
  @ms_bt
end

#ms_residualObject (readonly) Also known as: ems, mse

Returns the value of attribute ms_residual.



39
40
41
# File 'lib/statsample/reliability/icc.rb', line 39

def ms_residual
  @ms_residual
end

#ms_wtObject (readonly) Also known as: wms, msw

Returns the value of attribute ms_wt.



37
38
39
# File 'lib/statsample/reliability/icc.rb', line 37

def ms_wt
  @ms_wt
end

#nObject (readonly)

Returns the value of attribute n.



69
70
71
# File 'lib/statsample/reliability/icc.rb', line 69

def n
  @n
end

#nameObject

Returns the value of attribute name.



97
98
99
# File 'lib/statsample/reliability/icc.rb', line 97

def name
  @name
end

#rObject (readonly)

ICC value, set with :type



90
91
92
# File 'lib/statsample/reliability/icc.rb', line 90

def r
  @r
end

#total_meanObject (readonly)

Returns the value of attribute total_mean.



70
71
72
# File 'lib/statsample/reliability/icc.rb', line 70

def total_mean
  @total_mean
end

#typeObject

Type of analysis, for easy summarization By default, set to :icc_1

  • Shrout & Fleiss(1979) denominations

    • :icc_1_1

    • :icc_2_1

    • :icc_3_1

    • :icc_1_k

    • :icc_2_k

    • :icc_3_k

  • McGraw & Wong (1996) denominations

    • :icc_1

    • :icc_k

    • :icc_c_1

    • :icc_c_k

    • :icc_a_1

    • :icc_a_k



88
89
90
# File 'lib/statsample/reliability/icc.rb', line 88

def type
  @type
end

#uboundObject (readonly)

Returns the value of attribute ubound.



93
94
95
# File 'lib/statsample/reliability/icc.rb', line 93

def ubound
  @ubound
end

Instance Method Details

#computeObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/statsample/reliability/icc.rb', line 196

def compute
  @df_bt=n-1
  @df_wt=n*(k-1)
  @df_bj=k-1
  @df_residual=(n-1)*(k-1)
  @total_mean=@vectors.inject(0){|ac,v| ac+v.sum}.quo(n*k)
  vm=@ds.vector_mean
  
  @ss_bt=k*vm.ss(@total_mean)
  @ms_bt=@ss_bt.quo(@df_bt)
  
  @ss_bj=n*@vectors.inject(0){|ac,v| ac+(v.mean-@total_mean).square}
  @ms_bj=@ss_bj.quo(@df_bj)
  
  @ss_wt=@vectors.inject(0){|ac,v| ac+(v-vm).ss(0)}
  @ms_wt=@ss_wt.quo(@df_wt)
  
  @ss_residual=@ss_wt-@ss_bj
  @ms_residual=@ss_residual.quo(@df_residual)
  ###
  # Shrout and Fleiss denomination
  ###
  # ICC(1,1) / ICC(1)
  @icc_1_1=(bms-wms).quo(bms+(k-1)*wms) 
  # ICC(2,1) / ICC(A,1)
  @icc_2_1=(bms-ems).quo(bms+(k-1)*ems+k*(jms - ems).quo(n))  
  # ICC(3,1) / ICC(C,1)
  @icc_3_1=(bms-ems).quo(bms+(k-1)*ems) 
  
  
  
  # ICC(1,K) / ICC(K)
  @icc_1_k=(bms-wms).quo(bms) 
  # ICC(2,K) / ICC(A,k)
  @icc_2_k=(bms-ems).quo(bms+(jms-ems).quo(n))
  # ICC(3,K) / ICC(C,k) = Cronbach's alpha
  @icc_3_k=(bms-ems).quo(bms) 
  
  ###
  # McGraw and Wong
  ###
  
end

#icc_1_1_ci(alpha = 0.05) ⇒ Object

Intervale of confidence for ICC (1,1)



312
313
314
315
316
317
318
319
# File 'lib/statsample/reliability/icc.rb', line 312

def icc_1_1_ci(alpha=0.05)
  per=1-(0.5*alpha)
 
  fu=icc_1_f.f*Distribution::F.p_value(per, @df_wt, @df_bt)
  fl=icc_1_f.f.quo(Distribution::F.p_value(per, @df_bt, @df_wt))
  
  [(fl-1).quo(fl+k-1), (fu-1).quo(fu+k-1)]
end

#icc_1_f(rho = 0.0) ⇒ Object



240
241
242
243
244
# File 'lib/statsample/reliability/icc.rb', line 240

def icc_1_f(rho=0.0)
  num=msr*(1-rho)
  den=msw*(1+(k-1)*rho)
  Statsample::Test::F.new(num, den, @df_bt, @df_wt)
end

#icc_1_f_shroutObject

F test for ICC Case 1. Shrout and Fleiss



307
308
309
# File 'lib/statsample/reliability/icc.rb', line 307

def icc_1_f_shrout
  Statsample::Test::F.new(bms, wms, @df_bt, @df_wt)
end

#icc_1_k_ci(alpha = 0.05) ⇒ Object

Intervale of confidence for ICC (1,k)



322
323
324
325
326
327
# File 'lib/statsample/reliability/icc.rb', line 322

def icc_1_k_ci(alpha=0.05)
  per=1-(0.5*alpha)
  fu=icc_1_f.f*Distribution::F.p_value(per, @df_wt, @df_bt)
  fl=icc_1_f.f.quo(Distribution::F.p_value(per, @df_bt, @df_wt))
  [1-1.quo(fl), 1-1.quo(fu)]
end

#icc_1_k_f(rho = 0) ⇒ Object

One way random F, type k



246
247
248
249
250
# File 'lib/statsample/reliability/icc.rb', line 246

def icc_1_k_f(rho=0)
  num=msr*(1-rho)
  den=msw
  Statsample::Test::F.new(num, den, @df_bt, @df_wt)
end

#icc_2_1_ci(alpha = 0.05) ⇒ Object



350
351
352
# File 'lib/statsample/reliability/icc.rb', line 350

def icc_2_1_ci(alpha=0.05)
  icc_2_1_ci_mcgraw
end

#icc_2_1_ci_mcgraw(alpha = 0.05) ⇒ Object

Confidence interval ICC(A,1), McGawn



356
357
358
359
360
361
# File 'lib/statsample/reliability/icc.rb', line 356

def icc_2_1_ci_mcgraw(alpha=0.05)
  fd,fu=icc_2_1_fs(icc_2_1,alpha)
  cl=(n*(msr-fd*mse)).quo(fd*(k*msc+(k*n-k-n)*mse)+n*msr)
  cu=(n*(fu*msr-mse)).quo(k*msc+(k*n-k-n)*mse+n*fu*msr)
  [cl,cu]
end

#icc_2_1_fs(pp, alpha = 0.05) ⇒ Object

F* for ICC(2,1) and ICC(2,k)



338
339
340
341
342
343
344
345
346
347
# File 'lib/statsample/reliability/icc.rb', line 338

def icc_2_1_fs(pp,alpha=0.05)
  fj=jms.quo(ems)
  per=1-(0.5*alpha)
  vn=(k-1)*(n-1)*((k*pp*fj+n*(1+(k-1)*pp)-k*pp)**2)
  vd=(n-1)*(k**2)*(pp**2)*(fj**2)+((n*(1+(k-1)*pp)-k*pp)**2)
  v=vn.quo(vd)
  f1=Distribution::F.p_value(per, n-1,v)
  f2=Distribution::F.p_value(per, v, n-1)
  [f1,f2]
end

#icc_2_fObject

F test for ICC Case 2



330
331
332
# File 'lib/statsample/reliability/icc.rb', line 330

def icc_2_f
  Statsample::Test::F.new(bms, ems, @df_bt, @df_residual)
end

#icc_2_k_ci(alpha = 0.05) ⇒ Object



363
364
365
# File 'lib/statsample/reliability/icc.rb', line 363

def icc_2_k_ci(alpha=0.05)
  icc_2_k_ci_mcgraw(alpha)
end

#icc_2_k_ci_mcgraw(alpha = 0.05) ⇒ Object



367
368
369
370
371
372
373
374
# File 'lib/statsample/reliability/icc.rb', line 367

def icc_2_k_ci_mcgraw(alpha=0.05)
  f1,f2=icc_2_1_fs(icc_2_k,alpha)
  [
  (n*(msr-f1*mse)).quo(f1*(msc-mse)+n*msr),
  (n*(f2*msr-mse)).quo(msc-mse+n*f2*msr)
  ]
  
end

#icc_2_k_ci_shrout(alpha = 0.05) ⇒ Object



375
376
377
378
# File 'lib/statsample/reliability/icc.rb', line 375

def icc_2_k_ci_shrout(alpha=0.05)
  ci=icc_2_1_ci(alpha)
  [(ci[0]*k).quo(1+(k-1)*ci[0]), (ci[1]*k).quo(1+(k-1)*ci[1])]
end

#icc_3_1_ci(alpha = 0.05) ⇒ Object



385
386
387
388
389
390
# File 'lib/statsample/reliability/icc.rb', line 385

def icc_3_1_ci(alpha=0.05)
  per=1-(0.5*alpha)
  fl=icc_3_f.f.quo(Distribution::F.p_value(per, @df_bt, @df_residual))
  fu=icc_3_f.f*Distribution::F.p_value(per, @df_residual, @df_bt)
  [(fl-1).quo(fl+k-1), (fu-1).quo(fu+k-1)]
end

#icc_3_fObject



381
382
383
# File 'lib/statsample/reliability/icc.rb', line 381

def icc_3_f
  Statsample::Test::F.new(bms, ems, @df_bt, @df_residual)
end

#icc_3_k_ci(alpha = 0.05) ⇒ Object



392
393
394
395
396
397
# File 'lib/statsample/reliability/icc.rb', line 392

def icc_3_k_ci(alpha=0.05)
  per=1-(0.5*alpha)
  fl=icc_3_f.f.quo(Distribution::F.p_value(per, @df_bt, @df_residual))
  fu=icc_3_f.f*Distribution::F.p_value(per, @df_residual, @df_bt)
  [1-1.quo(fl),1-1.quo(fu)]
end

#icc_a_1_f(rho = 0) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/statsample/reliability/icc.rb', line 279

def icc_a_1_f(rho=0)
  fj=jms.quo(ems)
  num=msr
  den=a(rho)*msc+b(rho)*mse
  pp = @icc_2_1
  vn=(k-1)*(n-1)*((k*pp*fj+n*(1+(k-1)*pp)-k*pp)**2)
  vd=(n-1)*(k**2)*(pp**2)*(fj**2)+((n*(1+(k-1)*pp)-k*pp)**2)
  v=vn.quo(vd)
  Statsample::Test::F.new(num, den, @df_bt, v)        
end

#icc_a_k_f(rho = 0) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/statsample/reliability/icc.rb', line 290

def icc_a_k_f(rho=0)
  num=msr
  den=c(rho)*msc+d(rho)*mse
  
  fj=jms.quo(ems)
  
  pp = @icc_2_k
  vn=(k-1)*(n-1)*((k*pp*fj+n*(1+(k-1)*pp)-k*pp)**2)
  vd=(n-1)*(k**2)*(pp**2)*(fj**2)+((n*(1+(k-1)*pp)-k*pp)**2)
  v=vn.quo(vd)
  
  
  Statsample::Test::F.new(num, den, @df_bt,v)        

end

#icc_c_1_f(rho = 0) ⇒ Object



252
253
254
255
256
# File 'lib/statsample/reliability/icc.rb', line 252

def icc_c_1_f(rho=0)
  num=msr*(1-rho)
  den=mse*(1+(k-1)*rho)
  Statsample::Test::F.new(num, den, @df_bt, @df_residual)
end

#icc_c_k_ci(alpha = 0.05) ⇒ Object



399
400
401
402
403
404
# File 'lib/statsample/reliability/icc.rb', line 399

def icc_c_k_ci(alpha=0.05)
  per=1-(0.5*alpha)
  fl=icc_c_k_f.f.quo(Distribution::F.p_value(per, @df_bt, @df_residual))
  fu=icc_c_k_f.f*Distribution::F.p_value(per, @df_residual, @df_bt)
  [1-1.quo(fl),1-1.quo(fu)]
end

#icc_c_k_f(rho = 0) ⇒ Object



257
258
259
260
261
# File 'lib/statsample/reliability/icc.rb', line 257

def icc_c_k_f(rho=0)
  num=(1-rho)
  den=1-@icc_3_k
  Statsample::Test::F.new(num, den, @df_bt, @df_residual)
end

#report_building(b) ⇒ Object



405
406
407
408
409
410
411
412
# File 'lib/statsample/reliability/icc.rb', line 405

def report_building(b)
   b.section(:name=>name) do |s|
     s.text @icc_name
     s.text _("ICC: %0.4f") % @r
     s.parse_element(@f)
     s.text _("CI (%0.2f): [%0.4f - %0.4f]") % [(1-@alpha)*100, @lbound, @ubound]
   end
end