data frequency conversion problem

1 次查看(过去 30 天)
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 个评论
salva
salva 2012-8-1
These data are stocks, So interpolation is needed. It would be wrong to divide them by two
thank you oleg
salva
salva 2012-8-2
Is it possible to have some code as a help?
thanks

请先登录,再进行评论。

采纳的回答

Oleg Komarov
Oleg Komarov 2012-8-2
编辑:Oleg Komarov 2012-8-4
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 个评论
salva
salva 2012-8-4
编辑:salva 2012-8-4
Thank you Oleg for your reply. Regarding how to treat NaNs I would like to interpolate over them before changing the data frequency. That is, we can just replace them via interpolation.
I found some material on this
The second link is maybe what I am looking for
Having done that, then we can change the frequency.
thank you
Oleg Komarov
Oleg Komarov 2012-8-4
Added NaN interpolation with John D'Errico's inpaint_nan() (but you could also use interp1).
See the graph for the result.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by