Exemple #1
0
        public static double SemTheta_Calc(double thEst, CATItems it, int[] x = null, string model = null, double[] priorPar = null, int[] parInt = null, double D = 1, string method = "BM", string priorDist = "norm")
        {
            double result = 0;

            if (priorPar == null || priorPar.Length < 2)
            {
                priorPar    = new double[2];
                priorPar[0] = 0;
                priorPar[1] = 1;
            }

            if (parInt == null || parInt.Length < 3)
            {
                parInt    = new int[3];
                parInt[0] = -4;
                parInt[1] = 4;
                parInt[2] = 33;
            }

            if (method == ModelNames.EstimaatorMethods.EAP.EnumToString())
            {
                result = EapSEM.EapSEM_Calc(thEst, it, x, model, priorPar, D, priorDist, parInt[0], parInt[1], parInt[2]);
            }
            else
            {
                if (String.IsNullOrEmpty(model))   // Dichotomous Items
                {
                    #region "Function 'dr0' "
                    Func <double> dr0 = () =>
                    {
                        double res = 0;

                        if (method == ModelNames.EstimaatorMethods.BM.EnumToString())
                        {
                            switch (priorDist)
                            {
                            case "norm":
                                res = -1 / (Math.Pow(priorPar[1], 2));
                                break;

                            case "unif":
                                res = 0;
                                break;

                            case "Jeffreys":
                                IiList objIi = Ii.Ii_Calc(thEst, it, model, D);
                                res = (CatRcs.Utils.RowColumn.Sum(objIi.d2Ii) * CatRcs.Utils.RowColumn.Sum(objIi.Ii)
                                       - Math.Pow(CatRcs.Utils.RowColumn.Sum(objIi.dIi), 2)) / (2 * Math.Pow(CatRcs.Utils.RowColumn.Sum(objIi.Ii), 2));
                                break;
                            }
                        }
                        else
                        {
                            if (method == ModelNames.EstimaatorMethods.ML.EnumToString() || method == ModelNames.EstimaatorMethods.WL.EnumToString())
                            {
                                res = 0;
                            }
                        }

                        return(res);
                    };
                    #endregion

                    result = 1 / Math.Sqrt(-dr0() + CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(thEst, it, model, D).Ii));
                }
                else   // Polytomous Items
                {
                    double met = 0, pd = 0, optI = 0;
                    ModelNames.EstimaatorMethods md = ModelNames.StringToEnumMethods(method);

                    switch (md)
                    {
                    case ModelNames.EstimaatorMethods.ML:
                        met = 1;
                        break;

                    case ModelNames.EstimaatorMethods.BM:
                        met = 2;
                        break;

                    case ModelNames.EstimaatorMethods.WL:
                        met = 3;
                        break;

                    case ModelNames.EstimaatorMethods.EAP:
                        met = 4;
                        break;
                    }

                    switch (priorDist)
                    {
                    case "norm":
                        pd = 1;
                        break;

                    case "unif":
                        pd = 2;
                        break;

                    case "Jeffreys":
                        pd = 3;
                        break;
                    }

                    if (met == 1 || (met == 2 && pd == 2))
                    {
                        optI = CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(thEst, it, model, D).Ii);
                    }

                    if (met == 2 && pd == 1)
                    {
                        optI = CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(thEst, it, model, D).Ii) + (1 / Math.Pow(priorPar[1], 2));
                    }

                    if (met == 3 || (met == 2 && pd == 3))
                    {
                        IiList objIi = Ii.Ii_Calc(thEst, it, model, D);

                        if (met == 2)
                        {
                            optI = CatRcs.Utils.RowColumn.Sum(objIi.Ii) + (Math.Pow(CatRcs.Utils.RowColumn.Sum(objIi.dIi), 2) - CatRcs.Utils.RowColumn.Sum(objIi.d2Ii) *
                                                                           CatRcs.Utils.RowColumn.Sum(objIi.Ii)) / (2 * Math.Pow(CatRcs.Utils.RowColumn.Sum(objIi.Ii), 2));
                        }
                        else
                        {
                            optI = CatRcs.Utils.RowColumn.Sum(objIi.Ii);
                        }
                    }

                    result = 1 / Math.Sqrt(optI);
                }
            }

            return(result);
        }
