rm(list=ls())
source("http://klein.uk/R/myfunctions.R")
ls()
set.seed(107)
u = rnorm(30)*.5
x = runif(30)
y = exp( x + u )
lmLi = lm(y ~ x)
shccm(lmLi)
par(mfrow=c(1,2))
plot(y ~ x, main="Scatter of y against x"); abline(lmLi, col="red", lwd=2)
legend("topleft", "regression line", lwd = 2, col = "red")
plot(lmLi$resid ~ x, main="Residuals against x", ylim=c(-3.5,3.5)); abline(h=0, col="red", lwd=2)
legend("topleft", "mean of residuals (=0)", lwd = 2, col = "red")
lm.dia = lm(lmLi$resid^2 ~ x); shccm(lm.dia)
plot(lmLi$resid^2 ~ x, main="Squared residuals against x")
abline(lm.dia, col="blue", lwd=2)
legend("topleft", "diagnostic regression \n of e^2 on x", lwd = 2, col = "blue")
plot(lmLi$resid^2 ~ lmLi$fitted, main="Squared residuals against fitted values")
abline(lm(lmLi$resid^2 ~ lmLi$fitted), col="blue", lwd=2)
legend("topleft", "diagnostic regression \n of e^2 on y.hat", lwd = 2, col = "blue")
lm.aux = lm(lmLi$resid^2 ~ x)
R.sqr = summary(lm.aux)$r.squared
n = length(x)
T = R.sqr * n
pchisq(q=T, df=1, lower.tail=F)
library(lmtest)
bptest(lmLi)
bptest(lmLi, studentize=F)
bptest(lmLi, ~ x + I(x^2))
lmLoLi = lm(log(y) ~ x); shccm(lmLoLi)
par(mfrow=c(2,2))
plot(y ~ x, main="Scatter of y against x")
abline(lmLi, col="red", lwd=2)
legend("topleft", "linear regression of y on x \n TRUE MODEL: log-linear", lwd = 2, col = "red")
plot(log(y) ~ x, main="Scatter of log(y) against x")
abline(lmLoLi, col="red", lwd=2)
legend("topleft", "log-linear regression of log(y) on x \n TRUE MODEL: log-linear", lwd = 2, col = "red")
plot(lmLi$resid^2 ~ x, main="Linear model")
abline(lm(lmLi$resid^2 ~ x), col="blue", lwd=2)
legend("topleft", "regression of e^2 on x \n TRUE MODEL: log-linear", lwd = 2, col = "blue")
plot(lmLoLi$resid^2 ~ x, ylim=c(0,max(lmLi$resid^2)), main="Log-linear model")
abline(lm(lmLoLi$resid^2 ~ x), col="blue", lwd=2)
legend("topleft", "regression of e^2 on x \n TRUE MODEL: log-linear", lwd = 2, col = "blue")
set.seed(123)
epsilon <- rnorm(1000)
omega <- rnorm(1000)
eta <- rnorm(1000)
x1 <- 5 + omega + 0.3* eta
x2 <- 10 + omega
y <- 20 + x1 + x2 + epsilon
z <- 20 + x1 + x1^2 + epsilon
lm1 <- lm(y ~ x1); lm1$coef
par(mfrow=c(2,2))
plot(y ~ x1, main="General misspecification")
abline(lm1, col="red", lwd=2)
legend("topleft", c("True model: y = b0 + b1*x1 + b2*x2","Estimated: y = b0 + b1*x1"), lwd=2, col=c("white","red"))
plot(lm1$resid ~ x1, main = "Residual plot")
abline(h=0, col="red", lwd=2)
lm(y ~ x1 + x2)$coef
library(lmtest)
resettest(lm1)
lm2 <- lm(z ~ x1); lm2$coef
plot(z~x1, main="Misspecification of functional form")
abline(lm2,col="red", lwd=2)
legend("topleft", c("True model: z = b0 + b1*x1 + b2*x1^2","Estimated: z = b0 + b1*x1"), lwd=2, col=c("white","red"))
plot(lm2$resid~x1, main="Residual plot")
abline(h=0,col="red", lwd=2)
lm(z ~ x1 + I(x1^2))$coef
resettest(lm2)
library(tseries)
par(mfrow=c(1,2))
hist(lmLi$resid, freq=FALSE)
lines(dnorm(seq(-2,4,.1),sd=sd(lmLi$resid)) ~ seq(-2,4,.1), col="blue")
qqnorm(lmLi$resid); qqline(lmLi$resid)
jarque.bera.test(lmLi$res)
jarque.bera.test(lmLoLi$res)