STAT 360 Exam 3

Name: Rebecca Lewis

Set Up Work Space

Load R Libraries

library(rmarkdown)
library(knitr)

Set the Seed

set.seed(33)

The First Version of the Function

Step 1 (Score: Up to 15)

Construct the function and assign it to the object disease.sim.1.

disease.sim.1. <- function(n, u, t, y, display.lattice = TRUE) {
  Victims <- data.frame(susceptible = rep(NA, t), infected = rep(NA, t), recovered = rep(NA, t)) #makes 3 empty columns, the t length (timestep length)
  lattice.now <- matrix(data = NA, nrow = n, ncol = n) #makes an empty matrix n rows tall, n columns long
  lattice.next <- matrix(data = NA, nrow = n, ncol = n) #makes another empty matrix n rows tall, n columns long
  transmissibility<-matrix(data = NA, nrow = n, ncol = n) #makes a empty matrix that will store the uniform dist p values
  lattice.now[] <- 1 #makes the first matrix full of susceptible undiseased victims
  for (i in 1:n) # for each row up to the length of the matrix
  {
    for (j in 1:n) # for each column up to the length of the matrix
    {
      transmissibility[i,j]<- runif(1, min = 0, max = 1) # for each cell of transmissibility find the value of p that is in a uniform distribution
    } #closes the for each column loop
  } # closes the for each row loop
  
  lattice.now[sample(x = 1:n, size = 1), sample(x = 1:n, size = 1)] <- 2  #randomly makes one cell a infected victim
  for (step in 1:t) { #does this loop for t time steps
    lattice.next[] <- NA #resets the lattice next to NAs
    
    for (i in 1:n) { # this is for row in the matrix
      for (j in 1:n) { #this is for the column in the matrix
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 1)  #sees if the space is an susceptible cell in the first matrix
          {
            lattice.next[i, j] = 1 #  the cell is still susceptible in the second matrix
        }# closes the if its 1 loop
        # Transition from recovered to recovered
        if (lattice.now[i, j] == 0) { # checks if the cell has a recovered victim in the first matrix
          lattice.next[i, j] = 0 #if it does then the second matrix will also have a recovered victim
        } #closes check for recovered 
        if (lattice.now[i, j] == 2)  #sees if the space is an infected cell in the first matrix
          {
            lattice.next[i, j] = 2 #  the cell is still infected cell in the second matrix
        }# closes the if its 1 loop
      } # closes the column check
    } #closes the row check
    for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 2) { # checks to see if the cell is a infected victim
          if (j < n) { # makes sure that the cell to the right is within the matrix limit of n columns so cell is n-1 or less
            if(lattice.now[i, j + 1] == 1) { #now it sees if there is a susceptible person to the right of the infected victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) 
                { #now checks if the susceptible person will get infected thanks to the infected person's rate of infecting
                lattice.next[i, j + 1] = 2 # if it does then a infected person replaces the susceptible person in the next time step
               } #closes the possible infected in the next time step loop
            } #closes if the cell is to the right of the infected one is susceptible
          } # closes checking if the infected is in column n-1 or less
          if (i < n) { #checks to see if the infected person is in the n-1 row or less
            if (lattice.now[i + 1, j] == 1) { # checks to see if the cell to the bottom of the infected one is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible than it will check to see if the susceptible victim catches the infection
                lattice.next[i + 1, j] = 2 # writes that the susceptible victim gets sick in the next time step 
              } # closes the probability that the victim to the bottom will get sick
            } #Closes the check if there is a susceptible victim in the cell to the bottom of the infected one
          } # closes the check to see if the infected one is in the n-1 row or less
                    if (j > 1) { # checks to see if the infected victim is in the second column or above
            if (lattice.now[i, j - 1] == 1) { # checks to see if the cell to the left of the infected victim is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible victim than check to see if it catches the sickness in the next step 
                lattice.next[i, j - 1] = 2 # yep the victim caught the sickness and is no longer a susceptible victim but a infected one in the next step
              } #closes the check if it caught the sickness
            } # closes the if the victim to the left of a infected is a susceptible victim
          } # closes the if the infected victim is in the second column or above
          if (i > 1) { #checks to see if the victim is in the second row or above
            if (lattice.now[i - 1, j] == 1) { # if in the second row or above is the cell to the top of it a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if a susceptible victim is there what is the probability that it will catch the sickness
                lattice.next[i - 1, j] = 2 # the susceptible victim will be sick in the next step
              } #closes the check for if the susceptible victim to the top will catch the sickness 
            } #closes the is there a susceptible victim above the infected one check
          } # closes the if the infected one is in the second row or above loop
        } # closes the does the cell contain a infected victim check
                #Transition from infected to recovered
        if (lattice.now[i, j] == 2) { # this checks to see if the cell currently has a infected victim
          if(runif(n = 1, min = 0, max = 1) < y){ # can the infected victim recover?
          lattice.next[i, j] = 0 #Oh no we lost a victim and now they are recovered
          }# closes the recovery check
        } # closes the convert infected to recover check
        
      } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
      for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 0)  # checks to see if the cell is a recovered victim
          {
            if(runif(n = 1, min = 0, max = 1) < u) # checks to see if our virus can effect them again
            {
              lattice.next[i, j] = 1 # mahamah (evil laugh) we have another susceptible victim 
            }#closes the check if recover becomes infected loop
        } #closes the check for recover loop
        } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
    if (display.lattice == TRUE) { # checks to see if the display is true and if it is do the following
      image(t(lattice.now), col = c("gray", "green", "red"),breaks=c(-.5,.5,1.5,2.5)) # creates the image of matrix and make the matrix's recovered cell to gray, susceptible to green and its infected to red
      Sys.sleep(0.15) # loops each image created by the previous loop every 15 seconds
    } # closes the image loop
    lattice.now <- lattice.next # makes the next time step into the current one and erase the previous one in the process
    Victims$susceptible[step] <- sum(lattice.now == 1) # counts the number of the susceptible victims in the now current loop
    Victims$infected[step] <- sum(lattice.now == 2) # counts the number of the infected victims in the now current loop
    Victims$recovered[step] <- sum(lattice.now == 0) # counts the number of the recovered victims in the now current loop and puts it in the 
  } # closes the loop thru the time steps
    return(Victims) # returns the data frame with 3 now full columns that are t length (timestep length)
}# closes the function

