[R-SIG-Finance] Linear Regression on data from FRED

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Mar 2 00:19:20 CET 2011


Hi Stuart,

For future reference, this isn't really a finance question and would
be better suited for the R-help list.

There are no columns named "secA" or "secB" in your data.frame "t".
Looking at str(t) probably would have given you enough information to
solve this issue yourself.  Since "secA" and "secB" aren't in "t",
lm() has to search for them and finds them in your global environment.
 Those objects don't have the same length, hence the error.

You're also doing a lot of unnecessary steps.  All you need is:
> require(quantmod)
> getSymbols("DEXUSEU;DEXUSUK",src="FRED")
[1] "DEXUSEU" "DEXUSUK"
> Data <- merge(DEXUSEU, DEXUSUK, all=FALSE)
> (m <- lm(DEXUSEU ~ DEXUSUK + 0, data=Data))

Call:
lm(formula = DEXUSEU ~ DEXUSUK + 0, data = Data)

Coefficients:
DEXUSUK
  0.709

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



On Tue, Mar 1, 2011 at 3:55 PM, Stuart Snell <stuart.snell at gmail.com> wrote:
> I cannot see why the code below produces this error:-
>   Error in model.frame.default(formula = secA ~ secB + 0, data = t,
> drop.unused.levels = TRUE) :
>   variable lengths differ (found for 'secB')*
>
> I thought the merge would match up dates in secA and secB, therefore making
> the different lengths of secA and secB irrelevant.
>
> Any ideas.
>
> library(zoo)
> library(tseries)
> library(quantmod)   #for access to FRED
> require(quantmod)
> require(TTR)
> secA <- getSymbols("DEXUSEU",src="FRED") secB <-
> getSymbols("DEXUSUK",src="FRED")
> secA <- zoo(DEXUSEU[,1])
> secB <- zoo(DEXUSUK[,1])
> t.zoo <- merge(secA, secB, all=FALSE)
> t <- as.data.frame(t.zoo)
> cat("Date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")
> m <- lm(secA ~ secB + 0, data=t)
>
>
> Thanks
> Stuart
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>



More information about the R-SIG-Finance mailing list