Fitting method with multiple response variables (y1, y2, y3).
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Assuming I have the following data representing water level trends at different stations in this area:
X = linspace(0, 10, 100)';
Y1 = 2X.^2 - 3X + randn(size(X))*0.5;
Y2 = 2.5X.^2 - 3X + randn(size(X))*0.5;
Y3 = 4.1X.^2 - 3X + randn(size(X))*0.5;
These data are self-generated, and I want to create a fitting line or regression line in Matlab using the fitlm command to represent these three sets of data. Chatgpt has suggested the following code to solve my problem:
Y1 = 2X.^2 - 3X + randn(size(X))*0.5;
Y2 = 2.5X.^2 - 3X + randn(size(X))*0.5;
Y3 = 4.1X.^2 - 3X + randn(size(X))*0.5;
% Create tables of the sea level data for each region
data1 = table(X, Y1, 'VariableNames', {'X', 'Y'});
data2 = table(X, Y2, 'VariableNames', {'X', 'Y'});
data3 = table(X, Y3, 'VariableNames', {'X', 'Y'});
% Merge the data for all regions merged_data = [data1; data2; data3];
% Fit a linear trend to the data model = fitlm(merged_data, 'Y~ X');
% Plot the results plot(model);hold on % plot(X ,mean_Y) legend({'Region 1', 'Region 2', 'Region 3', 'Overall Trend'}, 'Location', 'Northwest');
However, I am not sure about the instruction "merged_data = [data1; data2; data3];" in the code. Can fitlm fit the data in this way?
Also, what does "YX" in "model = fitlm(merged_data, 'Y X');" mean?
2 Commenti
Torsten
il 17 Mar 2023
You want to fit 6 parameters (two for each data set) or only 4 (one parameter for each X.^2 (= 3) and the same parameter for the X (= 1)) ?
Risposte (1)
Torsten
il 17 Mar 2023
Modificato: Torsten
il 17 Mar 2023
rng("default")
X = linspace(0, 10, 100)';
Y1 = 2*X.^2 - 3*X + randn(size(X))*0.5;
Y2 = 2.5*X.^2 - 3*X + randn(size(X))*0.5;
Y3 = 4.1*X.^2 - 3*X + randn(size(X))*0.5;
A = [X.^2 X];
sol1 = A\Y1
sol2 = A\Y2
sol3 = A\Y3
%or
sol = A\[Y1,Y2,Y3]
If you really want to fit a linear function to the quadratic data, use
A = [X ones(size(X))];
instead of
A = [X.^2 X];
0 Commenti
Vedere anche
Categorie
Scopri di più su Regression 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!