Skip to content

Commit

Permalink
ensure dxy can't be NA in rw sims
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Jonsen committed Jun 4, 2024
1 parent 3523a0d commit b1104b0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions R/sim_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ sim_fit <-
}

reflect_y <- function(mu, y_rng) {
# if(is.na(mu[2])) browser()
if(mu[2] < y_rng[1]) {
c(mu[1], y_rng[1] * 2 - mu[2])
} else if(mu[2] > y_rng[2]) {
Expand Down Expand Up @@ -148,7 +149,7 @@ sim_fit <-
dts <- loc$date
dt <- as.numeric(difftime(dts, c(as.POSIXct(NA), dts[-length(dts)]), units = "hours"))
dt[1] <- 0

## get parameters from model fit object
switch(model,
crw = {
Expand All @@ -175,7 +176,7 @@ sim_fit <-
stop("Implausible travel rates detected, check SSM fit for implausible locations")
## implausible for pinnipeds but not for eg. birds** need to re-think this...***
})

###############################
## Simulate movement process ##
###############################
Expand All @@ -200,7 +201,6 @@ sim_fit <-

## wrap x values, reflect y values
mu1 <- wrap_x(mu[i-1,] + v[i,] * dt[i], ex[1:2])

mu1 <- reflect_y(mu1, ex[3:4])
if(!is.null(grad)) {
pv <- as.numeric(extract(grad, rbind(mu1)))
Expand Down Expand Up @@ -250,10 +250,12 @@ sim_fit <-
sigma = Sigma * dt[i]^2,
lower = vmin,
upper = vmax,
algorithm = "gibbs", burn.in.samples = 100)
algorithm = "gibbs",
burn.in.samples = 100)

dxy <- dxy[which(!is.na(dxy))[1],]

if(all(is.na(dxy))) dxy <- c(1,1)

## wrap x values, reflect y values
mu1 <- wrap_x(mu[i-1,] + dxy, ex[1:2])
mu1 <- reflect_y(mu1, ex[3:4])
Expand Down

0 comments on commit b1104b0

Please sign in to comment.