# This sample program shows the calculuation and limitation of R^2 # Refer to Sect 2.9 in NKNW, particularly Fig 2.8-2.9 x<-seq(0.5,20,.5) # X=0.5, 1, ..., 20 ne<-rnorm(40,0,1) # Generate 41 iid N(0,1) random errors y1<-1+ .5*x + ne y2<-2+4*sin(x)+ ne # Numerical Summary summary(y1);summary(y2);summary(x);summary(ne); # Graphical Displays par(mfrow=c(2,2)); # Setup the graphical device plot(x,y1);plot(x,y2) par(mfrow=c(2,2)); m1<-lm(y1~x) m2<-lm(y2~x) summary(m1);summary(m2); plot.lm(m1) plot.lm(m2) windows() par(mfrow=c(2,2)); plot(x,y1); abline(coef(m1)) plot(x,y2); abline(coef(m2)) x2<-sin(x) m3<-lm(y2~x2) summary(m3) plot(x2,y2); abline(coef(m3)) # Question: # a. How these figures will be if there are no errors terms # added? And how does it affect the R^2? # b. Construct Figure 2.9 # c. Refer to "Limitation of r^2 and r" on p 82--83, do you # think the larger r^2 indicates better fit? Explain.