Пример #1
0
        public static MANOVA RunMANOVA(double[][] y, int[] grpCat, double signficance_level = 0.05)
        {
            MANOVA output = new MANOVA();

            RunMANOVA(y, grpCat, output, signficance_level);
            return(output);
        }
Пример #2
0
        public static void RunMANOVA(double[][] y, int[] grpCat, MANOVA output, double signficance_level = 0.05)
        {
            output.GrandMeansY = GetGrandMeans(y);

            int N = grpCat.Length; // number of data points
            int p = y[0].Length;   // number of variables

            Dictionary <int, List <double[]> > groupped_y = new Dictionary <int, List <double[]> >();

            for (int i = 0; i < N; ++i)
            {
                int      grpId = grpCat[i];
                double[] y_i   = y[i];

                List <double[]> group_y = null;
                if (groupped_y.ContainsKey(grpId))
                {
                    group_y = groupped_y[grpId];
                }
                else
                {
                    group_y           = new List <double[]>();
                    groupped_y[grpId] = group_y;
                }

                group_y.Add(y_i);
            }

            output.SampleMeans = GetSampleMeans(groupped_y);
            output.T           = GetT(groupped_y, output.GrandMeansY, p);
            output.E           = GetE(groupped_y, output.SampleMeans, p);
            output.H           = GetH(output.SampleMeans, output.GrandMeansY, p);

            int g = output.SampleMeans.Count;

            output.dfT = N - 1;
            output.dfH = g - 1;
            output.dfE = N - g;

            output.pValue = GetWilksLambda(output.H, output.E, N, p, g, out output.F_crit);
        }
        public static void RunMANCOVA(double[][] X, double[][] Y, int[] grpCat, out MANCOVA output, double significance_level = 0.05)
        {
            output = new MANCOVA();

            int Dx = X[0].Length;
            int Dy = Y[0].Length;
            int N  = Y.Length;

            Dictionary <int, List <double> >[] groupped_x_at_dim = new Dictionary <int, List <double> > [Dx];
            Dictionary <int, List <double> >[] groupped_y_at_dim = new Dictionary <int, List <double> > [Dy];

            for (int l = 0; l < Dx; ++l)
            {
                groupped_x_at_dim[l] = new Dictionary <int, List <double> >();
            }
            for (int l = 0; l < Dy; ++l)
            {
                groupped_y_at_dim[l] = new Dictionary <int, List <double> >();
            }


            double[][] X_transpose = new double[Dx][];
            for (int i = 0; i < N; ++i)
            {
                double[] X_i = X[i];
                X_transpose[i] = new double[Dx];

                for (int d = 0; d < Dx; ++d)
                {
                    X_transpose[d][i] = X_i[d];
                }
            }

            double[][] Y_transpose = new double[Dy][];
            for (int i = 0; i < N; ++i)
            {
                double[] Y_i = Y[i];
                Y_transpose[i] = new double[Dy];

                for (int d = 0; d < Dx; ++d)
                {
                    Y_transpose[d][i] = Y_i[d];
                }
            }

            for (int i = 0; i < N; ++i)
            {
                int grpId = grpCat[i];

                for (int d = 0; d < Dx; ++d)
                {
                    List <double> group_x = null;
                    if (groupped_x_at_dim[d].ContainsKey(grpId))
                    {
                        group_x = groupped_x_at_dim[d][grpId];
                    }
                    else
                    {
                        group_x = new List <double>();
                        groupped_x_at_dim[d][grpId] = group_x;
                    }
                    group_x.Add(X_transpose[d][i]);
                }

                for (int d = 0; d < Dy; ++d)
                {
                    List <double> group_y = null;
                    if (groupped_y_at_dim[d].ContainsKey(grpId))
                    {
                        group_y = groupped_y_at_dim[d][grpId];
                    }
                    else
                    {
                        group_y = new List <double>();
                        groupped_y_at_dim[d][grpId] = group_y;
                    }
                    group_y.Add(Y_transpose[d][i]);
                }
            }

            int k = groupped_x_at_dim[0].Count;

            output.GrandMeansX = new double[Dx];
            output.GrandMeansY = new double[Dy];

            output.SSTx = new double[Dx];

            for (int d = 0; d < Dx; ++d)
            {
                output.SSTx[d] = GetSST(X_transpose[d], out output.GrandMeansX[d]);
            }
            output.SSTy = new double[Dy];

            for (int d = 0; d < Dy; ++d)
            {
                output.SSTy[d] = GetSST(Y_transpose[d], out output.GrandMeansY[d]);
            }

            output.SSBGx = new double[Dx];
            for (int d = 0; d < Dx; ++d)
            {
                output.SSBGx[d] = GetSSG(groupped_x_at_dim[d], output.GrandMeansX[d]);
            }

            output.SSBGy = new double[Dy];
            for (int d = 0; d < Dy; ++d)
            {
                output.SSBGy[d] = GetSSG(groupped_y_at_dim[d], output.GrandMeansY[d]);
            }

            output.SSWGx = new double[Dx];
            for (int d = 0; d < Dx; ++d)
            {
                output.SSWGx[d] = output.SSTx[d] - output.SSBGx[d];
            }

            output.SSWGy = new double[Dy];
            for (int d = 0; d < Dy; ++d)
            {
                output.SSWGy[d] = output.SSTy[d] - output.SSBGy[d];
            }

            output.SCT = new double[Dy][];
            for (int dy = 0; dy < Dy; ++dy)
            {
                output.SCT[dy] = new double[Dx];
                for (int dx = 0; dx < Dx; ++dx)
                {
                    output.SCT[dy][dx] = GetCovariance(X_transpose[dx], Y_transpose[dy]);
                }
            }

            output.SCWG = new double[Dy][];
            for (int dy = 0; dy < Dy; ++dy)
            {
                output.SCWG[dy] = new double[Dx];
                for (int dx = 0; dx < Dx; ++dx)
                {
                    output.SCWG[dy][dx] = GetCovarianceWithinGroup(groupped_x_at_dim[dx], groupped_y_at_dim[dy]);
                }
            }

            output.rT = new double[Dy][];
            for (int dy = 0; dy < Dy; ++dy)
            {
                output.rT[dy] = new double[Dx];
                for (int dx = 0; dx < Dx; ++dx)
                {
                    output.rT[dy][dx] = output.SCT[dy][dx] / System.Math.Sqrt(output.SSTx[dx] * output.SSTy[dy]);
                }
            }

            output.rWG = new double[Dy][];
            for (int dy = 0; dy < Dy; ++dy)
            {
                output.rWG[dy] = new double[Dx];
                for (int dx = 0; dx < Dx; ++dx)
                {
                    output.rWG[dy][dx] = output.SCWG[dy][dx] / System.Math.Sqrt(output.SSWGx[dx] * output.SSWGy[dy]);
                }
            }

            output.Slope = new double[Dy][]; //b[i][k] where i is the number of columns in X and k is the number of columns in Y
            for (int dy = 0; dy < Dy; ++dy)
            {
                output.Slope[dy] = new double[Dx];
                for (int dx = 0; dx < Dx; ++dx)
                {
                    output.Slope[dy][dx] = output.SCWG[dx][dy] / output.SSWGx[dx];
                }
            }

            output.MeanWithinGroups_x = GetMeanWithinGroup(groupped_x_at_dim);
            output.MeanWithinGroups_y = GetMeanWithinGroup(groupped_y_at_dim);

            output.Intercepts = GetIntercepts(output.MeanWithinGroups_x, output.MeanWithinGroups_y, output.GrandMeansX, output.Slope);

            double[][] Y_adj = new double[N][];
            for (int i = 0; i < N; ++i)
            {
                Y_adj[i] = new double[Dy];
                for (int dy = 0; dy < Dy; ++dy)
                {
                    Y_adj[i][dy] = Y[i][dy] - DotProduct(output.Slope[dy], X[i]);
                }
            }

            MANOVA.RunMANOVA(Y_adj, grpCat, output, significance_level);
        }