MATLAB: Computing rsquared value for data

biological modeldiscrete timersquared

Hi,
I am trying to compute the rsquared for a data set I have; I need two, for each category: juvenile and adult organisms. This is what I have for the RSS and rsquared values:
%Compute RSS y = sum(sum(E1.^2)) + sum(sum(E2.^2))
RSSJ = sum(sum(E1.^2))
RSSA = sum(sum(E2.^2))
%Compute R-squared
residualJ = sqrt(dataJ(2:21,:)) – mean(sqrt(dataJ(2:21,:)));
squareresidualJ = residualJ.^2;
denominator = sum(sum(squareresidualJ));
rsquared = 1 – (RSSJ/denominator)
However, it says that matrix dimensions don't agree for the line containing residualJ. Anyone know what I'm doing wrong?

Best Answer

The mean function will (by default) take the mean of the columns, so the result will be a vector. If you want ‘residualJ’ to be a matrix, use the bsxfun function with the @minus function.
I don’t have your data, so I can’t write specific code, but see if:
residualJ = bsxfun(@minus, sqrt(dataJ(2:21,:)), mean(sqrt(dataJ(2:21,:))));
works. This is obviously UNTESTED CODE.