14.14. Least Squares Regression Homework¶
14.14.1. Part 1 - Linear Regression¶
Enter the following commands into the MATLAB Command Window for an example of solving a linear regression problem using linear algebra.
>> t = linspace(0,20);
>> y = 10 - 0.75.*t + 5*randn(1,100);
>> scatter(t,y)
>> A = ones(100,2);
>> A(:,2) = t';
>> x_hat = (A'*A)\(A'*y');
>> p = A*x_hat;
>> hold on
>> plot(t,p)
>> title('Linear Regression')
>> hold off
>> x_hat
Save the plot as a ‘png’ file to submit on Canvas. In the comment area of
Canvas, indicate the values of the x_hat
vector.
14.14.2. Part 2 - Quadratic Regression¶
Enter the following commands into the MATLAB Command Window for an example
of solving a quadratic regression problem using MATLAB’s polyfit
and
polyval
functions.
>> t = linspace(-10,10);
>> y = 0.2*t.^2 - t + 5 + 3*randn(1,100);
>> scatter(t,y)
>> coef = polyfit(t, y, 2);
>> p = polyval(coef, t);
>> hold on
>> plot(t,p)
>> title('Quadratic Regression')
>> hold off
>> coef
Save the plot as a ‘png’ file to submit on Canvas. In the comment area of
Canvas, indicate the values of the coefficients returned from polyfit
.
14.14.3. Part 3 - Old Faithful Wait Prediction¶
You will complete this part of the assignment using MATLAB Grader.
Old Faithful is a cone geyser located in Yellowstone National Park in Wyoming, United States. It was named in 1870 during the Washburn-Langford-Doane Expedition and was the first geyser in the park to receive a name. It is a highly predictable geothermal feature, which makes it a favorite to visitors. In 1939, the average wait time between eruptions was 66.5 minutes, but wait times have slowly increased to an average of 90 minutes today. It has erupted every 44 minutes to two hours since 2000. The duration of the last eruption is a general indicator of the wait until the next eruption. A longer eruption predicts a longer wait until the next eruption starts.
Several years ago, measurements were taken of the duration and wait times until
the next eruption for 272 eruptions. This data is in the file
faithful.txt
. Use linear regression to fit a prediction line to
the data. Then calculate the coefficient of determination to assess the
accuracy of the prediction line. See Goodness of a Fit.