Non-linear regression
Afficher commentaires plus anciens
Hey everyone,
I want to make long-term load forecasting using GA. So the first step is to come up with a model, in one of the papers the objective function is a polynomial of tenth order:
obj= c10*x.^10 + c9*x.^9 + c8*x.^8 + c7*x.^7 + c6*x.^6 + c5*x.^5 + c4*x.^4 + c3*x.^3 + c2*x.^2 + c1*x.^1 + c0*x.^0;
In order to make the obj function ready for the GA I need to estimate the coefficients.
The rest of my code is as follows:
>> f = @(c,x) 1 + c(1)*x.^1 + c(2)*x.^2 + c(3)*x.^3 + c(4)*x.^4 + c(5)*x.^5 + c(6)*x.^6 + c(7)*x.^7 + c(8)*x.^8 + c(9)*x.^9 + c(10)*x.^10;
>> cfit = nlinfit(xdata,ydata,f,c)
all the data that I have are the years from 1982 till 1991 and the corresponding demand in each year.
I didn't understand nlinfit quite well,, what I am supposed to put in place of xdata, ydata and c.
Any help will be appreciated.
Réponse acceptée
Plus de réponses (5)
Richard Willey
le 19 Déc 2011
1 vote
I'd strongly suggestion that you watch a webinar titled "Electricity Load and Price Forecasting with MATLAB". The webinar is available at: http://www.mathworks.com/company/events/webinars/wbnr51423.html
All of the code and the data sets are available on MATLAB Central.
This webinar shows two different ways to model the demand for electric power. The first is based on a neural network. The second uses bagged decision trees. The code also includes safeguards to protect against overfitting.
I'm also going to point you at a blog posting that I wrote on data driven fitting. If you are primarily worried about interpolation you might find this a useful alternative to high order polynomials
1 commentaire
Yasmin Tamimi
le 19 Déc 2011
Greg Heath
le 19 Déc 2011
0 votes
You definitely do not want a high order polynomial for prediction.
Check out Richard's references.
Greg
Yasmin Tamimi
le 19 Déc 2011
0 votes
12 commentaires
bym
le 20 Déc 2011
I don't know anything much about GA but you can use
y = polyval(estimate_coefficients,x)
for your 2nd m-file. Also, shouldn't the objective function be some measure of the error (residual); you are just returning the function value ?!?
Yasmin Tamimi
le 20 Déc 2011
Image Analyst
le 20 Déc 2011
I wouldn't call it a "fitness" function. Those are your "fitted" or, more properly, "estimated" values.
Yasmin Tamimi
le 20 Déc 2011
Walter Roberson
le 20 Déc 2011
"??? Reference to non-existent field 'Verbosity'" -- so what happens if you add a Verbosity option in your gaoptimset ?
Greg Heath
le 23 Déc 2011
Is
y = c1*x(1)^2 + c2*x(2)^1 + c3;
a misprint?
Greg
Walter Roberson
le 23 Déc 2011
It looks consistent with the other code to me?
Yasmin Tamimi
le 23 Déc 2011
Yasmin Tamimi
le 23 Déc 2011
Yasmin Tamimi
le 23 Déc 2011
Yasmin Tamimi
le 23 Déc 2011
Yasmin Tamimi
le 23 Déc 2011
Richard Willey
le 20 Déc 2011
For what its worth, I just took a very quick look at the data set that you provided.
years = [1982 1983 1984 1985 1986 1987 1988 1989 1990 1991];
load = [1702 2344 2097 2313 2588 2885 4341 4779 5251 5721];
You can fit the years 1988 --> 1991 with an almost perfectly straight line. In a similar fashion, the years 1984 --> 1987 with another straight line. In both cases the R^2 is over .995.
I really don't understand that approach that you're taking... I feel like you're trying to force Genetic Algorithms into the solution space regardless of whether this is warranted.
Given that you're primarily interested in using GA, there's one last resource that I'd recommend looking at:
The "Global Optimization with MATLAB Products" provides a very good introduction to GA. You can watch the webinar at: http://www.mathworks.com/company/events/webinars/wbnr43346.html?seq=1
All of the code is available for download from MATLAB Central.
2 commentaires
Yasmin Tamimi
le 20 Déc 2011
Alex
le 25 Sep 2012
Yasmine, can you solve this? What do you do?
anahita
le 24 Mar 2025
0 votes
hello, i want to write a script of nonelinear regression but i have a problem to write that because first idont know which one works for me so i wrote a code for nonelinear least squared regression but th R was 0.56. caan you help me in this clc; clear; close all;
data = readmatrix('analysis_1.xlsx', 'Sheet', 'Main');
S = data(:, 6);
mu = data(:, 2);
sigma = data(:, 3);
gamma = data(:, 4);
Y = data(:, 5);
ZR = data(:, 7);
%% Ryan-Joiner mothode
sorted_S = sort(S);
n = length(S);
z_scores = norminv(((1:n) - 0.5) / n, 0, 1);
S_mean = mean(S);
S_std = std(S);
e_i = (sorted_S - S_mean) / S_std;
Rp = sum(e_i .* z_scores) / sqrt(S_std^2 * (n - 1) * sum(z_scores.^2));
disp(['Ryan-Joiner Rp = ', num2str(Rp)]);
%% (Nonlinear Least Squares)
% S = a * mu^b * sigma^c * gamma^d * Y^e * ZR^f
modelFun = @(params, X) params(1) * (X(:,1) .^ params(2)) .* ...
(X(:,2) .^ params(3)) .* (X(:,3) .^ params(4)) .* ...
(X(:,4) .^ params(5)) .* (X(:,5) .^ params(6));
X = [mu, sigma, gamma, Y, ZR];
initial_guess = [1, 1, 1, 1, 1, 1];
params_opt = lsqcurvefit(modelFun, initial_guess, X, S);
a = params_opt(1);
b_coeff = params_opt(2:end);
disp(['a = ', num2str(a)]);
disp(['b = ', num2str(b_coeff(1))]);
disp(['c = ', num2str(b_coeff(2))]);
disp(['d = ', num2str(b_coeff(3))]);
disp(['e = ', num2str(b_coeff(4))]);
disp(['f = ', num2str(b_coeff(5))]);
%% R²
S_Estimate = modelFun(params_opt, X);
SS_res = sum((S - S_Estimate).^2);
SS_tot = sum((S - mean(S)).^2);
R2 = 1 - (SS_res / SS_tot);
disp(['R2 = ', num2str(R2)]);
results_table = table(S, S_Estimate, 'VariableNames', {'Actual_Storage', 'Estimated_Storage'});
coeff_table = array2table([a; b_coeff'], 'VariableNames', {'Value'}, ...
'RowNames', {'a', 'b', 'c', 'd', 'e', 'f'});
r2_table = table(R2, 'VariableNames', {'R2_Value'});
filename = 'matlab_nonlinear_regression1.xlsx';
writetable(results_table, filename, 'Sheet', 'Storage Comparison');
writetable(coeff_table, filename, 'Sheet', 'Regression Coefficients', 'WriteRowNames', true);
writetable(r2_table, filename, 'Sheet', 'R2 Value');
disp('Excel report generated successfully!');
figure;
scatter(S, S_Estimate, 'b', 'filled');
hold on;
plot([min(S), max(S)], [min(S), max(S)], 'r--', 'LineWidth', 2);
hold off;
xlabel('Actual Storage (S)');
ylabel('Estimated Storage (S_{Estimate})');
title(['Nonlinear Regression (R^2 = ', num2str(R2), ')']);
legend('Data Points', '1:1 Line', 'Location', 'Best');
grid on;
Catégories
En savoir plus sur Linear and Nonlinear Regression dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!