Exercise 1 (Score: 2, 1, or 0)

Use disease.sim.1() to simulate the spread of the disease on a 10 by 10 grid for 30 years with a recover rate of 0.5 and a probability of becoming susceptible again of 0.25. Use replicate() to repeat this simulation 100 times, calculate the maximum number of infected individuals during each of the 100 simulations, and display the average maximum number of infections.

mean(replicate(100, max(disease.sim.1.(n=10,t=30, y=.5,u=.25,display.lattice = FALSE)$infected)))
## [1] 17.76

The Second Version of the Function

Step 2 (Score: Up to 4)

Construct the function and assign it to the object disease.sim.2.

disease.sim.2. <- function(n, u, t, y, display.lattice = TRUE, vaccine.number) {
  Victims <- data.frame(susceptible = rep(NA, t), infected = rep(NA, t), recovered = rep(NA, t), vaccinated=rep(NA,t)) #makes 3 empty columns, the t length (timestep length)
  lattice.now <- matrix(data = NA, nrow = n, ncol = n) #makes an empty matrix n rows tall, n columns long
  lattice.next <- matrix(data = NA, nrow = n, ncol = n) #makes another empty matrix n rows tall, n columns long
  transmissibility<-matrix(data = NA, nrow = n, ncol = n) #makes a empty matrix that will store the uniform dist p values
  lattice.now[] <- 1 #makes the first matrix full of susceptible undiseased victims
  for (i in 1:n) # for each row up to the length of the matrix
  {
    for (j in 1:n) # for each column up to the length of the matrix
    {
      transmissibility[i,j]<- runif(1, min = 0, max = 1) # for each cell of transmissibility find the value of p that is in a uniform distribution
    } #closes the for each column loop
  } # closes the for each row loop
  if(vaccine.number>0){ #checks to see if above 0 in vaccine numbers
    transmissibility[order<-sample(1:(n^2),size=vaccine.number, replace=FALSE)]<-0
     for (i in 1:n) { # this is for row in the matrix
      for (j in 1:n) { #this is for the column in the matrix
        if(transmissibility[i,j]==0)
    {
      lattice.now[i,j] <- 3
        } #closes changing value
      } # closes the column in matrix check
     } # closes the row in matrix check
  }#closes the if test to see if there is above 0
  
  lattice.now[sample(x = 1:n, size = 1), sample(x = 1:n, size = 1)] <- 2  #randomly makes one cell a infected victim
  for (step in 1:t) { #does this loop for t time steps
    lattice.next[] <- NA #resets the lattice next to NAs
    
    for (i in 1:n) { # this is for row in the matrix
      for (j in 1:n) { #this is for the column in the matrix
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 1)  #sees if the space is an susceptible cell in the first matrix
          {
            lattice.next[i, j] = 1 #  the cell is still susceptible in the second matrix
        }# closes the if its 1 loop
        # Transition from recovered to recovered
        if (lattice.now[i, j] == 0) { # checks if the cell has a recovered victim in the first matrix
          lattice.next[i, j] = 0 #if it does then the second matrix will also have a recovered victim
        } #closes check for recovered 
        if (lattice.now[i, j] == 2)  #sees if the space is an infected cell in the first matrix
          {
            lattice.next[i, j] = 2 #  the cell is still infected cell in the second matrix
        }# closes the if its 2 loop
        if (lattice.now[i, j] == 3)  #sees if the space is an vaccinated cell in the first matrix
          {
            lattice.next[i, j] = 3 #  the cell is still vaccinated cell in the second matrix
        }# closes the if its 3 loop
      } # closes the column check
    } #closes the row check
    for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 2) { # checks to see if the cell is a infected victim
          if (j < n) { # makes sure that the cell to the right is within the matrix limit of n columns so cell is n-1 or less
            if(lattice.now[i, j + 1] == 1) { #now it sees if there is a susceptible person to the right of the infected victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) 
                { #now checks if the susceptible person will get infected thanks to the infected person's rate of infecting
                lattice.next[i, j + 1] = 2 # if it does then a infected person replaces the susceptible person in the next time step
               } #closes the possible infected in the next time step loop
            } #closes if the cell is to the right of the infected one is susceptible
          } # closes checking if the infected is in column n-1 or less
          if (i < n) { #checks to see if the infected person is in the n-1 row or less
            if (lattice.now[i + 1, j] == 1) { # checks to see if the cell to the bottom of the infected one is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible than it will check to see if the susceptible victim catches the infection
                lattice.next[i + 1, j] = 2 # writes that the susceptible victim gets sick in the next time step 
              } # closes the probability that the victim to the bottom will get sick
            } #Closes the check if there is a susceptible victim in the cell to the bottom of the infected one
          } # closes the check to see if the infected one is in the n-1 row or less
                    if (j > 1) { # checks to see if the infected victim is in the second column or above
            if (lattice.now[i, j - 1] == 1) { # checks to see if the cell to the left of the infected victim is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible victim than check to see if it catches the sickness in the next step 
                lattice.next[i, j - 1] = 2 # yep the victim caught the sickness and is no longer a susceptible victim but a infected one in the next step
              } #closes the check if it caught the sickness
            } # closes the if the victim to the left of a infected is a susceptible victim
          } # closes the if the infected victim is in the second column or above
          if (i > 1) { #checks to see if the victim is in the second row or above
            if (lattice.now[i - 1, j] == 1) { # if in the second row or above is the cell to the top of it a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if a susceptible victim is there what is the probability that it will catch the sickness
                lattice.next[i - 1, j] = 2 # the susceptible victim will be sick in the next step
              } #closes the check for if the susceptible victim to the top will catch the sickness 
            } #closes the is there a susceptible victim above the infected one check
          } # closes the if the infected one is in the second row or above loop
        } # closes the does the cell contain a infected victim check
                #Transition from infected to recovered
        if (lattice.now[i, j] == 2) { # this checks to see if the cell currently has a infected victim
          if(runif(n = 1, min = 0, max = 1) < y){ # can the infected victim recover?
          lattice.next[i, j] = 0 #Oh no we lost a victim and now they are recovered
          }# closes the recovery check
        } # closes the convert infected to recover check
        
      } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
      for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 0)  # checks to see if the cell is a recovered victim
          {
            if(runif(n = 1, min = 0, max = 1) < u) # checks to see if our virus can effect them again
            {
              lattice.next[i, j] = 1 # mahamah (evil laugh) we have another susceptible victim 
            }#closes the check if recover becomes infected loop
        } #closes the check for recover loop
        } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
    if (display.lattice == TRUE) { # checks to see if the display is true and if it is do the following
      image(t(lattice.now), col = c("gray", "green", "red", "blue"),breaks=c(-.5,.5,1.5,2.5,3.5)) # creates the image of matrix and make the matrix's recovered cell to gray, susceptible to green and its infected to red
      Sys.sleep(0.15) # loops each image created by the previous loop every 15 seconds
    } # closes the image loop
    lattice.now <- lattice.next # makes the next time step into the current one and erase the previous one in the process
    Victims$susceptible[step] <- sum(lattice.now == 1) # counts the number of the susceptible victims in the now current loop
    Victims$infected[step] <- sum(lattice.now == 2) # counts the number of the infected victims in the now current loop
    Victims$recovered[step] <- sum(lattice.now == 0) # counts the number of the recovered victims in the now current loop 
     Victims$vaccinated[step] <- sum(lattice.now == 3) # counts the number of the vaccinated victims in the now current loop 
  } # closes the loop thru the time steps
    return(Victims) # returns the data frame with 3 now full columns that are t length (timestep length)
}# closes the function

