NaN values in fitglm

조회 수: 19 (최근 30일)
Sanjana H
Sanjana H 2024년 1월 29일
댓글: Sanjana H 2024년 2월 29일
Hello, I have 2 models, whose coefficients matrix has been designed. I would like to use these matrices and the response variable to obtain the values for the predictors in my models. I have used fitglm for the same. My code is as follows:
Some background: I have 6 conditions in my response variable, (so Y has 6 values), the values to which I am trying to model with my predictors - model 1 has 3 predictors and model 2 has 6.
In model 2, the first 3 predictors are same as model 1, and the next 3 are interactions between the first 3. I have included the interactions as distinct terms.
for type = 1:2
if type == 1
%bar graph specifications
r = [2 0 0;1 1 0;1 0 1;0 2 0;0 1 1;0 0 2];
elseif type == 2
%bar graph specifications
r = [2 0 0 0 0 0;1 1 0 1 0 0;1 0 1 0 1 0;0 2 0 0 0 0;0 1 1 0 0 1 ;0 0 2 0 0 0 ];
%Solving the linear model
estimate = [];
SE = [];
tstat = [];
pValue = [];
adj_R_sq = [];
aic = [];
for i = 1:length(data_final)
%Getting the response for one condition
y = data_final(i,:)';
mdl = fitglm(r,y,'Intercept',false);
%saving the needed variables
estimate = cat(1,estimate,mdl.Coefficients.Estimate');
SE = cat(1,SE,mdl.Coefficients.SE');
tstat = cat(1,tstat,mdl.Coefficients.tStat');
pValue = cat(1,pValue,mdl.Coefficients.pValue');
% Getting R-squared
R_squared = mdl.Rsquared.Ordinary;
% Get the number of predictors and observations
num_predictors = mdl.NumPredictors;
num_observations = mdl.NumObservations;
% Calculate adjusted R-squared
adjusted_R_squared = 1 - ((1 - R_squared) * (num_observations - 1) / (num_observations - num_predictors - 1));
adj_R_sq = cat(1,adj_R_sq,adjusted_R_squared);
%Saving AIC
aic = cat(1,aic,mdl.ModelCriterion.AIC);
My first model works well, but my second model gives estimates, but the other values are all NaN. Please help me figure out if:
  1. I am going about the right way for my question
  2. If so, if this NaN issue can be sorted.
Thanks for any help in advance.

답변 (1개)

Debadipto 2024년 2월 29일
편집: Debadipto 2024년 2월 29일
Hello @Sanjana,
The NaN values you're encountering could be due to a conflict where one function is being inadvertently masked by another. To address this, I recommend resetting your MATLAB path to its default settings. Here's how you can do it:
1. First, safeguard your current search path with a backup. Run this command to find out where 'pathdef.m' is located:
>> which -all pathdef
If you've customized your 'pathdef.m' with additional paths, make sure to copy this file to a secure location outside of your MATLAB path. This precaution is optional but important if you have made specific changes to your path settings.
2. Next, execute the following commands in the MATLAB Command Window. These will reset your search path to its original state and refresh the toolbox cache:
>> restoredefaultpath
>> rehash toolboxcache
3. Once you've completed the above steps, try using MATLAB again to check if the NaN issue has been resolved. If everything is working correctly, you can then save the updated search path with this command:
>> savepath
Hope you should be able to eliminate any function conflicts that might be causing the NaN value anomaly.
  댓글 수: 1
Sanjana H
Sanjana H 2024년 2월 29일
Hello, thank you for your response. Even after trying these steps, the issue still persists. Is there anything else that might be causing this?

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

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

Start Hunting!

Translated by