Calculating the Regression Coefficient

48 visualizaciones (últimos 30 días)
Ahmed Abdulla
Ahmed Abdulla el 2 de Jun. de 2020
Respondida: Codeshadow el 2 de Jun. de 2020
I have two column matrices that contain data and i wanted to caculate the regression coefficient between the two matrices

Respuesta aceptada

Codeshadow
Codeshadow el 2 de Jun. de 2020
For simple linear regression in the least-square sense, you could do something like this:
% Calculating the regression coefficient
close all
x = linspace(0,1,100); % Data from your first column/independent variable
y = x + rand(1, length(x)); % Data from your second column/dependent variable
% Compute the mean of your data
yhat = mean(y);
xhat = mean(x);
% Compute regression coefficients in the least-square sense
b = (x - xhat)*(y' - yhat)/sum((x - xhat).^2); % Regression coefficient
a = yhat - b*xhat; % Y-intercept
% Plot data
figure()
scatter(x, y);
hold on
plot(x, a + b*x, 'r');
legend(['b = ' num2str(b)]); % Your regression coefficient is b
For more details on the math, you could check the link below:
Hope this helps!

Más respuestas (0)

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by