[R] Getting lm() to work with a matrix
Berwin A Turlach
berwin at maths.uwa.edu.au
Wed May 20 16:39:16 CEST 2009
G'day Luc,
On Wed, 20 May 2009 09:58:41 -0400
Luc Villandre <villandl at dms.umontreal.ca> wrote:
> MikSmith wrote:
> > [...]
> Indeed, functions like /lm()/ require the object fed to the /data/
> argument to be either [...]
But the data argument is optional and does not need to be specified.
> In your situation, I suggest you typecast your matrix into a data
> frame using /as.data.frame()/. [...]
My guess is that he is already working with a data frame and does not
work with matrices, otherwise he should not have encountered problems:
R> response <- matrix(rnorm(120), ncol=4)
R> spectra.spec <- matrix(rnorm(900), ncol=30)
R> spectra.lm <- lm(response[,3]~spectra.spec[,2:20])
R> spectra.lm
Call:
lm(formula = response[, 3] ~ spectra.spec[, 2:20])
Coefficients:
(Intercept) spectra.spec[, 2:20]1
-0.48404 0.42503
spectra.spec[, 2:20]2 spectra.spec[, 2:20]3
-0.08955 -0.27605
spectra.spec[, 2:20]4 spectra.spec[, 2:20]5
-0.16832 -0.14107
spectra.spec[, 2:20]6 spectra.spec[, 2:20]7
-0.47009 -0.23672
spectra.spec[, 2:20]8 spectra.spec[, 2:20]9
0.12920 0.23306
spectra.spec[, 2:20]10 spectra.spec[, 2:20]11
-0.28586 0.03579
spectra.spec[, 2:20]12 spectra.spec[, 2:20]13
0.10676 -0.34407
spectra.spec[, 2:20]14 spectra.spec[, 2:20]15
0.20253 -0.17259
spectra.spec[, 2:20]16 spectra.spec[, 2:20]17
0.19765 0.40705
spectra.spec[, 2:20]18 spectra.spec[, 2:20]19
-0.12448 -0.17149
Cheers,
Berwin
More information about the R-help
mailing list