function [y,x,n,T,dates_] = load_data(infl_id,lags,start_date,end_date,standardize,select,trans)

    %% load_data.m   Function for Loading the data
    % =========================================================================
    % Written by: 
    %
    %    Maximilian Schroeder
    %    Norwegian BI Business School
    %
    % First Version: 10 April 2024
    % This version : 10 April 2024
    % =========================================================================
    
    
    %% |---- Inflation data
    data = table2timetable(readtable('data.xlsx'));
    
    dates_ = data.Properties.RowTimes;
    starid = find(ismember(dates_,start_date));
    endid  = find(ismember(dates_,end_date));
    y = table2array(data(starid-1:endid,find(ismember(data.Properties.VariableNames,infl_id))));
    
    if ~strcmp(infl_id,'A191RI1Q225SBEA')
        y = 400*(log(y(2:end))-log(y(1:end-1)));
    else
        y = y(2:end);
    end
    
    dates_ = dates_(starid:endid);
    
    %% |---- Aggregate Monthly data
    
    data_m = table2timetable(readtable('indices.xlsx','sheet','monthly'));
    data_m = retime(data_m,'quarterly','mean');
    dates_m = data_m.Properties.RowTimes;
    starid = find(ismember(dates_m,start_date));
    endid  = find(ismember(dates_m,end_date));
    
          
    idd = [];
    for i = 1:length(select)
       idd = [idd find(ismember(data_m.Properties.VariableNames,select(i)))]; 
    end
    data_m = table2array(data_m(starid-1:endid, idd));
    data_m_trans = zeros(size(data_m,1)-1,length(select));
    for i = 1:length(select)
        if trans(i) == 4
            data_m_trans(:,i) = 400*(log(data_m(2:end,i))-log(data_m(1:end-1,i)));
        else
            data_m_trans(:,i) = data_m(2:end,i);
        end
    end
    
    %% |------- Construct Business Cycle Factor
    
    [data,chars] = xlsread('FREDQD.xlsx','quarterly');
    factors      = data(1,:);
    dates_panel  = datetime(chars(5:end,1),'InputFormat','MM/dd/uuuu');
    names_panel  = chars(1,2:end);
    groups       = data(2,:);
    transf       = data(3,:);
    data         = data(4:end,:);
    
    starid = find(ismember(dates_panel,datetime(start_date)+calmonths(2)));
    endid  = find(ismember(dates_panel,datetime(end_date)+calmonths(2)));
    
    % drop aggregated stuff
    data        = data(starid-3:endid,logical(factors));
    groups      = groups(logical(factors));
    names_panel = names_panel(logical(factors));
    transf      = transf(logical(factors));
    
    data_trans = zeros(size(data));
    for i = 1:size(data,2)
        data_trans(:,i) = transxf(data(:,i),transf(i));
    end
    
    data_trans = data_trans(4:end,:);
    data_trans = data_trans(:,~sum(isnan(data_trans))>0);
    
    
    
    xx = lagmatrix(data_trans,1);
    [~,~,xs,~,beta] = plsregress(zscore(xx(2:end,:)),y(2:end),1); 
    ffac = [ones(size(data_trans,1),1) zscore(data_trans)]*beta;
 
    x = [lagmatrix(y,[0:lags-1]) ffac data_m_trans];
    y = y(lags+1:end,:);
    x = x(lags+1:end,:);
    
    dates_ = dates_(lags+1:end);
    
    % standardize
    meanX = mean(x);
    stdX   = std(x);
    meanY = mean(y);
    stdY   = std(y);
    
    if standardize == 1
        y = (y-meanY)./stdY;
        x = (x-meanX)./stdX;   
    elseif standardize == 2
        x = (x-meanX)./stdX;   
    end
    
    % construct regressors
    n = length(y);
    x = [ones(n,1) x];
    
    T = size(y,1);
end