SEEG-Oxford / movement

R package containing useful functions for the analysis of movement data in disease modelling and mapping
20 stars 16 forks source link

Enforce the integer values for the movement_matrix in the movement() method #87

Closed goldingn closed 9 years ago

goldingn commented 9 years ago

Running the example for movement, the step predicting form a flux model issues the following warning:

predictedMovement  <- predict(originalRadiation(theta = 0.1), locationData, symmetric = TRUE)
Warning message:
In as.movement_matrix.matrix(prediction$prediction) :
  The given movement_matix contains non-integer values. Rounding was used to return a valid movement_matrix object.

Whilst we want integer predictions in this cases, in general, predictions from a flux or movement_model object should not be coerced to integer. The following should therefore work and not issue any warnings (note changes to object names):

data(kenya)
kenya10 <- raster::aggregate(kenya, 10, sum)
net <- getNetwork(kenya10, min = 50000)
location_data <- data.frame(location = net$locations, population = net$population, x = net$coordinate[,1], y = net$coordinate[,2])
class(location_data) <- c("location_dataframe", "data.frame")

# simulate movements (note the values of movementmatrix must be integer)
predicted_movement  <- predict(originalRadiation(theta = 0.1), location_data, symmetric = TRUE)
movement_matrix <- as.movement_matrix(round(predicted_movement$movement_matrix))

# fit a new model to these data
movement_model <- movement(movement_matrix ~ location_data, radiationWithSelection(theta = 0.5))
# print movement_model
print(movement_model)
# predict the population movements
predicted_movements  <- predict(movement_model, kenya10)
# display the predicted movements
showprediction(predicted_movements)
KathrinTessella commented 9 years ago

@goldingn However, I assume that the predict(flux) method still should internally ensure that the generated movement_matrix is of the correct class type (that is, it inherits from the 'movement_matrix' class and the 'matrix class'?

goldingn commented 9 years ago

Hmmm... thinking about this a bit more, I think you're right that the output should have class movement_matrix. So the integer enforcement should just be done in movement(). Feel free to rename this issue!

It may (or may not) be helpful to add an attribute to the class signifying whether it is an 'observed' (i.e. integer) movement matrix or not. I guess that would only be to save checking that it is integer.

KathrinTessella commented 9 years ago

Just to recap: the movement() method, using an observed movement_matrix, required integer values within the matrix. However, the predict methods, for flux or movement_models, can generate matrices with non-integer values. Therefore, move the the rounding to integer values (with the creation of a warning) from the as.movement_model functions into the movement() method.

goldingn commented 9 years ago

yes, exactly!

KathrinTessella commented 9 years ago

Task completed after merging the code.