Help with error please
11 次查看(过去 30 天)
显示 更早的评论
Good day!
Having trouble to identify the source of the problem is the following code
%Code
clear
load data1
Z = [ones(111,1) st1 m1];
X = [ones(111,1) d];
pihat = inv(Z'*Z)*(Z'*d);
tau = ['15';'25';'35';'45';'50';'55';'65';'75';'85'];
for i = 1:size(tau,1),
disp(i);
hamster = str2num(strcat('.',tau(i,:)));
boo = rq([ones(size(y)) Z*pihat],y,hamster);
[b,vc] = vcqr(boo,y,[ones(size(y)) Z*pihat],hamster);
[b,f,c,seq,conv] = mcmc_flat_fish1_mon2('iqrobj_fish',boo,vc,50000,y,[ones(size(y)),d],Z,hamster);
save(strcat('fish',tau(i,:),'_storm_v3_mon2'),'b','f','c','seq','conv');
[b,f,c,seq,conv] = mcmc_flat_fish2('iqrobj_fish',boo,vc,50000,y,[ones(size(y)),d],Z,hamster);
save(strcat('fish',tau(i,:),'_storm_v3_2'),'b','f','c','seq','conv');
end
%Function
function [b,vc,J] = vcqr(bhat,y,x,tau)
% [b,vc,J] = vciqr(bhat,y,x,tau)
% Inputs -
% bhat - estimated coefficients from QR
% y - dependent variable
% d - RHS endogenous variable
% x - covariates (If there are no covariates, pass x = [].)
% z - instruments
% tau - quantile index
% Outputs -
% b - estimated coefficients with standard errors
% vc - covariance matrix of b
% J - matrix in asymptotic variance formula (J'SJ) used in process testing
n = size(y,1); % Number of observations
x = [x,ones(n,1)]; % Add constant term
k = size(x,2); % Number of regressors
vc = zeros(k,k);
b = zeros(k,2);
S = (1/n)*x'*x;
e = y-x*bhat; % Generate residuals
%h = 1.364*((2*sqrt(pi))^(-1/5))*sqrt(var(e))*(n^(-1/5)); %Calculate bandwidth using Silverman's rule of thumb
h = iqr(e)*(n^(-1/3));
J = (1/(n*h))*((normpdf(e/h)*ones(1,size(x,2))).*x)'*x;
vc = (1/n)*(tau-tau^2)*inv(J')*S*inv(J);
b(:,1) = phat;% check if pihat as i have changed this.
b(:,2) = (sqrt(diag(vc)));
J = inv(J);
I get these errors:
Error using *
Inner matrix dimensions must agree.
Error in vcqr (line 27)
e = y-x*bhat; % Generate residuals
Error in run_fish_5 (line 17)
[b,vc] = vcqr(boo,y,[ones(size(y)) Z*pihat],hamster);
Any help please?
Thanks
oz
1 个评论
Adam Danz
2019-2-26
Not enough information. What's the full error message? What's the function vcqr() supposed to do and what are its inputs? Could you provide a concise example that produces the error?
回答(1 个)
Adam Danz
2019-2-26
编辑:Adam Danz
2019-2-26
The following line in your code is what's throwing the error "Inner matrix dimensions must agree."
e = y-x*bhat; % Generate residuals
When you multiply two matricies, as the error indicates, the inner dimensions must agree.
This example is ok since the inner dimensions are both 'n' [m x n] * [n x p] .
This example will cause an error [n x m] * [n x p] .
If you're trying to multiply the matrices element-wise (which appears to be the case in your code), then you need to add a dot symbol befor the multiplication symbol.
e = y-x .* bhat; % Generate residuals
Assuming x and y are vectors of the same size, the result will be another vector of the same length. If one vector is a column and the other is a row, this will result in a matrix.
One final example to show the difference between multiplication with and without the dot.
A = [2, 4, 6, 8, 10]; %row vector of size [1,5]
B = [2; 2; 2; 2; 2] %column vector of size [5,1]
A * B
ans =
60
A.*B % one is a row, the other is a column
ans =
4 8 12 16 20
4 8 12 16 20
4 8 12 16 20
4 8 12 16 20
4 8 12 16 20
A.*B' % both are rows
ans =
4 8 12 16 20
>>
14 个评论
Adam Danz
2019-2-28
Most importantly, you should focus on what the variables should look like. If they aren't the size/shape that you expect, then dig in to find out where they are being created.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!