Calculates the linear regression coefficient between two variables on the given dimensions.
function regCoef_n ( x : numeric, y : numeric, dims_x [*] : integer, dims_y [*] : integer ) return_val : float or double
An array of any dimensionality. Missing values should be indicated by x@_FillValue. If x@_FillValue is not set, then the NCL default (appropriate to the type of x) will be assumed.y
An array of any dimensionality. The last dimension of y must be the same as the last dimension of x. Missing values should be indicated by y@_FillValue. If y@_FillValue is not set, then the NCL default (appropriate to the type of y) will be assumed.dims_x
A scalar integer indicating which dimension of x to do the calculation on. Dimension numbering starts at 0.dims_y
A scalar integer indicating which dimension of y to do the calculation on. Dimension numbering starts at 0.
If either x or y are of type double, then the return array is returned as double. Otherwise, the returned variable is returned as type float. The dimensionality is a bit more complicated; see the description and examples below.
regCoef_n computes the linear regression coefficient via least-squares across the given dimensions. It is designed to work with multi-dimensional x and y arrays. If the regression information for a single best fit line for 1-dimensional x and y data is desired, then regline is the appropriate choice. Missing data are allowed.
The regCoef_n function returns the following attributes: yintercept (y-intercept), tval (t-statistic), rstd (standard error of the estimated regression coefficient) and nptxy (number of elements used). These will be returned as scalars for one-dimensional x/y; otherwise, as one-dimensional attributes of the return variable (call it rc). The type of tval and rstd will be the same as rc while nptxy will be of type integer. These attributes may be used for statistical testing (see examples).
The dimensions of rc are illustrated as follows:
x(N), y(N) rc, tval, mptxy are scalars x(N), y(K,M,N) rc, tval, mptxy are arrays of size (K,M) x(I,N), y(K,M,N) rc, tval, mptxy are arrays of size (I,K,M) x(J,I,N), y(L,K,M,N) rc, tval, mptxy are arrays of size (J,I,L,K,M)There's a special case when all dimensions of x and y are identical:
x(J,I,N), y(J,I,N) rc, tval, mptxy are arrays of size (J,I)Note on the units of the returned regression coefficient(s): if x has units of, say, degrees Kelvin (K), and y has units of, say, meters (M), then the units of the regression coefficient are M/K. The function does not standardize x (or y) prior to calculating the regression coefficient. If this is desired, then it is the user's responsibility do so. The NCL function dim_standardize can be used.
Example 1: Here use simple one-dimensional arrays.
begin x = (/ 1190.,1455.,1550.,1730.,1745.,1770. \ , 1900.,1920.,1960.,2295.,2335.,2490. \ , 2720.,2710.,2530.,2900.,2760.,3010. /) y = (/ 1115.,1425.,1515.,1795.,1715.,1710. \ , 1830.,1920.,1970.,2300.,2280.,2520. \ , 2630.,2740.,2390.,2800.,2630.,2970. /) rc = regCoef_n(x,y, 0, 0) print(rc)The (edited) output is:
Variable: rc Type: float Total Size: 4 bytes 1 values Number of Dimensions: 1 Dimensions and sizes:  Coordinates: Number Of Attributes: 5 _FillValue : 9.96921e+36 nptxy : 18 rstd : 0.02515461 yintercept : 15.35228 tval : 38.74286 (0) 0.9745615
Example 2: Assume x is a one dimensional array (1D) array of size ntim and type float. Assume y is a three-dimensional array (3D) array with named dimensions, "time", "lat" and "lon" with the following ordering y(time,lat,lon).
; 0 0 1 2 (dimension number) ; x[ntim], y(ntim,nlat,mlon) ) rc = regCoef_n(x, y, 0, 0) ; rc(nlat,mlon)If y has coordinate variables these may readily be assigned via NCL syntax:
rc!0 = "lat" ; name dimensions rc!1 = "lon" rc&lat = y&lat ; assign coordinate values to named dimensions rc&lon = y&lon
Alternatively, more compactly
copy_VarCoords(y(0,:,:), rc) ; trick to copy coord information