c() creates the vector of selected data, seq make the data sequential
length() function gets or sets the length of a vector (list) or other objects.
negative indexing allows you to drop a specific element from a vector
#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
census <- 1:39
year.t <- 1959:1997
females.N <- c(44,47,46,44,46, 45,46,40,39,39, 42,39,41,40,33, 36,34,39,35,34, 38,36,37,41,39, 51,47,57,48,60, 65,74,69,65,57, 70,81,99,99)
lambda.i <- females.N[-1]/females.N[-length(females.N)]
lambda.i <- c(lambda.i,NA)
lambda_log <- log(lambda.i)
bear_N <- data.frame(census, year.t, females.N, lambda.i, lambda_log)
plot(females.N ~ year.t, data = bear_N, type = “b”, ylab = “Population index (females + cubs)”, xlab = “Year”)
abline(v = 1970)
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)
Sample takes a sample of the specified size from the elements of x using either with or without replacement.
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
Obtain the first several rows of a matrix or data frame using head, and use tail to obtain the last several rows.
a place holder for the number of bears next year
1.2280799 lambda_rand.tN.1997 N.1998 <- lambda_rand.t*N.1997
simulating and ignoring the possibility of the density defendants
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
there are lots of possibility in our simulation