set.seed(123)
numPts = 14
err <- runif(numPts, -10, 10)
x1 <- runif(numPts,10,100)
y<- 50 + 0.44 * x1 +  err/2
x1[numPts+1] <- 147 ; y[numPts+1] <- 89
x1[numPts+2] <- 149 ; y[numPts+2] <- 90
x1[numPts+3] <- 151 ; y[numPts+3] <- 92
fitted <- lm(y~x1)
plot(x1,y , xlab="Hours of advertisement" , ylab = "Amount sold" , xlim=c(4,148) , ylim=c(52, 108))
abline(fitted$coef, lwd = 2, col = "blue") grid(NA, 6, lwd = 2) rStuRes <- rstudent(fitted) yHat <- predict(fitted) res <- y - yHat plot(yHat, res) abline(0, 0)  qqnorm(rStuRes, datax = TRUE, main = "Normal Probability Plot") qqline(rStuRes, datax = TRUE ) set.seed(123) numPts = 20 err <- runif(numPts, -10, 10) x1 <- runif(numPts,0,100) x2 <- runif(numPts,0,100) y<- 50 + 0.1 * x1 + 0.3 * x2 + err # Exercise : Add a "fake outlier" to the observations such that the added point is not an outlier but looks like one in the first plot. (You want a point looks like an outlier in the first one, but in the second plot we verify that it is not an outlier.) fitted <- lm(y~x1+x2) # First plot plot(x2,y) yHat = predict(fitted) res= y - yHat # Second Plot plot (yHat,res) # Optional 3rd plot #library("plot3D") #library("scatterplot3d") # sc1 <- scatterplot3d(x1,x2,y, pch=17 , type = 'p', angle = 15 , highlight.3d = T ) # sc1$points3d (x1,x2,y, cex=.02, col="blue")

preview the HTML file).

LS0tCnRpdGxlOiAiRmluYWwgRXhhbSAgKHNvbWUgZXhhbXBsZXMpIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgoKYGBge3J9CgpzZXQuc2VlZCgxMjMpCm51bVB0cyA9IDE0IAplcnIgPC0gcnVuaWYobnVtUHRzLCAtMTAsIDEwKQoKeDEgPC0gcnVuaWYobnVtUHRzLDEwLDEwMCkKeTwtIDUwICsgMC40NCAqIHgxICsgIGVyci8yIAoKeDFbbnVtUHRzKzFdIDwtIDE0NyA7IHlbbnVtUHRzKzFdIDwtIDg5ICA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