commit
message
[RSQ] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 02:22:47
stats
8 file(s) changed,
109 insertions(+),
6 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Statistical.js
src/Formulas/AllFormulas.ts
src/Formulas/Statistical.ts
tests/Formulas/StatisticalTest.ts
tests/SheetFormulaTest.ts
1diff --git a/DOCS.md b/DOCS.md
2index 5cbe883..0e497a6 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -2029,6 +2029,16 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### RSQ
11+
12+```
13+ Returns the square of the Pearson correlation coefficient based on the given values.
14+@param rangeY - An array or range of data points.
15+@param rangeX - An array or range of data points.
16+@returns {number}
17+@constructor
18+```
19 ## Text
20
21
22diff --git a/TODO.md b/TODO.md
23index de1297d..d26cc78 100644
24--- a/TODO.md
25+++ b/TODO.md
26@@ -70,7 +70,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
27 * RANK
28 * RANK.AVG
29 * RANK.EQ
30-* RSQ
31 * SKEW
32 * STEYX
33 * T.INV
34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
35index d50175b..a8f463a 100644
36--- a/dist/Formulas/AllFormulas.js
37+++ b/dist/Formulas/AllFormulas.js
38@@ -188,6 +188,7 @@ exports.VARP = Statistical_1.VARP;
39 exports.VARA = Statistical_1.VARA;
40 exports.VAR = Statistical_1.VAR;
41 exports.PERMUT = Statistical_1.PERMUT;
42+exports.RSQ = Statistical_1.RSQ;
43 var Text_1 = require("./Text");
44 exports.ARABIC = Text_1.ARABIC;
45 exports.CHAR = Text_1.CHAR;
46diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
47index b27389b..49b5474 100644
48--- a/dist/Formulas/Statistical.js
49+++ b/dist/Formulas/Statistical.js
50@@ -1691,3 +1691,37 @@ var PERMUT = function (total, objects) {
51 return _fact(total) / _fact(total - objects);
52 };
53 exports.PERMUT = PERMUT;
54+/**
55+ * Returns the square of the Pearson correlation coefficient based on the given values.
56+ * @param rangeY - An array or range of data points.
57+ * @param rangeX - An array or range of data points.
58+ * @returns {number}
59+ * @constructor
60+ */
61+var RSQ = function (rangeY, rangeX) {
62+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "RSQ");
63+ if (!Array.isArray(rangeY)) {
64+ rangeY = [rangeY];
65+ }
66+ if (!Array.isArray(rangeX)) {
67+ rangeX = [rangeX];
68+ }
69+ var dataX = Filter_1.Filter.flattenAndThrow(rangeX).filter(function (value) {
70+ return typeof value !== "string";
71+ }).map(function (value) {
72+ return TypeConverter_1.TypeConverter.valueToNumber(value);
73+ });
74+ var dataY = Filter_1.Filter.flattenAndThrow(rangeY).filter(function (value) {
75+ return typeof value !== "string";
76+ }).map(function (value) {
77+ return TypeConverter_1.TypeConverter.valueToNumber(value);
78+ });
79+ if (dataX.length !== dataY.length) {
80+ throw new Errors_1.NAError("SLOPE has mismatched argument count " + dataX.length + " vs " + dataY.length + ".");
81+ }
82+ if (dataY.length === 1 && dataX.length === 1) {
83+ throw new Errors_1.DivZeroError("Evaluation of function RSQ caused a divide by zero error.");
84+ }
85+ return Math.pow(PEARSON(dataX, dataY), 2);
86+};
87+exports.RSQ = RSQ;
88diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
89index a56c3ab..76c0ffb 100644
90--- a/src/Formulas/AllFormulas.ts
91+++ b/src/Formulas/AllFormulas.ts
92@@ -193,7 +193,8 @@ import {
93 VARP,
94 VARA,
95 VAR,
96- PERMUT
97+ PERMUT,
98+ RSQ
99 } from "./Statistical";
100 import {
101 ARABIC,
102@@ -461,5 +462,6 @@ export {
103 VARP,
104 VARA,
105 VAR,
106- PERMUT
107+ PERMUT,
108+ RSQ
109 }
110\ No newline at end of file
111diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
112index 871a693..9bf7558 100644
113--- a/src/Formulas/Statistical.ts
114+++ b/src/Formulas/Statistical.ts
115@@ -1674,6 +1674,41 @@ var PERMUT = function (total, objects) {
116 };
117
118
119+/**
120+ * Returns the square of the Pearson correlation coefficient based on the given values.
121+ * @param rangeY - An array or range of data points.
122+ * @param rangeX - An array or range of data points.
123+ * @returns {number}
124+ * @constructor
125+ */
126+var RSQ = function (rangeY, rangeX) {
127+ ArgsChecker.checkLength(arguments, 2, "RSQ");
128+ if (!Array.isArray(rangeY)) {
129+ rangeY = [rangeY];
130+ }
131+ if (!Array.isArray(rangeX)) {
132+ rangeX = [rangeX];
133+ }
134+ var dataX = Filter.flattenAndThrow(rangeX).filter(function (value) {
135+ return typeof value !== "string";
136+ }).map(function (value) {
137+ return TypeConverter.valueToNumber(value);
138+ });
139+ var dataY = Filter.flattenAndThrow(rangeY).filter(function (value) {
140+ return typeof value !== "string";
141+ }).map(function (value) {
142+ return TypeConverter.valueToNumber(value);
143+ });
144+ if (dataX.length !== dataY.length) {
145+ throw new NAError("SLOPE has mismatched argument count " + dataX.length + " vs " + dataY.length + ".");
146+ }
147+ if (dataY.length === 1 && dataX.length === 1) {
148+ throw new DivZeroError("Evaluation of function RSQ caused a divide by zero error.");
149+ }
150+ return Math.pow(PEARSON(dataX, dataY), 2);
151+};
152+
153+
154 export {
155 AVERAGE,
156 AVERAGEA,
157@@ -1726,5 +1761,6 @@ export {
158 VARP,
159 VARA,
160 VAR,
161- PERMUT
162+ PERMUT,
163+ RSQ
164 }
165\ No newline at end of file
166diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
167index 04e2798..ea666a3 100644
168--- a/tests/Formulas/StatisticalTest.ts
169+++ b/tests/Formulas/StatisticalTest.ts
170@@ -50,7 +50,8 @@ import {
171 VARP,
172 VARA,
173 VAR,
174- PERMUT
175+ PERMUT,
176+ RSQ
177 } from "../../src/Formulas/Statistical";
178 import * as ERRORS from "../../src/Errors";
179 import {
180@@ -1040,4 +1041,21 @@ test("PERMUT", function() {
181 catchAndAssertEquals(function() {
182 PERMUT.apply(this, [1, 2, 3]);
183 }, ERRORS.NA_ERROR);
184+});
185+
186+test("RSQ", function() {
187+ assertEquals(RSQ([10, 22, 4], [1, 3, 7]), 0.2500000000000001);
188+ assertEquals(RSQ([10, 22], [1, 3]), 1);
189+ catchAndAssertEquals(function() {
190+ RSQ([1, 2, 3], [1, 2]);
191+ }, ERRORS.NA_ERROR);
192+ catchAndAssertEquals(function() {
193+ RSQ(1, 1);
194+ }, ERRORS.DIV_ZERO_ERROR);
195+ catchAndAssertEquals(function() {
196+ RSQ.apply(this, [[1], [1], [1]]);
197+ }, ERRORS.NA_ERROR);
198+ catchAndAssertEquals(function() {
199+ RSQ.apply(this, [[1]]);
200+ }, ERRORS.NA_ERROR);
201 });
202\ No newline at end of file
203diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
204index 4f2431f..f6d8518 100644
205--- a/tests/SheetFormulaTest.ts
206+++ b/tests/SheetFormulaTest.ts
207@@ -910,6 +910,9 @@ test("Sheet PERMUT", function(){
208 assertFormulaEquals('=PERMUT(4, 2)', 12);
209 });
210
211+test("Sheet RSQ", function(){
212+ assertFormulaEquals('=RSQ([10, 22, 4], [1, 3, 7])', 0.2500000000000001);
213+});
214
215 test("Sheet *", function(){
216 assertFormulaEquals('= 10 * 10', 100);