# This sample program shows how that simple linear regression may not
be good enough
# when the underlying model is complex. Nonetheless, it can be use as
an approximation
# locally. Be careful. Extrapolation might lead to unexpected and even
ridiculous conclusion.
# We will illustrate these points by simulation studies.
#
# The program is divided into the following steps
#
# 1. Generate the random "errors" e_1, ..., e_20 iid ~ N(0, sgm2)
# 2. Create X=1, 2, ...,20 with a) Y_i = 1 +2 X_i^2 +
e_i
#
b) Y_i = 4*sin(X_i) + e_i
#
c) Y_i = exp(X_i /4) + e_i
#
d) Y_i = 1 + 2 X_i + e_i, i=1, ..., 20.
# 3. Sketch the scatterplots and the fitted lines, respectively.
#
# Question: 1. Based on the
simulated examples, when is it appropriate to use simple linear
regression?
#
2. When the underlying relation between Y and X is not linear, do
you find the fitted model
#
based on simple linear model satisfactory?
par(mfrow=c(2,2)); #
Setup the graphical device
sgm<-2;
# sgm = sqrt(sgm^2)
x<-as.vector(seq(1,20,1));
y1<-1+ 2*x^2; y2<-4*sin(x);
y3<-exp(x/4); y4<-1+2*x;
# Generate the random errors and add them to y
e<-as.vector(rnorm(mean=0,sd=sgm,n=20)) ;
y1<-as.vector(y1+e); y2<-as.vector(y2+e);
y3<-as.vector(y3+e); y4<-as.vector(y4+e);
plot(x, y1); m1<-lm(y1~x); summary(m1); abline(coef(m1))
plot(x, y2); m2<-lm(y2~x); summary(m2);
abline(coef(m2))
plot(x, y3); m3<-lm(y3~x); summary(m3);
abline(coef(m3))
plot(x, y4); m4<-lm(y4~x); summary(m4);
abline(coef(m4))
# You are welcom to try
something other relations nd see what happens!
quit()
# Then Exit R without saving workpace image.
# Created 030930 by YesKno&Mhouse