# [R] Novice question

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 4 19:08:26 CEST 2003

```Could you try to use a sensible subject line, and a description of your
problem?  We are left guessing what you really meant.

If you want to regress xm on ys, you need

lm(xm ~ ys)

You seem to have a single y and two x's, in which case this is not
`multivariate linear regression' and you want

lm(ys ~ xs1 + xs2)

You got the predictions from xm, which is not in samples.

On Mon, 4 Aug 2003, Mike Mascari wrote:

> Hello.
>
> I am new R user, so this question is probably quite stupid, but for
> the life of me I cannot figure out how to get predications using
> multivariate linear regression analysis. Single variable predictions
> work fine. I am trying the following:
>
> -- Known y's for known x's1 and x's2
>
> ys <- c(133890, 135000, 135790, 137300, 138130, 139100, 139900,
> 141120, 141890, 143230, 144000, 145290)
>
> xs1 <- c(1:12)
> xs2 <- c(22, 24.5, 27, 33, 36.8, 40, 44, 57, 59, 62, 74, 77)
> xm <- cbind(xs1, xs2)
>
> -- New x's1 and x's2
>
> nx1 <- c(13:17)
> nx2 <- c(82, 85, 88.3, 90, 95)
>
> -- Generate some predictions
>
> samples <- data.frame(xs1=nx1, xs2=nx2)
> f <- predict(lm(ys ~ xm), samples)
>
> data.frame(f) yields:
>
>          f
> 1  133949.8
> 2  134970.2
> 3  135990.6
> 4  137008.1
> 5  138027.5
> 6  139047.3
> 7  140066.5
> 8  141078.3
> 9  142099.1
> 10 143119.1
> 11 144131.7
> 12 145151.7
>
> Not the predicted y's for the new x1's and x2's. I tried:
>
> f <- predict.mlm(lm(ys ~ xm), samples) and got:
>
> Error in object\$coefficients[piv, ] : incorrect number of dimensions
>
> I would have though that maybe it would be something like:
>
> f <- predict(mlm(ys ~ xm), samples)
>
> but there doesn't appear to be an mlm().
>
> If the y's and x1's look familiar, they are out of the Excel
> documentation for the TREND() function, which I am attempted to
> reproduce in R.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```