Question

So I'm having some trouble getting r to actually predict given a very, very simple linear model. Using the following,

> x=1:10
> y=1:10*2
> lm(y~x)

we get the corect answer, namely, y is twice x. But when I do,

predict(lm(y~x),newdata=2.5)

I just get

 1  2  3  4  5  6  7  8  9 10 
 1  2  3  4  5  6  7  8  9 10 

What is going on?

Was it helpful?

Solution

The newdata parameter should be a data.frame with column names matching the names used as covariates. So the correct case is

predict(lm(x~y),newdata=data.frame(y=2.5))

or

predict(lm(y~x),newdata=data.frame(x=2.5))

depending on which way you wanted to do the regression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top