I have been receiving a the above message while trying to test the accuracy of my model. The plan was to predict the last 15 time points and compare them to the actual data for error values, but for some reason I got the "Variable Lengths Differ" error message.
This is using johnson and johnson data (data(jj)) from the astsa package. Here is the code and relevant errors-
> ##set up JJ data and time because its quarterly data
> X.all<-jj[1:84]
> t<-time(jj)
>
> values<-length(t)-15
> ts<-t[1:values]
> tsq<-ts^2/factorial(2)
> X<-X.all[1:values]
> year.first<-values+1
> year.last<-length(t)
> ##setting t for 15 values using quarterly idea
> new<-data.frame(ts=t[year.first:year.last])
> X.true<-X.all[(values+1):length(t)]
> fit1<-lm(X~ts+tsq)
> Xhat<-predict(fit1,new,se.fit=TRUE)
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
variable lengths differ (found for 'tsq')
In addition: Warning message:
'newdata' had 15 rows but variables found have 69 rows
> X.hat<-round(Xhat$fit,2)
> error<-X.true-X.hat