Exercise 2 (Score: 2, 1, or 0)

Use disease.sim.2() to simulate the spread of the disease using the same parameters as above, but also while vaccinating 5 individuals at random at the start of the disease outbreak. Use replicate() to repeat this simulation 100 times, calculate the maximum number of infected individuals during each of the 100 simulations, and display the average maximum number of infections.

mean(replicate(100, max(disease.sim.2.(n=10,t=30, y=.5,u=.25,display.lattice = FALSE,vaccine.number = 5)$infected)))
## [1] 12.99

Exercise 3 (Score: 3, 2, 1, or 0)

Use for() to cycle through every whole-digit value of vaccine.number between 1 and 30 to determine the minimum number of vaccines that it will take to reduce the number of infected individuals at the end of the simulations to an average of 0.5 or below (i.e., approach the eradication of the disease). Hint: use replicate() to repeat the simulation for each value of vaccine.number at least 100 times.

victims2.0<-0
for(k in 1:30)
{
 victims2.0[k]<- mean(replicate(1000, (disease.sim.2.(n=10,t=30, y=.5,u=.25,display.lattice = FALSE,vaccine.number = k)$infected[30]))) #you said at least 100 so I am doing it 1000 times.
  if(victims2.0[k]<=.5)#sees if the average at the end of doing the vaccines is .5 infected or less
  {print (k)} # prints the number of vaccines if the avg was .5 or less infected
}
## [1] 27
## [1] 28
## [1] 29
## [1] 30

