You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Robust standard errors on coefficients in a robust linear regression
28 views (last 30 days)
Show older comments
T27667
on 30 Jul 2013
I am new in MATLAB and have performed a robust linear regression with the 2 commands:
ds = dataset('XLSFile','C:\...\data.xlsx','ReadObsNames',true);
mdl = LinearModel.fit(ds,'linear','RobustOpts','on');
The standard errors (SE) shown in the property "Coefficients", are these the heteroskedasticity robust standard errors? If not, how can I modify my commands such that I get the robust standard errors?
Accepted Answer
Shashank Prasanna
on 30 Jul 2013
The output is robust to outliers and are not heteroskedasticity consistent estimates.
If that is what you are interested in, please check out the HAC command in the Econometrics Toolbox:
21 Comments
T27667
on 30 Jul 2013
Yes, I am interested in estimates and standard errors which are both outlier robust AND heteroskedasticity consistent. From the robust regression, I get the outlier robust estimates and outlier robust standard errors, if I understand correctly, right?
In order to get estimates and standard errors which are also heteroskedasticity consistent, I have checked out http://www.mathworks.com/help/econ/hac.html but it says here that: "...returns robust covariance estimates for ordinary least squares (OLS) coefficient estimates". Then I guess that I cannot use this command as I do not have the ordinary least squares (OLS) coefficient estimates but the robust regression estimates (as I have used robust regression). Isn't that true?
Shashank Prasanna
on 30 Jul 2013
HAC takes in the fitted linear model with robust opts:
load hald
X = ingredients; % predictor variables
y = heat; % response
mdl = LinearModel.fit(X,y,'linear','RobustOpts','on')
EstCov = hac(mdl)
T27667
on 30 Jul 2013
Edited: T27667
on 30 Jul 2013
Ok, thanks a lot. The code lines that you provide above, are these from mathworks.se? Because then I will read that page. Or have you created them yourself? However, I get an error message using the 2 commands:
mdl = LinearModel.fit(ds,'linear','RobustOpts','on');
EstCov = hac(mdl)
Undefined function 'hac' for input arguments of type 'LinearModel'.
How can that be?
Shashank Prasanna
on 30 Jul 2013
These is directly from the documentation from LinearModel.fit but I've continued to use the same model in HAC.
You are getting the error because you don't have the Econometrics Toolbox installed. To confirm type the following on your command line.
>> ver
T27667
on 30 Jul 2013
All my commands are now:
ds = dataset('XLSFile','C:\...\data.xlsx','ReadObsNames',true);
mdl = LinearModel.fit(ds,'linear','RobustOpts','on');
ver
EstCov = hac(mdl)
Undefined function 'hac' for input arguments of type 'LinearModel'.
But I still I get the error above. Should I type more than ver?
Shashank Prasanna
on 30 Jul 2013
Edited: Shashank Prasanna
on 30 Jul 2013
ver won't solve your problem. You need the Econometric Toolbox, which is this product:
If you don't have it then you can't use HAC. All ver does is show you if you have the product installed on your machine.
>> ver
Just run the above and confirm if Econometrics Toolbox is installed or not based on what appears on the command line output.
This is what ver does:
T27667
on 31 Jul 2013
Great, now I got the heteroskedasticity consistent standard errors using the command:
EstCov = hac(mdl,'display','full')
Thanks a lot for your help!
Unfortunately, the command doesn't give the t-stats and p-values such that I can reduce my linear model. Can I modify the command such that t-stats and p-values are provided?
Shashank Prasanna
on 31 Jul 2013
Please read the documentation of HAC on how to get the coefficients and standard errors. From theory t-stats is their ratio.
T27667
on 31 Jul 2013
Edited: T27667
on 31 Jul 2013
I got the heteroskedasticity consistent standard errors using the command from http://www.mathworks.com/help/econ/hac.html:
EstCov = hac(mdl,'display','full')
But isn't it possible to also get the t-stats and p-values using a build-in command? I had hoped that columns with estimates, standard errors AND t-stats and p-values were generated as when you run a LinearModel.fit and open "Coefficients".
If there is no such build-in command, which code lines should I then write after the EstCov command in order to have t-stats and p-values calculated. Unfortunately, I have no programming experience in MATLAB. Would be lovely with a code that generate the estimates, robust SEs, t-stats and p-values in Workspace like in the output from LinearModel.fit
Shashank Prasanna
on 31 Jul 2013
Did you get a chance to read the documentation page?
You can ask HAC to return EstCov,se and coeff.
>> tstats = coeff./se
T27667
on 31 Jul 2013
Edited: T27667
on 31 Jul 2013
Yes, but the documentation page doesn't say anything about a command that generates tstats and p values. Getting HAC to return EstCov, robust SE and coeff works fine. I get the error below if I write the command tstats = coeff./se directly?
Undefined function or variable 'Coeff'.
How do I store the returned Coeffs and SEs from command Window (from command EstCov = hac(mdl,'display','full')) into variables such that I can calculate the tstats using your formula? And afterwards what command calculates the p values?
Shashank Prasanna
on 31 Jul 2013
MATLAB is case sensitive.
Please read the documentation on how to store the returned values in the variables. Go through the examples.
T27667
on 31 Jul 2013
I will. Thanks for all your help! Really appreciate it! If you know the formula for the p values, I would love to see it.
Shashank Prasanna
on 31 Jul 2013
This comes from theory.
dfe is the degrees of freedom = number of observations - number of estimated parameters. t is the t statistic.
pValue = 2*(tcdf(-|t|,dfe));
T27667
on 31 Jul 2013
Edited: T27667
on 31 Jul 2013
Thank you so much. However, I really can't see from the examples how to store the coeffs and robust SEs in the Workspace such that I can calculate the tstats (and afterwards the p values). The covariance matrix is stored automatically in the Workspace as a double by EstCov = hac(mdl,'display','full') but I can't find a way to store the coeffs and robust SEs. I can't see this is done in any of the examples. Should I convert a vector into a cell or? I can see that se and coeff are of the type vector. I know about converting a dataset into a cell using dataset2cell but can't find anything about converting a vector into a cell. Or am I on the right track at all?
Shashank Prasanna
on 31 Jul 2013
Did you try running the first example completely? When you do you should see 3 variables LSCov,LSSe,coeff in your workspace.
T27667
on 1 Aug 2013
Edited: T27667
on 1 Aug 2013
Finally got it using the commands:
ds = dataset('XLSFile','C:\...\data.xlsx','ReadObsNames',true);
mdl = LinearModel.fit(ds,'linear','RobustOpts','on');
[EstCov,se,coeff] = hac(mdl,'display','full');
tstats = coeff./se
pValue = 2*(tcdf(-abs(tstats),1803-17));
So nice finally to have all results. Thank you so much again!! I was 100% sure that I had the correct command in EstCov = hac(Mdl) and couldn't see until now that [EstCov,se,coeff] = hac(mdl,'display','full'); did the same + more.
Just to be sure, the degrees of freedom = number of observations - number of estimated parameters. Last term (Number of estimated parameters) does that include the intercept?
Shashank Prasanna
on 1 Aug 2013
I've been asking you to read the documentation from the very first post. If you did you would have saved this much time.
T27667
on 1 Aug 2013
Sorry but I misunderstood the example. I'm a completely new user of MATLAB and both using it and understanding the documentation pages are difficult here in the beginning. But getting better every day :)
Just to be sure, the degrees of freedom = number of observations - number of estimated parameters. Last term (Number of estimated parameters) does that include the intercept?
Shashank Prasanna
on 1 Aug 2013
That's a statistics question (along with how to compute tstats and pvalue)
I don't know what your application is but you should get hold of some statistics material to convince yourself before applying anything I mentioned.
If you want to get better with MATLAB, check out the Getting Started guide:
T27667
on 1 Aug 2013
I think those formulas are the correct ones in my case as I perform a backwards elimination of a robust linear regression.
More Answers (1)
T27667
on 30 Jul 2013
Yes, I am interested in estimates and standard errors which are both outlier robust AND heteroskedasticity consistent. From the robust regression, I get the outlier robust estimates and outlier robust standard errors, if I understand correctly, right?
In order to get estimates and standard errors which are also heteroskedasticity consistent, I have checked out http://www.mathworks.com/help/econ/hac.html but it says here that: "...returns robust covariance estimates for ordinary least squares (OLS) coefficient estimates". Then I guess that I cannot use this command as I do not have the ordinary least squares (OLS) coefficient estimates but the robust regression estimates (as I have used robust regression). Isn't that true?
See Also
Categories
Find more on Robust Control Toolbox in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)