0

I need to create a boundary for my correlated random walk. This boundary is based on a mask of my study area (raster file). In my for loop I want to add a repeat{} loop which breaks whenever the value of the raster is 0 (and not 1). How would I go about this? I used the circular package.

walk <- function(x0, y0, head0, n, parameterMu, parameterRho, parameterMean, parameterSd)
{
  # Get nr of individuals, call it k
  k = length(x0)

  # Create list to hold data
  all.paths <- list()
  
  for (j in 1:k)
  {
    # Create structure to hold data
    steps <- data.frame(matrix(0,n,6))
    colnames(steps) <- c("id","x", "y", "steplength", "heading", "turningangle")
    
    # Insert the id, starting location and heading
    steps[,"id"] = j
    steps[1,"x"] = x0[j]
    steps[1,"y"] = y0[j]
    steps[1,"heading"] = head0[j]
    
    # Simulate steps
    for(i in 2:n)
    {
      repeat{
      # Draw step length and turning angle, compute heading
      steplength = rnorm(n = 1, mean = parameterMean, sd = parameterSd)
      turningangle = as.numeric(rwrappedcauchy(n = 1, mu = parameterMu, rho = parameterRho))
      newbearing = as.numeric(circular(steps[i-1,"heading"]) + circular(turningangle)) %% (2*pi)
      
      # Get new location
      next.xy <- movement(x0=steps[i-1,"x"], y0=steps[i-1,"y"], step=steplength, heading=newbearing)
      
      # Set boundary
      if ??? break    
      }   

      # Store output (xy on row i, steplength/heading on row i-1)
      steps[i,"x"] <- next.xy[1,"x"]
      steps[i,"y"] <- next.xy[1,"y"]
      steps[i,"steplength"] <- next.xy[1,"step"]
      steps[i,"heading"] <- newbearing
      steps[i,"turningangle"] <- turningangle
    }
    
    # Store trajectory in list
    all.paths[[j]] <- steps
  }
  
  # Return output
  return(all.paths)
}

The function for movement:

movement <- function(x0, y0, step, heading)
{
  x_new <- x0 + sin(heading)*step
  y_new <- y0 + cos(heading)*step
  move.temp <- data.frame(x = x_new,
                          y = y_new,
                          step = step,
                          head = heading)
  return(move.temp)
}
neòinean
  • 39
  • 7
  • `if (identical(0, raster) break` – tester Feb 24 '21 at 00:44
  • Thanks for your reply! This however results in an infinite loop... – neòinean Feb 24 '21 at 01:00
  • If you want people to help you, you should also include all necessary packages in the code. I guess `rwrappedcauchy` is from the `circular` package, but where does the `movement` function come from? – tester Feb 24 '21 at 14:39
  • Sorry, my bad! I indeed used the `circular` package, and `movement` is a function I created myself, I forgot to add that one. It is in there now – neòinean Feb 24 '21 at 21:36
  • What do you mean by 'the raster'? – Hugh Feb 24 '21 at 21:41
  • @Hugh I have a raster file of the study area. Coordinates within the study area return a value of 1 whereas coordinates outside of it return a value of 0. – neòinean Feb 24 '21 at 21:48
  • @neòinean you need to precisely explain when you need the loop to break. E.g. is it when `next.xy$x <= 0`. I don't see any `raster` or a variable that indicates if you are within or outside your study area. Additionally, you should rename `colnames(steps.df)` to `steps` as there is no `steps.df` in the function. – tester Feb 24 '21 at 22:35
  • @tester Thanks, edited. I had in mind something like the function breaking and restarting when the `next.xy$x` or `next.xy$y` reaches a value which corresponds to a 0 in the raster of my study area. New to R so not sure how this would work out precisely... – neòinean Feb 24 '21 at 23:28

0 Answers0