1
votes

To provide a minimal example (I would like to use the solution of this in numerous applications), I am using the regress function in MATLAB.

The data I have in MATLAB is (monthly) time series, however, some data points are missing at the start for certain series.

Example Data

Lets say Y is a dependent variable in an arbitrary regression model and I would like to run 10 regression models,

$$Y_t=\alpha+X_{i,t-1}$$

I would therefore like to run 9 models, where the Y is the dependent variable and each model the explanatory variable changes from $X_2$ to $X_10$.

To be clear, I would like to run 9 models (see image),

Equations

The problem I am having is that I would like the first model that uses Y (i.e. data(:,1)) and X2 (i.e. data(:,2) to use the first 1000 observations, but for the X3, there is 988 observations, so I would like to use 988 observations for Y and X3 when it does the regression.

In other questions, people have suggested changing the NaNs to 0's but this is not possible. EDIT (its obviously possible to change to zeros, but it is not possible for my model specification).

%Data
data = rand(1000,10); %add NaNs randomly at the start of the series. 

%Regression
for ii = 2:10
b = regress(data(:,1),data(:,ii))
end

This question is different as it uses the regress function (yesterdays simple mean) also the answerer has provided lots of detail and a solution that actually works.

1
BTW, its not your model specification. Do not change them to zeros, because 0 is a number, is data and will have an effect in the end result. If you make it a zero, you are changing the measurements, it is not any different to changing it to pi, 1000 or -4. Zero is just another number, not a placeholder for lack of data. I have no idea where you've seen that changing to zero is a possibility, but it is very wrong.Ander Biguri
somebody suggested it in another question but I agree its totally wrong to change it to a zero.S30609
Duplicate of the question you asked yesterday: Dealing with NaN in matlab, simple tasks. Edit questions to add clarity, don't ask them again. The accepted answer is basically exactly the same as my comment on that questionWolfie
@Wolfie you are right. I suggest duping the other way around, as OP basically wanted to ask what they asked here, they just weren't clear enough yesterdayAnder Biguri
I don't believe I explicitly said to ask another question, I meant you should have improved the existing one. No worries, you've definitely written a better question, glad it yielded a better answer!Wolfie

1 Answers

4
votes

Just select the not NaNs

%Data
data = rand(1000,10); %add NaNs randomly at the start of the series. 

%Regression
for ii = 2:10
   notnans=~isnan(data(:,ii)); % assuming data(:,1) has no NaNs
   b = regress(data(notnans,1),data(notnans,ii))
end