Help with error please

11 views (last 30 days)
Ozmando
Ozmando on 26 Feb 2019
Commented: Adam Danz on 28 Feb 2019
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 Comment
Adam Danz
Adam Danz on 26 Feb 2019
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?

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 26 Feb 2019
Edited: Adam Danz on 26 Feb 2019
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 Comments
Ozmando
Ozmando on 28 Feb 2019
Thanks. Will go through the functions again.
Have looked further into the code again it seems that there are other functions associated with it. For example bhat is claculated from another function.
Will investigate further and hopfelly will get to the bottom of the issue.
Thaks again for your help.
oz
Adam Danz
Adam Danz on 28 Feb 2019
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.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!