LondonR 2025
Photo by Ken Suarez on Unsplash
Felienne Hermans - The Programmer’s Brain
SEXP Cdqrls(SEXP x, SEXP y, SEXP tol, SEXP chk)
{
SEXP ans;
SEXP qr, coefficients, residuals, effects, pivot, qraux;
int n, ny = 0, p, rank, nprotect = 4, pivoted = 0;
double rtol = asReal(tol), *work;
Rboolean check = asLogical(chk);
ans = getAttrib(x, R_DimSymbol);
if(check && length(ans) != 2) error(_("'x' is not a matrix"));
int *dims = INTEGER(ans);
n = dims[0]; p = dims[1];
if(n) ny = (int)(XLENGTH(y)/n); /* y : n x ny, or an n - vector */
if(check && n * ny != XLENGTH(y))
error(_("dimensions of 'x' (%d,%d) and 'y' (%d) do not match"),
n,p, XLENGTH(y));
/* These lose attributes, so do after we have extracted dims */
if (TYPEOF(x) != REALSXP) {
PROTECT(x = coerceVector(x, REALSXP));
nprotect++;
}
if (TYPEOF(y) != REALSXP) {
PROTECT(y = coerceVector(y, REALSXP));
nprotect++;
}
/* < more code > */
}
drop_strata <- function(expr, in_plus = TRUE) {
if (rlang::is_call(expr, "+", n = 2) && in_plus) {
lhs <- drop_strata(expr[[2]], in_plus = in_plus)
rhs <- drop_strata(expr[[3]], in_plus = in_plus)
if (rlang::is_call(lhs, "strata")) {
rhs
} else if (rlang::is_call(rhs, "strata")) {
lhs
} else {
rlang::call2("+", lhs, rhs)
}
} else if (rlang::is_call(expr)) {
expr[-1] <- purrr::map(as.list(expr[-1]), drop_strata, in_plus = FALSE)
expr
} else {
expr
}
}
test_that("`drop_strata()` removes strata term in a series of `+` calls", {
expect_equal(
drop_strata(rlang::expr(a + strata(x))),
rlang::expr(a)
)
expect_equal(
drop_strata(rlang::expr(a + strata(x) + b)),
rlang::expr(a + b)
)
})
test_that("`drop_strata()` does not remove strata in other cases", {
expect_equal(
drop_strata(rlang::expr(a * (b + strata(x)))),
rlang::expr(a * (b + strata(x)))
)
})
predict(survival_model,
type = "survival",
time = 2)
time
: the time points at which the survival probability is estimated
predict(survival_model,
type = "survival",
eval_time = 2)
to avoid overloading your working memory.
Photo by Ken Suarez on Unsplash
Full talk at https://hfrick.github.io/2023-latinr/