Help with Matlab code-please

Good afternoon,
I am trying to run the following code in MATLAB but get an error.
Error using *
Inner matrix dimensions must agree.
Error in vcqr (line 27)
e = y - x*bhat; % Generate residuals
Any help to fix the problem please?
Thanks
Oz
clear
load fish1
Z = [ones(111,1) stormy mixed];
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
% Here is the portion of the function related to the error:
vc = zeros(k,k);
b = zeros(k,2);
S = (1/n)*x'*x;
e = y - x*bhat; % Line 27

5 Comments

Ozmando - what are the dimensions of x and bhat? The error message is telling you that their dimensions are incompatible for matrix multiplication...
Thanks Geoff for your reply.
From the function bhat is defined as below b(:,1)=bhat which is I am assuming the first column of b.
Thanks
Oz
% [b,vc,J] = vciqr(bhat,y,x,tau)
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) = bhat;
b(:,2) = (sqrt(diag(vc)));
J = inv(J);
Ozmando
Ozmando on 18 Feb 2019
Edited: Ozmando on 18 Feb 2019
I have checked again and x is 111x2 and from what I can see b is 2 x 1
could the problem be that bhat is defined b(:,1)?
Thanks,
oz
@Ozmando,
You need to check the sizes of bhat and x at the moment the error actually occurs. To do this, execute
>>dbstop if error
at the command line and then re-run the code.
Ozmando commented:
Thanks Matt.
seems to be issues with size of x abd bhat. Will go through code again to try to figure out the source.
oz

Answers (0)

This question is closed.

Asked:

on 18 Feb 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!