在R中双重居中

时间:2017-04-26 15:52:31

标签: r matrix

你知道如何将矩阵变换为R中所谓的双重居中矩阵吗?这样变换矩阵的和(col)和sum(行)都是零向量。感谢。

1 个答案:

答案 0 :(得分:4)

使用以下算法对矩阵M进行双重定心:

1)生成两个与原始矩阵大小相同的矩阵,其中包含行方式和逐列方式。让我们称这两个矩阵为R和C:

           if ((start != null && end != null))
            {
                if (IncludeClosedQuotes == true && quotes1.QuoteStatus != null)
                {
                    if((ExcludeNoBid == false) && ((String.IsNullOrEmpty(ProductCode)) || (quotes1.ProductCode != ProductCode)))
                    {
                        myList.Add(quotes1);
                    }
                    if ((ExcludeNoBid == false) && (quotes1.ProductCode == ProductCode) && (quotes1.SalesCode.ToString() == SalesPerson))
                    {
                        myList.Add(quotes1);
                    }

                    if ((ExcludeNoBid == true && quotes1.NoBid == false) && (quotes1.ProductCode == ProductCode))
                    {
                        myList.Add(quotes1);
                    }
                    if((ExcludeNoBid == true && quotes1.NoBid == false) && (quotes1.ProductCode != ProductCode) && (quotes1.SalesCode.ToString() != SalesPerson))
                    {
                         myList.Add(quotes1);
                    }
                }

                if ((IncludeClosedQuotes == false) && (ExcludeNoBid == false))
                {
                    if (quotes1.ProductCode == ProductCode)
                    {
                        myList.Add(quotes1);
                    }
                    if ((quotes1.ProductCode == ProductCode) && (quotes1.SalesCode.ToString() == SalesPerson))
                    {
                        myList.Add(quotes1);
                    }
                }
                if ((IncludeClosedQuotes == false) && (ExcludeNoBid == true && quotes1.NoBid == false))
                {
                    if ((quotes1.ProductCode == ProductCode) && (quotes1.SalesCode.ToString() != SalesPerson))
                    {
                        myList.Add(quotes1);
                    }

                    if((String.IsNullOrEmpty(ProductCode) && quotes1.SalesCode.ToString() == SalesPerson))
                    {
                        myList.Add(quotes1);
                    }
                }
          }

    | mean(M[1,1:3])  mean(M[1,1:3])  mean(M[1,1:3]) | 
R = | mean(M[2,1:3])  mean(M[2,1:3])  mean(M[2,1:3]) | 
    | mean(M[3,1:3])  mean(M[3,1:3])  mean(M[3,1:3]) | 

2)将它们减去M并加上平均值: | mean(M[1:3,1]) mean(M[1:3,2]) mean(M[1:3,3]) | C = | mean(M[1:3,1]) mean(M[1:3,2]) mean(M[1:3,3]) | | mean(M[1:3,1]) mean(M[1:3,2]) mean(M[1:3,3]) |

以下是执行此操作的代码:

M - C - R + grand_mean(M)

您可以通过计算# example data M = matrix(runif(9),nrow=3,ncol=3) # compute the row-wise and column-wise mean matrices R = M*0 + rowMeans(M) # or `do.call(cbind, rep(list(rowMeans(tst)),3))` C = t(M*0 + colMeans(M)) # or `do.call(rbind, rep(list(colMeans(tst)),3))` # substract them and add the grand mean M_double_centered = M - R - C + mean(M[]) rowMeans(M_double_centered)来检查这是否给出了正确的答案。