Please see below my Newey West Standard Error. Somehow its not working as intendet but i have no clue why. The error message of Matlab does not really contain helpful advise.
Can anyone of you identify the error that i made in the Code?
The file should get X and g (145) from my main file. It always says that it might be unbalanced but i dont see any unbalanced term.
function HCsterr = HCse(resid,X,const)
[n,~] = size(X);
if const == 1
X = [ones(n,1),X];
end
g = floor(4*(n/100)^(2/9));
R = sum( arrayfun( @(h) sum(X(h+1:n).*e(h+1:n).*e(1:n-h).*Xprime(1:n-h) + X(1:n-h).*e(1:n-h).e(h+1:n).*Xprime(h+1:n)) / (1 + h/(g+1), 1:g) ));
VCV = (X' * X)^(-1) * ((X' * diag(resid.^2) * X)+R) * (X' * X)^(-1);
% HCSE
HCsterr = sqrt(diag(VCV));
end
%

 Akzeptierte Antwort

Steven Lord
Steven Lord am 30 Nov. 2015

0 Stimmen

I think you have a closing parenthesis in the wrong place. Look at the last piece of your longest line:
(1 + h/(g+1), 1:g) ));
Look at this piece of that expression. It is enclosed in parentheses (two opening parentheses are matched by two closing parentheses) but it has a comma in the middle. Usually that pattern occurs when you're calling a function with two inputs, but in this case you're not. That's what has MATLAB confused.
(1 + h/(g+1), 1:g)
I think you want this, with one of the parentheses moved from the end of the whole expression to right before 1:g.
(1 + h/(g+1)), 1:g) );
The parentheses that moved ends the body of the anonymous function and the comma separates the two inputs to ARRAYFUN, leaving 1:g as the second input to ARRAYFUN. The parenthesis after 1:g ends the ARRAYFUN call, and the parenthesis at the end of the line (just before the semicolon) ends the SUM call.
Personally in this case I would probably turn that sum-of-arrayfun-of-sum into a simpler double FOR loop to increase readability and clarity. Without some (many) comments you may have trouble remembering what this is doing a week, a month, a year from now.

5 Kommentare

Florian Fbr
Florian Fbr am 1 Dez. 2015
Bearbeitet: Florian Fbr am 1 Dez. 2015
It seems like you are right with "(1 + h/(g+1)), 1:g) );" but now, if i try to run the Code it says "Error using HCse (line 30) Error: Functions cannot be indexed using {} or . indexing." Line 30 is R=...
Can you give me some help of how to Change it to an "if" term that it works.
Walter Roberson
Walter Roberson am 1 Dez. 2015
Please show your current version of that line.
So if I run the my full Code and it refers to my Newey West test with:
NW = HCse(OLSAR2.Residuals.Raw,DTGDPt1t2,1)
Then the function runs which is:
function NW1 = HCse(resid,X,const)
[n,~] = size(X);
if const == 1
X = [ones(n,1),X];
end
g = floor(4*(n/100)^(2/9));
R = sum( arrayfun( @(h) sum(X(h+1:n).*e(h+1:n).*e(1:n-h).*Xprime(1:n-h) + X(1:n-h).*e(1:n-h).e(h+1:n).*Xprime(h+1:n)) / (1 + h/(g+1)), 1:g) );
VCV = (X' * X)^(-1) * ((X' * diag(resid.^2) * X)+R) * (X' * X)^(-1);
% HCSE
NW1 = sqrt(diag(VCV));
end
It says: Error using HCse (thats the Name of my function file (HCse.m) line 12 Error: Functions cannot be indexed using {} or . indexing
what shall i do ?!?! :/
Walter Roberson
Walter Roberson am 3 Dez. 2015
X(1:n-h).*e(1:n-h).e(h+1:n).*Xprime(h+1:n) is missing a "*" before the second e()
Florian Fbr
Florian Fbr am 3 Dez. 2015
Bearbeitet: Florian Fbr am 3 Dez. 2015
Thanks!
Now it says: "Undefined function or variable "e"" in my main Code. I forgot to create a Matrix for my e, which are the residuals. The residuals are in "OLSAR2.Residuals.Raw" (in Main Code) but I guess it doesnt just work that i replace all my "e" in the function with "resid". Because when I do so, it says "Error using .* Matrix dimensions must agree".

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by