Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longer object length is not a multiple of shorter object length? [duplicate]

Tags:

r

xts

I don't understand why R gives me a warning about "Longer object length is not a multiple of shorter object length"

I have this object which is generated by doing an aggregate over an xts series giving the weekday median:

u <- aggregate(d, list(Ukedag = format(index(d),"%w")), median)

1 314.0
2 282.5
3 270.0
4 267.0
5 240.5

Then I try to apply this to my original xts series, which looks like this (only a lot longer)

head(d)
2009-01-02 116
2009-01-05 256
2009-01-06 286

Using:

coredata(d) <- coredat(d) - u[format(index(d),"%w")];

Which results in a warning.

The intent is to subtract the weekday mean. It appears to work despite the warning, but what should I worry about?

Revised solution: Attempt 2

apply.daily(d, function(x) coredata(x) - u[format(index(x), "%w")] )

I did indeed have a serious error. This doesn't give any warnings and I tested it by doing:

apply.daily(d, function(x) u[format(index(x), "%w")] )

Then checking some dates, and it appeared that is was in alignment with the calendar.

like image 310
tovare Avatar asked Aug 03 '10 09:08

tovare


1 Answers

Yes, this is something that you should worry about. Check the length of your objects with nrow(). R can auto-replicate objects so that they're the same length if they differ, which means you might be performing operations on mismatched data.

In this case you have an obvious flaw in that your subtracting aggregated data from raw data. These will definitely be of different lengths. I suggest that you merge them as time series (using the dates), then locf(), then do your subtraction. Otherwise merge them by truncating the original dates to the same interval as the aggregated series. Just be very careful that you don't drop observations.

Lastly, as some general advice as you get started: look at the result of your computations to see if they make sense. You might even pull them into a spreadsheet and replicate the results.

like image 86
Shane Avatar answered Oct 29 '22 05:10

Shane