Fitting data with different type regression

Hi.
I have got some data in form of x and y. I have plotted these data in logarithmic scale. Now i want to use two regeression line. one in form of y = c and another in form of
y = beta*(x)^alpha. a sample of this work is shown below:
And my plot data is like this (my data is attached):
could someone help me in this problem?

Respuestas (2)

KSSV
KSSV el 2 de Jun. de 2020
You have to split your data into two sets....Let (x,y) be your m*n data and you want to divide them at a value val.
data1 = [x(x<=val) y(x<=val)] ;
data2 = [x(x>val) y(x>val)] ;
Now you can fit your required line into those sets. To fit a function you can read about polyfit, cftool.

3 comentarios

Thanks for the answer. but the regression lines are not connected to each other. What can i do about this problem?
KSSV
KSSV el 2 de Jun. de 2020
You can pick the end point of the first set line, the end point of the second set line.....from these points you can get the line. But don't know will this is okay for you?
Sorry but how can i do that?

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 2 de Jun. de 2020
Editada: Image Analyst el 2 de Jun. de 2020
mohammadreza: you can do a piecewise linear fit. What I did was to fit two lines. I picked a dividing line between the two sets of data, then fit a line to the left set and the right set. Then I moved that dividing point from 5% of the points being on the left all the way over to the right where 95% of the points were on the left. Then I looked for where the difference in slopes between the two lines is greatest. That was the final dividing point between the left set of data and the right set of data. Then I set the two lines equal to each other to find the crossing point. Here is the script:
% Find optimal pair of lines to fit noisy data, one line on left side and one line on right side. Separation/crossing x value is identified.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
markerSize = 20;
fprintf('Beginning to run %s.m ...\n', mfilename);
sx = load('x.mat')
sy = load('y.mat')
x = log(sx.qna16);
yNoisy = sy.naq16;
% User's x data seems to be scrambled. We need to sort it.
[x, sortOrder] = sort(x, 'ascend');
% Sort y the same way
yNoisy = yNoisy(sortOrder);
% Define number of points.
numPoints = length(x);
midIndex = round(numPoints/2);
% Plot lines.
subplot(2, 2, 1);
plot(x, yNoisy, 'b.', 'MarkerSize', markerSize);
grid on;
xlabel('log(x)', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
title('Noisy Y vs. X data', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Assume the crossing point will be somewhere in the middle half of the points.
% Fit a line through the right and left parts and get the slopes.
% Keep the point where the slope difference is greatest.
index1 = round(0.05 * numPoints); % 25% of the way through.
index2 = round(0.95 * numPoints); % 75% of the way through.
% In other words, assume that we need at least 25 percent of the points to make a good estimate of the line.
% Obviously if we took only 2 or 3 points, then the slope could vary quite dramatically,
% so let's use at least 25% of the points to make sure we don't get crazy slopes.
% Initialize structure array
for k = 1 : numPoints
lineData(k).slopeDifferences = 0;
lineData(k).line1 = [0,0];
lineData(k).line2 = [0,0];
end
for k = index1 : index2
% Get data in left side.
x1 = x(1:k);
y1 = yNoisy(1:k);
% Fit a line through the left side.
coefficients1 = polyfit(x1, y1, 1); % The slope is coefficients1(1).
% Get data in right side.
x2 = x(k+1:end);
y2 = yNoisy(k+1:end);
% Fit a line through the left side.
coefficients2 = polyfit(x2, y2, 1); % The slope is coefficients2(1).
% Compute difference in slopes, and store in structure array along with line equation coefficients.
lineData(k).slopeDifferences = abs(coefficients1(1) - coefficients2(1));
lineData(k).line1 = coefficients1;
lineData(k).line2 = coefficients2;
end
% Find index for which slope difference is greatest.
slopeDifferences = [lineData.slopeDifferences]; % Extract from structure array into double vector of slope differences only
% slope1s = struct2table(lineData.line1); % Extract from structure array into double vector of slopes only
% slope2s = [lineData.line2(1)]; % Extract from structure array into double vector of slopes only
[maxSlopeDiff, indexOfMaxSlopeDiff] = max(slopeDifferences)
% Plot slope differences.
subplot(2, 2, 2);
plot(slopeDifferences, 'b.', 'MarkerSize', markerSize);
grid on;
caption = sprintf('Slope Differences Maximum at Index = %d, x value = %.2f', indexOfMaxSlopeDiff, x(indexOfMaxSlopeDiff));
title(caption, 'FontSize', fontSize);
xlabel('Index', 'FontSize', fontSize);
ylabel('abs(Slope Difference)', 'FontSize', fontSize);
% Mark it with a red line.
line([indexOfMaxSlopeDiff, indexOfMaxSlopeDiff], [0, maxSlopeDiff], 'Color', 'r', 'LineWidth', 2);
% Show everything together all on one plot.
% Plot lines.
subplot(2, 2, 3:4);
plot(x, yNoisy, 'b.', 'MarkerSize', markerSize);
grid on;
xlabel('log(x)', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
hold on;
% Use the equation of line1 to get fitted/regressed y1 values.
slope1 = lineData(indexOfMaxSlopeDiff).line1(1);
intercept1 = lineData(indexOfMaxSlopeDiff).line1(2);
y1Fitted = slope1 * x + intercept1;
% Plot line 1 over/through data.
plot(x, y1Fitted, 'r-', 'LineWidth', 2);
% Use the equation of line2 to get fitted/regressed y2 values.
slope2 = lineData(indexOfMaxSlopeDiff).line2(1);
intercept2 = lineData(indexOfMaxSlopeDiff).line2(2);
y2Fitted = slope2 * x + intercept2;
% Plot line 2 over/through data.
plot(x, y2Fitted, 'g-', 'LineWidth', 2);
% Mark crossing with a magenta line.
xc = (intercept2 - intercept1) / (slope1 - slope2);
yc = slope2 * xc + intercept2;
yl = ylim;
fprintf('The intersection is at log(x) = %f, y = %f.\n', xc, yc);
line([xc, xc], [yl(1), yc], 'Color', 'm', 'LineWidth', 2);
caption = sprintf('Data with left and right lines overlaid. Lines intersect at (%f, %f)', xc, yc);
title(caption, 'FontSize', fontSize);
message1 = sprintf('Left Equation: y = %.3f * x + %.3f', slope1, intercept1);
message2 = sprintf('Right Equation: y = %.3f * x + %.3f', slope2, intercept2);
message = sprintf('%s\n%s', message1, message2);
fprintf('%s\n', message);
text(.01, 3, message, 'Color', 'r', 'FontSize', 15, 'FontWeight', 'bold');
uiwait(helpdlg(message));
If you want a perfectly flat line on the left, you can just take the mean of the left set of data and then find out where the right (green) line hits that mean y value. Just replace this line
% Fit a line through the left side.
coefficients1 = polyfit(x1, y1, 1); % The slope is coefficients1(1).
with this:
% Fit a line through the left side.
coefficients1 = [0, mean(y1)]; % The slope is coefficients1(1).
and you'll get this:

1 comentario

Really good thank you Image Analyst!! I learned a lot from your code!

Iniciar sesión para comentar.

Preguntada:

el 2 de Jun. de 2020

Comentada:

el 2 de Jun. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by