source("http://klein.uk/R/myfunctions.R")
ls()
set.seed(123)
x <- runif(1000)
e <- rnorm(1000)
par(mfrow=c(1,2))
y.hom <- x + e
lm.hom <- lm(y.hom ~ x)
plot(y.hom ~ x, ylim=c(-2,3)); abline(lm.hom, col="red", lwd=2)
y.het <- x + e*x
lm.het <- lm(y.het ~ x)
plot(y.het ~ x, ylim=c(-2,3)); abline(lm.het, col="red", lwd=2)
vcov(lm.het)
??hccm
library(car)
hccm(lm.het, type="hc0")
hccm(lm.het, type="hc3")
source("http://klein.uk/R/myfunctions.R")
summary(lm.hom)
shccm(lm.hom)
summary(lm.het)
shccm(lm.het)
rm(x, e, y.hom, y.het, lm.hom, lm.het)
oilprice1 <- read.csv("http://klein.uk/R/oilprice1",header=T,sep=",")
str(oilprice1)
plot(price ~ api, data=oilprice1)
lm1 <- lm(price ~ api, data=oilprice1)
summary(lm1)
t <- qt(p=0.975, df=13-2)
str(lm1)
b <- lm1$coef[2]
str(summary(lm1))
se_b <- summary(lm1)$coef[2,2]
b + c(-1,1)*se_b*t
ls(); rm(b, t, se_b)
confint(object=lm1, level=0.95)
set.seed(123)
epsilon <- rnorm(10000)
omega <- rnorm(10000)
eta <- rnorm(10000)
zeta <- rnorm(10000)
x1 <- 5 + omega + 0.3* eta
x2 <- 10 + omega
x3 <- 5 + eta
y <- 20 + x1 + x2 + epsilon
z <- 30 + x2 + x3 + zeta
hist(qnorm(runif(10000)), freq=F)
grid.x <- seq(-4,4,.1)
grid.y <- dnorm(grid.x)
lines(grid.x, grid.y, col="blue", lwd=2)
cor(cbind(x1, x2, x3))
lm2b <- lm(y ~ x1 + x2)
shccm(lm2b)
vif(lm2b)
lm2c <- lm(z ~ x2 + x3)
shccm(lm2c)
vif(lm2c)
lm(y ~ x1)$coef
lm(y ~ x1 + x2)$coef
lm(z ~ x2)$coef
lm(z ~ x2 + x3)$coef
gas <- read.csv("http://klein.uk/R/gasoline", header=T)
str(gas)
lm4 <- lm(log(gasoline) ~ log(gdp) + log(price), data=gas); summary(lm4)
library(car)
linearHypothesis(model=lm4, "log(gdp)=1")
metal <- read.table("http://klein.uk/R/usmetal.txt", header=T)
str(metal)
for(i in 1:3){
metal[,i+3] <- log(metal[,i])
}
names(metal)[4:6] <- c("lK","lL","lY")
str(metal)
par(mfrow=c(3,2))
for(i in 1:6){
name <- names(metal)[i]
hist(metal[,i], main=paste("Histogram of", name) , xlab=name)
}
lm5c <- lm(lY ~ lK + lL, data=metal); summary(lm5c)
par(mfrow=c(2,2)); plot(lm5c)
linearHypothesis(lm5c, "lK=lL")
linearHypothesis(lm5c, "lK + lL = 1")
lm5f <- lm(lY ~ I(lL - lK), offset=lK, data=metal); summary(lm5f)
anova(lm5c, lm5f)
bank <- read.csv("http://klein.uk/R/bank", header=T)
str(bank)
bank$logsalbegin <- log(bank$salbegin)
lm6i <- lm(logsal ~ educ + logsalbegin + gender + minority, data=bank); shccm(lm6i)
bank$female <- 1 - bank$male
bank$femaleandminority <- bank$female * bank$minority
lm5ii <- lm(logsal ~ educ + male + minority + femaleandminority, data=bank)
lm(logsal ~ educ + male + minority + female:minority, data=bank)
lm6iii <- lm(logsal ~ educ + female + minority , data=bank); shccm(lm6iii)
linearHypothesis(lm6iii, "educ = 0.07")
linearHypothesis(lm6iii, c("educ = 0.07", "female = minority"))
q("no")