MATLAB - “赋值具有比非单例下标更多的非单例rhs维度”

时间:2015-09-23 14:53:32

标签: matlab

我最近开始学习MATLAB,我正在尝试使用Ian D. Gow网站下面的FamaMacBeth_NW函数进行FM回归。

当我运行包含一个变量(市场测试版)的以下代码时,我从回归中得到了正确的结果:

**beta1=zeros(1,1);  
b = FamaMacBeth_NW(excess_ret, market_beta , date_gp, beta1 , 'NW'==12);

但是,当我使用如下两个变量时:

2vars = [   market_beta momentum   ]; 
beta2a=zeros(2,1); 
b2 = FamaMacBeth_NW(excess_ret, 2vars , date_gp,beta2a  , 'NW'==12); 

我收到以下消息:

Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts

Error in FamaMacBeth_NW (line 49)
    temp(:,t,1:3) = regress(y2,X2,0);

我在这里和其他网站搜索过,我意识到第49行的赋值中存在矢量大小的问题(temp(:,t,1:3)= regress(y2,X2,0);) 。但是,遗憾的是我无法弄清楚为什么以及如何解决它。我会非常感谢任何见解。非常感谢你提前。

Ian D. Gow的FamaMacBeth_NW功能:

来源:http://www.people.hbs.edu/igow/GOT/Code/FamaMacBeth_NW.m

代码:

function out = FamaMacBeth_NW(y, X, PART_VAR, beta, varargin)
% Routine for calculating Newey-West-adjusted Fama-MacBeth 
% standard errors and also Z1 and Z2 statistics.
%
% SYNTAX: ret = FamaMacBeth_NW(y, X, PART_VAR, BETA, VARARGIN)
%
%   y: vector of dependent variable
%   X: matrix of regressors
%   PART_VAR: vector containing variable by which regressions are 
%     partitioned  (e.g. MONTH, using the Fama-MacBeth approach on 
%     a data set in which MONTH uniquely identifies a month)
%   BETA: The vector of coefficients under the null hypothesis.
%   LAG_LENGTH: The number of lags to be considered in the Newey-West 
%     approach (if the Abarbanell and Bernard (2000) correction is 
%     specified, this indicates whether the Abarbanell and Bernard (2000) 
%     adjustment should be made (LAG_LENGTH==1) or the simpler approach
%     discussed in Petersen (2007) should    be used (LAG_LENGTH==0).
%   VARARGIN: Leave empty to get unadjusted Fama-MacBeth estimates. 
%     Use either 'NW' followed by the lag length for Newey West (1987) 
%     correction or use 'AB' followed by 1 for Abarbanell and Bernard 
%     (2000) correction or 'AB; followed by 0 for correction
%     discussed in Petersent (2007)
%
%   RET = [b se t Z1 Z2], the estimated coefficients (b), the estimated 
%     standard errors (se), and t-statistics (t), and the Z1 and Z2 
%     statistics.
%   

  k = size(X,2);

  % Get details on partition variable.
  PART = unique(PART_VAR);
  T = length(PART);

  % Create a location for estimated coefficients.
  temp = zeros(k, T, 4);

  % Get Fama-MacBeth standard error
  % First estimate t coefficients, one for each year
  bb=zeros(T,k);

  for t=1:T

    % Get y and X values for regression t of T
    y2 = y(find(PART_VAR==PART(t)),:);
    X2 = X(find(PART_VAR==PART(t)),:);

    % Store estimated coefficient, SEs, t-statistics, and df
    temp(:,t,1:3) = regress(y2,X2,0);
    temp(:,t,4) = size(X2,1)-size(X2,2);

    % Store estimated coefficients for t in BB vector
    bb(t,:) = temp(:,t,1);
  end

  % Calculate standard errors using the time-series distribution of the
  % estimated coefficients.
  for i=1:k 
    b(i) = mean(temp(i,:,1));
    se(i) = 1/sqrt(T) * std(temp(i,:,1));

    % Z1 statistic
    df = temp(i,:,4);
    tstats = (temp(i,:,1) - beta(i)) ./ temp(i,:,2);

    Z1(i) = 1/sqrt(T)*sum(tstats)/sqrt(df/(df-2));  

    % Z2 statistic
    Z2(i)=mean(tstats)/(std(tstats)/sqrt(T-1));
  end

  % Calculate associated t-statistics.
  if nargin ~= 4
    if nargin < 4 || nargin > 6
    error('Wrong number of arguments');
  end
    if varargin{1} =='NW'
      % Calculate Newey-West standard errors.
      lag_length = varargin{2};
      for i=1:k
        oness=ones(T,1);
        ret=NeweyWestPanelStata(bb(:,i), oness, lag_length, oness, oness, 0);
        se(i)=ret(1,2);
      end
    elseif varargin{1} =='AB'
      % Abarbanell and Bernard (2000) correction
      b1=bb(2:end,:);
      blag=bb(1:end-1,:);
      n = size(bb,1);
      theta=diag(corr(b1, blag))';
      if varargin{2} == 1
        adj = 2 * theta .* (1-theta .^ n);
        adj = adj ./ (n * (1-theta) .^ 2);
      else 
        adj = 0;
      end
      se = se .* sqrt((1+theta) ./(1-theta) - adj);
    end
  end

  t = (b-beta') ./ se;

  % Return results
  out = [b' se' t' Z1' Z2'];

end

0 个答案:

没有答案