This shows that the minimum number of vaccines to reduced the number of infected individuals at the end of the simulations to an average of .5 or below is 27.

The Third Version of the Function

Step 3 (Score: Up to 4)

Construct the function and assign it to the object disease.sim.3.

disease.sim.3. <- function(n, u, t, y, display.lattice = TRUE, vaccine.number, targeted) {
  Victims <- data.frame(susceptible = rep(NA, t), infected = rep(NA, t), recovered = rep(NA, t), vaccinated=rep(NA,t)) #makes 3 empty columns, the t length (timestep length)
  lattice.now <- matrix(data = NA, nrow = n, ncol = n) #makes an empty matrix n rows tall, n columns long
  lattice.next <- matrix(data = NA, nrow = n, ncol = n) #makes another empty matrix n rows tall, n columns long
  transmissibility<-matrix(data = NA, nrow = n, ncol = n) #makes a empty matrix that will store the uniform dist p values
  lattice.now[] <- 1 #makes the first matrix full of susceptible undiseased victims
  for (i in 1:n) # for each row up to the length of the matrix
  {
    for (j in 1:n) # for each column up to the length of the matrix
    {
      transmissibility[i,j]<- runif(1, min = 0, max = 1) # for each cell of transmissibility find the value of p that is in a uniform distribution
    } #closes the for each column loop
  } # closes the for each row loop
  if(targeted==TRUE)
  {
    if(vaccine.number>0){ #checks to see if above 0 in vaccine numbers
     transmissibility[order=order(transmissibility, decreasing=TRUE)][1:vaccine.number]<-0
      for (j in 1:n)  #this is for the column in the matrix
        {
        for (i in 1:n) # for each row up to the length of the matrix
  {
        if(transmissibility[i,j]==0)
    {
      lattice.now[i,j] <- 3
        } #closes changing value
      } # closes the tow in matrix check
     } # closes the column in matrix check
    }#closes the vaccine number if statement
  }
  if(targeted==FALSE)
  {
    if(vaccine.number>0){ #checks to see if above 0 in vaccine numbers
    transmissibility[order<-sample(1:(n^2),size=vaccine.number, replace=FALSE)]<-0
     for (i in 1:n) { # this is for row in the matrix
      for (j in 1:n) { #this is for the column in the matrix
        if(transmissibility[i,j]==0)
    {
      lattice.now[i,j] <- 3
        } #closes changing value
      } # closes the column in matrix check
     } # closes the row in matrix check
    }#closes the vaccine number if statement
  }
  
  lattice.now[sample(x = 1:n, size = 1), sample(x = 1:n, size = 1)] <- 2  #randomly makes one cell a infected victim
  for (step in 1:t) { #does this loop for t time steps
    lattice.next[] <- NA #resets the lattice next to NAs
    
    for (i in 1:n) { # this is for row in the matrix
      for (j in 1:n) { #this is for the column in the matrix
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 1)  #sees if the space is an susceptible cell in the first matrix
          {
            lattice.next[i, j] = 1 #  the cell is still susceptible in the second matrix
        }# closes the if its 1 loop
        # Transition from recovered to recovered
        if (lattice.now[i, j] == 0) { # checks if the cell has a recovered victim in the first matrix
          lattice.next[i, j] = 0 #if it does then the second matrix will also have a recovered victim
        } #closes check for recovered 
        if (lattice.now[i, j] == 2)  #sees if the space is an infected cell in the first matrix
          {
            lattice.next[i, j] = 2 #  the cell is still infected cell in the second matrix
        }# closes the if its 2 loop
        if (lattice.now[i, j] == 3)  #sees if the space is an vaccinated cell in the first matrix
          {
            lattice.next[i, j] = 3 #  the cell is still vaccinated cell in the second matrix
        }# closes the if its 3 loop
      } # closes the column check
    } #closes the row check
    for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 2) { # checks to see if the cell is a infected victim
          if (j < n) { # makes sure that the cell to the right is within the matrix limit of n columns so cell is n-1 or less
            if(lattice.now[i, j + 1] == 1) { #now it sees if there is a susceptible person to the right of the infected victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) 
                { #now checks if the susceptible person will get infected thanks to the infected person's rate of infecting
                lattice.next[i, j + 1] = 2 # if it does then a infected person replaces the susceptible person in the next time step
               } #closes the possible infected in the next time step loop
            } #closes if the cell is to the right of the infected one is susceptible
          } # closes checking if the infected is in column n-1 or less
          if (i < n) { #checks to see if the infected person is in the n-1 row or less
            if (lattice.now[i + 1, j] == 1) { # checks to see if the cell to the bottom of the infected one is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible than it will check to see if the susceptible victim catches the infection
                lattice.next[i + 1, j] = 2 # writes that the susceptible victim gets sick in the next time step 
              } # closes the probability that the victim to the bottom will get sick
            } #Closes the check if there is a susceptible victim in the cell to the bottom of the infected one
          } # closes the check to see if the infected one is in the n-1 row or less
                    if (j > 1) { # checks to see if the infected victim is in the second column or above
            if (lattice.now[i, j - 1] == 1) { # checks to see if the cell to the left of the infected victim is a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if it is a susceptible victim than check to see if it catches the sickness in the next step 
                lattice.next[i, j - 1] = 2 # yep the victim caught the sickness and is no longer a susceptible victim but a infected one in the next step
              } #closes the check if it caught the sickness
            } # closes the if the victim to the left of a infected is a susceptible victim
          } # closes the if the infected victim is in the second column or above
          if (i > 1) { #checks to see if the victim is in the second row or above
            if (lattice.now[i - 1, j] == 1) { # if in the second row or above is the cell to the top of it a susceptible victim
              if (runif(n = 1, min = 0, max = 1) < transmissibility[i,j]) { # if a susceptible victim is there what is the probability that it will catch the sickness
                lattice.next[i - 1, j] = 2 # the susceptible victim will be sick in the next step
              } #closes the check for if the susceptible victim to the top will catch the sickness 
            } #closes the is there a susceptible victim above the infected one check
          } # closes the if the infected one is in the second row or above loop
        } # closes the does the cell contain a infected victim check
                #Transition from infected to recovered
        if (lattice.now[i, j] == 2) { # this checks to see if the cell currently has a infected victim
          if(runif(n = 1, min = 0, max = 1) < y){ # can the infected victim recover?
          lattice.next[i, j] = 0 #Oh no we lost a victim and now they are recovered
          }# closes the recovery check
        } # closes the convert infected to recover check
        
      } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
      for (i in 1:n) {  # starts a new row in matrix check
      for (j in 1:n) { # starts a new column in matrix check
        # Transition from susceptible to infected
        if (lattice.now[i, j] == 0)  # checks to see if the cell is a recovered victim
          {
            if(runif(n = 1, min = 0, max = 1) < u) # checks to see if our virus can effect them again
            {
              lattice.next[i, j] = 1 # mahamah (evil laugh) we have another susceptible victim 
            }#closes the check if recover becomes infected loop
        } #closes the check for recover loop
        } # closes the starts a new column in matrix check loop
    } # closes the starts a new row in matrix check loop
    if (display.lattice == TRUE) { # checks to see if the display is true and if it is do the following
      image(t(lattice.now), col = c("gray", "green", "red", "blue"),breaks=c(-.5,.5,1.5,2.5,3.5)) # creates the image of matrix and make the matrix's recovered cell to gray, susceptible to green and its infected to red
      Sys.sleep(0.15) # loops each image created by the previous loop every 15 seconds
    } # closes the image loop
    lattice.now <- lattice.next # makes the next time step into the current one and erase the previous one in the process
    Victims$susceptible[step] <- sum(lattice.now == 1) # counts the number of the susceptible victims in the now current loop
    Victims$infected[step] <- sum(lattice.now == 2) # counts the number of the infected victims in the now current loop
    Victims$recovered[step] <- sum(lattice.now == 0) # counts the number of the recovered victims in the now current loop 
     Victims$vaccinated[step] <- sum(lattice.now == 3) # counts the number of the vaccinated victims in the now current loop 
  } # closes the loop thru the time steps
    return(Victims) # returns the data frame with 3 now full columns that are t length (timestep length)
}# closes the function

