# This sample program shows how sensitive the LSE is to the outliers
and influential points via simulation.
# 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 and Y_i = 1 +2 X_i + e_i
# 3. Sketch the scatterplots and the fitted lines.
#
# 4. Now change (X_20, Y_20) to some outlying numbers, say (30,-10)
# and then get the scatterplots and the
fitted lines
#
# Question: 1. Based on the
simulated example, when is appropriate to use LSE?
#
2. When does LSE suffer the most?
par(mfrow=c(2,2)); #
Setup the graphical device
sgm<-2;
# sgm = sqrt(sgm^2)
x<-as.vector(seq(1,20,1));
y<-1+ 2*x;
# Generate the random errors and add them to y
e<-as.vector(rnorm(mean=0,sd=sgm,n=20)) ;
y<-as.vector(y+e);
plot(x, y)
m1<-lm(y~x)
summary(m1)
abline(coef(m1))
# Assign new (X_20, Y_20) <- (30,-10)
x[20]<-30; y[20]<--10;
# Redo the calculation and graphing
plot(x, y)
m1<-lm(y~x)
summary(m1)
abline(coef(m1))
# You are welcom to try
something more daring and seg what happens!
quit()
# Then Exit R without saving workpace image.
# Created 030930 by YesKno&Mhouse