Help with error please

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

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

Good Day Adam,
Thanks for the clarification. You are right in terms of the size of the matrices. However, I went through the data to see where the problem comes from but could not find this out. I have suspicions that the problem comes from: this portion of the code: X = [ones(111,1) d]; which is a 2x2 and bhat is 3x1.
I wonder if it should have been X = [x, ones(111,1), d]; where x is a set of covariates (6 covariates).
Can post or send data if that helps.
Thanks,
oz
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.
Thanks Adam,
I think the problem is X = [ones(111,1) d] . It is a 111x2 as it is formed by the d and a constant. Given this I was expecting bhat to return 2 estimates and their standard errors which are obtained from b. Indeed, bhat returns a 2x1.
However, from some reasons X appears as a 111x3 and the constant is included twice which is the source of the problem as bhat is 2x1 and x becomes 111x3 but not sure as this occurs as by definition X = [ones(111,1) d] should be 111x2?
oz
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.
Thanks Adam.
Have done command line by line and all seem to work although a strange thing happens when I run last command "end"
phat which initially appears as a 3x1 turns into a 2x1 which explains why the matrix do not agree. cannot see from the code why this is ocurring.
I did run "dbstop if error" but this also indicate that the problem is with the agreement of matrices.
oz
Ozmando
Ozmando on 28 Feb 2019
Edited: Ozmando on 28 Feb 2019
After further invesitigation perhaps the problem might come from the rq function in the line:
% boo = rq([ones(size(y)) Z*pihat],y,hamster);
Having checked that function it appears that this is where x is derived from:
function b = rq_fnm(X, y, p)
[m n] = size(X);
u = ones(m, 1);
a = (1 - p) .* u;
b = -lp_fnm(X', -y', X' * a, u, a)';
function y = lp_fnm(A, c, b, u, x)
% Solve a linear program by the interior point method:
% min(c * u), s.t. A * x = b and 0 < x < u
% Set some constants
beta = 0.9995;
small = 1e-5;
max_it = 50;
[m n] = size(A);
% Generate inital feasible point
s = u - x;
y = (A' \ c')';
r = c - y * A;
z = r .* (r > 0);
w = z - r;
gap = c * x - y * b + w * u;
Any help? please?
Thanks
oz
Could you provide the values of
bhat,y,x,tau
Just before then are sent to the vcqr() function?
Havind done the command line by line before
[b,vc] = vcqr(boo,y,[ones(size(y)) Z*pihat],hamster);
I obtain the folowing values attached in matlab_help.
I obtain phat (but not bhat)
y, X(but not x) and tau.
Thanks
oz
After I run "dbstop if error" and run the whole command I obtain the following now
bhat and x as attached in file matlab_help2.
Thanks
oz
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'
tau is defined in the original code (please see above) as
tau = ['15';'25';'35';'45';'50';'55';'65';'75';'85'];
From my understanding it is not used in the the computation of e = y-x*bhat.
e = y-x*bhat is calculated for every tau.
Thanks,
oz
Adam Danz
Adam Danz on 28 Feb 2019
Edited: Adam Danz 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.
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
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

Asked:

on 26 Feb 2019

Commented:

on 28 Feb 2019

Community Treasure Hunt

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

Start Hunting!