I have three datasets:
response - matrix of 5(samples) x 10(dependent variables)
predictors - matrix of 5(samples) x 2(independent variables)
test_set - matrix of 10(samples) x 10(dependent variables defined in response)
response <- matrix(sample.int(15, size = 5*10, replace = TRUE), nrow = 5, ncol = 10)
colnames(response) <- c("1_DV","2_DV","3_DV","4_DV","5_DV","6_DV","7_DV","8_DV","9_DV","10_DV")
predictors <- matrix(sample.int(15, size = 7*2, replace = TRUE), nrow = 5, ncol = 2)
colnames(predictors) <- c("1_IV","2_IV")
test_set <- matrix(sample.int(15, size = 10*2, replace = TRUE), nrow = 10, ncol = 2)
colnames(test_set) <- c("1_IV","2_IV")
I'm doing a multivariate linear model using a training set defined as the combination of response and predictor sets, and I would like to use this model to make predictions for the test set:
training_dataframe <- data.frame(predictors, response)
fit <- lm(response ~ predictors, data = training_dataframe)
predictions <- predict(fit, data.frame(test_set))
However, the results for predictions are really odd:
predictions
First off the matrix dimensions are 5 x 10, which is the number of samples in the response variable by the number of DVs.
I'm not very skilled with this type of analysis in R, but shouldn't I be getting a 10 x 10 matrix, so that I have predictions for each row in my test_set?
Any help with this issue would be greatly appreciated, Martin
You are stepping into a poorly supported part in R. The model class you have is "mlm", i.e., "multiple linear models", which is not the standard "lm" class. You get it when you have several (independent) response variables for a common set of covariates / predictors. Although
lm()
function can fit such model,predict
method is poor for "mlm" class. If you look atmethods(predict)
, you would see apredict.mlm*
. Normally for a linear model with "lm" class,predict.lm
is called when you callpredict
; but for a "mlm" class thepredict.mlm*
is called.predict.mlm*
is too primitive. It does not allowse.fit
, i.e., it can not produce prediction errors, confidence / prediction intervals, etc, although this is possible in theory. It can only compute prediction mean. If so, why do we want to usepredict.mlm*
at all?! The prediction mean can be obtained by a trivial matrix-matrix multiplication (in standard "lm" class this is a matrix-vector multiplication), so we can do it on our own.Consider this small, reproduce example.
When you have a prediction data set:
we first need to pad an intercept column
Then do this matrix multiplication
Perhaps you have noticed that I did not even use a data frame here. Yes it is unnecessary as you have everything in matrix form. For those R wizards, maybe using
lm.fit
or evenqr.solve
is more straightforward.But as a complete answer, it is a must to demonstrate how to use
predict.mlm
to get our desired result.Note the
I()
when I usedata.frame()
. This is a must when we want to obtain a data frame of matrices. You can compare the difference between:Without
I()
to protect the matrix input, data are messy. It is amazing that this will not cause problem tolm
, butpredict.mlm
will have a hard time obtaining the correct matrix for prediction, if you don't useI()
.Well, I would recommend using a "list" instead of a "data frame" in this case.
data
argument inlm
as wellnewdata
argument inpredict
allows list input. A "list" is a more general structure than a data frame, which can hold any data structure without difficulty. We can do:Perhaps in the very end, I should stress that it is always safe to use formula interface, rather than matrix interface. I will use R built-in dataset
trees
as a reproducible example.Perhaps you still remember my saying that
predict.mlm*
is too primitive to supportse.fit
. This is the chance to test it.Oops... How about confidence / prediction intervals (actually without the ability to compute standard error it is impossible to produce those intervals)? Well,
predict.mlm*
will just ignore it.So this is so different compared with
predict.lm
.