diff --git a/bun.lockb b/bun.lockb index d493c2c..a619f29 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/deno.json b/deno.json index 08f662a..41d7226 100644 --- a/deno.json +++ b/deno.json @@ -2,5 +2,8 @@ "tasks": { }, "imports": { - } + }, + "exclude": [ + "notebooks/" + ] } diff --git a/notebooks/data_analytics_js.ipynb b/notebooks/data_analytics_js.ipynb index 96f8f27..6ff3fa8 100644 --- a/notebooks/data_analytics_js.ipynb +++ b/notebooks/data_analytics_js.ipynb @@ -493,6 +493,7 @@ ], "source": [ "import { display } from \"https://deno.land/x/display@v0.1.1/mod.ts\";\n", + "import { Plot } from \"https://l12.xyz/x/shortcuts/raw/plots.ts\";\n", "import pl from \"npm:nodejs-polars\";\n", "\n", "let data = await Deno.readTextFile(\"assets/CarPrice_Assignment.csv\");\n", @@ -674,86 +675,86 @@ "application/vnd.dataresource+json": { "bytes": null, "data": [ - { - "brand": "subaru" - }, - { - "brand": "renault" - }, - { - "brand": "buick" - }, - { - "brand": "vw" - }, - { - "brand": "chevrolet" - }, - { - "brand": "audi" - }, - { - "brand": "honda" - }, - { - "brand": "plymouth" - }, { "brand": "toyota" }, - { - "brand": "alfa-romero" - }, - { - "brand": "saab" - }, - { - "brand": "mazda" - }, { "brand": "mercury" }, + { + "brand": "mitsubishi" + }, + { + "brand": "honda" + }, + { + "brand": "mazda" + }, { "brand": "peugeot" }, - { - "brand": "nissan" - }, - { - "brand": "vokswagen" - }, - { - "brand": "volkswagen" - }, - { - "brand": "maxda" - }, - { - "brand": "bmw" - }, { "brand": "porcshce" }, { - "brand": "mitsubishi" - }, - { - "brand": "dodge" + "brand": "renault" }, { "brand": "isuzu" }, { - "brand": "porsche" + "brand": "dodge" + }, + { + "brand": "bmw" + }, + { + "brand": "vw" + }, + { + "brand": "maxda" + }, + { + "brand": "volkswagen" + }, + { + "brand": "alfa-romero" + }, + { + "brand": "chevrolet" }, { "brand": "toyouta" }, + { + "brand": "jaguar" + }, + { + "brand": "saab" + }, + { + "brand": "porsche" + }, + { + "brand": "audi" + }, + { + "brand": "vokswagen" + }, + { + "brand": "subaru" + }, + { + "brand": "nissan" + }, + { + "brand": "plymouth" + }, { "brand": "volvo" }, { - "brand": "jaguar" + "brand": "buick" } ], "description": null, @@ -786,7 +787,7 @@ "title": null }, "text/html": [ - "
brand
subaru
renault
buick
vw
chevrolet
audi
honda
plymouth
toyota
alfa-romero
saab
mazda
mercury
peugeot
nissan
vokswagen
volkswagen
maxda
bmw
porcshce
mitsubishi
dodge
isuzu
porsche
toyouta
volvo
jaguar
" + "
brand
toyota
mercury
mitsubishi
honda
mazda
peugeot
porcshce
renault
isuzu
dodge
bmw
vw
maxda
volkswagen
alfa-romero
chevrolet
toyouta
jaguar
saab
porsche
audi
vokswagen
subaru
nissan
plymouth
volvo
buick
" ] }, "execution_count": 4, @@ -823,70 +824,70 @@ "bytes": null, "data": [ { - "brand": "subaru" - }, - { - "brand": "peugeot" - }, - { - "brand": "plymouth" - }, - { - "brand": "mitsubishi" - }, - { - "brand": "porsche" - }, - { - "brand": "bmw" - }, - { - "brand": "chevrolet" - }, - { - "brand": "dodge" + "brand": "nissan" }, { "brand": "toyota" }, - { - "brand": "mazda" - }, - { - "brand": "mercury" - }, { "brand": "isuzu" }, { - "brand": "renault" + "brand": "plymouth" }, { - "brand": "buick" + "brand": "subaru" }, { "brand": "audi" }, { - "brand": "nissan" + "brand": "renault" }, { "brand": "honda" }, { - "brand": "jaguar" + "brand": "dodge" + }, + { + "brand": "bmw" + }, + { + "brand": "volvo" + }, + { + "brand": "peugeot" }, { "brand": "volkswagen" }, + { + "brand": "chevrolet" + }, + { + "brand": "mitsubishi" + }, + { + "brand": "buick" + }, { "brand": "alfa-romero" }, + { + "brand": "porsche" + }, + { + "brand": "mazda" + }, { "brand": "saab" }, { - "brand": "volvo" + "brand": "jaguar" + }, + { + "brand": "mercury" } ], "description": null, @@ -919,7 +920,7 @@ "title": null }, "text/html": [ - "
brand
subaru
peugeot
plymouth
mitsubishi
porsche
bmw
chevrolet
dodge
toyota
mazda
mercury
isuzu
renault
buick
audi
nissan
honda
jaguar
volkswagen
alfa-romero
saab
volvo
" + "
brand
nissan
toyota
isuzu
plymouth
subaru
audi
renault
honda
dodge
bmw
volvo
peugeot
volkswagen
chevrolet
mitsubishi
buick
alfa-romero
porsche
mazda
saab
jaguar
mercury
" ] }, "execution_count": 5, @@ -1818,11 +1819,11 @@ "brand_count": 3 }, { - "brand": "chevrolet", + "brand": "alfa-romero", "brand_count": 3 }, { - "brand": "alfa-romero", + "brand": "chevrolet", "brand_count": 3 }, { @@ -1838,11 +1839,11 @@ "brand_count": 6 }, { - "brand": "plymouth", + "brand": "audi", "brand_count": 7 }, { - "brand": "audi", + "brand": "plymouth", "brand_count": 7 }, { @@ -1865,22 +1866,22 @@ "brand": "volvo", "brand_count": 11 }, - { - "brand": "subaru", - "brand_count": 12 - }, { "brand": "volkswagen", "brand_count": 12 }, { - "brand": "mitsubishi", - "brand_count": 13 + "brand": "subaru", + "brand_count": 12 }, { "brand": "honda", "brand_count": 13 }, + { + "brand": "mitsubishi", + "brand_count": 13 + }, { "brand": "mazda", "brand_count": 17 @@ -1934,7 +1935,7 @@ "title": null }, "text/html": [ - "
brandbrand_count
mercury1
renault2
jaguar3
chevrolet3
alfa-romero3
isuzu4
porsche5
saab6
plymouth7
audi7
buick8
bmw8
dodge9
peugeot11
volvo11
subaru12
volkswagen12
mitsubishi13
honda13
mazda17
nissan18
toyota32
" + "
brandbrand_count
mercury1
renault2
jaguar3
alfa-romero3
chevrolet3
isuzu4
porsche5
saab6
audi7
plymouth7
buick8
bmw8
dodge9
peugeot11
volvo11
volkswagen12
subaru12
honda13
mitsubishi13
mazda17
nissan18
toyota32
" ] }, "execution_count": 8, @@ -2057,28 +2058,28 @@ "data": { "text/plain": [ "{\n", - " porsche: \u001b[33m31400.5\u001b[39m,\n", - " volkswagen: \u001b[33m10077.5\u001b[39m,\n", - " buick: \u001b[33m33647\u001b[39m,\n", - " plymouth: \u001b[33m7963.428571428572\u001b[39m,\n", - " peugeot: \u001b[33m15489.09090909091\u001b[39m,\n", " renault: \u001b[33m9595\u001b[39m,\n", - " volvo: \u001b[33m18063.18181818182\u001b[39m,\n", " jaguar: \u001b[33m34600\u001b[39m,\n", - " mitsubishi: \u001b[33m9239.76923076923\u001b[39m,\n", " saab: \u001b[33m15223.333333333334\u001b[39m,\n", - " honda: \u001b[33m8184.692307692308\u001b[39m,\n", - " audi: \u001b[33m17859.166714285715\u001b[39m,\n", - " mercury: \u001b[33m16503\u001b[39m,\n", - " chevrolet: \u001b[33m6007\u001b[39m,\n", - " dodge: \u001b[33m7875.444444444444\u001b[39m,\n", - " subaru: \u001b[33m8541.25\u001b[39m,\n", " nissan: \u001b[33m10415.666666666666\u001b[39m,\n", - " isuzu: \u001b[33m8916.5\u001b[39m,\n", - " \u001b[32m\"alfa-romero\"\u001b[39m: \u001b[33m15498.333333333334\u001b[39m,\n", - " mazda: \u001b[33m10652.882352941177\u001b[39m,\n", " toyota: \u001b[33m9885.8125\u001b[39m,\n", - " bmw: \u001b[33m26118.75\u001b[39m\n", + " mitsubishi: \u001b[33m9239.76923076923\u001b[39m,\n", + " porsche: \u001b[33m31400.5\u001b[39m,\n", + " plymouth: \u001b[33m7963.428571428572\u001b[39m,\n", + " dodge: \u001b[33m7875.444444444444\u001b[39m,\n", + " mazda: \u001b[33m10652.882352941177\u001b[39m,\n", + " honda: \u001b[33m8184.692307692308\u001b[39m,\n", + " peugeot: \u001b[33m15489.09090909091\u001b[39m,\n", + " audi: \u001b[33m17859.166714285715\u001b[39m,\n", + " volvo: \u001b[33m18063.18181818182\u001b[39m,\n", + " buick: \u001b[33m33647\u001b[39m,\n", + " chevrolet: \u001b[33m6007\u001b[39m,\n", + " isuzu: \u001b[33m8916.5\u001b[39m,\n", + " bmw: \u001b[33m26118.75\u001b[39m,\n", + " \u001b[32m\"alfa-romero\"\u001b[39m: \u001b[33m15498.333333333334\u001b[39m,\n", + " volkswagen: \u001b[33m10077.5\u001b[39m,\n", + " mercury: \u001b[33m16503\u001b[39m,\n", + " subaru: \u001b[33m8541.25\u001b[39m\n", "}" ] }, @@ -2115,16 +2116,16 @@ "brand_category": "Mid_Range" }, { - "brand_category": "Mid_Range" - }, - { - "brand_category": "Luxury" - }, - { - "brand_category": "Luxury" + "brand_category": "Budget" }, { "brand_category": "Mid_Range" + }, + { + "brand_category": "Mid_Range" + }, + { + "brand_category": "Budget" } ], "description": null, @@ -2157,7 +2158,7 @@ "title": null }, "text/html": [ - "
brand_category
Mid_Range
Mid_Range
Luxury
Luxury
Mid_Range
" + "
brand_category
Mid_Range
Budget
Mid_Range
Mid_Range
Budget
" ] }, "execution_count": 11, @@ -2245,22 +2246,22 @@ " { brand: \"mercury\", brand_count: 1 },\n", " { brand: \"renault\", brand_count: 2 },\n", " { brand: \"jaguar\", brand_count: 3 },\n", - " { brand: \"chevrolet\", brand_count: 3 },\n", " { brand: \"alfa-romero\", brand_count: 3 },\n", + " { brand: \"chevrolet\", brand_count: 3 },\n", " { brand: \"isuzu\", brand_count: 4 },\n", " { brand: \"porsche\", brand_count: 5 },\n", " { brand: \"saab\", brand_count: 6 },\n", - " { brand: \"plymouth\", brand_count: 7 },\n", " { brand: \"audi\", brand_count: 7 },\n", + " { brand: \"plymouth\", brand_count: 7 },\n", " { brand: \"buick\", brand_count: 8 },\n", " { brand: \"bmw\", brand_count: 8 },\n", " { brand: \"dodge\", brand_count: 9 },\n", " { brand: \"peugeot\", brand_count: 11 },\n", " { brand: \"volvo\", brand_count: 11 },\n", - " { brand: \"subaru\", brand_count: 12 },\n", " { brand: \"volkswagen\", brand_count: 12 },\n", - " { brand: \"mitsubishi\", brand_count: 13 },\n", + " { brand: \"subaru\", brand_count: 12 },\n", " { brand: \"honda\", brand_count: 13 },\n", + " { brand: \"mitsubishi\", brand_count: 13 },\n", " { brand: \"mazda\", brand_count: 17 },\n", " { brand: \"nissan\", brand_count: 18 },\n", " { brand: \"toyota\", brand_count: 32 }\n", @@ -2280,7 +2281,7 @@ ":where(.plot-d6a7b5 text),\n", ":where(.plot-d6a7b5 tspan) {\n", " white-space: pre;\n", - "}toyotanissanmazdahondamitsubishisubaruvolkswagenpeugeotvolvododgebmwbuickaudiplymouthsaabporscheisuzualfa-romerochevroletjaguarrenaultmercurybrand051015202530brand_count →" + "}toyotanissanmazdahondamitsubishisubaruvolkswagenpeugeotvolvododgebmwbuickaudiplymouthsaabporscheisuzualfa-romerochevroletjaguarrenaultmercurybrand051015202530brand_count →" ] }, "execution_count": 13, @@ -2289,7 +2290,8 @@ } ], "source": [ - "import { document, Plot } from \"https://l12.xyz/x/shortcuts/raw/plots.ts\";\n", + "\n", + "import { document } from \"https://l12.xyz/x/shortcuts/raw/plots.ts\";\n", "\n", "const brandCountRecords = brandCount.toRecords();\n", "console.log(brandCountRecords);\n", @@ -2805,35 +2807,6 @@ { "cell_type": "code", "execution_count": 20, - "id": "20b0317a-5807-4f28-9008-8540e5ee056a", - "metadata": { - "editable": true, - "slideshow": { - "slide_type": "" - }, - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\u001b[33m0.8006648595531076\u001b[39m" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import { ShapiroWilkW } from \"https://l12.xyz/x/shortcuts/raw/shapiro.ts\";\n", - "\n", - "ShapiroWilkW(df.price.sort());" - ] - }, - { - "cell_type": "code", - "execution_count": 21, "id": "8f8c6044-d3a1-4f8e-89ba-365fea3fbd8b", "metadata": { "editable": true, @@ -2859,7 +2832,7 @@ " " ] }, - "execution_count": 21, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -2870,33 +2843,6 @@ "await display(threeChart(log2df.toRecords(), \"price\"));" ] }, - { - "cell_type": "code", - "execution_count": 22, - "id": "9bdeba3c-2eac-44bc-8601-8241c5bfe502", - "metadata": { - "editable": true, - "slideshow": { - "slide_type": "" - }, - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "\u001b[33m0.948674582545282\u001b[39m" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ShapiroWilkW(log2df.price.sort());" - ] - }, { "cell_type": "markdown", "id": "09a9d9c0-7fb6-4353-91f8-4899520b36d7", @@ -2913,7 +2859,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 21, "id": "79f05578-b839-4278-afb1-0123c1d32d17", "metadata": { "editable": true, @@ -3337,7 +3283,7 @@ "
brand_categorybrandfueltypeaspirationdoornumbercarbodydrivewheelenginelocationwheelbasecarlengthcarwidthcarheightcurbweightenginetypecylindernumberenginesizefuelsystemboreratiostrokecompressionratiohorsepowerpeakrpmcitympghighwaympgprice
Mid_Rangealfa-romerogasstdtwoconvertiblerwdfront88.6168.864.148.82548dohcfour130mpfi3.472.6891115000212713495
Mid_Rangealfa-romerogasstdtwoconvertiblerwdfront88.6168.864.148.82548dohcfour130mpfi3.472.6891115000212716500
Mid_Rangealfa-romerogasstdtwohatchbackrwdfront94.5171.265.552.42823ohcvsix152mpfi2.683.4791545000192616500
Mid_Rangeaudigasstdfoursedanfwdfront99.8176.666.254.32337ohcfour109mpfi3.193.4101025500243013950
Mid_Rangeaudigasstdfoursedan4wdfront99.4176.666.454.32824ohcfive136mpfi3.193.481155500182217450
" ] }, - "execution_count": 23, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -3361,7 +3307,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "id": "e2df0c3f-dbc0-4820-b807-373ce3787645", "metadata": { "editable": true, @@ -3695,7 +3641,7 @@ "
brand_categoryfueltypeaspirationdoornumberdrivewheelwheelbasecarlengthcarwidthcarheightcurbweightenginetypecylindernumberenginesizefuelsystemboreratiohorsepowercitympghighwaympgprice
Mid_Rangegasstdtworwd88.6168.864.148.82548dohcfour130mpfi3.47111212713495
Mid_Rangegasstdtworwd88.6168.864.148.82548dohcfour130mpfi3.47111212716500
Mid_Rangegasstdtworwd94.5171.265.552.42823ohcvsix152mpfi2.68154192616500
Mid_Rangegasstdfourfwd99.8176.666.254.32337ohcfour109mpfi3.19102243013950
Mid_Rangegasstdfour4wd99.4176.666.454.32824ohcfive136mpfi3.19115182217450
" ] }, - "execution_count": 24, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -3729,7 +3675,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 23, "id": "9bc95dd8-0240-45c6-a0f9-21b1873ae04b", "metadata": { "editable": true, @@ -4453,7 +4399,7 @@ "
brand_category_Budgetbrand_category_Luxurybrand_category_Mid_Rangefueltype_dieselfueltype_gasaspiration_stdaspiration_turbodoornumber_fourdoornumber_twodrivewheel_4wddrivewheel_fwddrivewheel_rwdwheelbasecarlengthcarwidthcarheightcurbweightenginetype_dohcenginetype_dohcvenginetype_lenginetype_ohcenginetype_ohcfenginetype_ohcvenginetype_rotorcylindernumber_eightcylindernumber_fivecylindernumber_fourcylindernumber_sixcylindernumber_threecylindernumber_twelvecylindernumber_twoenginesizefuelsystem_1bblfuelsystem_2bblfuelsystem_4bblfuelsystem_idifuelsystem_mfifuelsystem_mpfifuelsystem_spdifuelsystem_spfiboreratiohorsepowercitympghighwaympgprice
00101100100188.6168.864.148.8254810000000010000130000001003.47111212713495
00101100100188.6168.864.148.8254810000000010000130000001003.47111212716500
00101100100194.5171.265.552.4282300000100001000152000001002.68154192616500
00101101001099.8176.666.254.3233700010000010000109000001003.19102243013950
00101101010099.4176.666.454.3282400010000100000136000001003.19115182217450
" ] }, - "execution_count": 25, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -4467,7 +4413,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 24, "id": "aff549ce-6736-43c7-8d40-b09b9ca7fa59", "metadata": { "editable": true, @@ -4509,7 +4455,7 @@ "name": "typescript", "nbconvert_exporter": "script", "pygments_lexer": "typescript", - "version": "5.5.2" + "version": "5.6.2" } }, "nbformat": 4, diff --git a/package.json b/package.json index 10d41a3..769735d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,10 @@ "typescript": "^5.0.0" }, "dependencies": { - "nodejs-polars": "^0.15.0" + "@observablehq/plot": "^0.6.16", + "linkedom": "^0.18.5", + "nodejs-polars": "^0.15.0", + "vega": "^5.30.0", + "vega-lite": "^5.21.0" } } diff --git a/shapiro.ts b/shapiro.ts deleted file mode 100644 index 3d0b761..0000000 --- a/shapiro.ts +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Ported from http://svn.r-project.org/R/trunk/src/nmath/qnorm.c - * - * Mathlib : A C Library of Special Functions - * Copyright (C) 1998 Ross Ihaka - * Copyright (C) 2000--2005 The R Core Team - * based on AS 111 (C) 1977 Royal Statistical Society - * and on AS 241 (C) 1988 Royal Statistical Society - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, a copy is available at - * http://www.r-project.org/Licenses/ - */ - -// The inverse of cdf. -function normalQuantile(p, mu, sigma) { - var p, q, r, val; - if (sigma < 0) { - return -1; - } - if (sigma == 0) { - return mu; - } - - q = p - 0.5; - - if (0.075 <= p && p <= 0.925) { - r = 0.180625 - q * q; - val = q * - (((((((r * 2509.0809287301226727 + 33430.575583588128105) * r + - 67265.770927008700853) * r + - 45921.953931549871457) * r + 13731.693765509461125) * - r + 1971.5909503065514427) * r + 133.14166789178437745) * r + - 3.387132872796366608) / - (((((((r * 5226.495278852854561 + 28729.085735721942674) * r + - 39307.89580009271061) * r + - 21213.794301586595867) * r + 5394.1960214247511077) * - r + 687.1870074920579083) * r + 42.313330701600911252) * r + - 1); - } else { - /* closer than 0.075 from {0,1} boundary */ - /* r = min(p, 1-p) < 0.075 */ - if (q > 0) { - r = 1 - p; - } else { - r = p; /* = R_DT_Iv(p) ^= p */ - } - - r = Math.sqrt( - -Math.log(r), - ); /* r = sqrt(-log(r)) <==> min(p, 1-p) = exp( - r^2 ) */ - - if (r <= 5.) { - /* <==> min(p,1-p) >= exp(-25) ~= 1.3888e-11 */ - r += -1.6; - val = - (((((((r * 7.7454501427834140764e-4 + 0.0227238449892691845833) * r + - .24178072517745061177) * r + - 1.27045825245236838258) * r + - 3.64784832476320460504) * r + 5.7694972214606914055) * r + - 4.6303378461565452959) * r + 1.42343711074968357734) / - (((((((r * 1.05075007164441684324e-9 + 5.475938084995344946e-4) * r + - .0151986665636164571966) * r + - 0.14810397642748007459) * r + - 0.68976733498510000455) * r + 1.6763848301838038494) * r + - 2.05319162663775882187) * r + 1); - } else { - /* very close to 0 or 1 */ - r += -5.; - val = - (((((((r * 2.01033439929228813265e-7 + 2.71155556874348757815e-5) * r + - 0.0012426609473880784386) * r + - 0.026532189526576123093) * r + - .29656057182850489123) * r + 1.7848265399172913358) * r + - 5.4637849111641143699) * r + - 6.6579046435011037772) / - (((((((r * 2.04426310338993978564e-15 + 1.4215117583164458887e-7) * r + - 1.8463183175100546818e-5) * r + - 7.868691311456132591e-4) * r + - .0148753612908506148525) * r + - .13692988092273580531) * r + .59983220655588793769) * r + 1.); - } - - if (q < 0.0) { - val = -val; - } - /* return (q >= 0.)? r : -r ;*/ - } - return mu + sigma * val; -} - -/* - * Ported from http://svn.r-project.org/R/trunk/src/library/stats/src/swilk.c - * - * R : A Computer Language for Statistical Data Analysis - * Copyright (C) 2000-12 The R Core Team. - * - * Based on Applied Statistics algorithms AS181, R94 - * (C) Royal Statistical Society 1982, 1995 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, a copy is available at - * http://www.r-project.org/Licenses/ - */ - -function sign(x) { - if (x == 0) { - return 0; - } - return x > 0 ? 1 : -1; -} - -export function ShapiroWilkW(x) { - function poly(cc, nord, x) { - /* Algorithm AS 181.2 Appl. Statist. (1982) Vol. 31, No. 2 - Calculates the algebraic polynomial of order nord-1 with array of coefficients cc. - Zero order coefficient is cc(1) = cc[0] */ - var p; - var ret_val; - - ret_val = cc[0]; - if (nord > 1) { - p = x * cc[nord - 1]; - for (j = nord - 2; j > 0; j--) { - p = (p + cc[j]) * x; - } - ret_val += p; - } - return ret_val; - } - x = x.sort(function (a, b) { - return a - b; - }); - var n = x.length; - if (n < 3) { - return undefined; - } - var nn2 = Math.floor(n / 2); - var a = new Array(Math.floor(nn2) + 1); /* 1-based */ - - /* ALGORITHM AS R94 APPL. STATIST. (1995) vol.44, no.4, 547-551. - - Calculates the Shapiro-Wilk W test and its significance level - */ - var small = 1e-19; - - /* polynomial coefficients */ - var g = [-2.273, 0.459]; - var c1 = [0, 0.221157, -0.147981, -2.07119, 4.434685, -2.706056]; - var c2 = [0, 0.042981, -0.293762, -1.752461, 5.682633, -3.582633]; - var c3 = [0.544, -0.39978, 0.025054, -6.714e-4]; - var c4 = [1.3822, -0.77857, 0.062767, -0.0020322]; - var c5 = [-1.5861, -0.31082, -0.083751, 0.0038915]; - var c6 = [-0.4803, -0.082676, 0.0030302]; - - /* Local variables */ - var i, j, i1; - - var ssassx, summ2, ssumm2, gamma, range; - var a1, a2, an, m, s, sa, xi, sx, xx, y, w1; - var fac, asa, an25, ssa, sax, rsn, ssx, xsx; - - var pw = 1; - an = n; - - if (n == 3) { - a[1] = 0.70710678; /* = sqrt(1/2) */ - } else { - an25 = an + 0.25; - summ2 = 0.0; - for (i = 1; i <= nn2; i++) { - a[i] = normalQuantile((i - 0.375) / an25, 0, 1); // p(X <= x), - var r__1 = a[i]; - summ2 += r__1 * r__1; - } - summ2 *= 2; - ssumm2 = Math.sqrt(summ2); - rsn = 1 / Math.sqrt(an); - a1 = poly(c1, 6, rsn) - a[1] / ssumm2; - - /* Normalize a[] */ - if (n > 5) { - i1 = 3; - a2 = -a[2] / ssumm2 + poly(c2, 6, rsn); - fac = Math.sqrt( - (summ2 - 2 * (a[1] * a[1]) - 2 * (a[2] * a[2])) / - (1 - 2 * (a1 * a1) - 2 * (a2 * a2)), - ); - a[2] = a2; - } else { - i1 = 2; - fac = Math.sqrt((summ2 - 2 * (a[1] * a[1])) / (1 - 2 * (a1 * a1))); - } - a[1] = a1; - for (i = i1; i <= nn2; i++) { - a[i] /= -fac; - } - } - - /* Check for zero range */ - - range = x[n - 1] - x[0]; - if (range < small) { - console.log("range is too small!"); - return undefined; - } - - /* Check for correct sort order on range - scaled X */ - - xx = x[0] / range; - sx = xx; - sa = -a[1]; - for (i = 1, j = n - 1; i < n; j--) { - xi = x[i] / range; - if (xx - xi > small) { - console.log("xx - xi is too big.", xx - xi); - return undefined; - } - sx += xi; - i++; - if (i != j) { - sa += sign(i - j) * a[Math.min(i, j)]; - } - xx = xi; - } - if (n > 5000) { - console.log("n is too big!"); - return undefined; - } - - /* Calculate W statistic as squared correlation - between data and coefficients */ - - sa /= n; - sx /= n; - ssa = ssx = sax = 0.; - for (i = 0, j = n - 1; i < n; i++, j--) { - if (i != j) { - asa = sign(i - j) * a[1 + Math.min(i, j)] - sa; - } else { - asa = -sa; - } - xsx = x[i] / range - sx; - ssa += asa * asa; - ssx += xsx * xsx; - sax += asa * xsx; - } - - /* W1 equals (1-W) calculated to avoid excessive rounding error - for W very near 1 (a potential problem in very large samples) */ - - ssassx = Math.sqrt(ssa * ssx); - w1 = (ssassx - sax) * (ssassx + sax) / (ssa * ssx); - var w = 1 - w1; - - /* Calculate significance level for W */ - - if (n == 3) { - /* exact P value : */ - var pi6 = 1.90985931710274; /* = 6/pi */ - var stqr = 1.04719755119660; /* = asin(sqrt(3/4)) */ - pw = pi6 * (Math.asin(Math.sqrt(w)) - stqr); - if (pw < 0.) { - pw = 0; - } - return w; - } - y = Math.log(w1); - xx = Math.log(an); - if (n <= 11) { - gamma = poly(g, 2, an); - if (y >= gamma) { - pw = 1e-99; /* an "obvious" value, was 'small' which was 1e-19f */ - return w; - } - y = -Math.log(gamma - y); - m = poly(c3, 4, an); - s = Math.exp(poly(c4, 4, an)); - } else { - /* n >= 12 */ - m = poly(c5, 4, xx); - s = Math.exp(poly(c6, 3, xx)); - } - - return w; -}