How to ignore columns which have only NaNs and move onto the next column for the mutli regression?

3 views (last 30 days)
Hi,
I am trying to run a multi regression with 6 variables. Some of the variables data have columns which consist only out of NaNs. (all-NaN columns are not always the same for all variables)
How could I ignore columns which consist of all NaNs and move to the next column if this is a case for any of the variables?
Please see below my code for the regression:
numRegressors = 6;
numTickers = size(y,2);
numDays = size (y,1);
mat_betas=nan(numRegressors+1,numTickers); %beta coefficients
mat_tstats=nan(numRegressors+1,numTickers); %t-stats
vec_rsqrds=nan(1,numTickers); %r-squares
mat_pval=nan(numRegressors+1,numTickers); %p-values
whichstats = {'beta','rsquare','tstat'};
for column = 1:size(y,2);
All_Factors = [a(:,column) b(:,column) c(:,column) d(:,column) e(:,column) f(:,column)];
all_stats = regstats(y(:,column),All_Factors,'linear',whichstats);
mat_betas(1:numRegressors+1,column)=all_stats.beta;
mat_tstats(:,column)=all_stats.tstat.t;
vec_rsqrds(:,column)=all_stats.rsquare;
mat_pval(:,column)=all_stats.tstat.pval;
end
Thank you in advance.

Accepted Answer

KSSV
KSSV on 27 Oct 2017
You can check whether a number is nan or not using isnan. Read about it.
You can remove the NaN's from the column and then proceed.
% some random data
data = rand(100,1) ;
data(randperm(100,30)) = NaN ; % introduced NaN's
%%remove NaN's
data(isnan(data)) = [] ;
  1 Comment
Klio
Klio on 27 Oct 2017
Thank you for your answer.
But by replacing NaNs the regression generates not precise results. Could you please possibly suggest how I could count the number of NaNs in the column and if it consists of only NaNs (or for example, has more than 200 NaNs) then move regression loop onto the next column?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!