How to fit power-law to each column of data arranged in a table?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Sara Woods
il 17 Ago 2024
Commentato: Star Strider
il 17 Ago 2024
Hello all! Here my question:
I have a table displaying 1000 columns (spikes_mtx). Each column has 400 rows of data displaying exponential decay over 100s time (std_time).
I'd like to fit a three-coefficient power-fit model (y = a*x^b+c) to each column so as to obtain:
A table with x1000 a coefficient (coming from the 1000 exponential decays)
A table with x1000 b coefficient (coming from the 1000 exponential decays)
A table with x1000 c coefficient (coming from the 1000 exponential decays)
A table with x1000 fit goodness parameters (coming from the 1000 exponential decays)
All tables stored in one single structure (results.STD). My code doesn't function, any recommendation? Thanks in advance!
ft = fittype( 'power2' ); % Power Fit: y = a*x^b+c
opts = fitoptions(ft); % Power fit options
opts.StartPoint = [1 -1 0];
opts.Lower = [0 -Inf -Inf];
opts.Upper = [Inf 0 Inf];
for i=1:length (spikes_mtx)
[xData, yData] = prepareCurveData(std_time,spikes_mtx(:,i)); % x = std_time y = spikes_mtx
[fitresult, gof] = fit( xData, yData, ft, opts ); % Goodnes of the Fit R^2
results.STD.Coeff.a(1,i)=S.std.fitmodel.a;
results.STD.Coeff.b(1,i)=S.std.fitmodel.b;
results.STD.Coeff.c(1,i)=S.std.fitmodel.c;
results.STD.gof.sse(1,i)=S.std.gof.sse;
results.STD.gof.rsquare(1,i)=S.std.gof.rsquare;
results.STD.gof.dfe(1,i)=S.std.gof.dfe;
results.STD.gof.adjrsquare(1,i)=S.std.gof.adjrsquare;
results.STD.gof.rmse(1,i)=S.std.gof.rmse;
end
0 Commenti
Risposta accettata
Star Strider
il 17 Ago 2024
Modificato: Star Strider
il 17 Ago 2024
It might be best to put all the parameters in a single table, and for that matter, put everything in a single table.
Since you want them in different tables, try this —
load('spikes_mtx.mat')
load('std_time')
% new_time = linspace(min(std_time), max(std_time), 1000*400);
% new_spikes = interp1(std_time, std_spk_avg, new_time);
%
% spikes_mtx = reshape(new_spikes, 1000, []).'
ft = fittype( 'power2' ); % Power Fit: y = a*x^b+c
opts = fitoptions(ft); % Power fit options
opts.StartPoint = [1 -1 0];
opts.Lower = [0 -Inf -Inf];
opts.Upper = [Inf 0 Inf];
for i=1:size(spikes_mtx,2)
[xData, yData] = prepareCurveData(std_time,spikes_mtx(:,i)); % x = std_time y = spikes_mtx
[fitresult, gof] = fit( xData, yData, ft, opts ); % Goodnes of the Fit R^2
results_a(i,:)=fitresult.a;
results_b(i,:)=fitresult.b;
results_c(i,:)=fitresult.c;
results_sse(i,:)=gof.sse;
results_rsquare(i,:)=gof.rsquare;
results_dfe(i,:)=gof.dfe;
results_adjrsquare(i,:)=gof.adjrsquare;
results_rmse(i,:)=gof.rmse;
end
Results_a = table(results_a)
Results_b = table(results_b)
Results_c = table(results_c)
Results_GOF = table(results_sse, results_rsquare, results_dfe, results_adjrsquare, results_rmse)
Single table —
Results = table(results_a, results_b, results_c, results_sse, results_rsquare, results_dfe, results_adjrsquare, results_rmse)
EDIT — (17 Aug 2024 at 14:33)
Added code to create a single table.
.
4 Commenti
Più risposte (1)
John D'Errico
il 17 Ago 2024
Modificato: John D'Errico
il 17 Ago 2024
Why should it work? I see these lines:
[fitresult, gof] = fit( xData, yData, ft, opts ); % Goodnes of the Fit R^2
results(i).STD.Coeff.a(1,i)=S.std.fitmodel.a;
What is S.std.fitmodel? Where do you think that comes from? You create something called fitresult and gof. So use it. Use the variable gof as returned. Should MATLAB be able to know what you want? It tries to execute the code you give it.
load std_time.mat
load spikes_mtx.mat
ft = fittype( 'power2' ) % Power Fit: y = a*x^b+c
opts = fitoptions(ft); % Power fit options
opts.StartPoint = [1 -1 0];
opts.Lower = [0 -Inf -Inf];
opts.Upper = [Inf 0 Inf];
The results struct is a highly confusing thing you are creating. I have no clue what you really wanted to do, so I'll make a guess.
N = length(spikes_mtx);
results.Coeff.a = zeros(1,N);
results.Coeff.b = zeros(1,N);
results.Coeff.c = zeros(1,N);
results.gof.sse = zeros(1,N);
results.gof.rsquare = zeros(1,N);
results.gof.dfe = zeros(1,N);
results.gof.adjrsquare = zeros(1,N);
results.gof.rmse = zeros(1,N);
for i=1:10 % Should be N, but I've just used 10 here.
[xData, yData] = prepareCurveData(std_time,spikes_mtx(:,i)); % x = std_time y = spikes_mtx
[fitresult, gof] = fit( xData, yData, ft, opts ); % Goodnes of the Fit R^2
results.Coeff.a(i)=fitresult.a;
results.Coeff.b(i)=fitresult.b;
results.Coeff.c(i)=fitresult.c;
results.gof.sse(i) = gof.sse;
results.gof.rsquare(i) = gof.rsquare;
results.gof.dfe(i) = gof.dfe;
results.gof.adjrsquare(i) = gof.adjrsquare;
results.gof.rmse(i) = gof.rmse;
end
results.Coeff
results.gof
Vedere anche
Categorie
Scopri di più su Spline Postprocessing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!