1
$\begingroup$

I have a time series (std) of 324 observations with no missing values, starting from January 1987 and ending in December 2013.

I want to regress via OLS the one in the question.

In R, the code:

 lm(dstd ~ lstd)

Where 'dstd' is the differenced variable, via:

 dstd<-diff(std,differences=1)

And lstd is the lagged variable via:

 lstd<-lag(std, k=1)

Does not work, as it gives the following error:

 Error in model.frame.default(formula = dstd ~ lstd, drop.unused.levels = TRUE) : 
 variable lengths differ (found for 'lstd')

Which is, of course, natural as the length of 'dstd' is 323 while the length of 'lstd' is 324.

Searching for the error, na.omit has been suggested as the answer for other models. However, no combinations of 'na.omit' or 'na.exclude' would work. This video (at the relevant time) says ' you need to change these', referring to these variables.

I have tried excluding while forming another object, as well as subsetting in the regression model, yet none of these seem to work.

What code would I need to fix this?

Here is a Minimal Reproducible Example:

An object 'stdshort' is created with the same characteristics of the parent object, via this code:

 > stdshort<-dput(window(std, 1987, 1989, 12))
 structure(c(4.5, 4.7, 4.2, 4.4, 3.9, 3.9, 3.7, 3.7, 3.4, 3.6, 
 3.5, 3.1, 3.5, 3.3, 3.7, 3.7, 3.7, 3.8, 3.6, 3.5, 3.5, 3.3, 3.5, 
 3.5, 3.4), .Tsp = c(1987, 1989, 12), class = "ts")

The following code replicates the steps previously mentioned: differencing, lagging and linear regression.

First, a differenced object is created. The object dtstdshort's structure looks like this:

 > dstdshort<-diff(stdshort,differences=1)
 > dput(dstdshort)
  structure(c(0.2, -0.5, 0.2, -0.5, 0, -0.2, 0, -0.3, 0.2, -0.1, 
 -0.4, 0.4, -0.2, 0.4, 0, 0, 0.0999999999999996, -0.2, -0.1, 0, 
 -0.2, 0.2, 0, -0.1), .Tsp = c(1987.08333333333, 1989, 12), class = "ts")

A following object, lstdshort, for 1 period lagging is constructed. This is the structure:

 > lstdshort<-lag(stdshort,k=1)
 > dput(lstdshort)
 structure(c(4.5, 4.7, 4.2, 4.4, 3.9, 3.9, 3.7, 3.7, 3.4, 3.6, 
 3.5, 3.1, 3.5, 3.3, 3.7, 3.7, 3.7, 3.8, 3.6, 3.5, 3.5, 3.3, 3.5, 
 3.5, 3.4), .Tsp = c(1986.91666666667, 1988.91666666667, 12), class = "ts")

The regression of those two objects as explained before, gives the following error (with code):

 > lm(dstdshort ~ lstdshort)
 Error in model.frame.default(formula = dstdshort ~ lstdshort, drop.unused.levels = TRUE) : 
   variable lengths differ (found for 'lstdshort')
$\endgroup$
7
  • 2
    $\begingroup$ This question appears to be off-topic because it is about programming without a reproducible example. $\endgroup$
    – Glen_b
    Commented Sep 12, 2014 at 11:35
  • $\begingroup$ Would uploading the data make it better? $\endgroup$
    – erasmortg
    Commented Sep 12, 2014 at 11:48
  • $\begingroup$ I voted to leave this question open because although it is phrased in terms of "code" it reflects a very basic problem that I believe will have a universal (code-independent) answer. $\endgroup$
    – whuber
    Commented Sep 12, 2014 at 13:40
  • $\begingroup$ I'd suggest the smallest reasonable example with the same issue. $\endgroup$
    – Glen_b
    Commented Sep 12, 2014 at 21:52
  • $\begingroup$ Added relevant Minimal Reproducible Example as requested. $\endgroup$
    – erasmortg
    Commented Sep 13, 2014 at 8:22

1 Answer 1

1
$\begingroup$

The function ts.intersect sets up the variables for you.

stdshortcomb <-  ts.intersect(dstdshort,lstdshort)
lm(dstdshort~lstdshort, stdshortcomb)

Call:
lm(formula = dstdshort ~ lstdshort, data = stdshortcomb)

Coefficients:
(Intercept)    lstdshort  
     0.6798      -0.1995  
$\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.