I'm not sure why you want this, but it is trivial to get from fit
. First, it is best not to delve into fitted objects like this with $
. Instead learn to use extractor functions. In this case, the equivalent of mean(fit$model[,2])
would be, for all columns of the data at once:
> colMeans(model.frame(fit))
y x1 x2
2.0783225 0.0283555 1.0481141
The model frame is just a copy of the data. What you want is the design matrix, or as R calls it the model matrix, which, unsurprisingly is obtained using the model.matrix()
function.
> head(model.matrix(fit))
(Intercept) x1 x2 x1:x2
1 1 -0.33406119 1.95054087 -0.65160001
2 1 -1.41848058 0.35429591 -0.50256186
3 1 -1.32877702 -0.00783884 0.01041607
4 1 0.54054637 1.34637056 0.72777572
5 1 -0.75686319 -0.36476471 0.27607699
6 1 0.04514449 1.62928315 0.07355316
Note that the response data aren't in the design matrix, but the interaction term is, in the final column. Use colMeans()
again to get the mean of each column of this design matrix:
> colMeans(model.matrix(fit))
(Intercept) x1 x2 x1:x2
1.0000000 0.0283555 1.0481141 1.0820110
For completeness I should show this is correct for my random data set:
> colMeans(transform(df[,-1], interaction = x1 * x2))
x1 x2 interaction
0.0283555 1.0481141 1.0820110