%% Week 3 Homework (Solution) — WB data panel → growth → grouping → plots
clear all; close all; clc;


%% Step 1) Import data for ALL countries from CVS
%% WORLD BANK: Annual Nominal GDP (current US$)
%% ----------- Real GDP  (Index, 2015=100) -----------
% Read the data from the CSV file
file = 'Input/NAME.csv'; %%--> CHANGE WITH THE NAME OF THE CVS FILE YOU DOWNLOADED

% 1) Detect options and treat unknown string as missing
opts = detectImportOptions(file, 'TextType', 'string'); % strings are easier to coerce
opts = setvaropts(opts, opts.VariableNames, ...
    'TreatAsMissing', {'', ' ', '..', 'n/a', 'NA', 'NaN', 'i'});  % <-- key fix

% 2) Force variables to double (NaN fill is automatic for missing)
opts = setvartype(opts, 'double');

% 3) Read
data_rgdp = readtable(file, opts);

%Now inspect the table and find the period column
%%%--->STUDENTS TO FILL <---


%Now inspect the second column name 
second_column_name = data_rgdp.Properties.VariableNames{2};
%disp(second_column_name); %Annual_GDP_constant2015US___Aruba_WB_WDI_A_NY_GDP_MKTP_KD_ABW_
%Now we need to extract from the second column name the country name and the country code
%First split the words in the string separated by _
split_name = strsplit(second_column_name, '_');
%Inspect split_name and find that the country name is the fourth element and the country code is the second to last element
country_name = split_name{4};
country_code = split_name{end-1};
%Now we need to extract the values for the country code
values_rgdp = data_rgdp.(second_column_name);

% Now subsitute the column names in the table data_rgdp with the country
% code and create wide table (Step 2)
WB_table=data_rgdp;
%%%--->STUDENTS TO FILL <---




%% Step 3) Compute Logs and YoY growth rates per each country only for positive values
% Initialize the log GDP column in the WB_table
WB_table_LogGDP =WB_table;

%%%--->STUDENTS TO FILL <---


%%%--->STUDENTS TO FILL <---



%% Step 4) Check that you get then same results as in homework from week 1
%%%--->STUDENTS TO FILL <---



%% Step 5) Select some countries of your choice and plot their Real GDP on the same graph
%%%--->STUDENTS TO FILL <---


%% Step 6) Export the table with the panel of selected countries with the growth rates to CSV
%%%--->STUDENTS TO FILL <---







