1
votes

I am trying to run this code

 r2<-lm(diff(Y)~.,data=credit.train)

Now, I get the error:

Error in model.frame.default(formula = diff(Y) ~ ., data = credit.train, : variable lengths differ (found for 't')

I understand that on differencing, number of rows of Y is decreasing by 1 which is not happening for the X variables. Any idea how to tackle this?

2
Well, do you want to regress the differences in Y against the original values of your predictors or the new values of your predictors?Gregor Thomas

2 Answers

6
votes

You can extend at one end or the other (with loss of one set of predictors) using the I()-function

From the ?lm page:

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(I(c(diff(weight),NA)) ~ group)  # no error

Also no error just with:

lm.D9 <- lm( c(diff(weight),NA) ~ group)
1
votes

I guess you want to regress the increase of value Y, namely Y(n+1)-Y(n) against some variable x(n) or t(n) etc., so just filter out the last row from your data frame:

  r2 <- lm(diff(Y) ~ ., data = credit.train[1:(length(credit.train)-1), ])