Skip to main content
100%

Predict.lm not working for matrix data

✓ Published0🌍 Public
LLeeMendelowitz
Last edited Jun 17, 2014
Created on Jun 17, 2014

This example shows the pitfalls of using R's `lm` function with matrix data. It demonstrates that `lm(y ~ X, data)` produces predictions of the wrong length when `X` is a matrix, while explicitly naming columns (`lm(y ~ X.1 + X.2 + X.3)`) or building a formula dynamically works correctly. The code uses the `poly` function to create a cubic polynomial regressor matrix and `predict.lm` to generate fitted values plotted with base R's `plot` and `lines`. A custom `make.predict` function highlights the length mismatch and logs warnings for erroneous cases.

AI-generated description

Similar vizzes