when i use regress(x,y),I obtain only one answer :shouldn't i get two answers which are the slope and y-intercept ?
MATLAB: Regress(x,y) for least square regression of two variables x,y
regressionStatistics and Machine Learning Toolbox
Related Solutions
% Define sample data.
X = [1,2,3,4,5,6,7,8,9];Y = [4 5 6 9 8 7 4 1 2];% Do the plotting:
plot(X, Y, 'bo', 'LineWidth', 3, 'MarkerSize', 15);grid on;% Maximize the figure window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Label the various parts of the plot.
fontSize = 20;title('lowcalorie Plot', 'FontSize', fontSize);xlabel('X', 'FontSize', fontSize);ylabel('Y', 'FontSize', fontSize);coefficients = polyfit(X, Y, 1);fittedY = polyval(coefficients, X);hold on;% Do the plotting:plot(X, fittedY, 'rs-', 'LineWidth', 3, 'MarkerSize', 15);legend('Y', 'Fitted Y');
The following code can be used to calculate the standard error on mock data both in the case that the intercept is set to 'false' and when an intercept is present:
%%Sample Data Definition
X = [1 2 3 4 5 6 7 8 9 10];Y = [1 1.5 2.1 4 5 6.3 7.3 7.8 9 9.9];%%Calculation of Standard Error Without Intercept
n = length(X); % Number of observations
Slope = sum(X.*Y)/sum(X.^2); % Calculates slope
yfit=X*Slope; % Fitted response values based on the slope
r = Y - yfit; % r is the residuals, which is the observed minus fitted values
SSE = sum(r.^2); % SSE is the sum of squared errors
MSE=SSE/(n-1); % Mean Squared Error
Code_NoIntercept_SE=sqrt(MSE/sum(X.^2)) % Standard error of the slope
model1 = fitlm(X,Y,'Intercept',false); % Calls 'fitlm'
MATLAB_NoIntercept_SE=model1.Coefficients.SE % Queries the standard error of MATLAB's model
%%Clear Variables then Redefine Data
clear allX = [1 2 3 4 5 6 7 8 9 10];Y = [1 1.5 2.1 4 5 6.3 7.3 7.8 9 9.9];%%Calculation of Standard Error With Intercept
n = length(X); % Number of observationsXBar=mean(X); % Calculates mean of X
YBar=mean(Y); % Calculates mean of Y
Sxx=sum((X-XBar).^2); Sxy=sum((X-XBar).*(Y-YBar));Slope = Sxy/Sxx; % Calculates Slope
Intercept= YBar-Slope*XBar; % Calculates Intercept
yfit=Intercept + X*Slope; % Fitted response values based on the sloper = Y - yfit; % r is the residuals, which is the observed minus fitted valuesSSE = sum(r.^2); % SSE is the sum of squared errorsMSE=SSE/(n-2); % Mean Squared Error Code_Intercept_SE=[ % Standard Error of the regression coefficients
sqrt(MSE*sum(X.^2)/(n*Sxx)); % Standard Error of the intercept coefficient
sqrt(MSE/Sxx)] % Standard Error of the slope coefficient
model2 = fitlm(X,Y,'Intercept',true); % Calls 'fitlm'MATLAB_Intercept_SE=model2.Coefficients.SE % Queries the standard error of MATLAB's model
Best Answer