buick <- ifelse(cars$Car=="Buick Estate Wagon",1,0)
f1 <- lm(MPG~Weight+Drive_Ratio+Horsepower+Cylinders, data=cars)
f2 <- lm(MPG~Weight+Drive_Ratio+Horsepower+Cylinders+buick, data=cars)
par(mfrow=c(2,3))
sresid <- function(f){
	h <- influence(f)$hat
	s <- summary(f)$sigma
	se <- s*sqrt(1-h)
	residuals(f)/se
	}
plot(fitted(f1),sresid(f1))
plot(fitted(f2),sresid(f2))
res1a <- residuals(lm(MPG~Drive_Ratio+Horsepower+Cylinders+buick, data=cars))
res1b <- residuals(lm(Weight~Drive_Ratio+Horsepower+Cylinders+buick, data=cars))
plot(res1a~res1b)
res2a <- residuals(lm(MPG~Weight+Horsepower+Cylinders+buick, data=cars))
res2b <- residuals(lm(Drive_Ratio~Weight+Horsepower+Cylinders+buick, data=cars))
plot(res2a~res2b)
res3a <- residuals(lm(MPG~Weight+Drive_Ratio+Cylinders+buick, data=cars))
res3b <- residuals(lm(Horsepower~Weight+Drive_Ratio+Cylinders+buick, data=cars))
plot(res3a~res3b)
res4a <- residuals(lm(MPG~Weight+Drive_Ratio+Horsepower, data=cars))
res4b <- residuals(lm(Cylinders~Weight+Drive_Ratio+Horsepower, data=cars))
plot(res4a~res4b)
#f2 <- lm(I(1/MPG)~Weight+Drive_Ratio+Horsepower+Cylinders, #data=cars)
#plot(fitted(f2),residuals(f2))

x <- rnorm(100,2, 0.3)
z <- rnorm(100,20,5)
y <- rnorm(100,I(exp(3*x)+exp(z/20)),5)
f1 <- lm(y~x+z)
plot(fitted(f1),sresid(f1))
a1 <- residuals(lm(y~z))
a2 <- residuals(lm(x~z))
plot(a2,a1)
b1 <- residuals(lm(y~x))
b2 <- residuals(lm(z~x))
plot(b2,b1)

cor(crime)
f <- lm(R~.,data=crime)
x <- model.matrix(f)[,-1]
e <- eigen(t(x)%*%x)
print(sqrt(e$val[1]/e$val))
r1 <- summary(lm(x[,1]~x[,-1]))$r.squared
print(1/(1-r1))
library(faraway)
print(vif(x))
 
 