How to manually perform linear regression from scratch
Show older comments
My code is below. Firstly, is my linear regression actually correct?
If it's right, I want to do the same but I want to create an original code for doing the linear regression, fitting a straight line to the data.
clc
clear
%input tensile strength data
TS=[72.5 73.8 68.1 77.9 65.5 73.23 71.17 79.92 65.67 74.28 67.95 82.84...
79.83 80.52 70.65 72.85 77.81 72.29 75.78 67.03 72.85 77.81 75.33...
71.75 72.28 79.08 71.04 67.84 69.2 71.53]';
%calculate mean
meanTS=mean(TS) %using matlab function
meanTSmanual=(sum(TS))/length(TS) %calculate mean manually
%calculate standard deviation
stdTS=std(TS) %using matlab function
%calculate standard deviation manually
meandifference = (TS-meanTSmanual).^2; %find the difference between each point of the mean then square it
TSvariance=sum(meandifference)/(length(TS)-1); %divide by sample size minus 1 to get the variance
stdTSmanual=sqrt(TSvariance) %square root the variance to get the standard deviation
%ascending order
[ascend,index]=sort(TS,'ascend'); %sort the data in ascending order
ascend
%probability of survival
survival=(1-(index/(length(TS)-1))) %applying given formula
%Weibull model
reg1=real(log((log(1./survival)))) %first dataset for linear regression, real parts only
reg2=log(TS) %second dataset for linear regression
%linear regression
idx = isfinite(reg1) & isfinite(reg2); %exclude infinite values and NaNs
pwf = fit(reg1(idx),reg2(idx),'poly1')
plot(pwf,reg1,reg2)
xlim([-5,5])
ylim([-5,5])
Accepted Answer
More Answers (0)
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!