1
$\begingroup$

I have a dataset dimensions 1142obs in 454 variables. I've used 'caret' to separate into training and testing datasets.
training =858 obs of 99 var
testing =284obs of 99 var

I make a linear regression model like so (apologies in advance for the extra $ sign before response, which I include here otherwise it won't show correctly):

lm1 <-lm(training$$response ~ training$balloons, data=training)

I then try to make a prediction like so:

lm.predict <-predict(lm1, newdata=testing)

R comes up with:

Warning message:  
'newdata' had 284 rows but variables found have 858 rows  

lm.predict also generates a vector 858 numbers long, instead of 284. Any ideas?

$\endgroup$

1 Answer 1

2
$\begingroup$

Cut down your problem to a minimal reproducible example and supply data or generate it. For your problem I can reproduce with a data frame with two columns (we don't care if its 99 or 2 columns):

training = data.frame(response=runif(858), balloons=runif(858))
testing = data.frame(response=runif(284), balloons=runif(284))

Now you are fitting the model thus, and getting this error:

lm1 <-lm(training$response ~ training$balloons, data=training)
lm.predict <-predict(lm1, newdata=testing)
## Warning message:
## 'newdata' had 284 rows but variables found have 858 rows 

Your problem is because you are specifying vectors in the model (ie columns of training using the $ column notation) and also specifying a data frame in the data argument (which is ignored). You get the same error if you leave it out:

> lm1 <-lm(training$response ~ training$balloons)
> lm.predict <-predict(lm1, newdata=testing)
Warning message:
'newdata' had 284 rows but variables found have 858 rows 

If your observations are coming from a data frame then you don't need to specify the data as vectors, you give a formula which is evaluated in the context of the data frame:

> lm1 <-lm(response ~ balloons, data=training)

and then:

> lm.predict <-predict(lm1, newdata=testing)

which is 284 predictions as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.