egg_slopes() requires knots, but is fixed within the function. Could knots be placed as a param to allow for use with different knots models please?
e.g.
egg_slopes <- function(
fit,
period = c(0, 0.5, 1.5, 3.5, 6.5, 10, 12, 17),
knots = c(2, 8, 12)
) {
stopifnot(inherits(fit, "lme"))
#knots <- c(2, 8, 12)
id_var <- names(fit[["groups"]])
slopes <- matrix(
data = NA_real_,
nrow = length(unique(fit$data[[id_var]])),
ncol = length(period) / 2
)
colnames(slopes) <- paste0(
"slope_",
sapply(split(
x = period,
f = rep(
x = seq(1, length(period), length(period) %/% 4),
each = length(period) %/% 4
)
), paste, collapse = "--")
)
pred <- matrix(
data = NA_real_,
nrow = length(unique(fit$data[[id_var]])),
ncol = length(period)
)
colnames(pred) <- paste0("pred_period_", round(period, digits = 1))
fxef <- nlme::fixef(fit)
fxef <- unname(fxef[
grep("\(Intercept\)|gsp\(.\)|poly\(.\)", names(fxef))
])
rnef <- nlme::ranef(fit)
rnef <- rnef[, grep("\(Intercept\)|gsp\(.\)|poly\(.\)", names(rnef))]
rnef <- cbind.data.frame(
as.matrix(rnef),
matrix(
data = rep(0, (length(fxef) - ncol(rnef)) * nrow(rnef)),
nrow = nrow(rnef),
ncol = length(fxef) - ncol(rnef)
)
)
for (i in seq_along(unique(fit$data[[id_var]]))) {
coeff <- fxef + as.numeric(rnef[i, ])
for (j in 1:(length(period) / 2)) {
x1 <- period[j * 2 - 1]
y1_tmp <- coeff *
c(x1^0, x1^1, x1^2, x1^3, (x1 - knots)^3) /
c(1, 1, 2, rep(6, 4))
y1 <- sum(y1_tmp[1:(4 + findInterval(x1, knots, left.open = TRUE))])
x2 <- period[j * 2]
y2_tmp <- coeff *
c(x2^0, x2^1, x2^2, x2^3, (x2 - knots)^3) /
c(1, 1, 2, rep(6, 4))
y2 <- sum(y2_tmp[1:(4 + findInterval(x2, knots, left.open = TRUE))])
pred[i, j * 2 - 1] <- y1
pred[i, j * 2] <- y2
slopes[i, j] <- (y2 - y1) / (x2 - x1)
}
}
out <- cbind.data.frame(ID = unique(fit$data[[id_var]]), pred, slopes)
names(out)[1] <- id_var
out
}
Thanks!