Prepare some group responses to the following questions:
First, install R. (If you have difficulties accessing a computer, please let me know). R can be downloaded from https://cran.r-project.org/. You should also download R Studio, a text editor for R. This can be downloaded from https://www.rstudio.com/.
R is a programming language for dealing with data and doing mathematical/statistical analyses. There are a few basic skills that you will need for this task. First, you will need to install R Markdown. To do this, go to File -> New File -> R Markdown… The first time you run this, it will probably download some files that it uses to render your R code into HTML or LaTeX (the beautifully typeset PDF file I showed you). When you load a fresh Markdown file, it will give you a template with some example code. Press “Knit HTML” or “Knit PDF” and it should render the file for you. Note that if you want to render into PDF, it will need to download LaTeX.
The core skill in R is writing programs. These are simply collections of commands that tells the computer what to do. In an R Markdown document, these should be enclosed in single quotes followed by an r (within a line), or three single quotes for multiline expressions.
Let’s launch into some coding. We’ll start easy, just looking at generating some variables and indexing them.
# If you preceed a line with a hash, the computer won't read it
# Let's generate a variable called A and give it a value 45
A <- 45
# Take a look at it
A
## [1] 45
# Now let's generate 20 random numbers from a normal
# distribution with mean 0 and standard deviation 3
B <- rnorm(20, 0, 3)
# Make a time-series plot
plot.ts(B)
# What if I only want to look at the 5'th
# element of B?
B[5]
## [1] -1.420477
# Now let's set the 5th element of B to be 100
B[5] <- 100
# Another time series plot
plot.ts(B)
Cool. What we have seen here is that we can generate numbers and index them. Now let’s introduce a loop. A loop is a way of doing the same thing over and over again—precisely what computers are good at. Let’s take a look at how a loop works in R.
# First we initialise a variable that we're going to fill in
# We use rep(NA, 100), which repeats NA 100 times
output <- rep(NA, 100)
# Take a look
output
## [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [24] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [47] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [70] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [93] NA NA NA NA NA NA NA NA
# Now set an initial value (could be any number)
output[1] <- 1
# now run a for loop
for(t in 2:100){
output[t] <- output[t-1]+ rnorm(1,0,1)
}
plot.ts(output)
What is going on here? What the loop does is cycles i through all values from 2 through 100. Inside the loop, we are using i as an index variable. That is, we’re setting each value of output at iteration i to be its previous value plus some random number with mean 0 and standard deviation 1. Note that when you do this, the line will look different (because you’ll be adding different random numbers to the series).
Suppose that economic output is solely a function of the capital that was installed in the economy at the end of the last period, \(t-1\), \(K_{t-1}\). Output is generated according to a functional form \(Y_{t} = A\times K_{t-1}^{\alpha}\). Here, let our parameters equal \(A=5\), and \(\alpha = 0.4\). We save some proportion \(s\) of economic output, here, let it be \(0.3\). The capital stock depreciates at 7 per cent per year, so \(\delta = 0.07\).
What is the steady state for capital stock in the economy? (Note that you can write mathematical notation in LaTeX formats—there are plenty of guides online to doing this).
Now, suppose that the initial value of \(K\) is 1. Write a for loop that simulates convergence of the capital stock to the steady state. You should plot the series using the function plot.ts(). It should look like this (present your code).
A few tips to writing the loop. First, before setting up the loop, create variables containing your two series and parameters. For your series, initialise these as Y <- rep(NA, 200) and K <- rep(NA, 200). Then set the initial values of your capital and output as above. In the loop, you will want to first generate Y[t], which is a function of K[t-1]. You can then use Y[t] to update K[t].
Well done!
Finally, let’s run a simulation where \(\delta\) actually varies over time. So rather than setting delta as a fixed parameter, you set it as a random number with mean \(0.07\) and standard deviation \(0.05\). This could be interpreted as a world in which the economic value of capital changes with the value that of goods that it produces. How would you model this?
When your work is complete, press “Knit HTML” and in the top right hand corner, press “Publish”. Make sure that your work is final before doing this!. Also, all group members’ names should be in the header of the document. After you have published, post the link to Slack or Facebook.
Happy economising!