R scripts for the lecture course
Machine Learning, pattern recognition and statistical data modelling
Coryn A.L. Bailer-Jones, 2007

Linear methods part 1
---------------------

*** Linear regression in R (from MASS section 1.3)

library(MASS)
x <- seq(1, 20, 0.5)
x
w <- 1 + x/2
set.seed(30)
y <- x + w*rnorm(x, mean=0, sd=1)       # rnorm(x) is same as rnorm(length(x))
dum <-  data.frame(x, y, w)
dum
rm(x, y, w)

fm <- lm(y ~ x,  data=dum)
#?lm
summary(fm)
attributes(fm)
fm$fitted.values
fitted(fm)
plot(x,y)                          # doesn't work as x,y are not on path
plot(dum$x,dum$y)
attach(dum)                        # put components of dum on path
lines(x,fitted(fm),col=2, lw=2)
fm1 <- lm(y ~ x,  data = dum, weight = 1/w^2)
summary(fm1)
abline(fm1,col=3, lw=2)                         # a simpler way to plot model fits
lrf <-  loess(y ~ x, dum)                       # local polynomial regression
lines(spline(x, fitted(lrf)), col = 4, lw=2)    # loess only predicts at points: spline 
				                # interpolation is used to plot 

par(mfrow=c(1,2))
plot(x,y)
abline(fm,col=2,lw=2) ; abline(fm1, col=3,lw=3) ; lines(spline(x, fitted(lrf)), col=4,lw=2)    
plot(y, resid(fm), col=2, xlab = "y", ylab = "Residuals")
abline(h=0, lty=2, lw=2)
points(y, resid(lrf), col=4, pch=25)

detach()
rm(fm,fm1,lrf,dum)
par(mfrow=c(1,1))
