# [ncl-talk] Regarding RegLine (coefficient of determination)

Barry Lynn barry.h.lynn at gmail.com
Thu Sep 6 11:44:51 MDT 2018

```Hello:

I just wanted to correct something that's in the record.

I may have copied something incorrectly, but when I used the equations for
calculating r sent previously in this e-mail chain, I did not get the right
answer.  Perhaps that is the reason or there is something else that's
changed that I am unaware of.

I found instead what I've copied below from

https://www.mathsisfun.com/data/correlation.html

However, you can find in the ncl library a function to calculate r as well
(and I think it may be part of the new regline).

https://www.ncl.ucar.edu/Document/Functions/Built-in/escorc.shtml

Here's the calculation of r following the page above, translating into ncl
language.

ave_x= avg(x)

ave_y= avg(y)

a =  x-ave_x

b =  y-ave_y

ab = a*b

a2 = a*a

b2 = b*b

sum_ab = sum(ab)

sum_a2 = sum(a2)

sum_b2 = sum(b2)

r= sum_ab/(sqrt(sum_a2*sum_b2))

print("r = " + r)

On Tue, Oct 3, 2017 at 5:10 PM Barry Lynn <barry.h.lynn at gmail.com> wrote:

> Hi:
>
> I didn't see that this function has been updated to calculate the
> coefficient of determination (r).
>
> I did see this e-mail and one after (e.g.:
> https://www.ncl.ucar.edu/Support/talk_archives/2009/2202.html
>
> but this (below) may be more useful for users wanting the simplest
> approach to calculating statistics for a line.
>
> begin
>
> x = (/1,2,3,4,5,6,7,8,9,10/)
>
> y = (/0.6,1.6,1.2,2.2,2.3,3.1,2.8,4.1,4.7,3.3/)
>
>
> rc = regline(x,y)  ; function as is.
>
> print("rc = " + rc)
>
> printVarSummary(rc)
>
>     sum_xy = sum(x*y)
>
>     sum_x = sum(x)
>
>     sum_x2 = sum(x^2)
>
>     sum_y = sum(y)
>
>     sum_y2 = sum(y^2)
>
>     n = dimsizes(x)
>
>     r_n = n*sum_xy - sum_x*sum_y
>
>     r_d =(n*sum_x2  - (sum_x)^2)*(n*sum_y2  - (sum_y)^2)
>
>     r = r_n/sqrt(r_d)
>
>     print(r)
>
>     r2 = r^2
>
>     print(r2)
>
> end
>
> Answer r = 0.9075 and r2 = 0.82364.
>
>
>
> --
> Barry H. Lynn, Ph.D
> Senior Lecturer,
> The Institute of the Earth Science,
> The Hebrew University of Jerusalem,
> Givat Ram, Jerusalem 91904, Israel
> Tel: 972 547 231 170
> Fax: (972)-25662581
>
> C.E.O, Weather It Is, LTD
> Weather and Climate Focus
> http://weather-it-is.com
> Jerusalem, Israel
> Local: 02 930 9525
> Cell: 054 7 231 170
> Int-IS: x972 2 930 9525
> US 914 432 3108
>

--
Barry H. Lynn, Ph.D
Senior Associate Scientist, Lecturer,
The Institute of the Earth Science,
The Hebrew University of Jerusalem,
Givat Ram, Jerusalem 91904, Israel
Tel: 972 547 231 170
Fax: (972)-25662581

C.E.O, Weather It Is, LTD
Weather and Climate Focus
http://weather-it-is.com
Jerusalem, Israel
Local: 02 930 9525
Cell: 054 7 231 170
Int-IS: x972 2 930 9525
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180906/d84d7f94/attachment.html>
```