I have very large input of time series data (over 30000 observations) that look like as follows:
data = {{{2011, 1, 2, 0, 0, 0.}, 439.}, {{2011, 1, 3, 0, 0, 0.},
482.}, {{2011, 1, 4, 0, 0, 0.}, 600.}, {{2011, 1, 5, 0, 0, 0.},
540.}, {{2011, 1, 6, 0, 0, 0.}, 448.}, {{2011, 1, 7, 0, 0, 0.},
409.}, {{2011, 1, 8, 0, 0, 0.}, 427.}, {{2011, 1, 9, 0, 0, 0.},
428.}, {{2011, 1, 10, 0, 0, 0.}, 511.}, {{2011, 1, 11, 0, 0, 0.},....}
My purpose is to clean the outliers. Currently, I have to replace the outliers by using a new calculated mean that based on days of week. That is, mean of Monday will replace the outlier of Monday. The following is my code
Given that newmean variable is as follows:
newmean = {{Sunday, 398.231}, {Monday, 495.385}, {Tuesday, 452.}, {Wednesday,
440.308}, {Thursday, 427.692}, {Friday, 434.154}, {Saturday,
439.583}}
I want to replace newmean to the original input data based on day of week. Currently, I am using the following code:
CleanData[data_,newmean_]:=
Module[{result},
result = data;
Do[
result={#[[1]],(#[[2]]/.val_/;DayName@#[[1]]==newmean[[i,1]]&&NumberQ[val]
->newmean[[i,2]])}&/@result,
{i,Length@newmean}];
result
]
My question is how to improve this code ? Are there any better ways to replace data without specifying new variable (result) since the user-defined function (CleanData[data_, newmean_]) does not allow me to replace the input (data) ?
DayName
is (still) rather slow. See here for a discussion. (Used to be calledDayOfWeek
before version 9). $\endgroup$