1.4.11 Matching R🔗ℹ

R’s glmnet takes a matrix x and a response y, and has no formula interface. A formula fit is R’s glmnet on R’s model.matrix(formula, data) without its intercept column, with intercept set to the formula’s. The parity tests check, on R’s mtcars, longley and iris, that each formula expands to the terms R’s terms gives, in R’s order, that formula-design-matrix is R’s model matrix, names and values, that a factor’s levels are R’s, that the fit is R’s, and that new data is coded and predicted as R’s predict codes and predicts it; and this package’s tests require a formula fit to be equal? to the matrix fit of its design matrix for every family. From R also comes how the results are named: R’s coef labels the intercept (Intercept) and each predictor by its column name, wt:hp for an interaction and Speciesversicolor for a level, and names the elements of a multinomial or multi-response result by class or response. The one change is the name of a transform or a factor, its Racket source: R’s log(hp), I(hp^2), log(hp):wt and factor(cyl)6 are "(log hp)", "(I (expt hp 2))" or "(sqr hp)", "(log hp):wt" and "(factor cyl)6" here, and the parity tests map R’s names to these before they compare them.

The formula language is smaller than R’s:

  • A factor is coded by treatment contrasts, R’s default, with its first level as the baseline. R’s other contrasts, such as contr.sum and the polynomial contrasts of ordered factors, and its ways to choose a baseline, relevel() and factor(x, levels = ...), have no counterpart.

  • String levels are sorted by code point, which is R’s order in the C locale. In another locale R sorts letters without regard to case first, and can choose a different baseline.

  • A level of (factor x) is labelled as Racket prints the number, "100000" where R writes 1e+05, and two numbers that R prints the same, to 15 digits, are two levels here.

  • A table has no factor type, so a column’s levels are the values it has: R keeps a factor’s unused levels as columns of zeros.

  • A transform is evaluated one row at a time, where R evaluates it on whole columns. R’s transforms that read the whole column, such as scale(x), x - mean(x) and poly(x, 2), the orthogonal polynomials, have no counterpart; add such columns to the table instead. R’s poly(x, 2, raw = TRUE) is x and (sqr x).

  • A transform’s names are resolved on the table it is fitted to, and predict reads the same columns of a new table. R resolves them again on the new data, where a name that the new data lacks falls back on a variable of the same name.

  • The response is a column, not a transform: R’s log(y) ~ x has no counterpart, and (~ (log y) x) is a syntax error that says so; add the column to the table. A response of several columns whose first name is a function where the formula is written reads as such a transform, so write those columns as strings.

  • R’s . is written all. R’s %in% and / (nesting) are syntax errors that say the language does not have them; for /, the error says to write a ratio as (I (/ x z)), R’s I(x / z). offset() has no counterpart.

  • An operator needs spaces around it, since the reader reads wt:hp as one name and -0 as 0, and a power is an exact integer of at least 2, where R truncates x^2.5 to x^2.

  • A column keeps the table’s name in the design matrix, where R’s model.matrix puts backticks around a name that R’s syntax does not allow, such as `blood pressure`. So a column named "wt:hp" beside the interaction of wt and hp, which R names `wt:hp` and wt:hp, gives two columns of the same name here, which is an error, since coef keys the coefficients by name; and so is a column named "(Intercept)", the intercept’s name.

  • The columns of a (surv time status) or multi-column response are each dropped from the right-hand side, as R drops a one-column response. R’s response there is the one variable Surv(time, status) or cbind(y1, y2), so R would keep time as a predictor.

  • A missing or non-finite value is an error that names its column and row, where R’s default na.action would drop the row. So is a transform’s value that is not a finite real, which names the transform: R drops a row where log(x) is NaN and keeps one where it is -Inf.