data frequency conversion problem
1 view (last 30 days)
Show older comments
Dear all,
Is there any function for converting data that are available every 2 months to monthly data
thank you
EDIT [01 Aug 2012, 19:33 BST - OK] Added example input from comment
Here is an example of a panel data set with 3 individuals
A = {
1 '1-2 2004' 0.256 0.385
1 '3-4 2004' 0.268 3.0394
1 '5-6 2004' 0.0504 0.6475
1 '7-8 2004' 14.0985 148.2583
1 '9-10 2004' 0.1128 1.1506
1 '11-12 2004' NaN 148.2583
1 '1-2 2005' NaN 148.2583
1 '3-4 2005' 2.5852 34.0146
1 '5-6 2005' 0.322 3.2846
1 '7-8 2005' 14.0985 148.2583
1 '9-10 2005' 2.5852 NaN
1 '11-12 2005' 0.2938 2.854
2 '1-2 2004' 0.256 0.385
2 '3-4 2004' 0.268 3.0394
2 '5-6 2004' 0.0504 0.6475
2 '7-8 2004' 14.0985 148.2583
2 '9-10 2004' 0.1128 1.1506
2 '11-12 2004' NaN 148.2583
2 '1-2 2005' NaN 148.2583
2 '3-4 2005' 2.5852 34.0146
2 '5-6 2005' 0.322 3.2846
2 '7-8 2005' 14.0985 148.2583
2 '9-10 2005' 2.5852 NaN
2 '11-12 2005' 0.2938 2.854
3 '1-2 2004' 0.256 0.385
3 '3-4 2004' 0.268 3.0394
3 '5-6 2004' 0.0504 0.6475
3 '7-8 2004' 14.0985 148.2583
3 '9-10 2004' 0.1128 1.1506
3 '11-12 2004' NaN 148.2583
3 '1-2 2005' NaN 148.2583
3 '3-4 2005' 2.5852 34.0146
3 '5-6 2005' 0.322 3.2846
3 '7-8 2005' 14.0985 148.2583
3 '9-10 2005' 2.5852 NaN
3 '11-12 2005' 0.2938 2.854}
Note that I have 30000 invividuals (instead of 3) and 20 numerical columns instead of the last 2 that I display above. The interpolation should be done for each i=1,2,3 separately.
10 Comments
Accepted Answer
Oleg Komarov
on 2 Aug 2012
Edited: Oleg Komarov
on 4 Aug 2012
EDIT#2 I didn't notice at first that it had different series. Added also linear interpolation of NaNs:
% Inpaint NaNs and keep numeric matrix (easier to work than with cell % arrays) with inpaint_nans() from the FEX.
data = inpaint_nans(cell2mat(A(:,3:4)),2);
% Partition interpolation in blocks (first column)
blocks = [A{:,1}];
unBlocks = unique(blocks);
% Preallocate
interpData = cell(numel(unBlocks),1);
% Interpolate each block
for b = unBlocks
idxBlock = b == blocks; % index the block
n = nnz(idxBlock)*2; % counts its length
interpData{b} = interp1((1:2:n)', data(idxBlock,:),(1:n-1)');
end
% This plot gives an idea of the type of interpolation (for the first block/series only)
subplot(311)
plot(1:2:n,[A{1:15,3}],'-or')
legend('original')
subplot(312)
plot(1:n-1,interpData{1}(:,1)','-db')
legend('linear interp')
subplot(313)
plot(1:2:n,[A{1:15,3}],'-or',1:n-1,interpData{1}(:,1)','-db')
As you can see, you need to decide what to do with NaN's (especially to avoid losing info)
% Concatenate the cells into one numeric matrix (optional)
interpData = cat(1,interpData{:});
WARNING: with this approach I assume every month has same length.
4 Comments
Oleg Komarov
on 4 Aug 2012
Added NaN interpolation with John D'Errico's inpaint_nan() (but you could also use interp1).
See the graph for the result.
More Answers (0)
See Also
Categories
Find more on Interpolating Gridded Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!