8
8
! - https://cran.r-project.org/web/packages/meboot/vignettes/meboot.pdf
9
9
! - https://gist.github.com/christianjauregui/314456688a3c2fead43a48be3a47dad6
10
10
11
+ interface compute_stats
12
+ module procedure :: compute_stats_real64
13
+ end interface
14
+
11
15
contains
12
16
! ------------------------------------------------------------------------------
13
17
module subroutine bs_linear_least_squares_real64 (order , intercept , x , y , &
@@ -29,15 +33,14 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
29
33
30
34
! Parameters
31
35
real (real64), parameter :: zero = 0.0d0
32
- real (real64), parameter :: half = 0.5d0
33
36
real (real64), parameter :: p05 = 5.0d-2
37
+ real (real64), parameter :: half = 5.0d-1
34
38
35
39
! Local Variables
36
- integer (int32) :: i, j, i1, i2, n, ns, nc, ncoeffs, flag
40
+ integer (int32) :: i, j, n, ns, nc, ncoeffs, flag
37
41
real (real64) :: eps, alph, ms
38
42
real (real64), allocatable , dimension (:) :: fLocal, yLocal, rLocal
39
43
real (real64), allocatable , dimension (:,:) :: allcoeffs
40
- type (t_distribution) :: dist
41
44
class(errors), pointer :: errmgr
42
45
type (errors), target :: deferr
43
46
@@ -60,10 +63,7 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
60
63
n = size (x)
61
64
ncoeffs = order + 1
62
65
nc = order
63
- i1 = floor (alph * ns, int32)
64
- i2 = ns - i1 + 1
65
66
if (intercept) nc = nc + 1
66
- dist% dof = real (ns - nc)
67
67
68
68
! Compute the fit
69
69
call linear_least_squares(order, intercept, x, y, coeffs, &
@@ -116,26 +116,7 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
116
116
117
117
! Perform statistics calculations, if needed
118
118
if (present (stats)) then
119
- ! Update the relevant statistical metrics for each coefficient based
120
- ! upon the actual distribution
121
- j = 1
122
- if (intercept) j = 0
123
- do i = 1 , nc
124
- j = j + 1
125
- ms = trimmed_mean(allcoeffs(j,:), p = half * alph)
126
- ! As we have a distribution of mean values, the standard deviation
127
- ! of this population yields the standard error estimate for the
128
- ! overall problem
129
- stats(i)% standard_error = standard_deviation(allcoeffs(j,:))
130
- ! As before, this is a distribution of mean values. The CI can
131
- ! be directly estimated by considering the values of the bottom
132
- ! alpha/2 and top alpha/2 terms.
133
- stats(i)% upper_confidence_interval = allcoeffs(j,i2)
134
- stats(i)% lower_confidence_interval = allcoeffs(j,i1)
135
- stats(i)% t_statistic = coeffs(i) / stats(i)% standard_error
136
- stats(i)% probability = regularized_beta(half * dist% dof, half, &
137
- dist% dof / (dist% dof + (stats(i)% t_statistic)** 2 ))
138
- end do
119
+ call compute_stats(coeffs, allcoeffs, alph, intercept, stats)
139
120
end if
140
121
141
122
! Compute the bias for each parameter, if needed
@@ -154,5 +135,56 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
154
135
end if
155
136
end subroutine
156
137
138
+ ! ------------------------------------------------------------------------------
139
+ subroutine compute_stats_real64 (mdl , coeffs , alpha , intercept , stats )
140
+ ! Arguments
141
+ real (real64), intent (in ), dimension (:) :: mdl
142
+ real (real64), intent (inout ), dimension (:,:) :: coeffs
143
+ real (real64), intent (in ) :: alpha
144
+ logical , intent (in ) :: intercept
145
+ type (bootstrap_regression_statistics), intent (out ), dimension (:) :: stats
146
+
147
+ ! Parameters
148
+ real (real64), parameter :: half = 0.5d0
149
+
150
+ ! Local Variables
151
+ integer (int32) :: i, j, i1, i2, ncoeffs, nc, nsamples
152
+ real (real64) :: ms
153
+ type (t_distribution) :: dist
154
+
155
+ ! Initialization
156
+ ncoeffs = size (coeffs, 1 )
157
+ nsamples = size (coeffs, 2 )
158
+ nc = ncoeffs
159
+ if (.not. intercept) nc = ncoeffs - 1
160
+ i1 = floor (half * alpha * nsamples, int32)
161
+ i2 = nsamples - i1 + 1
162
+ dist% dof = real (nsamples - nc)
163
+
164
+ ! Process
165
+ j = 1
166
+ if (intercept) j = 0
167
+ do i = 1 , nc
168
+ j = j + 1
169
+ ms = trimmed_mean(coeffs(j,:), p = half * alpha)
170
+
171
+ ! As we have a distribution of mean values, the standard deviation
172
+ ! of this population yields the standard error estimate for the
173
+ ! overall problem
174
+ stats(i)% standard_error = standard_deviation(coeffs(j,:))
175
+
176
+ ! As before, this is a distribution of mean values. The CI can
177
+ ! be directly estimated by considering the values of the bottom
178
+ ! alpha/2 and top alpha/2 terms.
179
+ stats(i)% upper_confidence_interval = coeffs(j,i2)
180
+ stats(i)% lower_confidence_interval = coeffs(j,i1)
181
+
182
+ ! Compute the remaining parameters
183
+ stats(i)% t_statistic = mdl(j) / stats(i)% standard_error
184
+ stats(i)% probability = regularized_beta(half * dist% dof, half, &
185
+ dist% dof / (dist% dof + (stats(i)% t_statistic)** 2 ))
186
+ end do
187
+ end subroutine
188
+
157
189
! ------------------------------------------------------------------------------
158
190
end submodule
0 commit comments