r – Roll regression for 4 years of data which moves one month ahead for each new regression and with NA treatment

Some time ago, I made a question here about a code that permit me to estimate rolling regression taking 4 years for each regression which moves one month ahead for each new estimation. The answer was of a lot of help and I finally reached to make my own code based on the answer. However, my database has a lot of variables that have NAs for many years, so I had to delete those variables in order to avoid the error I was getting because some windows for some variables were full of NAs. My original dataset called “Data_t” is:

Data

And what I did was:

thedata <- Data_t(,which(colMeans(!is.na(Data_t))>0.88))

Then, according to the answer to my previous question I applied:

library(zoo)

ndata <- data.frame(date = thedata$Fecha, thedata(,2:ncol(thedata)))
w <- 49 # number of trailing months to regress over 
depvars <- colnames(thedata(7:ncol(thedata)))
indep <- c("Mkt","SMB","HML","RMW", "CMA")

reg <- function(ym_, depvar, indep, data, w, ym) {
ok <- ym > ym_ - w/12 & ym <= ym_
fo <- reformulate(indep, depvar)
fm <- lm(fo, data, subset = ok, na.action = na.exclude)
co <- coef(fm)
n <- nobs(fm)
se_sq <- diag(vcov(fm))
fac <- se_sq / (se_sq + sigma(fm)^2)
fac2 <- sigma(fm)^2/(se_sq + sigma(fm)^2)
co= co * fac + co * fac2
c(co, n = n)

}

ym <- as.yearmon(ndata$date)
ym_u <- tail(unique(ym), -(w-1))

L <- Map(function(depvar) {
data.frame(yearmon = ym_u, 
         t(sapply(ym_u, reg, 
                  depvar = depvar, indep = indep, data = ndata, w = w, ym = ym)), 
         check.names = FALSE)
},depvars)

b_data <- dplyr::bind_rows(L, .id = "depvar")

HOWEVER I now have been asked not to omit those variables whit NA, but to include their estimation from the year where they have data. I was trying the option to built different data sets but it is so much work and not efficient at all. I would like to know if you can tell me a way to include within the functional code a way to say r to take variables from the year where NAs end and from there to estimate coefficients.

Thanks in advance.