Multiple Regression Analysis (Chapter 3)

Below you find the script for all examples in chapter 3 of Wooldridge (2013). The only difference to the last chapter is how to use multiple independent variables in the lm-function. This is easily achieved by separating each exogenous variable that should enter the model with a plus sign (e.g. lm(colGPA ~ hsGPA + ACT)).

The other commands that appear new are quite helpful and you should take a minute to think about them.

  • rm(list=ls())“: Sometimes it might be helpful to keep your memory clean in order to keep an overview of the data you use. This command removes positions from the window in the upper right, i.e. it deletes data that R temporarily stored in your memory. “list” tells R that you are going to delete more than one items. “ls()” is the so-called “environment” that you want to delete. In our case this is a quite big one. If you are interested type “ls(1)”, “ls(2)”, etc. to see what happens.
  • #“: This is the sign which you put in front of a script line to indicate that the rest of the line is a comment. R takes it as text which is not executed. Whenever you work with scripts you should always comment what you do in order to make it easier for others to understand how your script works and for yourself to know later what you did some time ago.
setwd('...')

rm(list=ls())

library(foreign)

# 3.1
# http://ideas.repec.org/p/boc/bocins/gpa1.html
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/gpa1.dta','gpa1.dta',mode='wb')
gpa1 <- read.dta('gpa1.dta')

lm.1 <- lm(colGPA ~ hsGPA + ACT, data = gpa1)
summary(lm.1)

lm.2 <- lm(colGPA ~ ACT, data=gpa1)
summary(lm.2)

# 3.2
rm(list=ls())
wage1 <- read.dta('wage1.dta')

lm.1 <- lm(lwage ~ educ + exper + tenure, data=wage1)
summary(lm.1)

# 3.3
rm(list=ls())
# http://ideas.repec.org/p/boc/bocins/401k.html
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/401k.dta','401k.dta',mode='wb')
a401k <- read.dta('401k.dta')

lm.1 <- lm(prate ~ mrate + age, data=a401k)
summary(lm.1)

# 3.4
rm(list=ls())
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/gpa1.dta','gpa1.dta',mode='wb')
gpa1 <- read.dta('gpa1.dta')

lm.1 <- lm(colGPA ~ hsGPA + ACT, data=gpa1)
summary(lm.1)

# 3.5
rm(list=ls())
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/crime1.dta','crime1.dta',mode='wb')
crime1 <- read.dta('crime1.dta')

lm.1 <- lm(narr86 ~ pcnv + ptime86 + qemp86, data=crime1)
lm.2 <- lm(narr86 ~ pcnv + avgsen + ptime86 + qemp86, data=crime1)
summary(lm.1)
summary(lm.2)

# 3.6
rm(list=ls())
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/wage1.dta','wage1.dta',mode='wb')
wage1 <- read.dta('wage1.dta')

lm.1 <- lm(lwage ~ educ, data=wage1)
summary(lm.1)

Regression through the origin (no intercept)

This is achieved by adding “-1” into the formula of the lm()-command. But only do this if you have a good reason for that, e.g. the population model goes through the origin.

lm.1 <- lm(narr86 ~ pcnv + ptime86 + qemp86 - 1, data=crime1)

An “alternative” way to obtain slope coefficients
The textbook (p. 74) mentions an additional way to get slope coefficients: Dividing the sum of the residuals of the regression of x1 on all the other independent variables multiplied by the outcome variable (dep. variable) throught the sum of those squared residuals. To express and check this in R should pose no problem since we already know commands that are needed for that:

# http://ideas.repec.org/p/boc/bocins/gpa1.html
download.file('http://fmwww.bc.edu/ec-p/data/wooldridge/gpa1.dta','gpa1.dta',mode='wb')
gpa1 <- read.dta('gpa1.dta')

lm.1 <- lm(colGPA ~ hsGPA + ACT, data=gpa1)
summary(lm.1)

lm.x <- lm(hsGPA ~ ACT, data=gpa1)
sum(lm.x$residuals*gpa1$colGPA)/sum(lm.x$residuals^2)

Compare the coefficients on hsGPA and you will find them to be the same.

Admittedly, this was kind of boring. Let’s see if chapter 4 is more exciting.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s