Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugzilla#18706] recognize stats::offset() in terms() #163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/library/stats/src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,13 @@ static int TermCode(SEXP termlist, SEXP thisterm, int whichbit, SEXP term)
}


int isOffset(const char *s) {
if (!strncmp(s, "offset(", 7)) return 1;
if (strncmp(s, "stats::", 7)) return 0;
int callStart = strncmp(s+7, ":", 1) ? 7 : 8;
return !strncmp(s+callStart, "offset(", 7);
}

/* Internal code for the ``terms'' function */
/* The value is a formula with an assortment */
/* of useful attributes. */
Expand Down Expand Up @@ -1897,8 +1904,9 @@ SEXP termsform(SEXP args)

/* first see if any of the variables are offsets */
R_xlen_t k = 0;
for (R_xlen_t l = response; l < nvar; l++)
if (!strncmp(CHAR(STRING_ELT(varnames, l)), "offset(", 7)) k++;
for (R_xlen_t l = response; l < nvar; l++) {
if (isOffset(CHAR(STRING_ELT(varnames, l)))) k++;
}
if (k > 0) {
#ifdef DEBUG_terms
Rprintf(" step 2b: found k=%ld offset(.)s\n", k);
Expand All @@ -1907,7 +1915,7 @@ SEXP termsform(SEXP args)
/* allocate the "offsets" attribute */
SETCAR(a, v = allocVector(INTSXP, k));
for (int l = response, k = 0; l < nvar; l++)
if (!strncmp(CHAR(STRING_ELT(varnames, l)), "offset(", 7))
if (isOffset(CHAR(STRING_ELT(varnames, l))))
INTEGER(v)[k++] = l + 1;
SET_TAG(a, install("offset"));
a = CDR(a);
Expand All @@ -1923,7 +1931,7 @@ SEXP termsform(SEXP args)
if(length(thisterm) == 0) break;
for (int i = 1; i <= nvar; i++)
if (GetBit(CAR(thisterm), i) &&
!strncmp(CHAR(STRING_ELT(varnames, i-1)), "offset(", 7)) {
isOffset(CHAR(STRING_ELT(varnames, i-1)))) {
have_offset = TRUE;
#ifdef DEBUG_terms
Rprintf(" i=%d: have_offset, ", i);
Expand Down
7 changes: 7 additions & 0 deletions src/library/stats/tests/offsets.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ stopifnot(exprs = {
is.null(model.weights(model.frame(fit5)))
})
## these were all (even the model fit!) wrong in R <= 4.2.1

## via formula, with stats:: namespace-qualification (#18706)
dropIrrelevant <- \(x) x[!names(x) %in% c("variables", ".Environment")]
termAttr1 <- dropIrrelevant(attributes(terms(y ~ 1 + offset(x))))
termAttr2 <- dropIrrelevant(attributes(terms(y ~ 1 + stats::offset(x))))
termAttr3 <- dropIrrelevant(attributes(terms(y ~ 1 + stats:::offset(x))))
stopifnot(identical(termAttr1, termAttr2), identical(termAttr2, termAttr3))
Loading