# [ncl-talk] Calculating a regression line & slope

Lauren Jean Vargo lvargo at unm.edu
Thu Aug 7 12:26:10 MDT 2014

```Hello,

I have a code that creates a scatter plot of 2 variables (ablation and surface altitude, both 2d arrays).

Now I’m trying to 1) include a regression line on the plot, and 2) calculate the slope of that line (the regression coefficient).
I’ve been trying to use regCoef since the arrays are both 2d, but the answers that are being printed are not reasonable.

The files are both very large, but I can upload them if needed.

I’m running ncl version 6.1.2, and the system is Darwin Kernel Version 13.3.0

Here is the script:

begin

; Read in temperature data & calculate PDD sum
tasP = b->tas(0:364,:,:)

TIMEP = cd_calendar(tasP&time,0)   ; TIMEP(ntim,6)

tasCp = tasP - 273.15  ; Convert K to C
tasCp = where(tasCp.gt.0.0, tasCp, 0.0)
pddP  = dim_sum_n_Wrap(tasCp, 0)       ; (lat,lon)
pddP at long_name = "degree days for year="+toint(TIMEP(0,0))
pddP at units = "degC"

; Calculate ablation (PDD * 5)
Ab = pddP * 5
printVarSummary (Ab)

Orog = c->orog(:,:)

; Calculate regression line
rc    = regCoef (Orog,Ab)
print (rc)

; Plot
res                                   = True                     ; plot mods desired
res at gsnMaximize           = True                    ; maximize plot in frame
res at xyMarkLineMode     = "Markers"           ; choose which have markers
res at xyMarker                  = 1                        ; choose type of marker
res at xyMarkerSizeF         = 0.005                 ; Marker size (default 0.01)

res at tiMainString        = "Ablation Gradient"  ; title

plot = gsn_csm_xy(wks,Orog(:,:),Ab(:,:),res)

end

Any help would be great!
Thank you,

Lauren Vargo

```