-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.html
333 lines (321 loc) · 11.3 KB
/
calibration.html
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
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/pms.css" rel="stylesheet" type="text/css" />
<script src="js/plotly-2.24.1.min.js"></script>
<!-- script src="../js/fast-stats.js"></script -->
<script src="js/sfunc.js"></script>
<script src="js/sprintf.js"></script>
<script src="js/readers.js"></script>
<script src="js/d3.v7.min.js"></script>
<script src="js/statistics-distribution.js"></script>
<style>
<style>
.resizable {
resize: both;
overflow: scroll;
border: 1px solid black;
width: 600px;
height: 300px;
}
</style>
</style>
<title>Calibration Tester</title>
</head>
<body>
<h2>Kalibrationstester</h2>
Eingabe von (x,y)-Datenpaaren. Es wird eine Kalibrationsgerade
bestimmt, und auf Outlier getestet.
<p>
Das innere an der Regressionsgeraden liegende Kurvenpaar
zeigt die Unsicherheit der Regressionsgeraden. Mit einer
Wahrscheinlichkeit con 95% liegt die wahre Ausgleichskurve zwischen den beiden Kurven.
</p>
<table>
<tr>
<td>Titel</td><td><input type="text" id="title" value="" /></td>
</tr>
<tr>
<td>Einheit x:</td><td><input type="text" id="xtitle" value="" /></td>
</tr>
<tr>
<td>Einheit y:</td><td><input type="text" id="ytitle" value="" /></td>
</tr>
<tr>
<td>Daten</td>
<td colspan="3">
<textarea id="tdata" rows="8" cols="60"></textarea>
</td>
</tr>
<tr>
<td><button onclick="makeDiag()">Make</button></td>
<td><button onclick="makeImg('myDiv', 'jpg-export')">Copy</button></td>
</tr>
<tr>
<td>Width</td><td><input type="number" id="cWidth" value="600" onchange="cHWChange()" /></td>
</tr>
<tr>
<td>Height</td><td><input type="number" id="cHeight" value="300" onchange="cHWChange()" /></td>
</tr>
<tr>
<td>Y-Max</td><td><input type="number" id="yMax" value=""/></td>
</tr>
<tr>
<td>Y-Min</td><td><input type="number" id="yMin" value=""/></td>
</tr>
<tr>
<td>Show CI</td><td><input type="checkbox" id="cbShowCI" checked="true"/></td>
<td>Show pred SE</td><td><input type="checkbox" id="cbShowSE" checked="true"/></td>
</tr>
</table>
<p>
<table id="pTab">
<tr><td>N</td><td id="resN"</td></tr>
<tr><td>r²</td><td id="resR2"</td></tr>
<tr><td>a</td><td id="resA"</td></tr>
<tr><td>b</td><td id="resB"</td></tr>
<tr><td>SE</td><td id="resSE"</td></tr>
</table>
</p>
<p>
<div id='myDiv' class="resizable" onresize="doResize(this)"></div>
<br>
</p>
<img id="jpg-export"></img>
</body>
<script>
const arrayRange = (start, stop, step) =>
Array.from(
{ length: (stop - start) / step + 1 },
(value, index) => start + index * step
);
cHWChange();
function makeDiag(){
let title = document.getElementById( 'title').value;
let xtitle = document.getElementById('xtitle').value;
let ytitle = document.getElementById('ytitle').value;
let sampleData = readPairs(document.getElementById('tdata').value);
n = Math.min(sampleData[0].length, sampleData[1].length);
let reg = linearRegression(sampleData[0], sampleData[1]);
// from: https://www.real-statistics.com/regression/regression-analysis/
let xMean = reg.sX/reg.n;
let ssT= reg.sY2 - reg.sY*reg.sY/reg.n;
let ssRes = ssT*(1-reg.r*reg.r);
let ssReg = ssT-ssRes;
let sRes = Math.sqrt(ssRes/(reg.n-2));
let ssX = reg.sX2-reg.sX*reg.sX/reg.n;
let seB0 = sRes*Math.sqrt(1/reg.n+xMean*xMean/ssX);
let seB1 = sRes/Math.sqrt(ssX);
let tCrit = tdistr(reg.n-2, 0.025);
console.log("ssT: %f, ssReg: %f, ssRes: %f", ssT, ssReg, ssRes);
console.log("seB0: %f seB1: %f", seB0, seB1)
// stderror for normalized residuals of
// https://statologie.de/standardisierte-residuen-excel/
console.log("s_Res : %f", sRes);
let x1 = Math.min(...sampleData[0]);
let x2 = Math.max(...sampleData[0]);
let xL = 0.5*(x1+x2)-0.55*(x2-x1);
let xH = 0.5*(x1+x2)+0.55*(x2-x1);
let plData = [];
let ss = {
x: sampleData[0],
y: sampleData[1],
name: 'Data',
showlegend: true,
type : 'scatter',
mode : 'markers',
marker: {
color: 'blue',
symbol: 'circle',
size: 8
}
};
plData.push(ss);
// Compute standardized residuals and mark outliers as red
let xViol=[];
let yViol=[];
for(let i=0; i<reg.n; i++){
// residual
let x0 = sampleData[0][i];
let resid = -(x0*reg.b+reg.a - sampleData[1][i]);
// Leverage:
let lever = 1/reg.n + (x0-xMean)*(x0-xMean)/ssX;
let stdResid = resid/(sRes*Math.sqrt(1-lever));
//console.log(resid, lever, stdResid);
if(Math.abs(stdResid)>2){ // Should be two or three
xViol.push(x0);
yViol.push(sampleData[1][i]);
}
}
if(xViol.length > 0){
plData.push({
type: 'scatter',
x: xViol,
y: yViol,
mode: 'markers',
name: 'Outlier',
showlegend: true,
marker: {
color: 'rgb(255,65,54)',
line: {width: 3},
opacity: 0.5,
size: 12,
symbol: 'circle-open'
}
});
}
// Plot regression line
plData.push({
x: [xL, xH],
y: [xL*reg.b + reg.a, xH*reg.b + reg.a],
name: sprintf("%.4fx+%.4f", reg.b, reg.a), // 'berechnet',
type : 'scatter',
mode : 'lines',
line: {
color: 'blue',
width: 2,
},
});
//
let xRange = arrayRange(xL, xH, (xH-xL)/20 );
let ciL = [];
let ciH = [];
let predL = [];
let predH = [];
for(let i=0; i<=20; i++){
let x0 = xRange[i];
let yd = reg.a+reg.b*x0;
// Error of regression line
let confSE = sRes*Math.sqrt(1/reg.n + (x0-xMean)*(x0-xMean)/ssX);
// error of predictor line
let predSE = sRes*Math.sqrt(1+1/reg.n+(x0-xMean)*(x0-xMean)/ssX);
ciL.push(yd-tCrit*confSE);
ciH.push(yd+tCrit*confSE);
predL.push(yd-tCrit*predSE);
predH.push(yd+tCrit*predSE);
}
/** KILLME **/
// https://stats.stackexchange.com/questions/342632/how-to-understand-se-of-regression-slope-equation
let confSE1 = sRes*Math.sqrt(1/reg.n + (x1-xMean)*(x1-xMean)/ssX);
let confSE2 = sRes*Math.sqrt(1/reg.n + (x2-xMean)*(x2-xMean)/ssX);
console.log(confSE1*tCrit, confSE2*tCrit);
let y1L = reg.a+reg.b*x1 - tCrit*confSE1;
let y1H = reg.a+reg.b*x1 + tCrit*confSE1;
let y2L = reg.a+reg.b*x2 - tCrit*confSE2;
let y2H = reg.a+reg.b*x2 + tCrit*confSE2;
console.log((y2H-y1L)/(x2-x1), (y2L-y1H)/(x2-x1));
/** To here **/
if(document.getElementById('cbShowCI').checked){
ss = {
x: xRange,
y: ciL,
name: 'CI',
type : 'scatter',
mode : 'lines',
line: { color: 'rgb(255,0,155)',width: 1, dash:'dash'},
};
plData.push(ss);
ss = {
x: xRange,
y: ciH,
showlegend: false,
name: 'ciH',
type : 'scatter',
mode : 'lines',
line: { color: 'rgb(255,0,155)',width: 1, dash:'dash'},
};
plData.push(ss);
}
if(document.getElementById('cbShowSE').checked){
ss = {
x: xRange,
y: predL,
name: 'SE predict',
type : 'scatter',
mode : 'lines',
line: { color: 'rgb(155,0,255)',width: 1, dash:'dash'},
};
plData.push(ss);
ss = {
x: xRange,
y: predH,
showlegend: false,
name: 'predH',
type : 'scatter',
mode : 'lines',
line: { color: 'rgb(155,0,255)',width: 1, dash:'dash'},
};
plData.push(ss);
}
//console.log(plData);
let layout = {
title: title,
xaxis: {
title: xtitle,
},
yaxis: {
title: ytitle,
},
};
let yMin = document.getElementById('yMin').value;
let yMax = document.getElementById('yMax').value;
if( (yMin !=="") && (yMax !== "") ){
layout.yaxis.range = [yMin, yMax];
}
Plotly.newPlot('myDiv', plData, layout, {editable: true, responsive: true},);
document.getElementById('resN').innerHTML = reg.n;
document.getElementById('resR2').innerHTML = reg.r*reg.r;
document.getElementById('resA').innerHTML = sprintf("%.4f, [%.4f, %.4f]", reg.a, reg.a-seB0*tCrit, reg.a+seB0*tCrit);
document.getElementById('resB').innerHTML = sprintf("%.4f, [%.4f, %.4f]", reg.b, reg.b-seB1*tCrit, reg.b+seB1*tCrit);
document.getElementById('resSE').innerHTML = sRes;
}
function cHWChange(){
let W = document.getElementById('cWidth').value;
let H = document.getElementById('cHeight').value;
document.getElementById('myDiv').setAttribute("style","Width:"+ W +'px;Height:'+ H+'px');
}
</script>
</html>
<!--
// https://statologie.de/standardisierte-residuen-excel/
/*
8 41
12 42
12 39
13 37
14 35
16 39
17 45
22 46
24 39
26 49
29 55
30 57
Set 2
0 125
20 2133
40 3988
60 6123
80 8976
100 10102
*/
https://www.real-statistics.com/regression/confidence-and-prediction-intervals/
5;80
23;78
25;60
48;53
17;85
8;84
4;73
26;79
11;81
19;75
14;68
35;72
29;58
4;92
23;65
StdError for a and b
https://stats.stackexchange.com/questions/289457/proof-for-the-standard-error-of-parameters-in-linear-regression
-->