% function rma.m  Feb. 2005 L Braile
% function for the reduced major axis method of fitting
% a least squares straight line
function [a,b,sa,sb,r,p] = rma(x,y,npts)
[R,p] = corrcoef(x,y) % Calculate and print correlation matrix
% a p-value for testing the hypothesis of no correlation.  
% The  p-value is the probability of getting a correlation as 
% large as the observed value by random chance, when the true 
% correlation is zero.
r = R(1,2) % correlation coefficient
% Calculate a and b (y = a + bx) by rma method
sx = std(x);
sy = std(y);
b = sy/sx;
si = r/abs(r); % find sign of the correlation coefficient
b = si*b;
xb = mean(x);
a = mean(y) - b*xb;
% Calculate sigma a and sigma b
sa = sy*sqrt(((1-r*r)/npts)*(1 + (xb*xb/sx*sx)));
sb = b*sqrt(((1-r*r)/npts));