LINK : https://www.dropbox.com/s/p3o2fyeena3pcph/Class10Data1%281%29.zip?dl=0 1)
ID: 3755767 • Letter: L
Question
LINK : https://www.dropbox.com/s/p3o2fyeena3pcph/Class10Data1%281%29.zip?dl=0
1) Download the data file Class10Data1.mat from eCampus. Write a function in which you a) plot the data using a scatter plot of y vs. x to check that a linear model makes sense, b) use polyfit to fit a linear model to the data and output the slope and intercept, and c) plot the best-fit line on the same graph as the scatter plot. Your output should look like this:
2) Download the data file Class10Data2.mat from eCampus. Again, make a scatter plot ofy vs. x. Things shouldn’t look quite so linear. Use polyfit to fit a a) linear, b) quadratic,and c) cubic polynomial to the data. Plot the three fitted lines on your scatter plot. Your plot should look like this:
3) Now calculate the mean squared error (MSE) for each of the 3 fitted lines. Which model has the lowest MSE? How much did the MSE decrease as you went from a linear to quadratic? How much did the MSE decrease from quadratic to cubic? In your opinion, was it worthwhile fitting a higher order model in this example?
4) Now let’s go back to the data set from problem one, and make confidence intervals for the linear model. As presented in the reading you did before class, if n = the number of pairs used in the regression is large , an approximate confidence interval for the slope is given by
±
1 /2
where is the standard deviation of y, and is the standard deviation of x. A confidence interval for the intercept is given by
0 ± /2[()2]1/2
=1
Add code to your function that will calculate and output these confidence intervals for a degree of confidence of 95%.
Explanation / Answer
Answer :
1)
This submission contains four convenience polynomial fitting functions similar to POLYFIT.
1. POLYFITZERO - fit polynomial to data, forcing y-intercept to zero.
2. POLYFITB - force y-intercept to "b".
3. POLYFITB0 - force y-intercept to "b" and slope at (0,b) to zero.
4. POLYFITBM - force y-intercept to "b" and slope at x=0 to "m", e.g.: dy/dx = m.
5. POLYFITBROOT - force intercept and root
6. POLYFITBMROOT - force intercept, slope and root
7. POLYFITBMROOTTERMS - force intercept, slope, root and terms
Forcing the y-intercept to zero is accomplished by noting for polynomial p = [aN, ..., a3, a2, a1, a0],
IE: y = aN*y^N + ... + a3*y^3 + a2*y^2 + a1*y + a0
when x is zero, then y is the constant term, "a0". Therefore a0 = 0, or in the general case when forcing the y-intercept to an arbitrary value, a0 = b.
Forcing the slope at x=0, is accomplished by noticing that the derivative of p
IE: dy/dx = N*aN*y^(N-1) + ... + 3*a3*y^2 + 2*a2*y + a1
evaluated at x = 0 yields "a1". Therefore a1 = 0 for zero slope, or in the general case when forcing the slope to an arbitrary value, a1 = m.
lsline is in the Statistics Toolbox, if you do not have that product you can use polyfit() to fit a 1st order polynomial.
x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
P = polyfit(x,y1,1);
yfit = P(1)*x+P(2);
hold on;
plot(x,yfit,'r-.');
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.