星期二, 11月 02, 2004

306 Lab Notes (Temporary)

Lab 8 Polynomial Regression

The model for a pth degree polynomial (p >= 2)

Yi = B0 + B1X1 + B2X2^2 + ... + BpXp^p

Parameters: p+1

Design Matrix:

X = [1 X X^2 ... X^p] (n x p Matrix)

R Code:

data8=read.table("dataforlab8.txt",T)
attach(data8)
Year=Year-1970
g1=lm(Cost~Year)
g2=lm(Cost~cbind(Year,Year^2))
plot(Year,Cost)
lines(Year,g1$fit,lty=1,col=1)
lines(Year,g2$fit,lty=2,col=2)

Try g3, g4 and so on... See if there's significant improvement on the data.

Solution:

g3=lm(Cost~cbind(Year,Year^2,Year^3))
lines(Year,g3$fit,lty=3,col=3)
g4=lm(Cost~cbind(Year,Year^2,Year^3,Year^4))
lines(Year,g4$fit,lty=4,col=4)

and

g5=lm(Cost~cbind(Year,Year^2,Year^3,Year^4,Year^5))
lines(Year,g5$fit,lty=5,col=4)

And we find the approximation has been greatly imporved as we increase the degree of parameters. Yet, the imporve is only minimal after degree 3. Therefore, the 3rd dimension is preferred over the 4th dimension. Simply because it's much easier to produce the result and the amount of improvement to 4th dimension does not worth the effort.

Side Note:

try

>legend(2,100,lty=1:5,legend=c("1","2","3","4","5")

After Lab:

Increase data sample from 10 to 15 and 20. And test to see the differences that'll make on the regression model. Also, do t-test and p-test on different degree of the data to find the if the numbers has significant values.

Conclustion:

Surprisingly easy... Remember to print this out later. Hell with the after lab.

沒有留言: