【问题标题】:How to use two dataset in the same code in one shot in MATLAB?如何在 MATLAB 中一次性使用同一代码中的两个数据集?
【发布时间】:2020-09-06 19:11:08
【问题描述】:

我有一个运行平稳的预先指定的模型。问题是我有两个相同模型的数据集。这两个数据集仅根据行进行更改:例如数据集 1 从 1 到 60,数据集 2 从 61 到 115。所有变量都相同。我想避免两次计算相同的代码。我宁愿把它写得好,一口气计算出来。

我会给你一个带有我的模型的示例数据集:

data = rand(115,5)

Y_data = data(1:60, :) % dataset 1
Y_data = data(61:115, :) % dataset 2

% This is the model that runs nicely on dataset Y_data. I wanted to avoid to run the model twice,
% first with Y_data from row 1 to 60 and then from row to 61 to 100. I would like to do it in one shot
% the code for the model is fully automated so it's just a matter of making it work first on dataset 1 and then
% on dataset 2 in one unique code
T = size(Y_data,1);
P  = 3; % number of lags used in LP for controls
H_min = 1; 
H_max = 25; 
y  = Y_data(:,1); % endogenous variable
x  = Y_data(:,2); % shock 
w  = lagmatrix(Y_data(:,[3:5]), 1:P ); 
newData = cat(2, y, x, w)
% Remove missings from data
newData(any(isnan(newData), 2), :) = [];
% Re-declare variables after removing missings
y  = newData(:,1); % endogenous variable
x  = newData(:,2); % shock
w = newData(:,3:size(newData,2)); % control variables and lags
r = 3; 
lambda = 10000; 
slp    = locproj(y,x,w,H_min,H_max,'smooth',r,lambda); 
%% Cross-Validation Choice of Lambda
slp = locproj(y,x,w,H_min,H_max,'smooth',r,0.01);
lambda = [1:10:1000] * T;
slp    = locproj_cv(slp,5,lambda);
lambda_opt = lambda( min( slp.rss ) == slp.rss );
%% Confidence Intervals
r      = 3;
slp    = locproj(y,x,w,H_min,H_max,'smooth',r,lambda_opt); 
slp    = locproj_conf(slp,H_max,lambda_opt/2);

我认为它可以解决这个问题的是使用 if/else,比如:


% This is wrong but it gives you an idea of what I was trying to do and get
% trying to tell MATLAB, fun the code first from dataset 1 (row 1:60) and then the same on dataset 2 (from row 61:115)

k = 1:60

if  k == 1

Y_data = Y_data;
    
else

       Y_data = data(61:115, :);
end

% model code as above just here - not to make it too long
% the output therefore should save both results for dataset1 and dataset2

我被卡住了,无法继续前进。谁能帮我?这会让我很开心。

非常感谢!

【问题讨论】:

    标签: matlab loops for-loop if-statement


    【解决方案1】:

    有很多方法可以实现这一点。这里有两个简单的。

    创建一个函数dataAnalysis(Y_data) 并简单地做

    [slp,lambdaOpt] = dataAnalysis(data(1:60,:))
    [slp2,lambdaOpt2] = dataAnalysis(data(61:115,:))
    

    你的函数可能看起来像

    function [slp,lambdaOpt] = dataAnalysis(data)
    % This is the model that runs nicely on dataset Y_data. I wanted to avoid to run the model twice,
    % first with Y_data from row 1 to 60 and then from row to 61 to 100. I would like to do it in one shot
    % the code for the model is fully automated so it's just a matter of making it work first on dataset 1 and then
    % on dataset 2 in one unique code
    T = size(Y_data,1);
    P  = 3; % number of lags used in LP for controls
    H_min = 1; 
    H_max = 25; 
    % etc. etc...
    % Add in whatever output variables are important
    

    或者,使用您尝试过的循环:

    indices = {1:60, 61:115};
    for k = 1:2
        Y_Data = data(indices{k},:);
    
        % your model code operating on Y_data
        % store any results here as either variable(k) (scalar data)
        % or variable {k} (non-scalar data)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2019-04-13
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多