1

How do I remove the intercept from the prediction when using predict.glm? I'm not talking about the model itself, just in the prediction. For example, I want to get the difference and standard error between x=1 and x=3

I tried putting newdata=list(x=2), intercept = NULL when using predict.glm and it doesn't work

So for example:

m <- glm(speed ~ dist, data=cars, family=gaussian(link="identity"))
prediction <- predict.glm(m, newdata=list(dist=c(2)), type="response", se.fit=T, intercept=NULL)
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Ash
  • 11
  • 2
  • 1
    Your question is not clear. You want to create a model with an intercept and then use the `predict()` function on that model to generate predictions that do not include the entire model (that is the intercept). Can you provide a small data set illustrating what you are trying to do? Alternatively use the `iris` data set that comes with R to illustrate your question. – dcarlson Feb 09 '23 at 03:09
  • I added some coding as an example – Ash Feb 09 '23 at 04:20

1 Answers1

0

I'm not sure if this is somehow implemented in predict, but you could the following trick1.

Add a manual intercept column (i.e. a vector of 1s) to the data and use it in the model while adding 0 to RHS of formula (to remove the "automatic" intercept).

cars$intercept <- 1L

m <- glm(speed ~ 0 + intercept + dist, family=gaussian, data=cars)

This gives us an intercept column in the model.frame, internally used by predict,

model.frame(m)
#    speed intercept dist
# 1      4         1    2
# 2      4         1   10
# 3      7         1    4
# 4      7         1   22
# ...

which allows us to set it to an arbitrary value such as zero.

predict.glm(m, newdata=list(dist=2, intercept=0), type="response", se.fit=TRUE)
# $fit
# 1 
# 0.3311351 
# 
# $se.fit
# [1] 0.03498896
# 
# $residual.scale
# [1] 3.155753
jay.sf
  • 60,139
  • 8
  • 53
  • 110