1+ module RobustHatRegression
2+
3+
4+ export robhatreg
5+
6+ import .. Basis: RegressionSetting, @extractRegressionSetting , designMatrix, responseVector
7+ import .. OrdinaryLeastSquares: ols, residuals, coef
8+ import .. LTS: iterateCSteps
9+
10+ import Distributions: quantile
11+ import LinearAlgebra: inv, diag
12+
13+
14+ function trimean (u:: AbstractVector{T} ):: Float64 where T <: Real
15+ return (quantile (u, 0.25 ) + 2.0 * quantile (u, 0.50 ) + quantile (u, 0.75 )) / 4.0
16+ end
17+
18+ function m (v:: Vector , u:: Vector ):: Float64
19+ return trimean (u .* v) * length (u)
20+ end
21+
22+ function m (mat:: AbstractMatrix , u:: AbstractVector ):: AbstractMatrix
23+ L = length (u)
24+ y = zeros (Float64, L, 1 )
25+ for i in 1 : L
26+ y[i, 1 ] = u[i]
27+ end
28+ result = m (mat, y)
29+ return result
30+ end
31+
32+ function m (m1:: AbstractMatrix , m2:: AbstractMatrix )
33+ n1, _ = size (m1)
34+ _ , p2 = size (m2)
35+ newmat = zeros (Float64, n1, p2)
36+ for i in 1 : n1
37+ for j in 1 : p2
38+ newmat[i, j] = m (m1[i, :], m2[:, j])
39+ end
40+ end
41+ return newmat
42+ end
43+
44+ function hatrob (x:: AbstractMatrix )
45+ return x * inv (m (x' , x)) * x'
46+ end
47+
48+
49+ """
50+ robhatreg(setting::RegressionSetting)
51+
52+ Perform robust regression using the robust hat matrix method.
53+
54+ # Arguments
55+ - `setting::RegressionSetting`: The regression setting.
56+
57+ # Returns
58+
59+ - A dictionary containing the following
60+ - `betas::AbstractVector`: The estimated coefficients.
61+
62+ # References
63+
64+ Satman, Mehmet Hakan, A robust initial basic subset selection
65+ method for outlier detection algorithms in linear regression, In Press
66+ """
67+ function robhatreg (setting:: RegressionSetting )
68+ X, y = @extractRegressionSetting setting
69+ return robhatreg (X, y)
70+ end
71+
72+
73+ """
74+ robhatreg(X, y)
75+
76+ Perform robust regression using the robust hat matrix method.
77+
78+ # Arguments
79+
80+ - `X::AbstractMatrix`: The design matrix.
81+ - `y::AbstractVector`: The response vector.
82+
83+ # Returns
84+
85+ - A dictionary containing the following
86+ - `betas::AbstractVector`: The estimated coefficients.
87+
88+ # References
89+
90+ Satman, Mehmet Hakan, A robust initial basic subset selection
91+ method for outlier detection algorithms in linear regression, In Press
92+ """
93+ function robhatreg (X, y)
94+ n, p = size (X)
95+ h = Int (ceil ((n + p + 1 )/ 2 ))
96+ myhat = hatrob (X)
97+ diagonals = diag (myhat)
98+ prms = sortperm (diagonals)
99+ bestindices = prms[1 : (p+ 1 )]
100+ _, indices = iterateCSteps (X, y, bestindices, h)
101+ betas = X[indices, :] \ y[indices]
102+ return Dict (" betas" => betas)
103+ end
104+
105+
106+
107+ end # end of module RobustHatRegression
0 commit comments