getting complex-valued Jacobian Matrix using NonLinearModel.fit in matlab - matlab

I am trying to use the NonLinearModel.fit() function in Matlab to regress two variables. However, I am getting the following error:
Error using internal.stats.getscheffeparam>ValidateParameters
(line 182)
If non-empty, JW must be a numeric, real matrix.
Error in internal.stats.getscheffeparam (line 110)
[J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,usingJ] =
ValidateParameters(J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,allowedIntopt);
Error in nlinfit (line 340)
sch =
internal.stats.getscheffeparam('WeightedJacobian',J(~nans,:),'Intopt','observation','VQ',VQ);
Error in NonLinearModel/fitter (line 1121)
[model.Coefs,~,J_r,model.CoefficientCovariance,model.MSE,model.ErrorModelInfo,~]
= ...
Error in classreg.regr.FitObject/doFit (line 219)
model = fitter(model);
Error in NonLinearModel.fit (line 1484)
model = doFit(model);
Error in getMatrix (line 101)
nlm =
NonLinearModel.fit(regressorMatrix',temp2',modelfun,beta0);
My regressorMatrix is 2-by-N (so the transpose is N-by-2), temp2' is N-by-1, and beta0 and model are given by:
model =#(b,x)b(1).*x(:,1).*x(:,2).^b(2);
beta0=[.14 .6];
nlm = NonLinearModel.fit(regressorMatrix',temp2',model,beta0);
Could someone please help me in figuring out what is causing this error?
EDIT: ok, no help so far so I will try to be more specific. I know that this error is referring to the weighted Jacobian matrix. I am just not sure why this Jacobian would not be real-valued.
Here are the first few rows of my regressor matrix:
regressorMatrix =
1.0e+07 *
0.000000000776613 3.762601240855837
0.000000001683014 3.762601240855837
0.000000001496807 3.762601240855837
0.000000000753495 3.762601240855837
and my response matrix:
temp2 =
-0.011811061934317
0.987582922964869
0.010621342764736
0.135001167018444
0.091950680609212
I can see that something is wrong here (the orders of magnitude in col2 of my regressor matrix are off. I will fix this and get back with an explanation if it turns out to the the cause. I am also working on printing out J. and JW
EDIT2:
I was able to print out JW before the error occurs and discovered that JW is a Nx2 complex matrix. So the specific reason the error is occurring is that the weighted Jacobian matrix is not real valued. Not sure why...

After a lot of digging, I found that the answer to this problem is quite obvious. I didn't realize that some of the values for one of my regressor variables was going negative sometimes. This lead (jumping a few steps ahead) to a complex Jacobian.

Related

Error during rm ANOVA (Matlab) with random factor

I am trying to run a repeated measures ANOVA in Matlab with 4 factors including one factor representing my subjects which I want as a random factor.
The code I have is as follows:
[p,table,stats] = anovan(COORDS_SUBJ_II,{group_hand,group_stim,group_time,group_subs},'random',4,'varnames',{'HAND','STIM','TIME','SUBS'});
Here, all variables have the same dimension, which is 1350x1(all types are 'double'). I checked my code with some proposed code on the net and it matches, yet I keep getting the following error...
Error using chi2inv (line 3)
P and V must be of common size or scalars
Error in anovan>varcompest (line 838)
L = msTerm .* dfTerm ./ chi2inv(1-alpha/2,dfTerm);
Error in anovan>getRandomInfo (line 811)
[varest,varci] = varcompest(ems,randomterms,msTerm,dfTerm,alpha);
Error in anovan (line 296)
getRandomInfo(msterm,dfterm,mse,dfe,emsMat,randomterm,...
My dependent variable (COORDS_SUBJ_II) has a couple of NaN's in it, although I ran the code once where I replaced those NaN's with random numbers and it still gives me the same error. I am kind of lost right now and would appreciate any help.
Best
ty
Found it out. A toolbox I downloaded a while ago also had the chi2inv command and this prompted the error =)

MATLAB: findpeaks function

I'm using MATLAB 2013a and trying to find peak points of my data. When I tried code example given in
Find Peaks with Minimum Separation
I am getting the following error:
Error using uddpvparse (line 122)
Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84)
hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
I tried simple x and y vectors and got the same error. What can be the problem?
I have the same problem as you (R2013a on OSX) with the example by the Mathworks. For some reason it seems we can't use findpeaks with the x-and y-data as input arguments, We need to call the function with the y data and use the [peaks,locations] output to get the peaks/plot them.
It looks like in R2014b they changed some stuff about findpeaks that does not work with older versions...like calling the function with not output argument in R2014b plots the data/peaks without any additional step...but it does not for earlier versions.
Anyhow here is a way to workaround the problem. Call findpeaks with a single input argument (y data that is, you can use property/value pairs as well) and use the indices (locations) to show the peaks:
clc
clear
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
[peaks, locations] = findpeaks(avSpots)
plot(year,avSpots)
hold on
scatter(year(locations),avSpots(locations),40,'filled')
hold off
Output:
It might be worthwhile to contact The Mathworks about this. Hope that helps!

