Starting in version 3.4, grpreg offers an interface for setting up, fitting, and visualizing additive models. Numeric features are automatically expanded using spline basis functions. The basic idea was first proposed by Ravikumar et al. (2009), who called it SPAM, for sparse additive models. The original proposal involved the group lasso penalty, but any of grpreg’s penalty functions can be used instead. The basic usage is illustrated below.

Let’s start by generating some nonlinear data:

Data <- gen_nonlinear_data(n=1000)
Data$X[1:5, 1:5] # V01 V02 V03 V04 V05 # [1,] 0.64911299 0.03224357 0.9933272 0.9287773 0.5569106 # [2,] 0.81727454 0.84579373 0.5652167 0.4362362 0.8743847 # [3,] 0.36158335 0.34545913 0.1125257 0.6107257 0.7968171 # [4,] 0.04955389 0.61866125 0.4741857 0.9689460 0.9410883 # [5,] 0.75974467 0.30542934 0.1367209 0.8086477 0.6281695 dim(Data$X)
# [1] 1000   16

The matrix Data$X contains 16 numeric features, named V01, V02, and so on. Each of those features can be expanded via the expand_spline() function: X <- expand_spline(Data$X)
X$X[1:5, 1:5] # V01_1 V01_2 V01_3 V02_1 V02_2 # [1,] 0.54833503 0.3530067 -0.10341703 -0.023635120 0.07069011 # [2,] 0.39077855 0.3306963 0.25011939 0.309934267 0.34295057 # [3,] 0.01835036 0.5467323 -0.36380505 0.004608488 0.54817560 # [4,] -0.03594686 0.1089585 -0.07250477 0.524502175 0.37108888 # [5,] 0.49723305 0.3207741 0.11724579 -0.048667101 0.53249143 dim(X$X)
# [1] 1000   48
head(X$group) # [1] "V01" "V01" "V01" "V02" "V02" "V02" The resulting object is a list that contains the expanded matrix X$X and the group assignments X$group, along with some metadata needed by internal functions. Note that X$X now contains 48 columns – each of the 16 numeric features (V01) has been expanded into a 3-column matrix (V01_1, V01_2, and V01_3). By default, expand_spline() uses natural cubic splines with three degrees of freedom, but consult its documentation for additional options.

This expanded matrix can now be passed to grpreg():

fit <- grpreg(X, Data$y) Note that it is not necessary to pass grouping information in this case, as it is contained with the X object. At this point, all of the usual tools coef(), predict(), etc., can be used, as well as plot.grpreg(). However, grpreg also offers a function, plot_spline(), specific to additive models: plot_spline(fit, "V02", lambda = 0.03) Partial residuals can be included in these plots as well: plot_spline(fit, "V02", lambda = 0.03, partial=TRUE) By default, these plots are centered such that at the mean of $$x$$ (where $$x$$ denotes the feature being plotted), the $$y$$ value is zero. Alternatively, if type="conditional" is specified, plot_spline() will construct a plot in which the vertical axis represents model predictions as $$x$$ varies and all other features are fixed at their mean value: plot_spline(fit, "V02", lambda = 0.03, partial=TRUE, type='conditional') In comparing these two plots, note that the general contours are the same; the only difference is the value of the vertical axis. Here are the plots for the first 9 coefficients: for (i in 1:9) plot_spline(fit, sprintf("V%02d", i), lambda = 0.03, partial=TRUE, warn=FALSE) In the generating model, variables 3 and 4 had a linear relationship with the outcome, variables 1, 2, 5, and 6 had nonlinear relationships, and all other variables were unrelated. The sparse additive model has captured this nicely. These tools work with cross-validation as one would expect (by default plotting the fit that minimizes cross-validation error): cvfit <- cv.grpreg(X, Data$y)
plot_spline(cvfit, "V02", partial=TRUE)

Finally, these tools work with survival and glm models as well. Here, all plots are returned on the linear predictor scale, and the residuals are deviance residuals.