How to score uncenterized term-wise prediction using predict.glm()

Viewed 122

I am building a simple GLM model as follows:

model1 = glm(y ~ x1 + x2 + x3, data=train)

And I use predict function to score new prediction

newpred = predict(object=model1, newdata= validation, type = 'term')

By specifying the option type = 'term' I was hoping to get the the individual term predictions (i.e., beta1 * x1, beta2 * x2 etc). However, it turned out the type = 'term' option would return 'Centerized' prediction that centers the column values at 0 (as explained here: What does predict.glm(, type="terms") actually do?)

My question is if there is a simple way to get the plain vanilla term prediction rather than the centerized term predictions. The model has categorical variables, I want a single term for each categorical variables (same as the output of the type = 'term' option) rather than a series of dummy indicator variables.

1 Answers

If your model is really that simple (e.g., only simple continuous predictor variables) then I feel like

X <- model.matrix(formula(model), data=train)
sweep(X, coef(model), MARGIN=2, FUN="*")

should work (I haven't tested); a lot of the complex internal machinery of predict(.,"terms") is for collecting columns that belong to the same "term" (e.g. a set of polynomial or spline coefficients).

Related