Exemple #2
0
        public static double ThetaEst_Calc(CATItems it, int[] x, string model = null, double[] priorPar = null, double[] range = null, int[] parInt = null, double D = 1, string method = "BM", string priorDist = "norm")
        {
            double result = 0; double[] RANGE = null;

            #region "Parameter Validation"

            if (priorPar == null || priorPar.Length < 2)
            {
                priorPar    = new double[2];
                priorPar[0] = 0;
                priorPar[1] = 1;
            }

            if (range == null || range.Length < 2)
            {
                range    = new double[2];
                range[0] = -4;
                range[1] = 4;
            }

            if (parInt == null || parInt.Length < 3)
            {
                parInt    = new int[3];
                parInt[0] = -4;
                parInt[1] = 4;
                parInt[2] = 33;
            }

            #endregion

            if (method == ModelNames.EstimaatorMethods.EAP.EnumToString())
            {
                result = EapEST.EapEST_Calc(it, x, model, priorPar, D, priorDist, parInt[0], parInt[1], parInt[2]);
            }
            else
            {
                if (String.IsNullOrEmpty(model))   // Dichotomous Items
                {
                    #region "Function 'r0' "
                    Func <double, string, double> r0 = (th, met) =>
                    {
                        double res = 0;

                        if (met == ModelNames.EstimaatorMethods.BM.EnumToString())
                        {
                            switch (priorDist)
                            {
                            case "norm":
                                res = (priorPar[0] - th) / Math.Pow(priorPar[1], 2);
                                break;

                            case "unif":
                                res = 0;
                                break;

                            case "Jeffreys":
                                IiList objIi = Ii.Ii_Calc(th, it, model, D);
                                res = CatRcs.Utils.RowColumn.Sum(objIi.dIi) / (2 * CatRcs.Utils.RowColumn.Sum(objIi.Ii));
                                break;
                            }
                        }
                        else
                        {
                            ModelNames.EstimaatorMethods md = ModelNames.StringToEnumMethods(met);

                            switch (md)
                            {
                            case ModelNames.EstimaatorMethods.ML:
                                res = 0;
                                break;

                            case ModelNames.EstimaatorMethods.WL:
                                res = CatRcs.Utils.RowColumn.Sum(Ji.Ji_Calc(th, it, model, D).Ji) / (2 * CatRcs.Utils.RowColumn.Sum(Ii.Ii_Calc(th, it, model, D).Ii));
                                break;
                            }
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'r' "
                    Func <double, double> r = (th) =>
                    {
                        double   res = 0;
                        double[] Q   = new double[it.NumOfItems], x_p = new double[it.NumOfItems], p_q = new double[it.NumOfItems], temp = new double[it.NumOfItems];

                        double[] pi  = Pi.Pi_Calc(th, it, model, D).Pi;
                        double[] dpi = Pi.Pi_Calc(th, it, model, D).dPi;

                        for (int ind_p = 0; ind_p < pi.Length; ind_p++)
                        {
                            Q[ind_p]    = 1 - pi[ind_p];
                            x_p[ind_p]  = x[ind_p] - pi[ind_p];
                            p_q[ind_p]  = pi[ind_p] * Q[ind_p];
                            temp[ind_p] = dpi[ind_p] * (x_p[ind_p] / p_q[ind_p]);
                        }

                        res = temp.Sum();

                        return(res);
                    };
                    #endregion

                    #region "Function 'f' "
                    Func <double, double> f = (th) =>
                    {
                        double res = 0;
                        return(res);
                    };


                    if (method == ModelNames.EstimaatorMethods.BM.EnumToString() && priorDist == "unif")
                    {
                        f = (th) =>
                        {
                            double res = 0;

                            string methodName = ModelNames.EstimaatorMethods.ML.EnumToString();

                            res = r0(th, methodName) + r(th);

                            return(res);
                        };
                    }
                    else
                    {
                        f = (th) =>
                        {
                            double res = 0;

                            res = r0(th, method) + r(th);

                            return(res);
                        };
                    }

                    #endregion

                    if (method == ModelNames.EstimaatorMethods.BM.EnumToString() && priorDist == "unif")
                    {
                        RANGE = priorPar;
                    }
                    else
                    {
                        RANGE = range;
                    }

                    if (f(RANGE[0]) < 0 && f(RANGE[1]) < 0)
                    {
                        result = RANGE[0];
                    }
                    else
                    {
                        if (f(RANGE[0]) > 0 && f(RANGE[1]) > 0)
                        {
                            result = RANGE[1];
                        }
                        else
                        {
                            result = CatRcs.Utils.Maths.UniRoot(f, RANGE).root;
                        }
                    }
                }
                else   // Polytomous Items
                {
                    int met = 0, pd = 0;
                    ModelNames.EstimaatorMethods md = ModelNames.StringToEnumMethods(method);

                    #region "Conditional Block"

                    switch (md)
                    {
                    case ModelNames.EstimaatorMethods.ML:
                        met = 1;
                        break;

                    case ModelNames.EstimaatorMethods.BM:
                        met = 2;
                        break;

                    case ModelNames.EstimaatorMethods.WL:
                        met = 3;
                        break;

                    case ModelNames.EstimaatorMethods.EAP:
                        met = 4;
                        break;
                    }

                    switch (priorDist)
                    {
                    case "norm":
                        pd = 1;
                        break;

                    case "unif":
                        pd = 2;
                        break;

                    case "Jeffreys":
                        pd = 3;
                        break;
                    }

                    if (met == 2 && pd == 2)
                    {
                        RANGE    = new double[priorPar.Length];
                        RANGE[0] = Math.Max(priorPar[0], range[0]);
                        RANGE[1] = Math.Min(priorPar[1], range[1]);
                    }
                    else
                    {
                        RANGE = range;
                    }

                    #endregion

                    #region "Function 'dl' "
                    Func <double, double> dl = (th) =>
                    {
                        double res = 0;

                        PiListPoly p = Pi.Pi_Poly_Calc(th, it, model, D);
                        double[,] pr  = p.Pi;
                        double[,] dpr = p.dPi;

                        for (int i = 0; i < x.Length; i++)
                        {
                            res = res + dpr[i, x[i]] / pr[i, x[i]];
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'f0' "
                    Func <double, double> f0 = (th) =>
                    {
                        double res = 0;

                        if (met == 2)
                        {
                            switch (pd.ToString())
                            {
                            case "1":
                                res = (priorPar[0] - th) / Math.Pow(priorPar[1], 2);
                                break;

                            case "2":
                                res = 0;
                                break;

                            case "3":
                                IiList objIi = CatRcs.Ii.Ii_Calc(th, it, model, D);
                                res = CatRcs.Utils.RowColumn.Sum(objIi.dIi) / (2 * CatRcs.Utils.RowColumn.Sum(objIi.Ii));
                                break;
                            }
                        }
                        else
                        {
                            switch (met.ToString())
                            {
                            case "1":
                                res = 0;
                                break;

                            case "2":
                                res = 0;
                                break;

                            case "3":
                                res = CatRcs.Utils.RowColumn.Sum(CatRcs.Ji.Ji_Calc(th, it, model, D).Ji) / (2 * CatRcs.Utils.RowColumn.Sum(CatRcs.Ii.Ii_Calc(th, it, model, D).Ii));
                                break;
                            }
                        }

                        return(res);
                    };
                    #endregion

                    #region "Function 'optF' "
                    Func <double, double> optF = (th) =>
                    {
                        double res = 0;
                        res = dl(th) + f0(th);
                        return(res);
                    };
                    #endregion

                    if (optF(RANGE[0]) < 0 && optF(RANGE[1]) < 0)
                    {
                        result = RANGE[0];
                    }
                    else
                    {
                        if (optF(RANGE[0]) > 0 && optF(RANGE[1]) > 0)
                        {
                            result = RANGE[1];
                        }
                        else
                        {
                            result = CatRcs.Utils.Maths.UniRoot(optF, RANGE).root;
                        }
                    }
                }
            }

            return(result);
        }
Exemple #3
0
        public static IiList Ii_Calc(double th, CATItems it, string model, double D)
        {
            IiList objIi = null;

            double[] Ii = null; double[] dIi = null; double[] d2Ii = null;

            try
            {
                if (String.IsNullOrEmpty(model))
                {
                    #region "Calculation for Dichotmous Items"

                    double[] P = null; double[] dP = null; double[] d2P = null; double[] d3P = null;

                    PiList pr = Pi.Pi_Calc(th, it, model, D); // Check return value of Pi

                    if (pr == null)
                    {
                        objIi           = new IiList();
                        objIi.Exception = "No Pi values found!";
                        return(objIi);
                    }

                    P   = pr.Pi;
                    dP  = pr.dPi;
                    d2P = pr.d2Pi;
                    d3P = pr.d3Pi;

                    Ii   = new double[dP.Length];
                    dIi  = new double[d2P.Length];
                    d2Ii = new double[d3P.Length];

                    objIi = new IiList(P.Length);

                    double[] Q = new double[P.Length];

                    for (int i = 0; i < P.Length; i++)
                    {
                        Q[i] = 1 - P[i];
                    }

                    for (int j = 0; j < Ii.Length; j++)
                    {
                        Ii[j] = Math.Pow(dP[j], 2) / (P[j] * Q[j]);
                    }

                    for (int k = 0; k < dIi.Length; k++)
                    {
                        dIi[k] = dP[k] * (2 * P[k] * Q[k] * d2P[k] - Math.Pow(dP[k], 2) * (Q[k] - P[k])) / (Math.Pow(P[k], 2) * Math.Pow(Q[k], 2));
                    }

                    for (int l = 0; l < d2Ii.Length; l++)
                    {
                        d2Ii[l] = (2 * P[l] * Q[l] * (Math.Pow(d2P[l], 2) + dP[l] * d3P[l]) - 2 * Math.Pow(dP[l], 2) * d2P[l] * (Q[l] - P[l])) / (Math.Pow(P[l], 2) * Math.Pow(Q[l], 2)) -
                                  (3 * Math.Pow(P[l], 2) * Q[l] * Math.Pow(dP[l], 2) * d2P[l] - P[l] * Math.Pow(dP[l], 4) * (2 * Q[l] - P[l])) / (Math.Pow(P[l], 4) * Math.Pow(Q[l], 2)) +
                                  (3 * P[l] * Math.Pow(Q[l], 2) * Math.Pow(dP[l], 2) * d2P[l] - Q[l] * Math.Pow(dP[l], 4) * (Q[l] - 2 * P[l])) / (Math.Pow(P[l], 2) * Math.Pow(Q[l], 4));
                    }

                    #endregion
                }
                else
                {
                    #region "Calculation for Polytomous Items"

                    double[,] P = null; double[,] dP = null; double[,] d2P = null; double[,] d3P = null;

                    PiListPoly pr = Pi.Pi_Poly_Calc(th, it, model, D);

                    if (pr == null)
                    {
                        objIi           = new IiList();
                        objIi.Exception = "No Pi values found!";
                        return(objIi);
                    }

                    P   = pr.Pi;
                    dP  = pr.dPi;
                    d2P = pr.d2Pi;
                    d3P = pr.d3Pi;

                    int rowLength    = pr.Pi.GetLength(0);
                    int columnLength = pr.Pi.GetLength(1);

                    objIi = new IiList(rowLength);

                    double[,] pr0 = new double[rowLength, columnLength];
                    double[,] pr1 = new double[rowLength, columnLength];
                    double[,] pr2 = new double[rowLength, columnLength];

                    Ii   = new double[rowLength];
                    dIi  = new double[rowLength];
                    d2Ii = new double[rowLength];

                    for (int i = 0; i < rowLength; i++)
                    {
                        for (int j = 0; j < columnLength; j++)
                        {
                            pr0[i, j] = Math.Pow(dP[i, j], 2) / P[i, j];

                            pr1[i, j] = 2 * dP[i, j] * (d2P[i, j] / P[i, j]) - (Math.Pow(dP[i, j], 3) / Math.Pow(P[i, j], 2));

                            pr2[i, j] = (2 * Math.Pow(d2P[i, j], 2) + 2 * dP[i, j] * d3P[i, j]) / P[i, j] - 2 * Math.Pow(dP[i, j], 2) * (d2P[i, j] / -3) * dP[i, j] * (d2P[i, j] / Math.Pow(P[i, j], 2)) + 2 * (Math.Pow(dP[i, j], 4) / Math.Pow(P[i, j], 3));
                        }
                    }

                    Ii   = RowColumn.rowSums(pr0);
                    dIi  = RowColumn.rowSums(pr1);
                    d2Ii = RowColumn.rowSums(pr2);

                    #endregion
                }

                objIi.Add(Ii, dIi, d2Ii);
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    if (objIi == null)
                    {
                        objIi = new IiList();
                    }
                    objIi.Exception = ex.Message;
                }
                return(objIi);
            }

            return(objIi);
        }
Exemple #4
0
        public void testIi_P(ModelNames.Models paramModel)
        {
            REngine.SetEnvironmentVariables();

            REngine engineObj = REngine.GetInstance();

            // Loading a library from R
            engineObj.Evaluate("library(catR)");

            // Polytomous Items
            CharacterVector modelName = engineObj.CreateCharacterVector(new string[] { paramModel.EnumToString() });

            engineObj.SetSymbol("modelName", modelName);
            DataFrame PolyItems = engineObj.Evaluate("PolyItems <- genPolyMatrix(10, 5, model = modelName, same.nrCat = FALSE)").AsDataFrame();

            engineObj.SetSymbol("PolyItems", PolyItems);

            // Adapting with the existing "CAT-Items" type (Wrapper)

            Console.WriteLine("*******************************************");
            Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString());
            Console.WriteLine("*******************************************");

            CATItems itemBank = new CATItems(NumOfItems: PolyItems[0].Length, model: paramModel.EnumToString(), same_nrCat: false, nrCat: 5);

            for (int i = 0; i < itemBank.colSize; i++)
            {
                itemBank.all_items_poly[i] = PolyItems[i].Select(y => (double)y).ToArray();
            }

            for (int j = 0; j < itemBank.colSize; j++)
            {
                for (int k = 0; k < itemBank.NumOfItems; k++)
                {
                    if (CatRcs.Utils.CheckNaValues.IsNaNvalue(itemBank.all_items_poly[j][k]))
                    {
                        itemBank.all_items_poly[j][k] = double.NaN;
                    }
                }
            }

            #region "Test block for Ability Values (th)"

            IiList objIi        = null;
            int    decimalPoint = 6;

            double[] th_Values = CatRcs.Utils.CommonHelper.Sequence(-4, 4, by: 1);

            Console.WriteLine("******* TEST for Ability value Theta ********");

            for (int k = 0; k < th_Values.Length; k++)
            {
                // Sequence Generation for Theta (Ability) values
                NumericVector th_val = engineObj.CreateNumericVector(new double[] { th_Values[k] });
                engineObj.SetSymbol("th_val", th_val);

                // Calling the Ii function from R
                GenericVector result_Ii = engineObj.Evaluate("result_Ii <- Ii(th = th_val, PolyItems, model = modelName, D = 1)").AsList();

                // Getting the function result
                NumericVector Ii   = result_Ii[0].AsNumeric();
                NumericVector dIi  = result_Ii[1].AsNumeric();
                NumericVector d2Ii = result_Ii[2].AsNumeric();

                Console.WriteLine("Value of Theta: " + th_Values[k]);

                objIi = CatRLib.Ii(th_Values[k], itemBank, paramModel.EnumToString(), 1);

                #region "Ii"

                int z = 0;
                resultFlag = true;

                Console.WriteLine("****** Ii ******");

                for (int i = 0; i < objIi.Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.Ii[i]), decimalPoint))
                    {
                        Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of Ii result is Passed!");

                #endregion

                #region "dIi"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** dIi ******");

                for (int i = 0; i < objIi.dIi.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(dIi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.dIi[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of dIi result is Passed!");

                #endregion

                #region "d2Ii"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** d2Ii ******");

                for (int i = 0; i < objIi.d2Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(d2Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.d2Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of d2Ii result is Passed!");

                #endregion
            }

            #endregion

            #region "Test block for Metric values"

            objIi = null;

            double[] D_Values = CatRcs.Utils.CommonHelper.Sequence(0.5, 1, by: 0.1);

            Console.WriteLine("******* TEST for Metric Constant ********");

            for (int k = 0; k < D_Values.Length; k++)
            {
                // Sequence Generation for Metric values
                NumericVector D_val = engineObj.CreateNumericVector(new double[] { D_Values[k] });
                engineObj.SetSymbol("D_val", D_val);

                // Calling the Ii function from R
                GenericVector result_Ii = engineObj.Evaluate("result_Ii <- Ii(th = 0, PolyItems, model = modelName, D = D_val)").AsList();

                // Getting the function result
                NumericVector Ii   = result_Ii[0].AsNumeric();
                NumericVector dIi  = result_Ii[1].AsNumeric();
                NumericVector d2Ii = result_Ii[2].AsNumeric();

                Console.WriteLine("Value of Metric Constant: " + D_Values[k]);

                objIi = CatRLib.Ii(0, itemBank, paramModel.EnumToString(), D_Values[k]);

                #region "Ii"

                int z = 0;
                resultFlag = true;

                Console.WriteLine("****** Ii ******");

                for (int i = 0; i < objIi.Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of Ii result is Passed!");

                #endregion

                #region "dIi"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** dIi ******");

                for (int i = 0; i < objIi.dIi.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(dIi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.dIi[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of dIi result is Passed!");

                #endregion

                #region "d2Ii"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** d2Ii ******");

                for (int i = 0; i < objIi.d2Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(d2Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.d2Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of d2Ii result is Passed!");

                #endregion
            }

            #endregion
        }
Exemple #5
0
        public void testIi_D()
        {
            REngine.SetEnvironmentVariables();

            REngine engineObj = REngine.GetInstance();

            // Loading a library from R
            engineObj.Evaluate("library(catR)");

            // Dichotomous Items
            DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = 10)").AsDataFrame();

            engineObj.SetSymbol("DichoItems", DichoItems);

            // Adapting with the existing "CAT-Items" type (Wrapper)
            CATItems itemBank = new CATItems(DichoItems[0].Length, false);

            // New Dictionary
            itemBank.SetItemParamter(CATItems.ColumnNames.a, DichoItems[0].Select(y => (double)y).ToArray());
            itemBank.SetItemParamter(CATItems.ColumnNames.b, DichoItems[1].Select(y => (double)y).ToArray());
            itemBank.SetItemParamter(CATItems.ColumnNames.c, DichoItems[2].Select(y => (double)y).ToArray());
            itemBank.SetItemParamter(CATItems.ColumnNames.d, DichoItems[3].Select(y => (double)y).ToArray());

            #region "Test block for Ability Values (th)"

            IiList objIi        = null;
            int    decimalPoint = 6;

            double[] th_Values = CatRcs.Utils.CommonHelper.Sequence(-4, 4, by: 1);

            Console.WriteLine("******* TEST for Ability value Theta ********");

            for (int k = 0; k < th_Values.Length; k++)
            {
                // Sequence Generation for Theta (Ability) values
                NumericVector th_val = engineObj.CreateNumericVector(new double[] { th_Values[k] });
                engineObj.SetSymbol("th_val", th_val);

                // Calling the Ii function from R
                GenericVector result_Ii = engineObj.Evaluate("result_Ii <- Ii(th = th_val, DichoItems)").AsList();

                // Getting the function result
                NumericVector Ii   = result_Ii[0].AsNumeric();
                NumericVector dIi  = result_Ii[1].AsNumeric();
                NumericVector d2Ii = result_Ii[2].AsNumeric();

                Console.WriteLine("Value of Theta: " + th_Values[k]);

                objIi = CatRLib.Ii(th_Values[k], itemBank, "", 1);

                #region "Ii"

                int z = 0;
                resultFlag = true;

                Console.WriteLine("****** Ii ******");

                for (int i = 0; i < objIi.Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of Ii result is Passed!");

                #endregion

                #region "dIi"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** dIi ******");

                for (int i = 0; i < objIi.dIi.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(dIi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.dIi[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of dIi result is Passed!");

                #endregion

                #region "d2Ii"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** d2Ii ******");

                for (int i = 0; i < objIi.d2Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(d2Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.d2Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of d2Ii result is Passed!");

                #endregion
            }

            #endregion

            #region "Test block for Metric values"

            objIi = null;

            double[] D_Values = CatRcs.Utils.CommonHelper.Sequence(0.5, 1, by: 0.1);

            Console.WriteLine("******* TEST for Metric Constant ********");

            for (int k = 0; k < D_Values.Length; k++)
            {
                // Sequence Generation for Theta (Ability) values
                NumericVector D_val = engineObj.CreateNumericVector(new double[] { D_Values[k] });
                engineObj.SetSymbol("D_val", D_val);

                // Calling the Ii function from R
                GenericVector result_Ii = engineObj.Evaluate("result_Ii <- Ii(th = 0, DichoItems, D = D_val)").AsList();

                // Getting the function result
                NumericVector Ii   = result_Ii[0].AsNumeric();
                NumericVector dIi  = result_Ii[1].AsNumeric();
                NumericVector d2Ii = result_Ii[2].AsNumeric();

                Console.WriteLine("Value of Metric Constant: " + D_Values[k]);

                objIi = CatRLib.Ii(0, itemBank, "", D_Values[k]);

                #region "Ii"

                int z = 0;
                resultFlag = true;

                Console.WriteLine("****** Ii ******");

                for (int i = 0; i < objIi.Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of Ii result is Passed!");

                #endregion

                #region "dIi"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** dIi ******");

                for (int i = 0; i < objIi.dIi.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(dIi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.dIi[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of dIi result is Passed!");

                #endregion

                #region "d2Ii"

                z          = 0;
                resultFlag = true;

                Console.WriteLine("****** d2Ii ******");

                for (int i = 0; i < objIi.d2Ii.Length; i++)
                {
                    z = i + 1;  // item number

                    if (decimal.Round(Convert.ToDecimal(d2Ii[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objIi.d2Ii[i]), decimalPoint))
                    {
                        //Console.WriteLine("Test for Item No. # " + z + " is not Passed!");
                        resultFlag = false;
                    }
                }

                Assert.IsTrue(resultFlag);
                Console.WriteLine("Values of d2Ii result is Passed!");

                #endregion
            }

            #endregion
        }
Exemple #6
0
        // Function "Ii"
        public static IiList Ii(double th, CATItems it, string model, double D)
        {
            IiList objIi = CatRcs.Ii.Ii_Calc(th, it, model, D);

            return(objIi);
        }