# How to draw a line of best fit on filtered discrete data sets

1 ビュー (過去 30 日間)
Rizwan Khan 2021 年 6 月 4 日
コメント済み: Mathieu NOE 2021 年 6 月 7 日
How can i draw a line of best fit on a subset of the full data set?
I want a line of best fit but only using the data in blue below
So ignore the green, yellow, orange, red, only use the blue part
Now that is easily filtered down to, so know which parts are the blue part is easy, but getting to somehow draw a line of best fit to only use the blue is the challeging part I can filter the data set to only show the data i want to use for the line of best fit, as shown in graph below Now all i need it to find a way to draw a line of best fit on this data set, but when i try polyfit and other best fit functions, it gets all messed up
I'm assuming because of the gaps in the dataset.
So how can i draw a line of best fit for the chart above?
##### 5 件のコメント表示非表示 4 件の古いコメント
Scott MacKenzie 2021 年 6 月 4 日
You post data via the Attachments button when you are writing your question: If you insert code in your question, it's always best to have it properly appear as code. To do this, select the code and click the code button to get the code propertly formatted in your question. サインインしてコメントする。

### 採用された回答

Mathieu NOE 2021 年 6 月 4 日
I used this simple example and created a gap in the data (not NaN's)
there is no problem to use polyfit in that situation % Example: simple linear regression with polyfit
% Fit a polynomial p of degree 1 to the (x,y) data:
x = 1:100;
x(x>25 & x <75) = [];
y = 0.3*x + 2*randn(1,length(x));
p = polyfit(x,y,1);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
legend('data','linear fit')
##### 7 件のコメント表示非表示 6 件の古いコメント
Mathieu NOE 2021 年 6 月 7 日
Thank you @Star Strider for your help
@Rizwan Khan try ths , hopefully I didn't make any wrong usage of polypredci
this is the plot associated with this demo code : % Example: simple log regression with polyfit
% Fit a polynomial p of degree 1 to the (x,y) data:
x = 1:100;
x(x>25 & x <75) = [];
y = exp(0.03*x + 0.5*randn(1,length(x)));
yl = log10(y);
%%%%%%%%%%%%%%%%%%%%
n = 1; % Polynomial Order
alfa = 0.99; % Desired Significance
xv = linspace(min(x), max(x))'; % Optional High-Resolution Vector
[ p, yhat, ci ] = polypredci( x, yl, n, alfa, xv ); % Define Both ‘alfa’ & ‘xv’
yhat = 10.^(yhat); % convert back from log to lin
figure(1)
semilogy(x, y, 'bp');
hold on
if length(yhat) == length(x)
semilogy(x, yhat, '--r')
semilogy(x, yhat+ci, '--g')
semilogy(x, yhat-ci, '--g')
elseif length(yhat) == length(xv)
semilogy(xv, yhat, '--r')
semilogy(xv, yhat+ci, '--g')
semilogy(xv, yhat-ci, '--g')
end
hold off
grid on

サインインしてコメントする。

R2020b

### Community Treasure Hunt

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

Start Hunting!