library(rmarkdown)
library(knitr)
set.seed(33)
The following lattice model was originally proposed by Bak et al. (1990; Phys Lett A 147:297-300). Consider an 'n' by 'n' grid of cells where each cell is either empty (value of 0), occupied by a healthy tree (value of 1), or occupied by a burning tree (value of 2). At each time step, (1) empty cells are recolonized by healthy trees at a rate of 'p', (2) burning trees spread fire to healthy trees in neighboring cells with a probability of 'r', and (3) burning trees burn down and leave behind empty cells.
Assign the values 20, 0.5, and 0.85 to the objects n, p, and r, respectively - these will represent the parameters of the simulation.
n<-20
p<-.5
r<-.85
Initialize two blank matrices with n rows and n columns each and assign them to the objects lattice.now and lattice.next - these will represent the lattice-based landscapes at different time steps.
lattice.now<-matrix(nrow = n, ncol = n)
lattice.next<-matrix(nrow = n, ncol = n)
Fill all cells in lattice.now with healthy trees.
lattice.now[1:n,1:n]<-1
Use sample() to transform a single cell in lattice.now from a healthy tree to a burning tree - this will represent the start of the fire. Hint: think about first selecting a random row and then selecting a random column in terms of where to start the fire.
row<-sample(1:nrow(lattice.now),1,replace=FALSE)
col<-sample(1:ncol(lattice.now),1,replace=FALSE)
lattice.now[row,col]<-2
Set all values in lattice.next to NA.
lattice.next[1:n,1:n]<-NA
Determine if the first cell in lattice.now (i.e., row 1 and column 1) is empty. Hint: your command should output either a TRUE or FALSE
lattice.now[1,1]==0
## [1] FALSE
Use runif() to determine if an empty cell should be recolonized during the next time step (i.e., transition from an empty cell to a healthy tree). Hint: you will need to use the object p as a theshold for whether or not the cell is recolonized, and your command should output either a TRUE or FALSE.
(runif(1)<p)
## [1] TRUE
Use if() to determine if the first cell in lattice.now is empty; if it is, use if() and runif() to determine if it should be recolonized; if is should, assign a healthy tree to the first cell in lattice.next. Hint: all transitions should be applied to lattice.next, not lattice.now.
if(lattice.now[1,1]==0){
if(runif(1)<p){
lattice.next[1,1]=1
}
}
Determine if the first cell in lattice.now is occupied by a healthy tree. Hint: your command should output either a TRUE or FALSE.
lattice.now[1,1]==1
## [1] TRUE
Use if() to determine if the first cell in lattice.now is occupied by a healthy tree; if it is, assign a healthy tree to the first cell in lattice.next (i.e., a healthy tree remains a healthy tree during the next time step).
if(lattice.now[1,1]==1){
lattice.next[1,1]<-1
}
Determine if the first cell in lattice.now is occupied by a burning tree. Hint: your command should output either a TRUE or FALSE.
lattice.now[1,1]==2
## [1] FALSE
Determine if the cell to the right of the first cell in lattice.now is occupied by a healthy tree. Hint: your command should output either a TRUE or FALSE.
lattice.now[1,2]==1
## [1] TRUE
Use runif() to determine if a healthy tree to the right of the first cell in lattice.now should catch fire during the next time step (i.e., transition from a healthy tree to a burning tree). Hint: you will need to use the object r as a theshold for whether or not the tree catches fire, and your command should output either a TRUE or FALSE.
runif(1)<r
## [1] FALSE
Use if() to determine if the first cell in lattice.now is occupied by a burning tree; if it is, use if() to determine if the cell to the right of it is occupied by a healthy tree; if it is, use if() and runif() to determine if that healthy tree should catch on fire; if is should, assign a burning tree to that cell in lattice.next. Hint: all transitions should be applied to lattice.next, not lattice.now.
if(lattice.now[1,1]==2){
if(lattice.now[1,2]==1){
if(runif(1)<r){
lattice.next[1,2]=2
}
}
}
Use if() to determine if the first cell in lattice.now is occupied by a burning tree; if it is, use if() to determine if the cell below it is occupied by a healthy tree; if it is, use if() and runif() to determine if that healthy tree should catch on fire; if is should, assign a burning tree to that cell in lattice.next. Hint: all transitions should be applied to lattice.next, not lattice.now.
if(lattice.now[1,1]==2){
if(lattice.now[2,1]==1){
if(runif(1)<r){
lattice.next[2,1]<-2
}
}
}
Use if() to determine if the first cell in lattice.now is occupied by a burning tree; if it is, assign an empty cell to that cell in lattice.next (i.e., transition from a burning tree to an empty cell). Hint: all transitions should be applied to lattice.next, not lattice.now.
if(lattice.now[1,1]==2){
lattice.next[1,1]=0
}