Finding R squared in a loop
Show older comments
I am trying to extract out the various R squared values for a data set, with the actual data and the modelled data. However, I am having trouble finding R squared values with the various period value. I am getting negative R squared values, which is definitely not right. Specifically, I am not sure how to get the matrix right.
I get this warning "Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN. "
I have attached my code and a sample file.
for period = 0:0.5:1
% Assigning the data provided, sealevel to "d"
d = sealevel{1};
t = timesea{1};
% Finding the size of the data d
[Rdata , Cdata] = size(d);
% Creating figure for data plot
figure
% Plotting the observed data d against time
plot(t,d,'gX')
% holding the current plot and all axis properties
hold on
% Creating third variable for equation
thirdco = cos((2*pi/period) * t);
% Creating fourth variable for equation
fourthco = sin((2*pi/period) * t);
% Creating the design matrix, "G"
% G contains the variables in the equation
same = ones(Rdata,1);
G = [t same thirdco fourthco];
% Create the matrix "Parameters"
% Parameters is a vector containing the model's parameters
% Equation for finding Parameters is derived from [d] = [G] * [m]
Parameters = inv(G'*G) * G'* d;
% Assigning "v","constant","inphase" and "inquad" from calculated Parameters
v = Parameters(1);
constant = Parameters(2);
inphase = Parameters(3);
inquad = Parameters(4);
%%Finding the periodicdata output
% Since the equation is known, can assign "periodicdata", which is the output [d] as the parameters have been
% determined. Can use matrix multiplication of "G * Parameters" since
% they correspond to the coefficients and variables.
[periodicdata] = G * Parameters;
% Calculate amplitude
Amp = sqrt(inphase^2 + inquad^2);
% Calculate phase shift
phaseshf = atan(inquad/inphase);
% Finding R squared "coeff_deterloop"
Rsquared = 1 - sum((periodicdata - d).^2) / sum((mean(periodicdata) - periodicdata).^2)
end
Accepted Answer
More Answers (2)
Rik
on 30 Oct 2018
2 votes
Just a quick note: a negative R-squared is actually possible. It just means your fit is so bad it is even worse than using the mean as your fit.
As for your actual question: you should not explicitly use the inv function to invert your matrix (so the line with Parameters = inv(G'*G) * G'* d;). This is mentioned in the doc of inv as well: link.
Madlab
on 31 Oct 2018
7 Comments
Rik
on 31 Oct 2018
This is not an answer, but a comment. Please use the comment field instead, as it will keep the conversation readable.
Also, did you read the doc for inv? It recommends using other tools, which might help you get rid of the warning.
The solution for this period question is to index it:
saved_R=zeros(size(period));
%calculation Rsquared based on period(period_ind)
saved_R(period_ind)=Rsquared;
Madlab
on 31 Oct 2018
Rik
on 31 Oct 2018
Did you read the code I proposed? The most important thing about it is that you shouldn't use period itself to index anything, but you should index it with an index variable that takes the values 1:numel(period)
Rik
on 1 Nov 2018
You should have used that index variable in the for declaration. Then you can use that to index both index the period vector and the output.
Rik
on 1 Nov 2018
So your code should become this:
period = 0:1/12:0.5;
calcper=cell(1,numel(time));
Rvalue=cell(1,numel(time));
% Find best period for different places
for k=1:numel(time)
saved_R =zeros(size(period));
% Set range of period
for periodindex =1:numel(period)
[Rsquared] = functionRsq(time{k},sealevel{k},period(periodindex),alltimesea{k});
saved_R(periodindex)=Rsquared;
end
% Finding max R squared value
[~,indexmaxR] = max(saved_R);
calcper{k} = period(indexmaxR);
Rvalue{k} = saved_R(indexmaxR);
end
Your code would have returned several errors. A nice thing to use only during debugging is clearvars. That way you can make sure you actually only use variables that you intended, instead of having old variable names hanging around.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!