how can i find the slope of a regression line

19 次查看(过去 30 天)
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.
  3 个评论
Omer hakan Yaran
Omer hakan Yaran 2023-4-18
%% Regression Calculation
% Center and scale the data
centered_timestamps = Td_no_nan - mean(Td_no_nan);
scaled_Td = (corresponding_timestamp_no_nan - mean(corresponding_timestamp_no_nan)) / std(corresponding_timestamp_no_nan);
% Create a scatter plot of the centered and scaled data
scatter(scaled_Td, centered_timestamps);
xlabel('Scaled corresponding_timestamp_no_nan');
ylabel('Centered Td_no_nan');
title('Scatter Plot of Centered Td_no_nan vs Scaled corresponding_timestamp_no_nan');
% Fit a linear regression model using the 'fitlm' function
lm = fitlm(scaled_Td, centered_timestamps);
% Get the fitted values (regression line) from the linear regression model
regression_line = lm.Fitted;
% Add the regression line to the scatter plot
hold on;
plot(scaled_Td, regression_line, 'r');
legend('Data Points', 'Regression Line');
hold off;
this is how i got this regression line on the graph. i can clearly see the line on the graph but after hours of trying i still can't get the right slope of the line. i trust DanzGPT on this
Adam Danz
Adam Danz 2023-4-28
😀
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.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2023-4-18
% 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)
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ______ _________ (Intercept) 1.2979 1.1682 1.111 0.29882 x1 3.2412 0.18827 17.215 1.319e-07 Number of observations: 10, Error degrees of freedom: 8 Root Mean Squared Error: 1.71 R-squared: 0.974, Adjusted R-Squared: 0.97 F-statistic vs. constant model: 296, p-value = 1.32e-07
% 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 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by