11module Diagnostics
22
33
4- export dffit,
4+ export dffit,
55 dffits,
66 dfbeta,
77 dfbetas,
88 hatmatrix,
99 studentizedResiduals,
1010 adjustedResiduals,
1111 jacknifedS,
12- cooks, cooksoutliers,
12+ cooks,
13+ cooksoutliers,
1314 mahalanobisSquaredMatrix,
1415 covratio,
1516 hadimeasure,
16- diagnose
17+ diagnose
1718
1819export coordinatwisemedians, mahalanobisBetweenPairs, euclideanDistances
1920
@@ -47,8 +48,8 @@ function euclideanDistances(dataMatrix::Array{Float64,2})::Array{Float64,2}
4748 for i = 1 : n
4849 for j = i: n
4950 if i != j
50- @inbounds d[i, j] = sqrt (sum ((dataMatrix[i, :] .- dataMatrix[j, :]) .^ 2.0 ))
51- @inbounds d[j, i] = d[i, j]
51+ d[i, j] = sqrt (sum ((dataMatrix[i, :] .- dataMatrix[j, :]) .^ 2.0 ))
52+ d[j, i] = d[i, j]
5253 end
5354 end
5455 end
@@ -66,7 +67,7 @@ function mahalanobisSquaredBetweenPairs(pairs::Matrix; covmatrix = nothing)
6667 try
6768 invm = inv (covmatrix)
6869 for i = 1 : n
69- @inbounds for j = i: n
70+ for j = i: n
7071 newmat[i, j] =
7172 ((pairs[i, :] .- pairs[j, :])' * invm * (pairs[i, :] .- pairs[j, :]))
7273 newmat[j, i] = newmat[i, j]
@@ -113,12 +114,12 @@ function mahalanobisBetweenPairs(dataMatrix::Array{Float64,2})::Array{Float64,2}
113114 for i = 1 : n
114115 for j = i: n
115116 if i != j
116- @inbounds d[i, j] = sqrt (
117+ d[i, j] = sqrt (
117118 (dataMatrix[i, :] .- dataMatrix[j, :]) *
118119 covinv *
119120 (dataMatrix[i, :] .- dataMatrix[j, :])' ,
120121 )
121- @inbounds d[j, i] = d[i, j]
122+ d[j, i] = d[i, j]
122123 end
123124 end
124125 end
@@ -497,7 +498,7 @@ function cooks(X::Array{Float64,2}, y::Array{Float64,1})::Array{Float64,1}
497498 s2 = sum (res .* res) / (n - p)
498499 d = zeros (Float64, n)
499500 for i = 1 : n
500- @inbounds d[i] = ((res[i]^ 2.0 ) / (p * s2)) * (hat[i, i] / (1 - hat[i, i])^ 2.0 )
501+ d[i] = ((res[i]^ 2.0 ) / (p * s2)) * (hat[i, i] / (1 - hat[i, i])^ 2.0 )
501502 end
502503 return d
503504end
@@ -519,21 +520,17 @@ Calculates Cooks distance for a given regression setting and reports the potenti
519520- `["cutoff"]`: Quantile of the F distribution.
520521- `["potentials"]`: Vector of indices of potential regression outliers.
521522"""
522- function cooksoutliers (setting:: RegressionSetting ; alpha:: Float64 = 0.5 ):: Dict
523+ function cooksoutliers (setting:: RegressionSetting ; alpha:: Float64 = 0.5 ):: Dict
523524 X, y = @extractRegressionSetting setting
524525 return cooksoutliers (X, y, alpha = alpha)
525526end
526527
527- function cooksoutliers (X:: Array{Float64, 2} , y:: Array{Float64, 1} ; alpha:: Float64 = 0.5 ):: Dict
528+ function cooksoutliers (X:: Array{Float64,2} , y:: Array{Float64,1} ; alpha:: Float64 = 0.5 ):: Dict
528529 n, p = size (X)
529530 d = cooks (X, y)
530531 cutoff = cookscritical (n, p)
531532 potentials = filter (i -> d[i] >= cutoff, 1 : n)
532- return Dict (
533- " distance" => d,
534- " cutoff" => cutoff,
535- " potentials" => potentials
536- )
533+ return Dict (" distance" => d, " cutoff" => cutoff, " potentials" => potentials)
537534end
538535
539536
@@ -597,14 +594,11 @@ See also: [`dfbeta`](@ref)
597594"""
598595function dfbetas (setting)
599596 y = responseVector (setting)
600- results = map (i -> dfbeta (setting, i), 1 : length (y))
597+ results = map (i -> dfbeta (setting, i), 1 : length (y))
601598 return mapreduce (permutedims, vcat, results)
602599end
603600
604- function dfbetas (
605- X:: Array{Float64,2} ,
606- y:: Array{Float64,1}
607- )
601+ function dfbetas (X:: Array{Float64,2} , y:: Array{Float64,1} )
608602 results = map (i -> dfbeta (X, y, i), 1 : length (y))
609603 return mapreduce (permutedims, vcat, results)
610604end
@@ -724,7 +718,7 @@ function hadimeasure(X::Array{Float64,2}, y::Array{Float64,1}; c::Float64 = 2.0)
724718 hat = hatmatrix (X)
725719 H = zeros (Float64, n)
726720 for i = 1 : n
727- @inbounds H[i] =
721+ H[i] =
728722 (p * res2[i]) / ((1 - hat[i, i]) * (sumres - res2[i])) +
729723 (hat[i, i] / (1 - hat[i, i]))
730724 end
@@ -736,23 +730,23 @@ end
736730
737731function dffitcritical (n:: Int , p:: Int ):: Float64
738732 return 2.0 * sqrt ((p + 1 ) / (n - p - 1 ))
739- end
733+ end
740734
741735function dfbetacritical (n:: Int ):: Float64
742736 return 2.0 / sqrt (n)
743- end
737+ end
744738
745- function covratiocritical (n:: Int , p:: Int ):: Tuple{Float64, Float64}
739+ function covratiocritical (n:: Int , p:: Int ):: Tuple{Float64,Float64}
746740 c = 3.0 * p / n
747741 return (1.0 - c, 1.0 + c)
748- end
742+ end
749743
750- function hatcritical (n:: Int , p:: Int ):: Float64
744+ function hatcritical (n:: Int , p:: Int ):: Float64
751745 return 2.0 * sqrt (p / n)
752- end
746+ end
753747
754- function cookscritical (n:: Int , p:: Int ; alpha = 0.5 ):: Float64
755- return quantile (FDist (p, n- p), alpha)
748+ function cookscritical (n:: Int , p:: Int ; alpha = 0.5 ):: Float64
749+ return quantile (FDist (p, n - p), alpha)
756750end
757751
758752
@@ -772,32 +766,32 @@ Diagnose a regression setting and report potential outliers using [`dffits`](@re
772766function diagnose (setting:: RegressionSetting ; alpha = 0.5 )
773767 X, y = @extractRegressionSetting setting
774768 return diagnose (X, y, alpha = 0.5 )
775- end
769+ end
776770
777771
778- function diagnose (X:: Array{Float64, 2} , y:: Array{Float64, 1} ; alpha = 0.5 )
772+ function diagnose (X:: Array{Float64,2} , y:: Array{Float64,1} ; alpha = 0.5 )
779773 n, p = size (X)
780- resultdffits = dffits (X, y)
781- resultdfbetas = dfbetas (X, y)
782- resultcook = cooks (X, y)
783- hatresult = hatmatrix (X)
774+ resultdffits = dffits (X, y)
775+ resultdfbetas = dfbetas (X, y)
776+ resultcook = cooks (X, y)
777+ hatresult = hatmatrix (X)
784778
785- dffitcrit = dffitcritical (n, p)
786- dfbetacrit = dfbetacritical (n)
787- hatcrit = hatcritical (n, p)
788- cookscrit = cookscritical (n, p, alpha = alpha)
779+ dffitcrit = dffitcritical (n, p)
780+ dfbetacrit = dfbetacritical (n)
781+ hatcrit = hatcritical (n, p)
782+ cookscrit = cookscritical (n, p, alpha = alpha)
789783
790- dffitpotential = filter (i -> abs (resultdffits[i]) >= dffitcrit, 1 : n)
784+ dffitpotential = filter (i -> abs (resultdffits[i]) >= dffitcrit, 1 : n)
791785 dfbetaspotential = filter (i -> any (x -> (abs (x) > dfbetacrit), resultdfbetas), 1 : n)
792- hatpotential = filter (i -> abs (hatresult[i]) > hatcrit, 1 : n)
793- cookpotential = filter (i -> abs (resultcook[i]) > cookscrit, 1 : n)
786+ hatpotential = filter (i -> abs (hatresult[i]) > hatcrit, 1 : n)
787+ cookpotential = filter (i -> abs (resultcook[i]) > cookscrit, 1 : n)
794788
795789 return Dict (
796790 " dffit_potentials" => dffitpotential,
797791 " dfbeta_potentials" => dfbetaspotential,
798792 " hat_potentials" => hatpotential,
799- " cooks_potentials" => cookpotential
793+ " cooks_potentials" => cookpotential,
800794 )
801- end
795+ end
802796
803797end # end of module Diagnostics
0 commit comments