public void testKL_D(int NumOfItems) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = " + NumOfItems + ")").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()); // Dichotomous Items DataFrame it_given_R = engineObj.Evaluate("it_given_R <- DichoItems[c(4, 8),]").AsDataFrame(); engineObj.SetSymbol("it_given_R", it_given_R); CATItems it_given = itemBank.FindItem(new int[] { 4, 8 }); //Creation of a response pattern NumericVector x_val = engineObj.Evaluate("x_val <- c(0, 1)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = new int[] { 0, 1 }; // Call "thetaEST" function from R, method = \"ML\" NumericVector Theta = engineObj.Evaluate("Theta <- thetaEst(it_given_R, x_val, method = \"ML\")").AsNumeric(); // Call "KL" function from R NumericVector r_KL = engineObj.Evaluate("r_KL <- KL(DichoItems, 1, x_val, it_given_R, theta = Theta)").AsNumeric(); // Call "thetaEST" function from CS double th = CatRLib.ThetaEst(it_given, x, "", method: "ML"); // Call "KL" function from CS double cs_KL = CatRLib.KL(itemBank, 1, x, it_given, "", theta: th); if (r_KL[0] - cs_KL > testEpsilon) { resultFlag = false; } Assert.IsTrue(resultFlag); }
public void testKL_P(int NumOfItems, ModelNames.Models paramModel) { resultFlag = true; 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(" + NumOfItems + ", 5, model = modelName, same.nrCat = TRUE)").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(PolyItems[0].Length, paramModel.EnumToString(), 5); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { itemBank.SetItemParamter(cols[i], PolyItems[i].Select(y => (double)y).ToArray()); } DataFrame it_given_R = engineObj.Evaluate("it_given_R <- PolyItems[c(4, 8),]").AsDataFrame(); engineObj.SetSymbol("it_given_R", it_given_R); CATItems it_given = itemBank.FindItem(new int[] { 4, 8 }); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(0, it_given_R, model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); // Call "thetaEST" function from R, method = \"ML\" NumericVector Theta = engineObj.Evaluate("Theta <- thetaEst(it_given_R, x_val, method = \"ML\", model = modelName)").AsNumeric(); // Call "KL" function from R NumericVector r_KL = engineObj.Evaluate("r_KL <- KL(PolyItems, 1, x_val, it_given_R, theta = Theta, model = modelName)").AsNumeric(); // Call "thetaEST" function from CS double th_est = CatRLib.ThetaEst(it_given, x, paramModel.EnumToString(), method: "ML"); // Call "KL" function from CS double cs_KL = CatRLib.KL(itemBank, 1, x, it_given, paramModel.EnumToString(), theta: th_est); if (r_KL[0] - cs_KL > testEpsilon) { resultFlag = false; } Assert.IsTrue(resultFlag); }
public void test_ThetaEST_SemTheta_D(int numberOfItems) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); //DataFrame tcals = engineObj.Evaluate("tcals <- read.delim(\"D:/PROJECT/DIPF_Project/catR/data/tcals.txt\")").AsDataFrame(); //engineObj.SetSymbol("tcals", tcals); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items //DataFrame DichoItems = engineObj.Evaluate("DichoItems <- tcals[1:80,1:4]").AsDataFrame(); DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = " + numberOfItems + ")").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); //Creation of a response pattern engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(-1, DichoItems)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); Console.WriteLine("Start of R Processing: " + DateTime.Now.TimeOfDay.ToString()); // Call "thetaEST" function from R NumericVector r_ThetaEst = engineObj.Evaluate("r_Theta <- thetaEst(DichoItems, x_val, method=\"EAP\")").AsNumeric(); engineObj.SetSymbol("r_ThetaEst", r_ThetaEst); Console.WriteLine("R Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); // TESTING PURPOSE CATItems itemBank = new CATItems(DichoItems[0].Select(y => (double)y).ToArray(), DichoItems[1].Select(y => (double)y).ToArray(), DichoItems[2].Select(y => (double)y).ToArray(), DichoItems[3].Select(y => (double)y).ToArray()); int[] x = x_val.Select(y => (int)y).ToArray(); // Call "SemTheta" function from R //NumericVector r_ThetaSem = engineObj.Evaluate("r_ThetaSem <- semTheta(r_ThetaEst, DichoItems)").AsNumeric(); Console.WriteLine("Start of CS Processing: " + DateTime.Now.TimeOfDay.ToString()); var stopwatch = new Stopwatch(); stopwatch.Restart(); // Call "thetaEST" function from CS double cs_ThetaEst = CatRLib.ThetaEst(itemBank, x, "", new double[] { 0, 1 }, new double[] { -4, 4 }, new int[] { -4, 4, 33 }, 1, method: "EAP"); Console.WriteLine("Time taken: " + stopwatch.ElapsedMilliseconds); Console.WriteLine("CS Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); // Call "SemTheta" function from CS //double cs_ThetaSem = CatRLib.SemTheta(cs_ThetaEst, itemBank); // Compare result of function "ThetaEst" if (decimal.Round(Convert.ToDecimal(r_ThetaEst[0]), decimalPoint) - decimal.Round(Convert.ToDecimal(cs_ThetaEst), decimalPoint) > decimal.Round(Convert.ToDecimal(testEpsilon), decimalPoint)) { resultFlag = false; } // Compare result of function "SemTheta" /*if (decimal.Round(Convert.ToDecimal(r_ThetaSem[0]), decimalPoint) - decimal.Round(Convert.ToDecimal(cs_ThetaSem), decimalPoint) > decimal.Round(Convert.ToDecimal(testEpsilon), decimalPoint)) * { * resultFlag = false; * }*/ Console.WriteLine("Theta Values: " + decimal.Round(Convert.ToDecimal(r_ThetaEst[0]), decimalPoint) + " , " + decimal.Round(Convert.ToDecimal(cs_ThetaEst), decimalPoint)); Assert.IsTrue(resultFlag); }
public static void Main(string[] args) { //f<double[], double> delObj = new f<double[], double>(Method2); //Console.WriteLine(delObj(new double[] {1, 4, 5}).ToString()); ModelNames.Models paramModel = ModelNames.Models.GRM; int NumOfItems = 50; resultFlag = true; var stopwatch = new Stopwatch(); 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(" + NumOfItems + ", 5, model = modelName, same.nrCat = TRUE)").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: NumOfItems, model: paramModel.EnumToString(), nrCat: 5); for (int i = 0; i < itemBank.colSize; i++) { itemBank.all_items_poly[i] = PolyItems[i].Select(y => (double)y).ToArray(); } //Creation of a response pattern engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(0, PolyItems, model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); Console.WriteLine("Start of R Processing: " + DateTime.Now.TimeOfDay.ToString()); stopwatch.Restart(); // Call "ThetaEST" function from R, method = \"ML\" NumericVector r_ThetaEst = engineObj.Evaluate("r_ThetaEst <- thetaEst(PolyItems, x_val, model = modelName, method = \"BM\", priorDist = \"norm\", priorPar = c(-2, 2))").AsNumeric(); engineObj.SetSymbol("r_ThetaEst", r_ThetaEst); Console.WriteLine("R Time taken: " + stopwatch.ElapsedMilliseconds); Console.WriteLine("R Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); double[] priorPar = new double[2]; priorPar[0] = -2; priorPar[1] = 2; Console.WriteLine("Start of CS Processing: " + DateTime.Now.TimeOfDay.ToString()); stopwatch.Restart(); // Call "ThetaEST" function from CS double cs_ThetaEst = CatRLib.ThetaEst(it: itemBank, x: x, method: "BM", model: paramModel.EnumToString(), priorPar: priorPar, priorDist: "norm"); Console.WriteLine("CS Time taken: " + stopwatch.ElapsedMilliseconds); Console.WriteLine("CS Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); // Compare result of function "ThetaEst" if (decimal.Round(Convert.ToDecimal(r_ThetaEst[0]), decimalPoint) - decimal.Round(Convert.ToDecimal(cs_ThetaEst), decimalPoint) > decimal.Round(Convert.ToDecimal(testEpsilon), decimalPoint)) { resultFlag = false; } if (resultFlag) { Console.WriteLine("Values matched !!"); } else { Console.WriteLine("Values are not matched !!"); } }
public void test_ThetaEST_SemTheta_P(int NumOfItems, ModelNames.Models paramModel) { resultFlag = true; var stopwatch = new Stopwatch(); 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(" + NumOfItems + ", 4, model = modelName, same.nrCat = TRUE)").AsDataFrame(); engineObj.SetSymbol("PolyItems", PolyItems); // Adapting with the existing "CAT-Items" type (Wrapper) Console.WriteLine("*******************************************"); Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString()); Console.WriteLine("*******************************************"); // Create item object CATItems itemBank = new CATItems(NumOfItems: NumOfItems, model: paramModel.EnumToString(), nrCat: 4, same_nrCat: false); //DataFrame iii = engineObj.Evaluate("item_new <- as.matrix(PolyItems)").AsDataFrame(); for (int k = 0; k < itemBank.colSize; k++) { itemBank.DataWrapper(PolyItems[k].Select(y => (double)y).ToArray(), k); } //itemBank = RtoCSDataHandler.DataConverterRtoCS(itemBank, iii.Select(y => (double)y).ToArray()); //itemBank = RtoCSDataHandler.DataConverterRtoCS(itemBank, PolyItems); //Creation of a response pattern engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(1, PolyItems, model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); Console.WriteLine("Start of R Processing: " + DateTime.Now.TimeOfDay.ToString()); stopwatch.Restart(); // Call "ThetaEST" function from R, method = \"ML\" //NumericVector r_ThetaEst = engineObj.Evaluate("r_ThetaEst <- thetaEst(PolyItems, x_val, model = modelName, method = \"BM\", priorDist = \"norm\", priorPar = c(-2, 2))").AsNumeric(); NumericVector r_ThetaEst = engineObj.Evaluate("r_ThetaEst <- thetaEst(PolyItems, x_val, modelName)").AsNumeric(); engineObj.SetSymbol("r_ThetaEst", r_ThetaEst); Console.WriteLine("R Time taken: " + stopwatch.ElapsedMilliseconds + " ms"); Console.WriteLine("R Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); double[] priorPar = new double[2]; priorPar[0] = -2; priorPar[1] = 2; Console.WriteLine("Start of CS Processing: " + DateTime.Now.TimeOfDay.ToString()); stopwatch.Restart(); double cs_ThetaEst = CatRLib.ThetaEst(itemBank, x, paramModel.EnumToString()); // Call "ThetaEST" function from CS //double cs_ThetaEst = CatRLib.ThetaEst(it: itemBank, x: x, method:"BM", model: paramModel.EnumToString(), priorPar: priorPar, priorDist: "norm"); Console.WriteLine("CS Time taken: " + stopwatch.ElapsedMilliseconds + " ms"); Console.WriteLine("CS Theta Calculation Finished on: " + DateTime.Now.TimeOfDay.ToString()); // Compare result of function "ThetaEst" if (decimal.Round(Convert.ToDecimal(r_ThetaEst[0]), decimalPoint) - decimal.Round(Convert.ToDecimal(cs_ThetaEst), decimalPoint) > decimal.Round(Convert.ToDecimal(testEpsilon), decimalPoint)) { resultFlag = false; } // Call "SemTheta" function from R NumericVector r_ThetaSem = engineObj.Evaluate("r_ThetaSem <- semTheta(r_ThetaEst, PolyItems, model = modelName, method = \"BM\", priorDist = \"norm\", priorPar = c(-2, 2))").AsNumeric(); // Call "SemTheta" function from CS double cs_ThetaSem = CatRLib.SemTheta(thEst: cs_ThetaEst, it: itemBank, method: "BM", model: paramModel.EnumToString(), priorPar: priorPar, priorDist: "norm"); // Compare result of function "SemTheta" if (decimal.Round(Convert.ToDecimal(r_ThetaSem[0]), decimalPoint) - decimal.Round(Convert.ToDecimal(cs_ThetaSem), decimalPoint) > decimal.Round(Convert.ToDecimal(testEpsilon), decimalPoint)) { resultFlag = false; } Assert.IsTrue(resultFlag); }