What is the MATLAB equivalent command of robust standard error in linear regression in Stata (e.g. reg y x, robust)?
I guess HAC might be the answer (http://www.mathworks.com/help/econ/hac.html).
Can anyone show a simple example of MATLAB that can generate the same result as generated with Stata code listed below.
webuse iris, clear
reg seplen sepwid
reg seplen sepwid, r
In MATLAB, I found robustfit (http://www.mathworks.com/help/stats/robustfit.html), but it should not be the equivalent command as it will affect the value of the estimated beta value, what's the relation between robustfit and robust standard error?
As stated by Nick Cox in a comment, you're not going to use robustfit. Instead you're going to estimate the robust standard errors separately like in the following little piece of code using hac. The coefficient estimates are found using the fitlm command.
% Load/define data
load fisheriris;
sepwid = meas(:,2);
seplen = meas(:,1);
% Estimates
fit = fitlm(sepwid,seplen);
[~,SE,coef] = hac(fit,'type','HC','weights','HC1','display','off');
% Output non-robust
fit.Coefficients(:,1:2)
% Output Robust
[coef SE]
Note that MATLAB places the constant/intercept in the top rather than in the bottom (like Stata). Alternatively you can use regstats2 by Oleg Komarov (*), which will give you p-values etc. as well.
% Estimates
fit2 = regstats2(seplen,sepwid,'linear','all');
% Output
[fit2.beta fit2.hc1.se]
(*) http://www.mathworks.com/matlabcentral/fileexchange/26169-regstats2
Related
I'm trying to plot and fill the area under the graph curve using area(). It works when I give the method simple functions, ie:
WS = linspace(0,100,500);
x = 2.*(WS)
area(WS,x)
but, for some reason, this method doesn't work in Octave with 'more complex functions'.
This is the script
WS = linspace(0,100,500);
TW_LCV = q.*( ( CD_min./WS) + k.*( (n./q).^2 ).*(WS) ); %the parameters are not relevant
figure()
plot(WS,TW_LCV, 'r');
hold on
area(WS, TW_LCV, 'FaceColor','y')
grid on;
I've tried the same script in Matlab, and it works. How can I fix this in Octave? The output:
Note. I'm using Windows 10
You are likely having infinite / nan values in your array (as in the example above, resulting from a division-by-zero operation).
Replace those values by suitable values (e.g. either get rid of inf values, or replace them by a suitably high value - or in your case possibly a zero value if that is deemed to be the limit of (x/WS)*WS for WS -> 0.
Once you only have appopriate numerical values, the area function will work as expected.
Having said that, if matlab does something with infinite values which octave treats differently, feel free to report it to the octave team in their bug tracker, since the octave team tends to treat deviations from matlab behaviour as 'bugs'.
I'm trying to breakdown how bandpass function makes the filtering and stumped upon this line (after the filter is created).
y = signal.internal.filteringfcns.filterData(x,opts);
x is the data and opts has the filter structure.
I've been looking around and haven't been able to find anything about signal.internal.filteringfcns.filterData function. I compared that output with filter(opts.FilterObject,x) and they are not the same.
Next is a minimal working example (data2.txt).
load('data2.txt')
srate=64;
freqrange=[0.4 3.5];
var{1}=freqrange;
var{2}=srate;
m=numel(data2);
x=data2;
R=0.1;%10% of signal
Nr=50;
NR=min(round(m*R),Nr);%At most 50 points
x1=2*x(1)-flipud(x(2:NR+1));%maintain continuity in level and slope
x2=2*x(end)-flipud(x(end-NR:end-1));
x=[x1;x;x2];
opts=signal.internal.filteringfcns.parseAndValidateInputs(x,'bandpass',var);
opts = designFilter(opts);
xx = signal.internal.filteringfcns.filterData(x,opts);
x_fil=xx(NR+1:end-NR);
xx = filter(opts.FilterObject,x);
x_fil2=xx(NR+1:end-NR);
plot([data x_fil x_fil2])
legend('raw','filterData','filter')
Here is the plot:
And here are the psd plot of both filtered signal (filtData first).
So, any help on this ...filtData function or I doing something wrong in my analysis?
Hi again :) If you type edit signal.internal.filteringfcns.filterData, you can even look at what is inside this filterData function. You will see that this function (depending on the options opts) will either,
right zero pad the signal with N/2 zeros and call filter
call filtfilt with the signal
This is also described in the docs of bandpass. So probably this zero padding explains why your output of filter(opts.FilterObject,x) is different.
You cannot find this function described in the documentation of Matlab since it is part of the internal functions of the signal processing toolbox.
I have some troubles to understand how to implement the following MIQP (Mixed Integer Quadratic Programming) with linear constraints in Matlab calling Gurobi.
Let me explain in a schematic way my setting.
(1) x is the unknown and it is a column vector with size 225x1.
(2) The objective function (which should be minimised wrto x) looks like
which can be rewritten as
I have a Matlab script computing alpha, Q,c (Q,c sparse) when some_known_parameters1 are given:
function [alpha, Q,c]=matrix_objective_function(some_known_parameters1)
%...
end
(3) The constraints are linear in x, include equalities and inequalities, and are written in the form
I have a Matlab script computing Aeq,beq,Aineq,bineq (Aeq,Aineq sparse) when some_known_parameters2 is given:
function [Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)
%...
end
(4) Some components of x are restricted to be in {0,1}. I have a Matlab script producing a string of letters B (binary), C (continous) when some_known_parameters3 is given:
function type=binary_continuous(some_known_parameters3)
%...
end
Now, I need to put together (1)-(4) using Gurobi. I am struggling to understand how. I found this example but it looks very cryptic to me. Below I report some lines I have attempted to write, but they are incomplete and I would like your help to complete them.
clear
rng default
%Define some_known_parameters1,
some_known_parameters2,some_known_parameters3 [...]
%1) generate alpha,Q,c,Aeq,beq,Aineq,bineq,type with Q,c,Aeq, Aineq sparse
[alpha, Q,c]=matrix_objective_function(some_known_parameters1)
[Aeq,beq,Aineq,bineq]=constraints(some_known_parameters2)
type=binary_continuous(some_known_parameters3)
%2) Set up Gurobi
clear model;
model.A=[Aineq; Aeq];
model.rhs=full([bineq(:); beq(:)]);
model.sense=[repmat('<', size(Aineq,1),1); repmat('=', size(Aeq,1),1)];
model.Q=Q; %not sure?
model.alpha=alpha; %not sure?
model.c=c; %not sure?
model.vtype=type;
result=gurobi(model); %how do I get just the objective function here without the minimiser?
Questions:
(1) I'm not sure about
model.Q=Q;
model.alpha=alpha;
model.c=c;
I'm just trying to set the matrices of the objective function using the letters provided here but it gives me error. The example here seems to me doing
model.Q=Q;
model.obj=c;
But then how do I set alpha? Is it ignoring it because it does not change the set of solutions?
(2) How do I get as output stored in a matrix just the minimum value of the objective function without the corresponding x?
(1) You're right, there's no need to pass the constant alpha since it doesn't affect the optimal solution. Gurobi's MATLAB API only accepts sparse matrices. Furthermore model.obj is always the c vector in the problem statement:
model.Q = sparse(Q);
model.obj = c;
(2) To get the optimal objective value, you first need to pass your model to gurobi and solve it. Then you can access it via the objval attribute:
results = gurobi(model);
val = results.objval + alpha
I am writing a program regarding solving the following boundary value problem using shooting-bisection method:
y''-y+x=0, y(0)=y(1)=0.
I first convert this to a system of first order equations, set
y'=z
then I let dydt represent the vector (y',z'), and come up with the script file:
function dydt=shoot(t,y)
dydt=[y(2);y(1)-t]
end
With this, I then came up with the following code:
clear
clc
a=0;
b=1;
alpha=0;
beta=0;
s(1)=(beta-alpha)/(b-a);
s(2)=-1
[G,Y]=ode113('shoot',[a b],[alpha;s(1)]);
[G,Z]=ode113('shoot',[a b],[alpha;s(2)])
hold
tol=1e-4
u=s(1);
v=s(2);
while abs(u-v)>tol;
s(3)=(u+v)/2;
[G,W]=ode113('shoot',[a b],[alpha;s(3)]);
if W(end,1)>0
u=s(3);
else
v=s(3);
end
end
[G,W]=ode113('shoot',[a b],[alpha;s(3)])
plot(G,Y(:,1),'-o', G,Z(:,1),'-o',G,W(:,1),'-o')
Then I run the program, MATLAB said I'm using the plot argument incorrectly, where plot vectors must be the same lengths. I have no idea how to fix this problem. Any help is appreciated.
Your Y, Z and W outputs are from different runs of ode113. The output independents variables, G from each run are different because ode113 is an adaptive solver. There are two ways you can fix this. You can save your G outputs as separate variables:
...
[Gy,Y]=ode113('shoot',[a b],[alpha;s(1)]);
[Gz,Z]=ode113('shoot',[a b],[alpha;s(2)]);
...
[Gw,W]=ode113('shoot',[a b],[alpha;s(3)]);
plot(Gy,Y(:,1),'-o', Gz,Z(:,1),'-o',Gw,W(:,1),'-o');
Or you could specify a fixed set of output points by specifying more than two points for tspan (second argument to ode113):
...
tspan = linspace(a,b,50);
[G,Y]=ode113('shoot',tspan,[alpha;s(1)]);
[G,Z]=ode113('shoot',tspan,[alpha;s(2)]);
...
[G,W]=ode113('shoot',tspan,[alpha;s(3)]);
plot(G,Y(:,1),'-o', G,Z(:,1),'-o',G,W(:,1),'-o');
Unless your version of Matlab is more than 10 years old, you should also specify your integration function, shoot, via a function handle, not a string, i.e.:
[Gw,W]=ode113(#shoot,[a b],[alpha;s(3)]);
I have several sets of data that I want to fit but not all of them look the same (some look like a Gaussian with one peak, some like two Gaussians with 2 peaks or Lorentzians). I wanted to try this method
http://www.mathworks.com/matlabcentral/fileexchange/31562-data-driven-fitting-with-matlab/content/fitit.m
but the program given is not complete so I can not use it (there is no line that defines 'train' and 'test'). I am writing it so that it suits and works for my data (based on the code that it is given and the demo). I was able to find the best fit but I am also trying to use the bootstrap technique in order to find the confidence intervals. My data is xdata and ydata and they are sorted and the duplicates have been removed before I use them in my program.
cpart=cvpartition(size(xdata,1),'k',10);
tr_x=xdata(training(cpart,1));
tr_y=ydata(training(cpart,1));
tst_x=xdata(test(cpart,1));
tst_y=ydata(test(cpart,1));
all_span=linspace(0.01,0.99,99);
s=zeros(length(all_span);
for k=1:length(all_span)
f = #(tr_x,tr_y,tst_x,tst_y) norm(tst_y mylowess (tr_x, tr_y, tst_x, all_span (k)))^2
s(k) = sum(crossval(f,datax,datay,'partition',cpart));
end
[~,mj]=min(s);
n_span=all_span(mj);%n_span is the optimal span
function ys=mylowess(x1,y1,xs,span)
ys1 = smooth(x1,y1,span,'loess');
ys = interp1(x1,ys1,xs,'linear',NaN);
if any(isnan(ys))
ys(xs<x1(1)) = ys1(1);
ys(xs>x1(end)) = ys1(end);
end
So up to this point I understand the program and I have managed to find the optimal span. I want to find the confidence intervals but so far I was not able to make it work.
When I type:
NB=length(xdata);
f=#(xdata,ydata) mylowess(xdata,ydata,xdata,n_span);
yboot2 = bootstrp(NB,f,xdata,ydata)';
I get the following error
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 186)
F = griddedInterpolant(X,V,method);
Error in mylowess (line 26)
ysmooth=interp1(xdata,ysmooth1,xinput,'linear',NaN);
As I said before there are no duplicates in xdata and I have already sorted xdata before I used them in the program. Can anyone see the mistake I am making? Or is there an easier way to get the confidence intervals?
Thank you for your help.