Exercise 4 (Score: 1 or 0)

Use disease.sim.3() to simulate the spread of the disease using the same parameters as above, but also while vaccinating 5 individuals with the highest transmissibility at the start of the disease outbreak. Use replicate() to repeat this simulation 100 times, calculate the maximum number of infected individuals during each of the 100 simulations, and display the average maximum number of infections.

mean(replicate(100, max(disease.sim.3.(n=10,t=30, y=.5,u=.25,display.lattice = FALSE,vaccine.number = 5, targeted=TRUE)$infected)))
## [1] 10.82

Exercise 5 (Score: 2, 1, or 0)

Use for() and disease.sim.3() to cycle through every whole-digit value of vaccine.number between 1 and 30 to determine the minimum number of vaccines that it will take to reduce the number of infected individuals at the end of the simulations to an average of .5 or below (i.e., approach the eradication of the disease). Hint: use replicate() to repeat the simulation for each value of vaccine.number at least 100 times.

victims3.0<-0
for(k in 1:30)
{
 victims3.0[k]<- mean(replicate(1000, (disease.sim.3.(n=10,t=30, y=.5,u=.25,display.lattice = FALSE,vaccine.number = k,targeted=TRUE)$infected[30]))) #you said at least 100 so I am doing it 1000 times.
  if(victims3.0[k]<=.5)#sees if the average at the end of doing the vaccines is .5 infected or less
  {print (k)} # prints the number of vaccines if the avg was .5 or less infected
}
## [1] 19
## [1] 20
## [1] 21
## [1] 22
## [1] 23
## [1] 24
## [1] 25
## [1] 26
## [1] 27
## [1] 28
## [1] 29
## [1] 30

Its 19 vaccines as the minimum number of vaccines to reduce the number of infected to an average of .5 or below.