if (FALSE) {
library(SiMRiv)
library(adehabitatLT)
## simulate "real" data, a Levy walk, for which we want to
## parameterize our model
real.data <- simm.levy(1:500)[[1]][, 1:2]
## Define a species model to adjust. Let's assume we don't know
## much about what kind of real data we have, hence define
## a flexible model: a two-state correlated random walk model
## with variable step lengths.
## This model implies "estimating" 6 parameters:
## - turning angle correlation of state 1 [0, 1]
## - turning angle correlation of state 2 [0, 1]
## - switching probability S1 -> S2 [0, 1]
## - switching probability S2 -> S1 [0, 1]
## - maximum step length of state 1 [0, ?]
## - maximum step length of state 2 [0, ?]
## Let's assume we want to simulate at a 20 times higher time frequency
## than real data.
## In order to allow our model to adjust to real data, we have
## to provide a maximum allowable step length to the optimization algorithm
## that allows to recover real data after downsampling 20 times.
## Let's make a simple calculation of the step lengths of real data:
tmp <- sampleMovement(real.data)
## and compute a good maximum allowed step length during optimization
## using the observed maximum divided by 20 (because each real step
## will comprise 20 simulated steps)
max.step.length <- max(tmp$stat[, "steplengths"]) / 20
## and finally build the species model with it.
## Note: "CRW.CRW.sl" is the short name for the model we want,
## as defined above
species.model <- speciesModel("CRW.CRW.sl", steplength = max.step.length)
## now run optimization
sol <- adjustModel(real.data, species.model, resol = 20
, nbins.hist = c(3, 3, 0), step.hist.log = TRUE)
## After finishing, we can extract the input parameters of the optimized
## solutions (100 by default) in the last generation (generation 1000
## by default):
pars <- sol[[length(sol)]]$par
## now we can take the optimized solutions and reconstruct species
## based on them:
optimized.species <- apply(pars, 1, species.model)
## and make some simulations with those optimized species.
## Plot real trajectory
par(mfrow = c(2, 2), mar = c(0, 0, 1, 0))
plot(real.data, type = "l", asp = 1, axes = F, main = "Real")
## plot three simulated trajectories with optimized species
for(i in 1:3) {
# remember we want to simulate at a 20 times higher frequency
# so we do 500 (real data) x 20 steps
sim <- simulate(optimized.species[[i]], 500 * 20)
# now we downsample frequency to match real
samp <- sampleMovement(sim, 20)
# and plot the simulated trajectory before and after
# downsampling 20 times.
plot(sim[, 1:2], type = "l", asp = 1, axes = F, col = "gray"
, main = "Simulated and downsampled")
lines(samp$relocs, col = "black")
}
## Now plot the evolution of parameters along algorithm's generations.
## This is good to assess whether the final solutions converged
## but see ?generationPlot for details
generationPlot(sol, species.model)
}
Run the code above in your browser using DataLab