forked from fslaborg/FSharp.Stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodnessOfFit.fsx
More file actions
357 lines (289 loc) · 12 KB
/
GoodnessOfFit.fsx
File metadata and controls
357 lines (289 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
(**
---
title: Fit quality
index: 11
category: Documentation
categoryindex: 0
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: FSharpAux.Core, 2.0.0"
#r "nuget: FSharpAux, 2.0.0"
#r "nuget: FSharpAux.IO, 2.0.0"
#r "nuget: OptimizedPriorityQueue, 5.1.0"
#r "nuget: FsMath, 0.0.2"
#I "../src/FSharp.Stats/bin/Release/.net8.0/"
#r "FSharp.Stats.dll"
#r "nuget: Plotly.NET, 4.0.0"
open FsMath
Plotly.NET.Defaults.DefaultDisplayOptions <-
Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference)
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, 4.0.0"
#r "nuget: Plotly.NET.Interactive, 4.0.0"
#r "nuget: FSharp.Stats"
open Plotly.NET
#endif // IPYNB
open Plotly.NET
(**
# Fit quality
[](https://mybinder.org/v2/gh/fslaborg/FSharp.Stats/gh-pages?urlpath=/tree/home/jovyan/GoodnessOfFit.ipynb)
[]({{root}}{{fsdocs-source-basename}}.ipynb)
_Summary:_ this tutorial shows how to assess fit quality with FSharp.Stats
## Linear regression report
Consider this simple linear regression:
*)
open FSharp.Stats
open FSharp.Stats.Fitting
open LinearRegression.OLS
open GoodnessOfFit.OLS.Linear.Univariable
open FSharp.Stats.Distributions
//data sorted by x values
let x = vector [|1. .. 10.|]
let y = vector [|4.;10.;9.;7.;13.;17.;16.;23.;15.;30.|]
///linear regression line fitting function
let coefficients = Linear.Univariable.fit x y
let predictFunc = Linear.Univariable.predict coefficients
let fittedValues = x |> Seq.map predictFunc
let chart =
[
Chart.Point(x,y) |> Chart.withTraceInfo "raw"
Chart.Line(fittedValues|> Seq.mapi (fun i y -> x.[i],y)) |> Chart.withTraceInfo "fit"
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
(*** condition: ipynb ***)
#if IPYNB
chart
#endif // IPYNB
(***hide***)
chart |> GenericChart.toChartHTML
(***include-it-raw***)
(**
Various quality parameters can be accessed via the `GoodnessOfFit` module:
*)
//In the following some quality/interval/significance values are computed:
let sos = GoodnessOfFit.calculateSumOfSquares predictFunc x y
let n = sos.Count
let meanX = sos.MeanX
let meanY = sos.MeanY
let slope = coefficients.[1]
let intercept = coefficients.[0]
//coefficient of determination
let rSq = GoodnessOfFit.calculateDeterminationFromValue y fittedValues
//adjusted coefficient of determination; variable=number of coefficints (excluding intercept)
let rSqAdj = GoodnessOfFit.calculateDeterminationAdj y fittedValues 1
//pearson correlation coefficient
let r = sqrt rSq
//total sum of squares
let ssTotal = sos.Total
//regression sum of squares
let ssReg = sos.Regression
//residual sum of squares
let ssResidual = sos.Error
//sum of squares xx
let ssxx = sos.SSxx
//sum of products xy
let ssxy = sos.SSxy
//standard error of the regression slope
let stdErrSlope = GoodnessOfFit.standardErrorSlope sos
//standard error of the regression intercept
let stdErrIntercept = GoodnessOfFit.standardErrorIntercept sos
//standard error of the estimate (S)
let stdErrEstimate = GoodnessOfFit.standardErrorEstimate sos
//confidence intervals (df = n-#coefficients; a=5%)
let criticalT = Testing.TTest.getCriticalTValue (n - 2.) 0.05 Testing.TTest.TwoTailed
let lowerS = slope - criticalT * stdErrSlope
let upperS = slope + criticalT * stdErrSlope
let lowerI = intercept - criticalT * stdErrIntercept
let upperI = intercept + criticalT * stdErrIntercept
//significance tests
let testSlope = GoodnessOfFit.ttestSlope slope sos
let testInterc = GoodnessOfFit.ttestIntercept intercept sos
let outputTable =
let header = ["<b>ParameterName</b>";"Value";"StandardError (SE Coeff)"]
let rows =
let print f = sprintf "%.3f" f
[
["n"; sprintf "%.0f" n; "-"]
["meanX"; print meanX; "-"]
["meanY"; print meanY; "-"]
["slope"; print slope; print stdErrSlope]
["intercept" ; print intercept; print stdErrIntercept]
["<b>Goodness of fit</b>";""; ""]
["SS_total"; print ssTotal; ""]
["SS_regression"; print ssReg; ""]
["SS_residual"; print ssResidual; ""]
["r (pearson cor. coef."; print r; ""]
["r_squared"; print rSq; ""]
["r_squared_adj"; print rSqAdj; ""]
["SE Estimate"; print stdErrEstimate; ""]
["<b>95% Confidence interval</b>";"<b>min</b>"; "<b>max</b>"]
["slope"; print lowerS; print upperS]
["intercept"; print lowerI; print upperI]
["<b>significances</b>";""; ""]
["slope p Value"; print testSlope.PValue; ""]
["intercept p Value";print testInterc.PValue;""]
]
Chart.Table(
header,
rows,
HeaderFillColor = Color.fromString "#deebf7",
CellsFillColor = Color.fromColors [Color.fromString "#deebf7"; Color.fromString "white";Color. fromString "white"],
CellsMultiAlign = [StyleParam.HorizontalAlign.Left;StyleParam.HorizontalAlign.Center]
)
|> Chart.withTitle "Regression report"
(*** condition: ipynb ***)
#if IPYNB
outputTable
#endif // IPYNB
(***hide***)
outputTable |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Confidence bands
A confidence band shows the uncertainty of an curve estimate. It widens towards the periphery.
A prediction band shows the uncertainty of a value of a new data point.
In both cases homoscedasticity is assumed.
*)
//data sorted by x values
let xData = vector [|1. .. 10.|]
let yData = vector [|4.;10.;9.;7.;13.;17.;16.;23.;15.;30.|]
//let xData = vector [|1.47;1.50;1.52;1.55;1.57;1.60;1.63;1.65;1.68;1.70;1.73;1.75;1.78;1.80;1.83|]
//let yData = vector [|52.21;53.12;54.48;55.84;57.20;58.57;59.93;61.29;63.11;64.47;66.28;68.10;69.92;72.19;74.46|]
let values = Seq.zip xData yData
///linear regression line fitting function
let coeffs = Linear.Univariable.fit xData yData
let predictionFunction = Linear.Univariable.predict coeffs
let fitValues = xData |> Seq.map (fun xi -> xi,(predictionFunction xi))
///calculate confidence band errors for every x value
let confidence =
xData
|> Array.map (calculateConfidenceBandError xData yData 0.95)
///lower and upper bounds of the 95% confidence band sorted according to x values
let (lower,upper) =
xData
|> Array.mapi (fun i xi -> (predictionFunction xi) - confidence.[i],(predictionFunction xi) + confidence.[i])
|> Array.unzip
let rangePlot =
[
Chart.Range (
xy=fitValues,
lower=lower,
upper=upper,
mode = StyleParam.Mode.Lines,
LineColor = Color.fromKeyword ColorKeyword.Blue,
RangeColor = Color.fromKeyword ColorKeyword.LightBlue
)
|> Chart.withTraceInfo "CI95"
Chart.Point (values,MarkerColor=Color.fromString "#000000") |> Chart.withTraceInfo "raw"
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withTitle "Confidence band 95%"
(*** condition: ipynb ***)
#if IPYNB
rangePlot
#endif // IPYNB
(***hide***)
rangePlot |> GenericChart.toChartHTML
(***include-it-raw***)
(**
The confidence band calculation is not limited to the original x values. To get a smooth confidence band, introduce additional x values in small steps.
*)
let newXValues =
vector [|1. .. 0.5 .. 11.|]
///calculate confidence band errors for every x value
let newConfidence =
newXValues
|> Array.map (calculateConfidenceBandError xData yData 0.95)
///lower and upper bounds of the 95% confidence band sorted according to x values
let (newLower,newUpper) =
newXValues
|> Array.mapi (fun i xi -> (predictionFunction xi) - newConfidence.[i],(predictionFunction xi) + newConfidence.[i])
|> Array.unzip
let linePlot =
[
Chart.Point(xData,yData) |> Chart.withTraceInfo (sprintf "%.2f+%.4fx" coeffs.[0] coeffs.[1])
Chart.Line(fitValues) |> Chart.withTraceInfo "linear regression"
Chart.Line(newXValues,newLower,LineColor= Color.fromString "#C1C1C1") |> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "lower"
Chart.Line(newXValues,newUpper,LineColor= Color.fromString "#C1C1C1") |> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "upper"
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withTitle "Confidence band 95%"
(*** condition: ipynb ***)
#if IPYNB
rangePlot
#endif // IPYNB
(***hide***)
linePlot |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Prediction bands
*)
let predictionXValues = vector [|1. .. 0.5 .. 15.|]
///calculate preditcion band errors for every x value
let prediction =
predictionXValues
|> Array.map (calculatePredictionBandError xData yData 0.95)
///lower and upper bounds of the 95% prediction band sorted according to x values
let (pLower,pUpper) =
predictionXValues
|> Array.mapi (fun i xi -> (predictionFunction xi) - prediction.[i],(predictionFunction xi) + prediction.[i])
|> Array.unzip
let predictionPlot =
[
Chart.Point(xData,yData) |> Chart.withTraceInfo (sprintf "%.2f+%.4fx" coeffs.[0] coeffs.[1])
Chart.Line(fitValues) |> Chart.withTraceInfo "linear regression"
Chart.Line(predictionXValues,pLower,LineColor= Color.fromString "#C1C1C1") |> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "pLower"
Chart.Line(predictionXValues,pUpper,LineColor= Color.fromString "#C1C1C1") |> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "pUpper"
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withTitle "Prediction band"
(***hide***)
predictionPlot |> GenericChart.toChartHTML
(***include-it-raw***)
(**
## Cook's distance
Leverage: Leverage describes the potential impact of data points regarding their regression line. Points that show a great dependent-variable-distance to all other points, have a
higher potential to distort the regression line coefficients (high-leverage points).
Cooks distance (D) is a measure to describe the influence of each data point to the regression line.
If D is low, the influence is low, while a high D indicates an 'influential observation' that is worth taking a closer look.
Cooks distance is a mixture of the residual sum of squares at the particular point and its leverage.
A linear threshold is arbitrarily defined by either 1, 4/n, or 3*mean(D).
Because outliers have a strong influence to D of all other points as well, the thresholds should not be applied without checking the issues by eye.
*)
open LinearRegression.OLS.Linear
let xD = vector [|1. .. 10.|]
let yD = vector [|4.;6.;9.;7.;13.;17.;16.;23.;14.;26.|]
let cooksDistance = Univariable.cooksDistance xD yD
let nD = float xD.Length
let meanCook = Seq.mean cooksDistance
let threshold1 = 1.
let threshold2 = 4. / nD
let threshold3 = 3. * meanCook
let cook =
[
Chart.Column (Seq.zip xD cooksDistance) |> Chart.withTraceInfo "cook's distance"
Chart.Line([0.5,threshold1;10.5,threshold1])|> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "t=1"
Chart.Line([0.5,threshold2;10.5,threshold2])|> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "t=4/n"
Chart.Line([0.5,threshold3;10.5,threshold3])|> Chart.withLineStyle(Dash=StyleParam.DrawingStyle.Dash)|> Chart.withTraceInfo "t=3*mean(D)"
]
|> Chart.combine
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withYAxisStyle "cook's distance"
|> fun l -> [(Chart.Point(xD,yD) |> Chart.withTraceInfo "raw");l] |> Chart.Grid(2,1)
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withTitle "Cook's distance"
|> Chart.withSize (650.,650.)
(*** condition: ipynb ***)
#if IPYNB
cook
#endif // IPYNB
(***hide***)
cook |> GenericChart.toChartHTML
(***include-it-raw***)