plant
plant copied to clipboard
`interpolate_to_heights` drops nodes in largest size bracket
I discovered some unexpected behaviour in interpolate_to_heights
. When using a coarse grid, individuals in the the largest class are removed, where instead I expected to keep them.
It's not quite right to represent their density at the highest interpolation point, so I think we need to append the largest individual in each time step.
Here is a pared down reprex using pieces of the code from interpolate_to_heights
# helper from `interpolate_to_heights`
f <- function(x, y, xout) {
y_pred <- stats::spline(x, y, xout=xout, method="natural")$y
y_pred[!dplyr::between(xout, min(x), max(x))] <- NA
y_pred
}
# interpolation grid - expect to include density between 3-4
pts <- 1:4
# but nodes in large size bracket are not represented by final solution
data <- data.frame(step = 1,
species = 1,
time = 10,
height = c(0.1, 1.2, 2.3, 3.3),
density = c(0.1, 0.1, 0.1, 0.2))
data %>%
dplyr::group_by(species, time, step) %>%
dplyr::summarise(
dplyr::across(where(is.double), ~f(height, .x, xout=pts)),
.groups = "keep")
# A tibble: 4 × 5
# Groups: species, time, step [1]
species time step height density
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 10 1 1 0.102
2 1 10 1 2 0.0914
3 1 10 1 3 0.163
4 1 10 1 NA NA