How should I detangle this group of lapply/sapply/mapply

Viewed 20

all, could someone help me figure out what this code is doing? New to R, I'm assigned to figure out the logic in the code below. Any tips would be greatly appreciated!

temp contains regression coefficients

    f = function(l, i, flip) t(sapply(l[!sapply(l, is.null)],
    function(x) setNames(x[, i][fields], fields) * (if (flip) sign else 1)))

    g = if (calc.tstat){
        function(...) FUN(...)} else 
        {function(...) FUN(..1)}
  
    temp = lapply(temp, 
         function(l, 
                coef = f(l, 1, T), 
                tstat = coef/f(l, 2, F)) 
                lapply(unname(split(1:nrow(coef), horzcode[1:nrow(coef)])), 
                    function(i) if (length(i)) g(coef[i, , drop = F], tstat[i, , drop = F])))

I'm completely lost in the multiple layers of apply/sapply/mapply below:

    temp = lapply(1:horizon, 
        function(k) sapply(names(temp[[which.max(last)]][[1]]), 
            function(n) do.call(rbind, 
    setNames(mapply(function(i, j) if (!is.na(j)) temp[[j]][[k]][[n]][i, ][fields] 
                                    else replace(NULL, fields, NA),        
    na.fill(replace(rep(NA, length(forms)), horzcode == k, seq_len(sum(horzcode == k)))), 
        as.integer(forms), SIMPLIFY = F), 
        levels(datecode))), simplify = F))
0 Answers
Related