setwd("C:/Dokumente und Einstellungen/Thilo/Desktop/Lab_SessionsLT/Data")
source("http://klein.uk/R/myfunctions.R")
ls()
eaef <- read.csv("http://klein.uk/R/Lent/eaef21.csv", header=T)
str(eaef)
attach(eaef)
hist(ASVABC)
summary(ASVABC)
BACH <- ifelse(S > 12, 1, 0)
lm1b <- lm(BACH ~ ASVABC); shccm(lm1b)
glm1b <- glm(BACH ~ ASVABC, family=binomial(link=logit))
summary(glm1b)
par(mfrow=c(1,2))
plot(lm1b$fitted ~ ASVABC)
plot(glm1b$fitted ~ ASVABC)
M <- model.matrix(glm1b)
str(data.frame(M))
F.log <- function(x){
1/(1 + exp(-x))
}
f.log <- function(x){
F.log(x)*(1-F.log(x))
}
M[,2] <- 40; glm1b$coef[2] * f.log(mean(M%*%glm1b$coef))
library(stats)
M[,2] <- 55; glm1b$coef[2] * dlogis(mean(M%*%glm1b$coef))
glm1b$coef[2] * dlogis(c(1,70)%*%glm1b$coef)
par(mfrow=c(1,2))
plot(BACH ~ ASVABC, ylim=c(-1,2))
lm1e <- lm(BACH ~ ASVABC)
abline(lm1e, col="red")
plot(lm1e$res ~ ASVABC)
abline(h=mean(lm1e$res), col="red")
glm1f.null <- glm(BACH ~ 1, family=binomial(link=logit))
1 - logLik(glm1b)[1]/logLik(glm1f.null)[1]
loan <- read.csv("http://klein.uk/R/Lent/loanapp.csv", header=T)
str(loan)
glm2a <- glm(approve ~ white, family=binomial(link=probit), data=loan)
summary(glm2a)
pnorm(c(1,1)%*%glm2a$coef)
pnorm(c(1,0)%*%glm2a$coef)
lm2a <- lm(approve ~ white, data=loan)
shccm(lm2a)
c(1,1)%*%lm2a$coef
c(1,0)%*%lm2a$coef
by(loan$approve, loan$white, mean)
glm2b <- glm(approve ~ white + hrat + obrat + loanprc + unem + male + married + dep
+ sch + cosign + chist + pubrec + mortlat1 + mortlat2 + vr
, family=binomial(link=probit), data=loan)
summary(glm2b)
glm2b2 <- glm(approve ~ white + hrat + obrat + loanprc + unem + male
+ sch + cosign + chist + pubrec + mortlat1 + mortlat2 + vr
, family=binomial(link=probit), data=subset(loan, is.na(dep)==F))
D <- 2*( logLik(glm2b)[1] - logLik(glm2b2)[1] )
pchisq(q=D, df=2, lower.tail=F)
M<-model.matrix(glm2b)
str(data.frame(M))
x<-seq(0,3,0.01)
y<-rep(NA,length(x))
for(i in 1:length(x)){
M[,5]<-x[i]
y[i]<- pnorm(mean(M%*%coef(glm2b)))
}
plot(y~x,type="l",ylab="P(approve=1)",xlab="loanprc", col="red", lwd=2)
points(rep(0.2, length(loan$loanprc)) ~ loan$loanprc, pch="|")
glm2b$coef[9] * dnorm(mean(M%*%coef(glm2b)))
glm2b$coef[2] * dnorm(mean(M%*%coef(glm2b)))
str(data.frame(M))
M1 <- M0 <- M
M1[,2] <- 1; M0[,2] <- 0
pnorm(mean(M1%*%coef(glm2b))) - pnorm(mean(M0%*%coef(glm2b)))
success <- ifelse(glm2b$fitted > 0.5, "success", "reject")
t <- table(success, glm2b$model$approve)
t/sum(t)
glm2c.logit <- glm(approve ~ white + hrat + obrat + loanprc + unem + male + married + dep
+ sch + cosign + chist + pubrec + mortlat1 + mortlat2 + vr
, family=binomial(link=logit), data=loan)
glm2c.logit; glm2b
glm2c.logit$coef[2]*0.625; glm2b$coef[2]
lm2d <- lm(approve ~ white + hrat + obrat + loanprc + unem + male + married + dep
+ sch + cosign + chist + pubrec + mortlat1 + mortlat2 + vr, data=loan)
data <- data.frame(mean.approve=glm2b$model$approve, mean.app_LPM=lm2d$fitted, mean.app_probit=glm2b$fitted, mean.app_logit=glm2c.logit$fitted)
INDICES <- data.frame(dependencies=glm2b$model$dep)
FUN <- mean
by(data, INDICES, FUN)
obrat.cat <- cut(glm2b$model$obrat, breaks=quantile(loan$obrat, seq(0,1,.1)))
fringe <- read.csv("http://klein.uk/R/Lent/FRINGE.csv", header=T)
str(fringe)
nopension <- ifelse(fringe$pension==0, 1, 0)
table(nopension)
summary(fringe$pension[nopension==0])
lm3b <- lm(pension ~ exper + age + tenure + educ + depends + married + white + male
, data=fringe)
library(VGAM)
vglm3b <- vglm(pension ~ exper + age + tenure + educ + depends + married + white + male
, data=fringe, tobit(Lower=0), trace=TRUE)
summary(vglm3b)
vglm3b2 <- vglm(pension ~ exper + age + tenure + educ + depends + married
, data=fringe, tobit(Lower=0), trace=TRUE)
D <- 2*( logLik(vglm3b)[1] - logLik(vglm3b2)[1] )
pchisq(q=D, df=2, lower.tail=F)
xb <- sum( vglm3b@coefficients[c(1,3:10)] * c(1, 10, 35, 10, 16, 0, 0, 1, 1) )
sigma2 <- exp(vglm3b@coefficients[2])
pnorm(xb/sigma2)*xb + sigma2*dnorm(xb/sigma2)
xb <- sum( vglm3b@coefficients[c(1,3:10)] * c(1, 10, 35, 10, 16, 0, 0, 0, 0) )
sigma2 <- exp(vglm3b@coefficients[2])
pnorm(xb/sigma2)*xb + sigma2*dnorm(xb/sigma2)
sum(lm3b$coef[8:9])
vglm3d <- vglm(pension ~ exper + age + tenure + educ + depends + married + white + male
+ union, data=fringe, tobit(Lower=0), trace=TRUE)
summary(vglm3d)
vglm3e <- vglm(peratio ~ exper + age + tenure + educ + depends + married + white + male
+ union, data=fringe, tobit(Lower=0), trace=TRUE)
summary(vglm3e)
mroz <- read.csv("http://klein.uk/R/Lent/mroz.csv", header=T)
str(mroz)
mroz.w <- subset(mroz, inlf==1)
lm4a <- lm(lwage ~ educ + exper + expersq + nwifeinc + age + kidslt6 + kidsge6, data=mroz.w)
summary(lm4a)
library(sampleSelection)
mroz$lfp <- as.logical(mroz$inlf)
heck4b <- heckit(selection = lfp ~ exper + expersq + nwifeinc + age + kidslt6 + kidsge6
, outcome = lwage ~ educ + exper + expersq + nwifeinc + age + kidslt6 + kidsge6
, data=mroz, method="2step")
summary(heck4b)
invMills <- heck4b$invMillsRatio
lm4c <- lm(invMills[mroz$inlf==1]
~ educ + exper + expersq + nwifeinc + age + kidslt6 + kidsge6, data=mroz.w)
summary(lm4c)
heck4d <- heckit(selection = lfp ~ exper + expersq + nwifeinc + age + kidslt6 + kidsge6
, outcome = lwage ~ educ + exper + expersq
, data=mroz, method="2step")
summary(heck4d)
fert <- read.csv("http://klein.uk/R/Lent/FERTIL1.csv", header=T)
str(fert)
fert$year <- as.factor(fert$year)
lm1a <- lm(kids ~ educ + age + agesq + black + east + northcen + west + farm + othrural
+ town + smcity + year, data=fert)
shccm(lm1a)
hc0 <- function(x) vcovHC(x, type = "HC0")
hc1 <- function(x) vcovHC(x, type = "HC1")
library(sandwich)
lht(lm1a, c("east=0","northcen=0","west=0"), vcov=hc0)
lht(lm1a, c("farm","othrural","town","smcity"), vcov=hc0)
lm1d <- lm(lm1a$res^2 ~ year, data=fert)
shccm(lm1d)
lht(lm1d, c("year74","year76","year78","year80","year82","year84"),vcov=hc0)
lm1e <- lm(kids ~ educ + age + agesq + black + east + northcen + west + farm + othrural
+ town + smcity + year + educ:year, data=fert)
shccm(lm1e)
lht(lm1e, c("educ:year74","educ:year76","educ:year78","educ:year80","educ:year82"
,"educ:year84"),vcov=hc0)
murder <- read.csv("http://klein.uk/R/Lent/MURDER.csv", header=T)
str(murder)
murder$id <- as.factor(murder$id)
murder$year <- as.factor(murder$year)
murder.y <- subset(murder, d90==1 | d93==1)
lm1b <- lm(mrdrte ~ d93 + exec + unem, data=murder.y)
shccm(lm1b)
lm2c.LSDV <- lm(mrdrte ~ d93 + exec + unem + id, data=murder.y)
summary(lm2c.LSDV)
library(plm)
plm2c.within <- plm(mrdrte ~ d93 + exec + unem, data=murder.y
, model="within", effect="individual", index=c("id","year"))
summary(plm1c.within)
plm2c.fd <- plm(mrdrte ~ d93 + exec + unem, data=murder.y
, model="fd", effect="individual", index=c("id","year"))
summary(plm2c.fd)
coeftest(lm2c.LSDV, vcov=hc0)
coeftest(plm2c.within, vcov=hc0)
tail( murder[order(murder$year, murder$exec),] )
plm2f.within <- plm(mrdrte ~ d93 + exec + unem, data=subset(murder.y, state!="TX")
, model="within", effect="individual", index=c("id","year"))
coeftest(plm2f.within); coeftest(plm2f.within, vcov=hc0)
plm2g.within <- plm(mrdrte ~ exec + unem, data=murder
, model="within", effect="twoways", index=c("id","year"))
coeftest(plm2g.within)
coeftest(plm2g.within, vcov=hc0)
wage <- read.csv("http://klein.uk/R/Lent/wagepan.csv",header=T)
str(wage)
waget <- pdata.frame(wage, c("nr","year"))
head(waget)
summary(waget)
wage$nr <- as.factor(wage$nr)
wage$year <- as.factor(wage$year)
wage.sub <- subset(wage, select=c("lwage","educ","black","hisp","exper","married","union"))
for(i in 1:7){
m <- mean(wage.sub[,i])
s.o <- sd(wage.sub[,i])
s.b <- sd(by(wage.sub[,i], wage$nr, mean))
s.w <- sd( unlist( by( wage.sub[,i], wage$nr, function(x) x - mean(x) ) ) )
cat("------------------------------------------ \n")
cat("Variable:", names(wage.sub)[i], "\n")
print(round( data.frame(Mean=m, SE.overall=s.o, SE.between=s.b, SE.within=s.w), 3))
cat("\n")
}
lm3b <- lm(lwage ~ year + educ + black + hisp + exper + married + union, data=wage)
shccm(lm3b)
e <- lm3b$res
e_1 <- unlist( by(e, wage$nr, function(x) c(NA, x[-length(x)])) )
e_2 <- unlist( by(e, wage$nr, function(x) c(NA, NA, x[-c(7:8)])) )
e_3 <- unlist( by(e, wage$nr, function(x) c(NA, NA, NA, x[-c(6:8)])) )
e_4 <- unlist( by(e, wage$nr, function(x) c(rep(NA,4), x[-c(5:8)])) )
C <- cbind(e, e_1, e_2, e_3, e_4)
library(Hmisc)
rcorr(C)
acf(e)
corstars <- function(x){
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .01, "***", ifelse(p < .05, "** ", ifelse(p < .1, "* ", " ")))
R <- format(round(R, 3))
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- colnames(x)
Rnew[upper.tri(Rnew,diag=TRUE)] <- ""
Rnew <- data.frame(Rnew)[-1,-length(colnames(x))]
return(Rnew)
}
corstars(C)
library(corrgram)
corrgram(C, order=FALSE, lower.panel=panel.ellipse,
upper.panel=panel.pts, text.panel=panel.txt,
diag.panel=panel.minmax,
main="Individual Level Part-Worths")
library(ellipse)
corC <- cor(C, use="complete")
colors <- c("#A50F15","#DE2D26","#FB6A4A","#FCAE91","#FEE5D9","white",
"#EFF3FF","#BDD7E7","#6BAED6","#3182BD","#08519C")
plotcorr(corC, col=colors[5*xc + 6], type = "lower")
library(lmtest)
coeftest(lm3b)
clx(lm3b, 1, wage$nr)
lm3d.ols <- plm(lwage ~ year + educ + black + hisp + exper + married + union
, data=wage, model="pooling", index=c("nr","year"))
coeftest(lm3d.ols)
coeftest(lm3d.ols, vcov=pvcovHC(lm3d.ols, method="arellano"))
coeftest(lm3d.ols)
lm3e.re <- plm(lwage ~ year + educ + black + hisp + exper + married + union
, data=wage, model="random", effect="individual", index=c("nr","year"))
summary(lm3e.re)
lm3e.fe <- plm(lwage ~ year + educ + black + hisp + exper + married + union
, data=wage, model="within", effect="individual", index=c("nr","year"))
coeftest(lm3e.fe)
phtest(lm3e.fe, lm3e.re)
plmtest(lm3e.ols, type="bp")
lm3e.fd <- plm(lwage ~ year + educ + black + hisp + exper + married + union
, data=wage, model="fd", effect="individual", index=c("nr","year"))
coeftest(lm3e.fd)
e <- lm3e.fd$res
acf(e)
lm3e.b <- plm(lwage ~ year + educ + black + hisp + exper + married + union
, data=wage, model="between", effect="individual", index=c("nr","year"))
coeftest(lm3e.b)
save.image("EndOfSession.RData")
q("yes")