# Overview

This is the first lab of Economic Demography (Econ/Demog C175). Our goals in this first lab are:

1. To get everyone started programming and doing their assignments with R, RStudio, and bCourses.

2. To use the exponential model to learn about world population history

This document is written in the “R markdown” format and should be read and edited within RStudio. The notebook interface allows you to execute and display R-code within a single window. You can edit this notebook directly and save it. We recommend you save it with a different name (e.g., “world_lab_1_newname.Rmd”) to avoid overwriting your edits.

Viewing note: the labs are formatted using hard line-endings. Try resizing viewing window if lines are overflowing.

# Writing and executing R commands

(Note: In addition to this introduction, make sure to read “The Rstudio 175 cloud server for Demog/Econ C175” by Carl Mason available at [https://courses.demog.berkeley.edu/goldstein175], which provides additional information.)

In the RStudio Notebook, we mix executable R commands in with regular written text by creating a “code chunk.” To open a code chunk, we type “”{r}" to start. To close the code chunk we type "“” to end. Here are some examples:

To print a string of text:

print("Hello, world")
[1] "Hello, world"
test <- "xd"
test
[1] "xd"

You can hit the green “play” icon to execute the chunk of code. Try modifying the chunk so it prints: “Hello, Berkeley”. (Note the [1] just means that it is the first element of the displayed object. You can ignore this.)

2+2
[1] 4

To assign the value 4 to a variable named “x”:

x <- 4
print(x)
[1] 4

The output of a chunk is only shown when you tell it to be. For example,

x <- 3
print(x)
[1] 3
y <- 4
y
[1] 4
## note: we don't tell R to show us the value of "y"
z <- 5
print(z)
[1] 5

Try to modify the above code chunk so it also prints the value the variable “y”

Which of the following is true for you? (Please answer this on a [google forms] (https://docs.google.com/forms/d/e/1FAIpQLSe7qGmTz_b_rt2olxxeEHCquCPLo2nx5zdvH54O1q3iOeTv3A/viewform) for us – we’re doing it on google right now so as to give everyone access. The graded questions at the bottom of the lab will need to be answered on bCourses).

A. I was able to login to the Rstudio 175 cloud server and change my password and run a chunk of R.

B. I was able to login but am having problems. I know that I can come to office hours and get help. I also know that I can get help this Friday, January 19 from 1-3 in the Demography Dept at 2232 Piedmont Avenue (across from the stadium).

C. I was not able to login to the Rstudio 175 cloud server. I will come to get help on Friday (see B).

D. I was not able to login to the Rstudio 175 cloud server and I am one of very few people who can’t come to the Friday help session. I am emailing Dr. Mason at cmason@berkeley.edu for help.

# Using R to calculate exponential population growth rates

## comments (within code chunks) begin with hashtags. They are ignored
## by R.
## We start by inputting world population sizes by hand, assigning
## them to variables named "N.1900" and "N.2000" using the assignment
## operator: "<-"
N.1900 <- 1650 # estimated world population in 1900 (in millions)
N.2000 <- 6127 # same for 2000
## we can display the value of the variable called N.1900 (hit the
## "play" button)
print(N.2000)
[1] 6127
## We now calculate growth rate from 1900 to 2000, using our formula
## for the slope of the logarithm of population.
R.twentieth.century <- ( log(N.2000) - log(N.1900) ) / (2000 - 1900)
R.twentieth.century
[1] 0.0131193

You should get “[1] 0.0131193”, or about 1.3 percent.

N.2000.check <- N.1900 * exp(100 * 0.0131193)
N.2000.check
[1] 6127

Which is “6127”, the correct value for the world population in 2000.

Now let’s calculate the exponential growth rate for the fifty years from 1850 to 1900. You need to alter the code below to get the right answer.

N.1850 <- 1262 # these are millions
## Note: We don't need to re-enter N.1900, since variables are saved
## across chunks.
## Now modify the code below to give the right answer. (Hint: you need
## to rename the variables N.2000 and N.1900 and change the dates
## "2000" and "1900".)
R.1850.to.1900 <- ( log(N.1900) - log(N.1850) ) / (1900 - 1850)
print( R.1850.to.1900 )
[1] 0.00536155

You should get $0.00536155$$0.00536155$, about 0.5 percent. (Hint: If you’re still getting 0.0131193, it means you haven’t modified the code and have assigned the new variable R.1850.to.1900 with the answer for 1900 to 2000.)

(Note: class demo ends about here.)

# The complete history of world population

First we’re going to read in data from a file that we’ve placed on the course lab website.

dat <- read.table("/data175/world_population_data.txt", header = T)
dat
 ABCDEFGHIJ0123456789
year
<int>
pop
<int>
-80005
1200
1000400
1500458
1600580
1700682
1750791
18001000
18501262
19001650

Look at the data set, which has the form of a table with two labeled columns “pop” and “year”. We’re going to extract the contents into our two vectors year.vec and N.vec. (You don’t need to understand this yet. We’ll be learning about why this works in later labs.)

million <- 1000000 ## or 1e6
N.vec <- dat$pop / 1000 ## converts units from "in millions" to "in billions" year.vec <- dat$year
names(N.vec) <- year.vec

Let’s look at both of these

year.vec
 [1] -8000     1  1000  1500  1600  1700  1750  1800  1850  1900  1950  1955  1960
[14]  1965  1970  1975  1980  1985  1990  1995  2000  2005  2010  2015
N.vec
-8000     1  1000  1500  1600  1700  1750  1800  1850  1900  1950  1955  1960
0.005 0.200 0.400 0.458 0.580 0.682 0.791 1.000 1.262 1.650 2.525 2.758 3.018
1965  1970  1975  1980  1985  1990  1995  2000  2005  2010  2015
3.322 3.682 4.061 4.440 4.853 5.310 5.735 6.127 6.520 6.930 7.349 

## Our first plot

plot(x = year.vec,
y = N.vec)

Wow, looks like world population is exploding.

Let’s see what is happening in terms of proportional changes (by taking logs)

log.N.vec <- log(N.vec)
plot(x = year.vec, y = log.N.vec)

Wow, it looks like even proportional rate of growth has increased.

You can now guess-timate the exponential population growth rate by eyeing the “slope” of the log rate of population growth is equal to the calculated growth rate. For example, the 8000 year period from -8000 to 0 saw an increase of about 4 in log-population size. The slope is thus about 4/8000 = 0.0005. We can check this with our calculations below.

## Calculating growth rates through history

Calculate a vector of exponential growth rate.

Here we will use the diff() function in R, which tells us the differences between elements in a vector.

For example,

x = c(4, 5, 7) ## this is a vector with three elements.
diff(x) # gives us the differences between elements
[1] 1 2

We’ll formulate this as the slope of the graph of log population sizes

rise.vec <- diff(log.N.vec) # these are the "rise", the vertical distances between points
run.vec <- diff(year.vec) # these are "run", the horizontal distances between points
slope.vec <- rise.vec / run.vec
R.vec <- slope.vec
R.vec
           1         1000         1500         1600         1700         1750
0.0004610523 0.0006938410 0.0002708093 0.0023615892 0.0016200155 0.0029653662
1800         1850         1900         1950         1955         1960
0.0046891462 0.0046539553 0.0053615505 0.0085093155 0.0176529433 0.0180177162
1965         1970         1975         1980         1985         1990
0.0191945302 0.0205778143 0.0195946332 0.0178450255 0.0177885388 0.0179989530
1995         2000         2005         2010         2015
0.0153991831 0.0132234966 0.0124338284 0.0121970875 0.0117408873 

Let’s make a bit more readable by expressing in percentage points, and rounding

R.vec.in.percent <- 100 * round(R.vec, 4)
R.vec.in.percent
   1 1000 1500 1600 1700 1750 1800 1850 1900 1950 1955 1960 1965 1970 1975 1980
0.05 0.07 0.03 0.24 0.16 0.30 0.47 0.47 0.54 0.85 1.77 1.80 1.92 2.06 1.96 1.78
1985 1990 1995 2000 2005 2010 2015
1.78 1.80 1.54 1.32 1.24 1.22 1.17 

We see that growth rates increased for nearly 10,000 years, but have recently begun to decrease a bit.

## Plotting the growth rates

Let’s try a plot:

end.year.each.period.vec <- names(R.vec.in.percent)
plot(x = end.year.each.period.vec,
y = R.vec.in.percent,
main = "Exponential Growth Rates of World Population through the Ages",
type = "l")

It looks like population growth rates have begun to decline. To see a bit better, we can graph only the more recent years

plot(x = end.year.each.period.vec,
y = R.vec.in.percent,
xlim = c(1900, 2020),              # limit the x-axis to 1900 to 2020
main = "Exponential Growth Rates of World Population, since 1900",
type = "o")

# Congratulations

We’ve finished all of the computing for the first lab!

1. [Multiple choice] Which of the following descriptions is best for the history of human population growth?

A. Constant exponential growth at a rate slighly larger than zero.

B. Thousands of years of near zero growth, followed by centuries of accelerating growth, with a recent slowing of growth.

1. Thousands of years of essentially zero growth, followed by a one time increase in growth rates.

2. Alternating lengthy periods of positive and negative growth.

1. [A numerical answer] How large would the world population be today if annual growth rates had always been .0005 larger for the last 10,000 years?

Hint 1: The rules of exponents tell us

$exp\left(\left(R+d\right)\ast t\right)=exp\left(R\ast t\right)\ast exp\left(d\ast t\right)$

Hint 2: To calculate exp(10000 * .0005), you can use a calculator or any other tool. You don’t have to use R.

Hint 3: You should get a population roughly 150 time as large as today.

Note: Imagine we increased the reproduction rate of each generation by a tiny bit – so, for example, instead of each woman having on average 1 surviving daughter, imagine she had 1.015 surviving daughters. If generations were 30 years in length, then this would mean an increase in the growth rate by about log(1.015/1.00)/30 = .0005. So, the calculation we just did tells us what would have happened to the human population if fertility rates had always been just slightly, only 1.5 percent, higher.

1. [A numerical answer] How large would the world population in 2115 be if current exponential growth rates continue? Use 7.3 billion as the population size in 2015 and assume \$R = 0.01. You can do this calculation however you choose (with R, by hand with a calculator, or any other way.)

2. [A short answer.] Do you think this estimate of the world population in 2115 is likely to be too high or too low? Explain your reasoning in a sentence.

3. [Optimum Population Exercise] Imagine there’s an island that can only sustain a few people. The marginal product starts at 5 units for the first person and declines by 1 unit for each additional person until the MP of the 6th person is zero. Each person needs to consume 2 units per year to subsist.

(Hint: if you are having trouble here with the definitions of the optimum populations, consult the lecture slides.)

1. Fill in the Average Product row of the table below. (We recommend you do this by hand to enhance understanding.)

(Hint: The average can be obtained by summing up the marginal product to produce the total product and then dividing by the number of people.)

Population Size 1 2 3 4 5 6 7 8 9 10
Marginal Product 5 4 3 2 1 0 0 0 0 0
Average Product 5 4.5 4 x x x x x x x
1. What size population gives Sauvy’s economic optimum?

2. What size population gives Sauvy’s power optimum?

3. What size population is the “maximum” sustainable population?

1. [non-graded] About how much time (in hours and minutes) did it take you to complete this lab?

Congratulations! You have completed Lab 1.