How can I get the contribution by each predictor to the final regression prediction in lm Using R when I use rlm or lm I would like to get the contribution of each predictor of the model.
I mean once the model is fitted I have $ \hat{Y} = M . \hat{\beta} + \epsilon$ where $M$ is a matrix of p 'predictors'


*

*I can get $\hat{Y}$ using lm.predict (for out-sample) or $Y - \epsilon$ with the residuals(lmObj)(in-sample)

*I get $\hat{\beta}$ with coefficients(lmObj)
My problem is how do I get


*

*$M[,j] * \hat{\beta}_j$ (a column) ie: the contribution from each predictor, basically suming all those vectors would give $\hat{Y}$ ?


The problem occurs when I have a model with interaction terms
Sample data:
set.seed(1)                                              
y <- rnorm(10)                                           
m <- data.frame(v1=rnorm(10), v2=rnorm(10), v3=rnorm(10))
lmObj <- lm(formula=y~0+v1*v3+v2*v3, data=m)               
betaHat <- coefficients(lmObj)                           

betaHat
      v1       v3       v2    v1:v3    v3:v2
 0.03455 -0.50224 -0.57745  0.58905 -0.65592

# How do I get the data.frame or matrix with columns (v1,v3,v2,v1:v3,v3:v2) 
# worth [M$v1*v1, ... , (M$v3*M$v2)*v3:v2]

 A: If you have an interaction, then "the" contribution of one of the predictors does not exist, because it depends on the value of the interacting predictors. In your example with an interaction $v_1\times v_3$, the impact of $v_1$ will depend on the value of $v_3$.
In your example of continuous predictors, you could calculate the coefficient for $v_1$ if $v_3$ is set to a certain value, for instance a quantile $q$ of the observed values of $v_3$, say the median or a quartile. The coefficient of $v_1$ would then be
$$ 0.03455 + 0.58905 q.$$
If the interacting variable is not continuous, but categorical, you could do this calculation for all possible values of the interacting variable and then average over all these calculated coefficients. 
Of course, if you are interested in the effect of $v_3$ instead of $v_1$, you have the added complexity of having two interactions, so you will need to think about what quantile to set both $v_1$ and $v_2$ to. If your model is larger, you may start running into combinatorial problems.
You may be interested in Grömping, 2015, "Variable importance in regression models". Wiley Interdisciplinary Reviews: Computational Statistics (note that there was an erratum).
A: I found it on SO
I wanted term-wise prediction:
predict(lmObj, type = "terms") 

See ?predict.lm.
