Simulating non-linear ARX model doesn't work - matlab

I estimated a non-linear ARX model with the sytem identification toolbox in matlab and tried to simulate the response to a validation data set. Unfortunatly the response seems to be not stable and doesn't lead to the results obtained from the compare fcn.
nlarx2 is my model:
opt3=simOptions("InputOffset",4,"InitialCondition",IO); sim(nlarx2,Data_val1.InputData,opt3)
The initial condition is obtained from the estimation data set from the compare fcn
I dont know if I have to add the discretness of the model or what else could be wrong. The error message says:
">> sim(nlarx2,Data_val1.InputData,opt3)
Warning: Model output became non-finite at time 58 seconds. Aborting the rest of the simulation.
In idnlarx/utsim>localInfNanWarn (line 79)
In idnlarx/utsim (line 61)
In idnlarx/sim_ (line 129)
In idmodel/sim (line 113) "
I obtain a figure, but not a useful one.
Thank you for your help

Related

How to create a dose response curve in MATLAB? doseResponse function rank deficient warning

I've been searching for a way to create dose response curves from data within MATLAB. So far, I've been running the doseResponse function downloaded from here. However, I've been having trouble generating a sigmoidal curve that fits my data. By running the following code, I get the following error and produce the following graph. I also uploaded my data to google drive here. Does anyone know how I can fix this? Or does anyone know of a better way to produce dose response curves? Thanks.
Code
doseResponse(drug_conc,resp_vals)
xlabel('5HT Dose (nm)','FontSize',20)
ylabel('Normalized Response to Stimuli (\DeltaF / F)','FontSize',20)'
Warning Message
Warning: Rank deficient, rank = 3, tol = 1.196971e-11.
> In nlinfit>LMfit (line 587)
In nlinfit (line 284)
In doseResponse (line 47)
Warning: Rank deficient, rank = 1, tol = 3.802134e-12.
> In nlinfit>LMfit (line 587)
In nlinfit (line 284)
In doseResponse (line 47)
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the model is insensitive to some of its parameters.
That may be because those parameters are not present in the model, or otherwise do not affect the predicted values. It may also be due to
numerical underflow in the model function, which can sometimes be avoided by choosing better initial parameter values, or by rescaling or
recentering. Parameter estimates may be unreliable.
> In nlinfit (line 381)
In doseResponse (line 47)
ans =
-5.2168e+07
Major of rank deficit problem during non-linear curve fitting comes from wrong initial value setting. You may refer to John's answer for the recognition of its importance. In case of the doseRespose function you used. The code was formulated as below for sigmoid curve fitting.
%calculate some rough guesses for initial parameters
minResponse=min(response);
maxResponse=max(response);
midResponse=mean([minResponse maxResponse]);
minDose=min(dose);
maxDose=max(dose);
%fit the curve and compute the values
[coeffs,r,J]=nlinfit(dose,response,sigmoid, [minResponse maxResponse midResponse 1]);
The initial cofficients for sigmoid was estimated using min, and max values of given dose and response. I'm not sure if that's the convention in the field of drug-response plot. However if not, you may use a smarter way to estimate the initial values of those if you have access to Optimization Toolbox.
lsqcurvefit(sigmoid, [minResponse maxResponse midResponse 1], dose, response);
> [0.898627445275206,-0.0795383479232744,57.8616285607284,0.537847599487817]
It can do two things that the Statistics Toolbox functions cannot: (1) accept bounds on the parameters, and (2) fit matrix dependent variables (https://kr.mathworks.com/matlabcentral/answers/131109-parameter-estimation-nlinfit-vs-fitnlm), and therefore provide better estimation. Afterward, simple substitution will give you the desired result.
%fit the curve and compute the values
beta_new = [0.898627445275206,-0.0795383479232744,57.8616285607284,0.537847599487817];
[coeffs,r,J]=nlinfit(dose,response,sigmoid, beta_new);

Iteration limit reached (too few observations in a category?)

in order to identify important variables in a dataset, I am starting out with making a univariate logistic regression model for each of the variables. However, for some categorical variables, there are only a few observations in some categories (the total number of observations is 1155; there are no missing values). Running the code
modelspec1='Y ~ X';
B1 = fitglm(table,modelspec1,'Distribution','binomial');
returns the error
Warning: Iteration limit reached.
In glmfit (line 324)
In GeneralizedLinearModel/fitter (line 575)
In classreg.regr.FitObject/doFit (line 94)
In GeneralizedLinearModel.fit (line 882)
In fitglm (line 142)
and the estimate of beta becomes around 100, with a huge standard error and a p-value close to 1. I have tried to increase the number of iterations using
opts = statset('glmfit');
opts.MaxIter = 10000; % default value for glmfit is 100.
but it did not help. I really need to get some kind of an estimate (just dropping a category is sadly not an option). How can I fix the problem?

Error when using cellfun to do regression on a cluster

I'm using Matlab 2016a on my university's cluster. My local PC has Matlab 2016b installed.
I'm attempting to do a very large regression using cellfun. The drought resistance and predictor layers are 2044x1572. The internal dimensions of "drought resistance" is 100x1 while the internal dimensions of "predictors" is 100x6, representing the different predictors for each cell.
d_regress=cellfun(#(x,y)regress(x,y),Drought_resistance,predictors,'UniformOutput',false);
When I run this code with sample data on my local PC I get no errors.
However, when I run this code on the cluster I get the error:
Error using *
Inner matrix dimensions must agree.
Error in regress (line 93)
b(perm) = R \ (Q'*y);
Error in #(x,y)regress(x,y)
So then I fix the inner matrix dimensions so that they are 1x100 and 100x6.
Then I get the following error, which makes sense because of how regress works.
Error using |
Matrix dimensions must agree.
Error in regress (line 66)
wasnan = (isnan(y) | any(isnan(X),2));
Error in #(x,y)regress(x,y)
I've been having to use mldivide on the cluster in order to do my regressions (mldivide gives me no errors), but I would prefer to use regress due to its additional functionality. Is there any way I can modify my code in order to use regress?

how to use streamline in matlab correctly

i have problem using streamline in matlab. my problem is i have vector field that compute u,v,x,y from a loop with a specific math function. i use this code to figure vector field :
quiver(x,y,u,v)
and it is get good answer and my answer is converge. but when i want to figure streamline with this code:
startx=-100;
starty=-100;
streamline(x,y,u,v,startx,starty)
i got this error:
Error using griddedInterpolant
Interpolation requires at least two sample points in each dimension.
Error in interp1 (line 183)
F = griddedInterpolant(X,V,method);
Error in stream2 (line 62)
sxi=interp1(xx(:),1:szu(2),sx(k));
Error in streamline (line 62)
verts = stream2(x,y,u,v,sx,sy,options);
Error in (line 44)
streamline(x,y,u,v,startx,starty)
thanks in advance.
Best Regard

Error in training neural network: Inputs and targets have different numbers of samples

I am trying to code neural network for face detection.
I have input as (1372*4096) and target as (1372*1). The inputs are images, each image is represented in a row. Therefore, I have 1372 images.
For each image I would like to output one value: output 1 if the image is a face and -1 if it is not a face.
I wrote this code:
[input target]=LoadImage();
net=newff(input,target,[10 5 1],{'tansig','tansig','purelin'}, 'trainrp');
net.trainParam.goal=1e-5;
net.trainParam.epochs=1000;
net.trainParam.lr=0.5;
net.trainParam.show=10;
% start training
net=train(net,input,target);
But I get this error:
Error using trainrp (line 107)
Inputs and targets have different numbers of samples.
Error in network/train (line 106)
[net,tr] = feval(net.trainFcn,net,X,T,Xi,Ai,EW,net.trainParam);
Error in train1 (line 12)
net=train(net,d,out_d);
What should I do to fix this error?
For the Neural Network Toolbox, each input must be a vector, so you will have a matrix with as many columns Q as there are different images. Then the target should be a 1xQ. So it looks like you need to reshape the inputs.
I would recommend using new function FEEDFORWARDNET over the obsolete (but still working) NEWFF.