qreg() quadratic regression with r
|
12-31-2017, 12:50 AM
(This post was last modified: 12-31-2017 01:21 AM by TheKaneB.)
Post: #1
|
|||
|
|||
qreg() quadratic regression with r
Hi,
I noticed that the HP Prime Statistics 2Var App has got the very useful quadratic regression function, but as I understand it doesn't calculate the correlation coefficient "r" (it just gives NaN). So, I thought it could be a fun exercise to reimplement the quadratic regression from scratch and including the r calculation as well. Code:
example usage Code:
The returned list gives the coefficient for a parabola with the formula f(x) = A + Bx + Cx^2. The order of the coefficient is chosen to be compatible with the built-in function "polynomial_regression". The fourth element in the list L2(4) is the correlation coefficient. You can compare the built-in polynomial_regression(L1, 2) and qreg(L1) and compare the results. From a few, totally non scientific, test that I made the errors seem to be around 1e-10 on average. Sources: Formula used http://keisan.casio.com/exec/system/14059932254941 I believe that the coefficient are correct at least to 8 significant digits, but I'm not 100% sure. Here it is another implementation I did in Python, to check if the algorithm was correct (I find it easier to do a prototype in Python and then translate it to HPPPL later). Code: import math The python version have embedded test data and prints the results. As I said, it's just a quick prototype, not intended for real use, so I didn't bother adding proper argument parsing from the command line. I hope you find it useful, I plan on translating it to the HP 35S later on, if I have time. EDIT: about the example data in the python code. It's a list of measured diameters (in millimeters) and sizes (in GB) of the reflective layer of several DVD-R that I've had around my house. As all of you may know, you can easily see the occupied area of a DVD-R, so I thought I would calculate a formula to get an approximation of the space left on a disk just by measuring the diameter of the written (lighter) area, and since the area grows with the square of the diameter, it seemed appropriate to use a quadratic regression fitting for this task. Software Failure: Guru Meditation -- Antonio IU2KIY |
|||
12-31-2017, 01:22 AM
Post: #2
|
|||
|
|||
RE: qreg() quadratic regression with r
@Mods: I think I've used the wrong subforum, I'm sorry
Software Failure: Guru Meditation -- Antonio IU2KIY |
|||
12-31-2017, 02:22 AM
(This post was last modified: 12-31-2017 03:48 AM by Tim Wessman.)
Post: #3
|
|||
|
|||
RE: qreg() quadratic regression with r
Problem is, it isn't really a correlation coefficient but a different measure of error that is not defined for polynomial fits. Thats why I didn't implement anything there because all the literature I studied basically said to do so was wrong...
R^2 is defined, and hence that one is calculated and presented. TW Although I work for HP, the views and opinions I post here are my own. |
|||
12-31-2017, 12:40 PM
Post: #4
|
|||
|
|||
RE: qreg() quadratic regression with r
I didn't know that, thanks for clarifying!
Anyway, it was a fun exercise for me to learn HPPPL, I must say it's way more similar to modern production languages than the simple calculator stuff we've had in the past. Software Failure: Guru Meditation -- Antonio IU2KIY |
|||
12-31-2017, 12:50 PM
Post: #5
|
|||
|
|||
RE: qreg() quadratic regression with r
(12-31-2017 02:22 AM)Tim Wessman Wrote: R^2 is defined, and hence that one is calculated and presented. How can I access this value outside the statistics App? Does the function polynomial_regression() calculate and store that value somewhere, or should I call another function after the fitting? Software Failure: Guru Meditation -- Antonio IU2KIY |
|||
12-31-2017, 01:28 PM
Post: #6
|
|||
|
|||
RE: qreg() quadratic regression with r
(12-31-2017 12:50 PM)TheKaneB Wrote: How can I access this value outside the statistics App? Does the function polynomial_regression() calculate and store that value somewhere, or should I call another function after the fitting? try Vars -> App -> Statistics 2var -> Results -> Corr and CoeffDet HNY! ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
12-31-2017, 04:22 PM
(This post was last modified: 12-31-2017 04:25 PM by Tim Wessman.)
Post: #7
|
|||
|
|||
RE: qreg() quadratic regression with r
That CAS function does not calculate that to my knowledge - there may be some other way or command, but I'm not aware of it. Only by using the app functionality with the variables containing all the results be calculated and stored.
TW Although I work for HP, the views and opinions I post here are my own. |
|||
01-01-2018, 12:14 AM
(This post was last modified: 01-01-2018 12:15 AM by TheKaneB.)
Post: #8
|
|||
|
|||
RE: qreg() quadratic regression with r
(12-31-2017 01:28 PM)salvomic Wrote:(12-31-2017 12:50 PM)TheKaneB Wrote: How can I access this value outside the statistics App? Does the function polynomial_regression() calculate and store that value somewhere, or should I call another function after the fitting? These are the values from the previous calculation inside the Statistics App, they aren't updated once I call manually "polynomial_regression" with a different data set. Tim Wessman Wrote:That CAS function does not calculate that to my knowledge - there may be some other way or command, but I'm not aware of it. Only by using the app functionality with the variables containing all the results be calculated and stored.All right, thanks. Software Failure: Guru Meditation -- Antonio IU2KIY |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)