Tuesday Code

census

c() creates the vector of selected data, seq make the data sequential

year

population size

population growth rate

population growth rate: vectorized

length() function gets or sets the length of a vector (list) or other objects.

negative indexing

negative indexing allows you to drop a specific element from a vector

calculate lambdas for all data

#finish putting together datafrome The function data. frame() creates data frames, tightly coupled collections of variables.

#examing the population growth rates the plot function plotting points and lines

Thursday Code

How do we determine if a population is likely to go extinct?

To model population dynamics we randomly pull population growth rates out of a hat.

  • hist(bear_N$lambda.i)

  • hat_of_lambdas <- bear_N$lambda.i

  • is.na(hat_of_lambdas)

  • any(is.na(hat_of_lambdas) == TRUE)

Drop the NA

Random sampling of lambda

Sample takes a sample of the specified size from the elements of x using either with or without replacement.

pulled a lambda from the hat

sample(x = hat_of_lambdas, size = 1,replace = TRUE) # pulled lambda from the hat and save to object lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) lambda_rand.t

Get initial population size

Obtain the first several rows of a matrix or data frame using head, and use tail to obtain the last several rows.

  • head(bear_N)
  • tail(bear_N)
  • summary(bear_N)
  • dim(bear_N)
  • N.1997 <- 99

one round of population simulate

a place holder for the number of bears next year

1.2280799 lambda_rand.tN.1997 N.1998 <- lambda_rand.t*N.1997

simulation the hard way

simulating and ignoring the possibility of the density defendants

1997 to 1998

lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.1998 <- lambda_rand.tN.1997 # 1998 to 1999 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.1999 <- lambda_rand.tN.1998 # 1999 to 2000 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2000 <- lambda_rand.tN.1999 # 2000 to 2001 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2001 <- lambda_rand.tN.2000 # 2001 to 2002 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2002 <- lambda_rand.tN.2001 # 2002 to 2003 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2003 <- lambda_rand.tN.2002 # 2003 to 2004 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2004 <- lambda_rand.tN.2003 # 2004 to 2005 lambda_rand.t <- sample(x = hat_of_lambdas, size = 1,replace = TRUE) N.2005 <- lambda_rand.tN.2004

plot population change

there are lots of possibility in our simulation

  • year <- seq(1997, 2004)
  • N.rand <- c(N.1998,N.1999,N.2000,N.2001,N.2002,N.2003,N.2004,N.2005)
  • df.rand <- data.frame(N.rand, year)
  • plot(N.rand ~ year, data = df.rand, type = “b”)