MATLAB Help - Least Squares Regression

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay for this screencast we're going to do something called least squares regression so I live in Mobile Alabama and here is a website that I found that lists the average max temperature from January through December for 1961 to 1990 now I have copied and pasted this into a script here so here is a verage max temperature in Fahrenheit average min temperature in Fahrenheit average total precipitation and inches and then it's 12 months January through February so I have months 1 through 12 so the first thing you want to do is you want to make a figure set the background to why big-big equals and then you want to plot the months and I'm going to do for this exam I'm going to do the average max temperature and since you have only single data points I'm gonna plot just blue stars and then I'm going to do grid on hold on and hit f5 and something I got an error undefined vanu okay yeah I didn't under find function a variable any underscore F okay cool so here is our our temperature in Fahrenheit as a function of months we should probably throw an X label on here a month Y label a ver äj-- max temperature in Fahrenheit run that again there we go so we've got our plot average max temperature months so this looks like it's quadratic so say I want to fit a line through this I need to generate my equation so I need y equals H a so Y is my vector of y coordinate so that's going to be average max temp temperature F I'm going to transpose it so it's a column and H is going to be a column of one's length of Y comma one and then as many orders as you want so if I just do X Italy linear if I do X dot x squared I'll be quadratic so let's just start with quadratic and then I'm going to say that X is my months and transpose that okay and then a star is just inverse H transpose times H times H transpose times y and then Y tilde is H times a star and then I'm going to plot X comma Y tilde and I'm going to do a red line and I'm going to make a line width of two now I'm going to run that and there we go there's my least square fit now if I'd like if I wanted to I could increase this in to a third order approximation and then here we go there's a looks like that's a better fit I could add an extra order here and now I've got fourth order and that's actually a really really good fit if I wanted to know what my residuals where are I would just do the sum of Y minus y tilde and I'd have to do dot hat squared and it looks like R is 6.4 if I do a cubic my R is 37 if I do a quadratic my R is 101 so as I keep increasing the order I reduce my my residuals here so if I do 5th order looks like it goes down even further acai mean I could just go ahead and do like six seventh order but so here's the thing this is starting to get tedious so what I probably want to do is this make right so I'm going to say one length of Y comma one and I'm just going to make a loop one two the order so N equals well you have to go you could actually just make H an empty matrix and as the order start at zero and then you could say H is equal to H comma X raised to the ID X power and and what that will do is every time you look through here you'll just concatenate an extra one so for here's first order so I just get a line and then if I do second order I get a quadratic and so now I can go ahead and I can just do like tenth order and oh I got a matrix is close to singular so here's the problem the length of Y is 12 so if you make this order very very close to the number of data points you have you start running into trouble so we can probably so nine it since it's very close to singular but I still got almost on there if we do say eight maybe that error will go and now the air is still not there how it's seven okay six five there we go so it looks like five is the only time that the air is close to zero so you want to you want to stay pretty much probably around five because the matrix is getting close to singular is badly scaled but the point is is that the regression curve matches really well and so now you have a red line that interpolates the data points and there you go and that's pretty much least squares regression in a nutshell and you can do whatever you want on that you could plot the you know the minimum of temperature mi N and do average min temperature change Y to min and then run it again and there you go there's the average mint temperature it looks almost the same that's probably because let's do let's plot the months comma average max initiative Maxim in temperature are pretty pretty close let's do green stars yeah I mean they're they're like right on top of each other so that's fine but you can regress any curve you want that's pretty much the bottom line so I hope that helped good luck
Info
Channel: Monte Carlos
Views: 59,145
Rating: 4.7707734 out of 5
Keywords: Regression Analysis, Least Squares, MATLAB (Programming Language), Statistics (Field Of Study)
Id: 5usW7aqo-bU
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Tue Mar 17 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.