MATLAB: How to do log-linear de-trending in data

detrendMATLAB

Here is the data that is about the GDP per capital of US and Japan from 1960 to 2015. Could you tell me how to use the log-linear detrending the data?

Best Answer

I’m not an econometrician, but on the basis of an online search, I would do it this way. I believe that doing a regression on the logarithm of a variable produces the wrong parameter estimates, and so the wrong end result, so I am doing an exponential regression here:
[d,s,r] = xlsread('sGDPpercapital.xlsx'); % Import Data
loglin_exp = @(b,x) b(1).*exp(b(2).*x) + b(3); % Exponential Objective Function
B0 = [1; -1; 1]*0.001; % Initial Parameter Estimates
for k1 = 1:2
y = d(:,k1+1); % Loop Through Data (USA = d(:,2), JPN - d(:,3))
SSECF = @(b) norm(y - loglin_exp(b,d(:,1))); % Sum-Squared Error Cost Function
B(:,k1) = fminsearch(SSECF, B0); % Estimate Parameters (Parameter Vector = B)
reg(:,k1) = loglin_exp(B(:,k1),d(:,1)); % Calculate Regression
end
figure(1)
plot(d(:,1), d(:,2))
hold on
plot(d(:,1), d(:,3))
plot(d(:,1), reg(:,1))
plot(d(:,1), reg(:,2))
hold off
grid
set(gca, 'YScale','log')
xlabel('Year (CE)')
ylabel('Per Capita GDP')
title('Original Data')
legend('United States', 'Japan', 'Location','E')
detrnd = d(:,2:3) ./ reg;
figure(2)
plot(d(:,1), detrnd(:,1))
hold on
plot(d(:,1), detrnd(:,2))
hold off
grid
set(gca, 'YScale','log')
xlabel('Year (CE)')
ylabel('Per Capita GDP')
title('Detrended Data')
legend('United States', 'Japan', 'Location','E')
EDIT Added comment documentation.