# Applied Econometrics with R # Kleiber, Christian, Zeileis, Achim 2008 # http://www.springer.com/978-0-387-77316-2 # Install packages install.packages("AER") # The demand for economics journals data("Journals", package = "AER") dim(Journals) names(Journals) head(Journals) plot(log(subs) ~ log(price/citations), data = Journals) j_lm <- lm(log(subs) ~ log(price/citations), data = Journals) summary(j_lm) abline(j_lm) # regression Journals$citeprice = Journals$price/Journals$citations jour_lm = lm(log(subs) ~ log(citeprice), data = Journals) jour_slm = summary(jour_lm) jour_slm$coefficients # point and interval estimates jour_lm$coefficients confint(jour_lm, level = 0.99) # linear hypothesis library(car) linear.hypothesis(jour_lm, "log(citeprice) = -.5") # multiple regression # Determinants of wages data("CPS1985", package = "AER") cps <- CPS1985 library("quantreg") cps_lm <- lm(log(wage) ~ experience + I(experience^2) + education, data = cps) cps_rq <- rq(log(wage) ~ experience + I(experience^2) + education, data = cps, tau = seq(0.2, 0.8, by = 0.15)) summary(cps_lm) summary(cps_rq) plot(summary(cps_rq)) # R ls() rm(qq) ?lm # plot(1:20, pch = 1:20, col = 1:20, cex = 2) curve(dnorm, from = -5, to = 5, col = "slategray", lwd = 3, main = "Density of the standard normal distribution") text(-5, 0.3, expression(f(x) == frac(1, sigma ~~ sqrt(2*pi)) ~~ e^{-frac((x - mu)^2, 2*sigma^2)}), adj = 0) curve(x^2) # Data data("CPS1985", package = "AER") head(CPS1985) attach(CPS1985) par(mfrow=c(2,1)) hist(wage, freq = FALSE) hist(log(wage), freq = FALSE) lines(density(log(wage)), col = 4) # One categorical variable summary(occupation) tab <- table(occupation) prop.table(tab) par(mfrow=c(1,2)) barplot(tab) pie(tab) # Two categorical variables xtabs(~ gender + occupation, data = CPS1985) plot(gender ~ occupation, data = CPS1985) # Nemerical tapply(log(wage), gender, mean) plot(log(wage) ~ education) plot(log(wage) ~ gender) detach(CPS1985)