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, "hc0")
summary(lm.het)
shccm(lm.het)
rm(x, e, y.hom, y.het, lm.hom, lm.het)
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))
lm1b <- lm(y ~ x1 + x2)
shccm(lm1b)
vif(lm1b)
lm1c <- lm(z ~ x2 + x3)
shccm(lm1c)
vif(lm1c)
lm(y ~ x1)$coef
lm(y ~ x1 + x2)$coef
lm(z ~ x2)$coef
lm(z ~ x2 + x3)$coef
salary <- read.table("http://klein.uk/R/salary.txt", header=T, sep=" ")
str(salary)
lm2 <- lm(LOGSAL ~ EDUC + LOGSALBEGIN + GENDER + MINORITY, data=salary)
shccm(lm2)
vif(lm2)
lm2ed <- lm(EDUC ~ LOGSALBEGIN + GENDER + MINORITY, data=salary)
print( paste( "The R2 is", summary(lm2ed)$r.squared ) )
print( paste( "variance inflation factor is", 1/(1- summary(lm2ed)$r.squared) ) )
cor(subset(salary, select=c("EDUC","LOGSALBEGIN","GENDER","MINORITY")))
attach(salary)
cor(cbind(EDUC, LOGSALBEGIN, GENDER, MINORITY))
detach(salary)
eaef21 <- read.csv("http://klein.uk/R/eaef21", header=T)
str(eaef21)
lm3w <- lm( SIBLINGS ~ SM + SF, data = eaef21[eaef21$ETHWHITE==1, ] )
lm3b <- lm( SIBLINGS ~ SM + SF, data = eaef21[eaef21$ETHBLACK==1, ] )
lm3h <- lm( SIBLINGS ~ SM + SF, data = eaef21[eaef21$ETHHISP==1, ] )
lm3w; lm3b; lm3h
library(lmtest); library(zoo); library(sandwich)
lm3w <- lm(SIBLINGS ~ SM + SF, data=eaef21[eaef21$ETHWHITE==1, ]); coeftest(lm3w, vcov=hc0)
cor( cbind(eaef21$SM, eaef21$SF)[eaef21$ETHWHITE==1, ] )
vif(lm3w)
lm3b <- lm( SIBLINGS ~ SM + SF, data=subset(eaef21, ETHBLACK ==1) ); coeftest(lm3b, vcov=hc0)
cor( subset(eaef21, ETHBLACK ==1, select=c(SM, SF)) )
vif(lm3b)
lm3h <- lm( SIBLINGS ~ SM + SF, data= subset(eaef21, ETHHISP==1) ); coeftest(lm3h, vcov=hc0)
cor( subset(eaef21, ETHHISP ==1, select=c(SM, SF)) )
vif(lm3h)
lm3.1 <- lm(SIBLINGS ~ SM + SF, data=eaef21); shccm(lm3.1)
linearHypothesis(model=lm3.1, "SM = SF", vcov=hc0)
eaef21$SP <- eaef21$SF + eaef21$SM
lm3.2 <- lm(SIBLINGS ~ SP, data=eaef21); shccm(lm3.2)
e <- rnorm(n=100, mean=1, sd=1)
x <- runif(100)
y <- 2*x + e
OLS <- lm(y ~ x); shccm(OLS)
par(mfrow=c(2,2))
plot(OLS)
bank <- read.csv("http://klein.uk/R/bank", header=T)
str(bank)
bank$logsalbegin <- log(bank$salbegin)
lm5i <- lm(logsal ~ educ + logsalbegin + gender + minority, data=bank); shccm(lm5i)
bank$jobcat.cler <- ifelse(bank$jobcat==1, 1, 0)
bank$jobcat.cust <- ifelse(bank$jobcat==2, 1, 0)
bank$jobcat.man <- ifelse(bank$jobcat==3, 1, 0)
lm5iia <- lm(logsal ~ educ + male + minority + jobcat.cler + jobcat.cust, data=bank); shccm(lm5iia)
bank$jobcat.fac <- factor(bank$jobcat)
levels(bank$jobcat.fac) <- c("Clerical", "Custodial", "Managerial")
lm5iib <- lm(logsal ~ educ + male + minority + jobcat.fac, data=bank); shccm(lm5iib)
bank$jobcat.fac <- relevel(bank$jobcat.fac, ref="Managerial")
linearHypothesis(model=lm5iia, "jobcat.cler = jobcat.cust", vcov=hc0)
bank$educ.cut <- cut(bank$educ, breaks=3)
bank$educ.cut <- cut(bank$educ, breaks=c(0,12,16,Inf))
write.csv(bank, "bank_cat.csv")
table(bank$jobcat.fac)
lm(logsal ~ educ + male + minority, data=subset(bank, jobcat==2) )
lm(logsal ~ educ + male + minority, data=subset(bank, jobcat==3) )
sum(bank$male[bank$jobcat==2])
bank$female <- 1 - bank$male
bank$femaleandminority <- bank$female * bank$minority
lm5iv <- lm(logsal ~ educ + male + minority + femaleandminority, data=bank)
lm(logsal ~ educ + male + minority + female:minority, data=bank)
linearHypothesis(model=lm5iv, "male = minority", vcov=hc0)
lm5vi <- lm(logsal ~ educ + female + minority , data=bank); shccm(lm5vi)
linearHypothesis(lm5vi, "educ = 0.07", vcov=hc0)
linearHypothesis(lm5vi, c("educ = 0.07", "female = minority"), vcov=hc0)
lm(logsal ~ educ + female + minority + logsalbegin + logsalbegin:jobcat.man, data=bank)
no2poll <- read.csv("http://klein.uk/R/no2pollution", header=T)
str(no2poll)
lm6 <- lm(lno2 ~ lcars + temp + tchng23 + wndspd + wnddir + day, data=no2poll); shccm(lm6)
linearHypothesis(lm6, "wndspd = wnddir", vcov=hc0)
data <- read.csv("http://klein.uk/R/dataset", header=T)
str(data)
q("no")