Lab 2 - Assignment 1 hints
This tutorial will go through all the code you need for assignment 1.
Simple arithmetic
You can perform arithmetic to saved objects:
a <- 2
b <- 3
c <- 4
a + b * c
## [1] 14
Load data
For an example dataset, I will load the Cobb-Douglas data directly from the internet:
mydat <- read.csv("https://rtgodwin.com/data/cobbdouglas.csv")
Note that you can load any .csv
dataset this way, by typing the
location of the file in the quotations above.
Calculate a sample mean
The sample mean for my dependent variable ,log(output)
, is:
mean(log(mydat$output))
## [1] 9.770502
Note that the mydat$
part is telling R where to find the variable
output
.
Estimate and save a model
I will estimate the Cobb-Douglas production function, and save it as an “object”. I can choose any name for the object (I choose “mymod”):
mymod <- lm(log(output) ~ log(labour) + log(capital), data = mydat)
Use the estimated model
To see the results of the estimation you can use summary(mymod)
to see
lots of information, but if you just want to see the estimated β,
run mymod
:
mymod
##
## Call:
## lm(formula = log(output) ~ log(labour) + log(capital), data = mydat)
##
## Coefficients:
## (Intercept) log(labour) log(capital)
## 0.1768 0.2652 0.9121
To get, and save, the LS residuals from the estimated model, use:
myresids <- residuals(mymod)
To get the estimated coefficients from the model use:
coefficients(mymod)
## (Intercept) log(labour) log(capital)
## 0.1768209 0.2651853 0.9121483
and to get only the estimate for labour (for example), extract the 2nd element:
coefficients(mymod)[2]
## log(labour)
## 0.2651853
Estimation without an intercept
Usually, it’s a good idea to include an intercept in the model, but
sometimes we don’t want it. R includes an intercept by default. To get
rid of the intercept, put -1
at the end of the equation:
lm(output ~ labour + capital -1, data = mydat)
##
## Call:
## lm(formula = output ~ labour + capital - 1, data = mydat)
##
## Coefficients:
## labour capital
## 42.914 1.621
(I omitted the log
for simplicity).