2
votes

I want to make a scatter plot of two matrices, including a trend line. My two matrices are: growth and trade. Both are of the size NxT, where N is the number of countries and T the number of observations (per country). I want to see whether growth and trade have a relationship for the entire sample (for all countries). So far, my code looks as follows:

figure
for j=1:N
    scatter(growth(:,j),trade(:,j))
    xlabel('growth')
    ylabel('trade')
    hold on
    lsline
end
hold off

The scatter plot looks fine but when I want to include a fitted line in order to see whether there exists a relationship between the two matrices I obtain a fitted line for each individual country. How can I get a fitted line between the two entire matrices in my scatter plot? In other words, I would like my scatter plot to include a fitted line for all countries, not for each individual countries. Thanks in advance.

2
You have to fit the full matrix, not just series of 2d fits. This can help you: polyfitn - Hoki

2 Answers

0
votes

You will then have to calculate the trendline for all the countries, not the countries individually. Which means that you can't lsline in each iteration of the loop, but will have to calculate a least squares fit for the column-wise aggregate of your matrix.

0
votes

What if you treat all values as one long vector? You would basically skip the country element and treat them all as growth and trade regardless of country.

% //Make all data points one long vector as all points represent the same thing
trade = reshape (trade,[1 prod(size(trade))]);
growth = reshape (growth,[1 prod(size(growth))]);

% //Normalise the data between 0-1 so that we can compare them
trade_norm = (trade - min(trade))./ (max(trade)-min(trade));
growth_norm = (growth - min(growth)) ./ (max(growth)-min(growth));

% //then fit a line
pf = polyfit( trade_norm, growth_norm, 1);
x1 = trade_norm;
y1 = polyval(pf, x1);

enter image description here