## -------------------- ##

## Regression Tutorial  ##

## Author: Tony Cookson ##

## Date: 28 Feb 2011    ##

## -------------------- ##

## ------------ ##

## Read in Data ##

## ------------ ##

library(foreign)  ## Package for Stata Data Files

earndta = read.dta("C://R//educearnbig.dta")  ## See Video R2 for more

## ------------ ##

## Explore Data ##

## ------------ ##

names(earndta)       ## Lists variable names

summary(earndta)     ## Basic summary statistics by variable

length(earndta[,1])  ## Counts number of observations

## ----------------- ##

## Simple Regression ##

## ----------------- ##

earnreg1 = lm(wage~educ, data = earndta)  ## "lm()" for "linear model"

summary(earnreg1)     ## Table of Output

anova(earnreg1)       ## Anova Table

## ---------------------- ##

## Reg without a constant ##

## ---------------------- ##

earnreg2 = lm(wage~0+educ, data = earndta)

earnreg3 = lm(wage~educ-1, data = earndta)

summary(earnreg2)

summary(earnreg3)

## ------------------------------------- ##

## Transformations using the I() wrapper ##

## ------------------------------------- ##

earnreg3 = lm(wage~I(educ-1), data = earndta)

summary(earnreg3)

earnreg3 = lm(log(wage) ~ educ, data = earndta)

summary(earnreg3)

## Advantage of transforming this way: don't accumulate "junk variables"

## -------------------- ##

## Multiple Regression  ##

## -------------------- ##

earnreg4 = lm(wage~educ+exper, data=earndta) ## Use "+" sign

summary(earnreg4)   ## An extra row for each extra variable (typical)

## ----------------------------- ##

## Regression with Interactions  ##

## ----------------------------- ##

earnreg5 = lm(wage~educ*exper,data = earndta)  ## full interaction

summary(earnreg5)

earnreg6 = lm(wage~educ+educ:exper, data = earndta) ## partial interaction

summary(earnreg6)