juliacontingencyproportions

Julia function for conditional proportions given margins of multi-dimensional array


I am new to Julia and looking for a function to compute the proportions of multidimensional array give some dimensions as margin. Basically, it is dividing each element of the array by the sum of element in the desired dimensions. In R, proportions() is a function to do this.


Solution

  • After some search and thanks to @DNF, I came up with this function:

    function proportions(x; d = 1)
        dim = (setdiff(1:ndims(x), d)...,)
        return x ./ sum(x, dims = dim)
    end
    

    and a similar function for the equivalent R function marginSums():

    function marginSums(x; d = 1)
        dim = (setdiff(1:ndims(x), d)...,)
        return  dropdims(sum(x, dims = dim), dims = dim)
    end