Predictor and response variables must have the same length; Matlab

Thanks in advance for the help.
I am trying to use stepwise regression on a set of data. I have the data in a table, with the single predictor variable on the far right of the table (as a column). Here is what my code looks like.
mdl = stepwiseglm(dummyTrainingTable,'modelspec',modelTech,'Criterion',criterion);
where modelTech and criterion are variables that hold strings dictating two name-value pair options. I am getting the following error
Error using classreg.regr.FitObject/assignData (line 257)
Predictor and response variables must have the same length.
Error in classreg.regr.TermsRegression/assignData (line 349)
model =
assignData#classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in GeneralizedLinearModel/assignData (line 794)
model =
assignData#classreg.regr.TermsRegression(model,X,y,w,asCat,dummyCoding,varNames,excl);
Error in GeneralizedLinearModel.fit (line 1165)
model =
assignData(model,X,y,weights,offset,binomN,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in GeneralizedLinearModel.stepwise (line 1271)
model = GeneralizedLinearModel.fit(X,y,start.Terms,'Distribution',distr,
...
Error in stepwiseglm (line 148)
model = GeneralizedLinearModel.stepwise(X,varargin{:});
This doesn't make sense to me since clearly my response and predictor variables have the same length; they're in a table together. If they weren't the same length, they couldn't be in a table right? Is this an issue with Matlab or is there just something simple that I am missing?
Note, I when I convert the table to a matrix, stepwiseglm runs just fine. i.e.,
dummyTrainingArray = table2array(dummyTrainingTable);
mdl = stepwiseglm(dummyTrainingArray(:,1:size(dummyTrainingArray,2) - 1), dummyTrainingArray(:,size(dummyTrainingArray,2)),modelTech,'VarNames', ...
dummyTrainingTable.Properties.VariableNames,'Criterion', criterion);
I figured out a solution. Although the documentation online states that the input can be a table, when I checked the manual within my version of Matlab (run 'help stepwiseglm'), I found that the function was compatible only with datasets. I then converted my table to a dataset and it ran fine.
Edit, I have Matlab version
8.2.0.701 (R2013b)
'modelspec' is not a valid argument name for the function. Try:
mdl = stepwiseglm(dummyTrainingTable, modelTech, 'Criterion', criterion);

About using quad in MATLAB

I encounter a strange problem with quad function.
I was using quad to calculate simple integral, and it worked for 10 to 20 times, then Matlab issues the following error:
Error using quad (line 75)
The integrand function must return an output vector of the same length as the input vector.
yteor(k) = quad(#(q)(exp(-(q).^2).*q.^2/(k.^2+1)), 0, 1);
Here q and k are scalars.
I can not get what is wrong and why it worked several hours ago.
Edit
Here is my code
for k=1:100,
xteor(k)=step*k;
yteor(k)=quad(#(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end plot(xteor,yteor,'r');
The following snippet works for me on Octave (Matlab GNU clone)
step = 1;
xteor = zeros(100,1);
yteor = zeros(100,1);
for k=1:100,
xteor(k)=step*k;
yteor(k)=quad(#(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end
plot(xteor,yteor,'r');
pause
My hypothesis is that your error is the consequence of something else happening earlier in your code (maybe related to step not being a scalar?). Instead of focusing on this line where the error arise. Try to search what you have changed just before the error appear.

Error using fmincon, hack fixes it but I'm uneasy about it

I'm having a strange error when using fmincon. The details about the objective function and the nonlinear constraint functions are many, so I'm going to try to ask this question without describing those if possible.
The minimization requires a single input. For some but not all inputs, I get the following error:
Subscript indices must either be real positive integers or logicals.
Error in qpsub>eqnsolv (line 888)
ACTSET = A(ACTIND,:);
Error in qpsub (line 157)
[Q,R,A,B,X,Z,how,ACTSET,ACTIND,ACTCNT,aix,eqix,neqcstr,ncstr, ...
Error in nlconst (line 619)
[SD,lambda,exitflagqp,outputqp,howqp,ACTIND] ...
Error in fmincon (line 794)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in PauliApproximate2.unitalChannelApproximate (line 170)
[pc, distance] = fmincon(#objective,x0, A, b, Aeq, beq,lb,ub, #constraint,
options);
When I check to see the value of ACTIND, the error is thrown when it equals [0], hence the indexing error. Similar to the hack that the OP of this thread used, before the line (in qpsub)
ACTSET = A(ACTIND,:);
I put the if statement:
if max(ACTIND) < 1
ACTIND = [1];
end
From preliminary testing this seems to "Fix" the problem. It is giving sensible results for inputs that used to throw the error (in that it is returning a good minimum value and the constraints are satisfied to within tolerance), and is returning the same results it always did for inputs that didn't have the error (obviously, as the if statement won't be invoked). So my question is, for people that know the inner workings of these functions, how bad is this? Am I sidestepping a horrible issue that results in ACTIND being [0] that I shouldn't be?