how can i find the slope of a regression line
20 views (last 30 days)
Show older comments
Omer hakan Yaran
on 17 Apr 2023
Commented: Adam Danz
on 28 Apr 2023
i have a large set of data, i can plot it and graph its regression line as can be seen on the figure below. all of my regression line slope calculations are giving me a wrong value, either an absurt number or 95 degrees (i think it can be 90 degrees rotated). chatgpt failed to find a solution so im trying my chances here.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1359268/image.png)
3 Comments
Adam Danz
on 28 Apr 2023
😀
I saw that the cyclist addressed your question quickly after your reply. I'm curious what issues you had with ChatGPT when trying to solve this problem. This was at least 11 days ago so I understand if those memories are faded.
Accepted Answer
the cyclist
on 18 Apr 2023
% Set seed, for reproduciblity
rng default
% Make up some data
x = (1:10)';
y = 2 + 3*x + randn(10,1);
% Fit the model
mdl = fitlm(x,y)
% Get the coefficient estimate from the model object
intercept = mdl.Coefficients.Estimate(1);
slope = mdl.Coefficients.Estimate(2); % <------- I think this is the slope you want
% To display the fit, pick two arbitrary points
x0 = [0; 10];
y0 = intercept + slope*([0; 10]);
% Plot the data and the fit
figure
hold on
scatter(x,y)
line(x0, y0,"Color","red")
legend(["data";"fit"],"Location","NorthWest")
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!