Help with error please
Show older comments
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
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?
Answers (1)
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
on 26 Feb 2019
Adam Danz
on 26 Feb 2019
Look at the values of bhat,y,x just before you send them into vcqr(). Try to do some problem solving. Ask yourself, what are the shapes and size of these 3 variables and what should they be? Do you expect them to be a vector, matrix or scalar? If they are vectors, should be be a column or row? Let me know if you get stuck and tomorrow I can give you further advice but the best learning experience with the longest lasting benefits will be if you figure out the solution.
Ozmando
on 27 Feb 2019
Adam Danz
on 27 Feb 2019
I don't follow that logic. In the code you provided, the variable "X" (upper case) is defined but it's never used. So the size of "X" doesn't matter.
There's a variable 'x' (lower case) in the vcqr() function but that's not the 'x' you're refering to.
I suggest you use clear all variables from the workspace and execute each line of your code one-by-one to see if each line is producing the expected results. Your code is short so this shouldn't take much time.
Ozmando
on 28 Feb 2019
Adam Danz
on 28 Feb 2019
Could you provide the values of
bhat,y,x,tau
Just before then are sent to the vcqr() function?
Ozmando
on 28 Feb 2019
Ozmando
on 28 Feb 2019
Adam Danz
on 28 Feb 2019
I copied your vcqr() function and entered the variables you provided
[b,vc,J] = vcqr(pihat,y,X,tau)
This produced an error. 'tau' is a [9x2] character vector. Is that supposed to be the case?
tau =
9×2 char array
'15'
'25'
'35'
'45'
'50'
'55'
'65'
'75'
'85'
Ozmando
on 28 Feb 2019
Now your second set of data contain 'tau' which is numerical and a single value (0.15). I'm assuming this is correct and the previous data are incorrect.
In the 2nd line of your vcqr() function, you take the 'x' input variable which originally is a [111 x 3] matrix and you add a 4th column of 1s to make it a [111 x 4] matrix. Then a few lines later, you muliply that [111 x 4] matrix with bhat which is a [2 x 1] vector. That natrually causes an error.
If you're trying to do matrix muliplication, the number of columns of the first element must equal the number of rows of the second element.
If you're trying to elementwise multiplication, you need to use " .* " but the matrix dimensions must agree. So, either way your data won't work.
Instead of trying to make the funciton work, what you need to do is go through every single individual line and 1) understand what it's supposed to do and 2) test each line to see if it's producing the expected outcome. Just because a line of code produces something doesn't mean it's working.
"bhat" is not produced anywhere in your code so it must be one of the variables loaded from file data1. Is it supposed to be a vector? A matrix? What's the shape supposed to be? What do those values represent? Should 'x' be a matrix ? A vector? What size should 'x' have? These are questions you should focus on.
Ozmando
on 28 Feb 2019
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!