// Find multiple items with Item Index public CATItems FindItem(int[] items) { CATItems result = null; ModelNames.Models tempModel = this.model; if (tempModel == ModelNames.Models.NoModel) { result = new CATItems(items.Length, this.IsGroupExist); } else { result = new CATItems(items.Length, this.model.EnumToString(), this.nrCat); } Tuple <CATItems.ColumnNames, int>[] cols = this.GetKeys(); for (int i = 0; i < cols.Length; i++) { double[] itemsTemp = this.CATItemColumns[cols[i]]; List <double> ItemValues = new List <double>(); for (int k = 0; k < items.Length; k++) { ItemValues.Add(itemsTemp[items[k] - 1]); } result.CATItemColumns[cols[i]] = ItemValues.ToArray(); } return(result); }
private void ProcessTheta(int[] items, int[] responses) { CATItems itemBank = null; if ((bool)Session["IsDicho"]) { itemBank = new CATItems(items.Length); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { string tempKey = cols[i].Item1.ToString() + cols[i].Item2.ToString(); itemBank.SetItemParamter(cols[i], cat_items[tempKey].Select(y => (double)y).ToArray()); } // Calculate theta CalculateTheta(itemBank, responses); } if ((bool)Session["IsPoly"]) { ModelNames.Models paramModel = ModelNames.Models.GRM; itemBank = new CATItems(items.Length, paramModel.EnumToString(), nrCat: 5); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { string tempKey = cols[i].Item1.ToString() + cols[i].Item2.ToString(); itemBank.SetItemParamter(cols[i], cat_items[tempKey].Select(y => (double)y).ToArray()); } // Calculate theta CalculateTheta(itemBank, responses, model: paramModel.EnumToString()); } }
// Find single item with Item Index public CATItems FindItem(int item) { CATItems result = null; ModelNames.Models tempModel = this.model; if (tempModel == ModelNames.Models.NoModel) { result = new CATItems(1); } else { result = new CATItems(1, this.model.EnumToString(), this.nrCat); } Tuple <CATItems.ColumnNames, int>[] cols = this.GetKeys(); for (int i = 0; i < cols.Length; i++) { double[] itemsTemp = this.CATItemColumns[cols[i]]; double[] singleItemValue = new double[] { itemsTemp[item - 1] }; result.CATItemColumns[cols[i]] = singleItemValue; } return(result); }
public void test_NextItem_P(int numofItems, ModelNames.Models paramModel, ModelNames.CriterionTypes paramCriterion) { 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()); } // Call "NextItem" function from R GenericVector r_NextItem = engineObj.Evaluate("r_NextItem <- nextItem(PolyItems, model = modelName, theta = 0, criterion = \"" + paramCriterion.ToString() + "\")").AsList(); // Selected item NumericVector item = r_NextItem[0].AsNumeric(); DataFrame par = r_NextItem[1].AsDataFrame(); // Value of "info" NumericVector thStart = r_NextItem[2].AsNumeric(); // Criterion CharacterVector startSelect = r_NextItem[3].AsCharacter(); // Call "NextItem" function from CS NextItemModel cs_NextItem = CatRLib.NextItem(itemBank, model: paramModel.EnumToString(), theta: 0, criterion: (int)paramCriterion); //Test passed for "MFI" if (item[0] != cs_NextItem.item) { resultFlag = false; } Assert.IsTrue(resultFlag); }
// Constructor for Dichotomous Items :: 4PL :: public CATItems(double[] a, double[] b, double[] c, double[] d, bool IsGroupExist = false) { this.NumOfItems = a.Length; this.modelName = ModelNames.Models.NoModel; this.colSize = 4; this.a = a; this.b = b; this.c = c; this.d = d; if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } }
public void testModelEnums() { resultFlag = false; //string result = ""; //result = ModelNames.EnumToString(ModelNames.Models.GRM); ModelNames.Models result = ModelNames.StringToEnum("MGRM"); resultFlag = true; /*if (!String.IsNullOrEmpty(result)) * { * resultFlag = true; * }*/ Assert.IsTrue(resultFlag, "Test Passed!"); }
// Constructor for Dichotomous Items with number of items public CATItems(int NumOfItems, bool IsGroupExist = false) { this.NumOfItems = NumOfItems; this.modelName = ModelNames.Models.NoModel; this.colSize = 4; this.a = new double[NumOfItems]; this.b = new double[NumOfItems]; this.c = new double[NumOfItems]; this.d = new double[NumOfItems]; if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } }
// Test constructor for polytomous items public CATItems(int NumOfItems, string model, bool same_nrCat, int nrCat = 3, bool IsGroupExist = false) { this.NumOfItems = NumOfItems; this.nrCat = nrCat; this.modelName = ModelNames.StringToEnum(model); this.same_nrCat = same_nrCat; if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } switch (this.modelName) { case ModelNames.Models.GRM: this.all_items_poly = new double[nrCat][]; this.colSize = nrCat; break; case ModelNames.Models.MGRM: this.all_items_poly = new double[nrCat + 1][]; this.colSize = nrCat + 1; break; case ModelNames.Models.PCM: this.all_items_poly = new double[nrCat - 1][]; this.colSize = nrCat - 1; break; case ModelNames.Models.GPCM: this.all_items_poly = new double[nrCat][]; this.colSize = nrCat; break; case ModelNames.Models.RSM: this.all_items_poly = new double[nrCat][]; this.colSize = nrCat; break; case ModelNames.Models.NRM: this.all_items_poly = new double[2 * (nrCat - 1)][]; this.colSize = 2 * (nrCat - 1); break; } }
public void testEapEST_and_EapSEM_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()); } #region "Test block for Ability Values (th)" double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); for (int j = 0; j < th.Length; j++) { string temp = th[j].ToString(nfi); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + "th = " + th[j].ToString(nfi) + ", PolyItems, " + "model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); // Call "EapEST" function from R NumericVector r_eapEst = engineObj.Evaluate("result_Eap <- eapEst(PolyItems, x_val, model = modelName)").AsNumeric(); // Call "EapEST" function from CatRCS double cs_eapEst = CatRLib.EapEST(itemBank, x, paramModel.EnumToString()); /* EapEst function, line 111 needs to be optimized for passing the Test * Check EapSEM function too !! */ // Call "EapSEM" function from R NumericVector r_eapSem = engineObj.Evaluate("result_EapSem <- eapSem(result_Eap, PolyItems, x_val, model = modelName)").AsNumeric(); // Call "EapSEM" function from CatRCS double cs_eapSem = CatRLib.EapSEM(cs_eapEst, itemBank, x, paramModel.EnumToString()); if (r_eapEst[0] - cs_eapEst > testEpsilon || r_eapSem[0] - cs_eapSem > testEpsilon) { resultFlag = false; } } Assert.IsTrue(resultFlag); #endregion }
// Constructor for Polytomous Items public CATItems(int NumOfItems, string model, int nrCat = 3, bool IsGroupExist = false) { this.NumOfItems = NumOfItems; this.nrCat = nrCat; this.modelName = ModelNames.StringToEnum(model); this.CATItemColumns = new Dictionary <Tuple <ColumnNames, int>, double[]>(); switch (this.modelName) { case ModelNames.Models.GRM: this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.alphaj, 0), new double[this.NumOfItems]); for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.betaj, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; case ModelNames.Models.MGRM: this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.alphaj, 0), new double[this.NumOfItems]); this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.betaj, 0), new double[this.NumOfItems]); for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.c, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; case ModelNames.Models.PCM: for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.deltaj, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; case ModelNames.Models.GPCM: this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.alphaj, 0), new double[this.NumOfItems]); for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.deltaj, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; case ModelNames.Models.RSM: this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.lambdaj, 0), new double[this.NumOfItems]); for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.delta, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; case ModelNames.Models.NRM: for (int i = 1; i < this.nrCat; i++) { this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.alpha, i), new double[this.NumOfItems]); this.CATItemColumns.Add(new Tuple <ColumnNames, int>(ColumnNames.c, i), new double[this.NumOfItems]); } if (IsGroupExist) { this.Group = new string[this.NumOfItems]; } this.colSize = this.CATItemColumns.Count; break; } }
// Function for calculating Next Item public static NextItemModel NextItem_Calc(CATItems itemBank, string model = null, double theta = 0, int[] Out = null, int[] x = null, int criterion = 5, /* MFI */ string method = "BM", string priorDist = "norm", double[] priorPar = null, double D = 1, double[] range = null, int[] parInt = null, int infoType = 2, /* observed */ int randomesque = 1, int rule = 1, /* Length */ double thr = 20, double?SETH = null, double AP = 1, int[] nAvailable = null, int maxItems = 50, CBControlList cbControl = null, string[] cbGroup = null) { CATItems par = null; NextItemModel result = 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; } ModelNames.CriterionTypes?crit = null; switch (criterion) { case (int)ModelNames.CriterionTypes.bOpt: crit = ModelNames.CriterionTypes.bOpt; break; case (int)ModelNames.CriterionTypes.thOpt: crit = ModelNames.CriterionTypes.thOpt; break; case (int)ModelNames.CriterionTypes.KL: crit = ModelNames.CriterionTypes.KL; break; case (int)ModelNames.CriterionTypes.KLP: crit = ModelNames.CriterionTypes.KLP; break; case (int)ModelNames.CriterionTypes.MEI: crit = ModelNames.CriterionTypes.MEI; break; case (int)ModelNames.CriterionTypes.MEPV: crit = ModelNames.CriterionTypes.MEPV; break; case (int)ModelNames.CriterionTypes.MFI: crit = ModelNames.CriterionTypes.MFI; break; case (int)ModelNames.CriterionTypes.MLWI: crit = ModelNames.CriterionTypes.MLWI; break; case (int)ModelNames.CriterionTypes.MPWI: crit = ModelNames.CriterionTypes.MPWI; break; case (int)ModelNames.CriterionTypes.progressive: crit = ModelNames.CriterionTypes.progressive; break; case (int)ModelNames.CriterionTypes.proportional: crit = ModelNames.CriterionTypes.proportional; break; case (int)ModelNames.CriterionTypes.random: crit = ModelNames.CriterionTypes.random; break; } if (crit == null) { result = new NextItemModel(true, "Invalid 'criterion' name!"); return(result); } int mod = 0; ModelNames.Models modelEnum = ModelNames.StringToEnum(model); if (!String.IsNullOrEmpty(model)) { switch (modelEnum) { case ModelNames.Models.GRM: mod = 1; break; case ModelNames.Models.MGRM: mod = 2; break; case ModelNames.Models.PCM: mod = 3; break; case ModelNames.Models.GPCM: mod = 4; break; case ModelNames.Models.RSM: mod = 5; break; case ModelNames.Models.NRM: mod = 6; break; } if (mod == 0) { result = new NextItemModel(true, "Invalid 'model' type!"); return(result); } } #endregion int[] OUT = null; double[] empProp = null; int nrGroup = 0; double[] thProp = null; #region Handling "cbControl" Parameter if (cbControl == null) { OUT = Out; } else { if (cbGroup == null) { result = new NextItemModel(true, "'cbGroup' argument must be provided for content balancing!"); return(result); } if (RowColumn.Sum(cbControl.Props) != 1) { double temp_sum = RowColumn.Sum(cbControl.Props); for (int i = 0; i < cbControl.Props.Length; i++) { cbControl.Props[i] = cbControl.Props[i] / temp_sum; } } nrGroup = cbControl.Names.Length; empProp = new double[nrGroup]; if (Out == null) { empProp = CatRcs.Utils.CommonHelper.Replicate(new double[] { 0 }, nrGroup); } else { string[] temp_grp = new string[Out.Length]; for (int i = 0; i < Out.Length; i++) { temp_grp[i] = cbGroup[Out[i]]; } for (int j = 0; j < nrGroup; j++) { string[] values = temp_grp.Where(m => m == cbControl.Names[j]).ToArray(); if (values != null && values.Length > 0) { empProp[j] = values.Length; } else { empProp[j] = 0; } } empProp = empProp.Select(n => n / CatRcs.Utils.RowColumn.Sum(empProp)).ToArray(); // Functional Testing needed !! } thProp = cbControl.Props; List <int> indGroup = new List <int>(); int selGroup = 0; // Array of Group Numbers ex: "1 2 3 4 5" for nrGroup = 5. int[] GrpIndValues = new int[nrGroup]; GrpIndValues = GrpIndValues.Select((a, i) => i + 1).ToArray(); if (empProp.Min() == 0) { for (int m = 0; m < empProp.Length; m++) { if (empProp[m] == 0) { indGroup.Add(GrpIndValues[m]); } } } else { double[] tempProp = thProp.Select((a, i) => a - empProp[i]).ToArray(); for (int n = 0; n < tempProp.Length; n++) { if (tempProp[n] == tempProp.Max()) { indGroup.Add(GrpIndValues[n]); } } } if (indGroup.Count == 1) { selGroup = indGroup[0]; } else { selGroup = CatRcs.Utils.RandomNumberHandler.Sample(indGroup.ToArray(), 1, false)[0]; } // Populating the OUT array string[] tempGrp = cbGroup.Where(c => c != cbControl.Names[selGroup]).ToArray(); OUT = tempGrp.Select((a, i) => i + 1).ToArray(); OUT = Out.Concat(OUT).ToArray(); OUT = CatRcs.Utils.CommonHelper.Unique(OUT); } #endregion #region Handling "nAvalilable Parameter" if (nAvailable != null) { List <int> ind_temp = new List <int>(); for (int k = 0; k < nAvailable.Length; k++) { if (nAvailable[k] == 0) { ind_temp.Add(k); } } OUT = OUT.Concat(ind_temp.ToArray()).ToArray(); OUT = CatRcs.Utils.CommonHelper.Unique(OUT); } #endregion int select = 0; #region Criterion Type "MFI" if (crit == ModelNames.CriterionTypes.MFI) { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] info = Ii.Ii_Calc(theta, itemBank, model, D).Ii; double[] ranks = CatRcs.Utils.CommonHelper.Rank(info).Select(m => m).ToArray(); int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); List <double> tempRanks = new List <double>(); for (int j = 0; j < items.Length; j++) { if (items[j] == 1) { tempRanks.Add(ranks[j]); } } tempRanks = tempRanks.OrderByDescending(n => n).ToList(); double[] keepRank = tempRanks.GetRange(0, nrIt).ToArray(); List <int> keep = new List <int>(); if (ranks.Length == items.Length) { for (int m = 0; m < keepRank.Length; m++) { for (int n = 0; n < ranks.Length; n++) { if ((items[n] == 1) && (ranks[n] == keepRank[m])) { keep.Add(n + 1); } } } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), info[select - 1], criterion, randomesque); } #endregion #region Criterion Type "bOpt" if (crit == ModelNames.CriterionTypes.bOpt) { if (string.IsNullOrEmpty(model)) { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] distance = itemBank.GetItemParamter(CATItems.ColumnNames.b).Select(a => Math.Abs(a - theta)).ToArray(); double[] ranks = CatRcs.Utils.CommonHelper.Rank(distance).Select(m => m).ToArray(); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { ranks[OUT[a] - 1] = -1; } } int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); List <double> tempRanks = new List <double>(); for (int j = 0; j < items.Length; j++) { if (items[j] == 1) { tempRanks.Add(ranks[j]); } } tempRanks = tempRanks.OrderBy(n => n).ToList(); double[] keepRank = tempRanks.GetRange(0, nrIt).ToArray(); keepRank = keepRank.Distinct().ToArray(); List <int> keep = new List <int>(); if (ranks.Length == items.Length) { for (int m = 0; m < keepRank.Length; m++) { for (int n = 0; n < ranks.Length; n++) { if ((items[n] == 1) && (ranks[n] == keepRank[m])) { keep.Add(n + 1); } } } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), distance[select - 1], criterion, randomesque); } else { result = new NextItemModel(true, "bOpt's rule cannot be considered with polytomous items!"); return(result); } } #endregion #region Criterion Type "MLWI" OR "MPWI" if (crit == ModelNames.CriterionTypes.MLWI || crit == ModelNames.CriterionTypes.MPWI) { if (Out != null) { if (Out.Length == 1) { par = itemBank.FindItem(Out[0]); } else { par = itemBank.FindItem(Out); } } else { result = new NextItemModel(true, "Out parameter can't be empty!"); return(result); } int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] likInfo = CatRcs.Utils.CommonHelper.Replicate(new double[] { 0 }, itemBank.NumOfItems); int mwiType = 0; if (criterion == (int)ModelNames.CriterionTypes.MLWI) { mwiType = (int)ModelNames.MWI_Type.MLWI; } if (criterion == (int)ModelNames.CriterionTypes.MPWI) { mwiType = (int)ModelNames.MWI_Type.MPWI; } if (x != null) { for (int j = 0; j < itemBank.NumOfItems; j++) { if (items[j] == 1) { likInfo[j] = MWI.MWI_Calc(itemBank, j + 1, x, par, model, mwiType, priorPar, D, priorDist, parInt[0], parInt[1], parInt[2]); /* item number is always 1 greater than the index */ } } } int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); double likVal = likInfo.ToList().OrderByDescending(n => n).ToArray()[nrIt - 1]; // First value with index 0 List <int> keep = new List <int>(); for (int k = 0; k < items.Length; k++) { if (likInfo[k] >= likVal) { keep.Add(k + 1); // Converting from index to item number } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), likInfo[select - 1], criterion, randomesque); } #endregion #region Criterion Type "KL" OR "KLP" if (crit == ModelNames.CriterionTypes.KL || crit == ModelNames.CriterionTypes.KLP) { if (Out != null) { if (Out.Length == 1) { par = itemBank.FindItem(Out[0]); } else { par = itemBank.FindItem(Out); } } else { result = new NextItemModel(true, "Out parameter can't be empty!"); return(result); } int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] klValue = CatRcs.Utils.CommonHelper.Replicate(new double[] { 0 }, itemBank.NumOfItems); double[] X = CatRcs.Utils.CommonHelper.Sequence(parInt[0], parInt[1], parInt[2]); #region "Function 'L' " Func <double, int[], CATItems, double> L = (th, r, param) => { double res = 0; var temp_1 = Pi.Pi_Calc(th, param, model, D).Pi.Select((p, i) => Math.Pow(p, r[i])).ToArray(); var temp_2 = Pi.Pi_Calc(th, param, model, D).Pi.Select((p, i) => Math.Pow(1 - p, 1 - r[i])).ToArray(); if (temp_1.Length == temp_2.Length) { var temp_3 = temp_1.Select((p, i) => p * temp_2[i]).ToArray(); res = temp_3.Aggregate((acc, val) => acc * val); } return(res); }; #endregion #region "Function 'LL' " Func <double, int[], CATItems, double> LL = (th, r, param) => { double res = 0; if (param.NumOfItems == 0) { res = 1; } else { double[,] prob = Pi.Pi_Poly_Calc(th, param, model, D).Pi; for (int i = 0; i < r.Length; i++) { res = res * prob[i, r[i] + 1]; } } return(res); }; #endregion double[] LF = null; if (string.IsNullOrEmpty(model)) { LF = X.Select(p => L(p, x, par)).ToArray(); } else { LF = X.Select(p => LL(p, x, par)).ToArray(); } int kType = 0; if (criterion == (int)ModelNames.CriterionTypes.KL) { kType = (int)ModelNames.KLTypes.KL; } if (criterion == (int)ModelNames.CriterionTypes.KLP) { kType = (int)ModelNames.KLTypes.KLP; } for (int j = 0; j < itemBank.NumOfItems; j++) { if (items[j] == 1) { klValue[j] = KL.KL_Calc(itemBank, j + 1, x, par, model, theta, priorPar, X, LF, kType, D, priorDist, parInt[0], parInt[1], parInt[2]); /* item number is always 1 greater than the index */ } } int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); double klVal = klValue.ToList().OrderByDescending(n => n).ToArray()[nrIt - 1]; List <int> keep = new List <int>(); for (int k = 0; k < items.Length; k++) { if (klValue[k] >= klVal) { keep.Add(k + 1); } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), klValue[select - 1], criterion, randomesque); } #endregion #region Criterion Type "MEI" if (crit == ModelNames.CriterionTypes.MEI) { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] infos = CatRcs.Utils.CommonHelper.Replicate(new double[] { 0 }, itemBank.NumOfItems); for (int j = 0; j < items.Length; j++) { if (items[j] > 0) { infos[j] = MEI.MEI_Calc(itemBank, j + 1, x, theta, itemBank.FindItem(Out), model, method, D, priorPar, priorDist, range, parInt, infoType); } } int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); double infoVal = infos.ToList().OrderByDescending(n => n).ToArray()[nrIt - 1]; List <int> keep = new List <int>(); for (int k = 0; k < items.Length; k++) { if (infos[k] >= infoVal) { keep.Add(k + 1); } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), infos[select - 1], criterion, randomesque); } #endregion #region Criterion Type "MEPV" if (crit == ModelNames.CriterionTypes.MEPV) { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] epvs = CatRcs.Utils.CommonHelper.Replicate(new double[] { 1000 }, itemBank.NumOfItems); for (int j = 0; j < items.Length; j++) { if (items[j] > 0) { epvs[j] = EPV.EPV_Calc(itemBank, j + 1, x, theta, itemBank.FindItem(Out), model, priorPar, parInt, D, priorDist); } } var tempVal = new int[] { randomesque, items.Sum() }.Min(); double epVal = epvs.ToList().OrderBy(n => n).ToArray()[tempVal - 1]; List <int> keep = new List <int>(); for (int k = 0; k < itemBank.NumOfItems; k++) { if (epvs[k] <= epVal) { keep.Add(k + 1); } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), epvs[select - 1], criterion, randomesque); } #endregion #region Criterion Type "random" if (crit == ModelNames.CriterionTypes.random) { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a]] = 0; } } int gen = Convert.ToInt32(Convert.ToInt32(CatRcs.Utils.RandomNumberHandler.Runif(1, 0, 1)[0]) * items.Sum()) + 1; List <int> indexs = new List <int>(); for (int k = 0; k < itemBank.NumOfItems; k++) { if (items[k] > 0) { indexs.Add(k + 1); } } select = indexs.ElementAt(gen); result = new NextItemModel(select, itemBank.FindItem(select), double.NaN, criterion, randomesque); } #endregion #region Criterion Type "progressive" if (crit == ModelNames.CriterionTypes.progressive) { int item_administered = Out.Length; int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] info = Ii.Ii_Calc(theta, itemBank, model, D).Ii; List <double> tempItems = new List <double>(); for (int j = 0; j < items.Length; j++) { if (items[j] == 1) { tempItems.Add(info[j]); } } double wq = 0; double itemMaxInfo = tempItems.Max(); double[] randomValues = CatRcs.Utils.RandomNumberHandler.Runif(items.Length, 0, itemMaxInfo); if (rule == (int)ModelNames.RuleType.Precision) { double infostop = Math.Pow((1 / thr), 2); double cuminfo = Math.Pow(double.Parse((1 / SETH).ToString()), 2); if (item_administered > 0) { wq = Math.Pow(new double[] { cuminfo / infostop, item_administered / (maxItems - 1) }.Max(), AP); } } if (rule == (int)ModelNames.RuleType.Length) { if (item_administered > 0) { List <double> tempNum = new List <double>(); for (int i = 1; i <= item_administered; i++) { tempNum.Add(Math.Pow(i, AP)); } double numerador = tempNum.Sum(); List <double> tempDenom = new List <double>(); for (int j = 1; j <= thr - 1; j++) { tempDenom.Add(Math.Pow(j, AP)); } double denominador = tempDenom.Sum(); wq = numerador / denominador; } } double[] funcPR = info.Select((d, i) => d * wq + randomValues[i] * (1 - wq)).ToArray(); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { funcPR[OUT[a] - 1] = 0; } } List <int> keep = new List <int>(); for (int k = 0; k < funcPR.Length; k++) { if (funcPR[k] == funcPR.Max()) { keep.Add(k + 1); } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), info[select - 1], criterion, randomesque); } #endregion #region Criterion Type "proportional" if (crit == ModelNames.CriterionTypes.proportional) { int item_administered = Out.Length; int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double wq = 0; if (rule == (int)ModelNames.RuleType.Precision) { double infostop = Math.Pow((1 / thr), 2); double cuminfo = Math.Pow(double.Parse((1 / SETH).ToString()), 2); if (item_administered > 0) { wq = infostop * Math.Pow(new double[] { cuminfo / infostop, item_administered / (maxItems - 1) }.Max(), AP); } } if (rule == (int)ModelNames.RuleType.Length) { if (item_administered > 0) { List <double> tempNum = new List <double>(); for (int i = 1; i <= item_administered; i++) { tempNum.Add(Math.Pow(i, AP)); } double numerador = tempNum.Sum(); List <double> tempDenom = new List <double>(); for (int j = 1; j <= thr - 1; j++) { tempDenom.Add(Math.Pow(j, AP)); } double denominador = tempDenom.Sum(); wq = thr * numerador / denominador; } } double[] info = Ii.Ii_Calc(theta, itemBank, model, D).Ii; double[] infoPR = info.Select(s => Math.Pow(s, wq)).ToArray(); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { infoPR[OUT[a] - 1] = 0; } } List <double> tempInfoPR = new List <double>(); for (int k = 0; k < items.Length; k++) { if (items[k] == 1) { tempInfoPR.Add(infoPR[k]); } } double totalInfoPR = tempInfoPR.Sum(); double[] probSelect = infoPR.Select(m => m / totalInfoPR).ToArray(); int[] selectItems = items.Select((n, i) => i + 1).ToArray(); select = CatRcs.Utils.RandomNumberHandler.Sample(selectItems, 1, false)[0]; // prob parameter will be added after Sample functiom modification result = new NextItemModel(select, itemBank.FindItem(select), info[select], criterion, randomesque); } #endregion #region Criterion Type "thOpt" if (crit == ModelNames.CriterionTypes.thOpt) { if (string.IsNullOrEmpty(model)) // Only for Dichotomous Items { int[] items = CatRcs.Utils.CommonHelper.Replicate(new int[] { 1 }, itemBank.NumOfItems); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { items[OUT[a] - 1] = 0; } } double[] u = itemBank.GetItemParamter(CATItems.ColumnNames.c).Select((s, i) => - 0.75 + (s + itemBank.GetItemParamter(CATItems.ColumnNames.d)[i] + -2 * s * itemBank.GetItemParamter(CATItems.ColumnNames.d)[i]) / 2).ToArray(); double[] v = itemBank.GetItemParamter(CATItems.ColumnNames.c).Select((n, i) => (n + itemBank.GetItemParamter(CATItems.ColumnNames.d)[i] - 1) / 4).ToArray(); double[] xstar = u.Select((m, i) => 2 * Math.Sqrt(-m / 3) * Math.Cos(Math.Acos(-v[i] * Math.Sqrt(-27 / Math.Pow(m, 3)) / 2) / 3 + 4 * (Math.PI / 3)) + 0.5).ToArray(); double[] thstar = itemBank.GetItemParamter(CATItems.ColumnNames.b).Select((o, i) => o + Math.Log((xstar[i] - itemBank.GetItemParamter(CATItems.ColumnNames.c)[i]) / (itemBank.GetItemParamter(CATItems.ColumnNames.d)[i] - xstar[i])) / (D * itemBank.GetItemParamter(CATItems.ColumnNames.a)[i])).ToArray(); double[] distance = thstar.Select(p => Math.Abs(p - theta)).ToArray(); double[] ranks = CatRcs.Utils.CommonHelper.Rank(distance).Select(m => m).ToArray(); if (OUT != null) { for (int a = 0; a < OUT.Length; a++) { ranks[OUT[a] - 1] = -1; } } int nrIt = new int[] { randomesque, (int)CatRcs.Utils.RowColumn.Sum(items) }.Min(); List <double> tempRanks = new List <double>(); for (int j = 0; j < items.Length; j++) { if (items[j] == 1) { tempRanks.Add(ranks[j]); } } tempRanks = tempRanks.OrderBy(n => n).ToList(); double[] keepRank = tempRanks.GetRange(0, nrIt).ToArray(); keepRank = keepRank.Distinct().ToArray(); List <int> keep = new List <int>(); if (ranks.Length == items.Length) { for (int m = 0; m < keepRank.Length; m++) { for (int n = 0; n < ranks.Length; n++) { if ((items[n] == 1) && (ranks[n] == keepRank[m])) { keep.Add(n + 1); } } } } if (keep.Count == 1) { select = keep[0]; } else { select = CatRcs.Utils.RandomNumberHandler.Sample(keep.ToArray(), 1, false)[0]; } result = new NextItemModel(select, itemBank.FindItem(select), distance[select - 1], criterion, randomesque); } else { result = new NextItemModel(true, "thOpt's rule cannot be considered with polytomous items!"); return(result); } } #endregion #region Handling "cbControl" Parameter if (cbControl == null) { if (result != null) { result.prior_prop = null; result.post_prop = null; result.cb_prop = null; } } else { result.prior_prop = empProp; double[] postProp = new double[nrGroup]; string[] temp_grp = new string[Out.Length + 1]; for (int i = 0; i < temp_grp.Length; i++) { if (i == 0) { temp_grp[i] = cbGroup[result.item]; } temp_grp[i] = cbGroup[Out[i]]; } for (int j = 0; j < postProp.Length; j++) { string[] values = temp_grp.Where(m => m == cbControl.Names[j]).ToArray(); if (values != null && values.Length > 0) { postProp[j] = values.Length; } else { postProp[j] = 0; } } result.post_prop = postProp.Select(n => n / CatRcs.Utils.RowColumn.Sum(postProp)).ToArray(); result.cb_prop = thProp; } #endregion return(result); }
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); }
public void test_StartItems_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()); } // Call "StartItems" function from R, "MFI" criterion GenericVector r_StartItems = engineObj.Evaluate("r_StartItems <- startItems(PolyItems, model = modelName, nrItems = 3, theta = 1, halfRange = 2)").AsList(); // Resulting item numbers IntegerVector items = r_StartItems[0].AsInteger(); DataFrame tempitems = r_StartItems[1].AsDataFrame(); // Resulting Items CATItems r_CatItems = new CATItems(tempitems[0].Length, paramModel.EnumToString(), 5); for (int i = 0; i < cols.Length; i++) { r_CatItems.SetItemParamter(cols[i], tempitems[i].Select(y => (double)y).ToArray()); } // Resulting NumericVector thStart = r_StartItems[2].AsNumeric(); CharacterVector startSelect = r_StartItems[3].AsCharacter(); // Call "StartItems" function from CS StartItemsModel cs_StartItems = CatRLib.StartItems(itemBank, paramModel.EnumToString(), nrItems: 3, theta: 1, halfRange: 2); // Check items if (items.Length == cs_StartItems.items.Length) { for (int ind = 0; ind < cs_StartItems.items.Length; ind++) { if (items[ind] != cs_StartItems.items[ind]) { resultFlag = false; } } } // Check starting ability values if (thStart.Length == cs_StartItems.thStart.Length) { for (int ind2 = 0; ind2 < cs_StartItems.thStart.Length; ind2++) { if (thStart[ind2] != cs_StartItems.thStart[ind2]) { resultFlag = false; } } } Assert.IsTrue(resultFlag); }
public void testPi_P_New(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(100, 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(), nrCat: 5); for (int i = 0; i < itemBank.colSize; i++) { itemBank.all_items_poly[i] = PolyItems[i].Select(y => (double)y).ToArray(); } PiListPoly objPI = null; int decimalPoint = 4; double[] th_Values = CatRcs.Utils.CommonHelper.Sequence(-4, 4, by: 1); NumericVector th_val = engineObj.CreateNumericVector(new double[] { th_Values[0] }); engineObj.SetSymbol("th_val", th_val); // Calling the "Pi" from R GenericVector result_Pi = engineObj.Evaluate("result_Pi <- Pi(th = th_val, PolyItems, model = modelName, D = 1)").AsList(); // Getting the "Pi" function result NumericVector Pi = result_Pi[0].AsNumeric(); NumericVector dPi = result_Pi[1].AsNumeric(); NumericVector d2Pi = result_Pi[2].AsNumeric(); NumericVector d3Pi = result_Pi[3].AsNumeric(); Console.WriteLine("Value of Theta: " + th_Values[0]); // Calling "Pi" function objPI = CatRLib.Pi_P(th_Values[0], itemBank, paramModel.EnumToString(), 1); #region "Pi" int z = 0, pi_index = 0; resultFlag = true; Console.WriteLine("****** Pi ******"); int len = objPI.Pi.GetLength(0); int len2 = objPI.Pi.GetLength(1); for (int i = 0; i < objPI.Pi.GetLength(1); i++) // column { z = i + 1; // item number for (int j = 0; j < objPI.Pi.GetLength(0); j++) // row { double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(Pi[pi_index]) ? Pi[pi_index] : 0; if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.Pi[j, i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } pi_index++; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of Pi are Matched!"); #endregion #region "dPi" z = 0; pi_index = 0; resultFlag = true; Console.WriteLine("****** dPi ******"); for (int i = 0; i < objPI.dPi.GetLength(1); i++) // column { z = i + 1; // item number for (int j = 0; j < objPI.dPi.GetLength(0); j++) // row { double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(dPi[pi_index]) ? dPi[pi_index] : 0; if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.dPi[j, i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } pi_index++; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of dPi are Matched!"); #endregion #region "d2Pi" z = 0; pi_index = 0; resultFlag = true; Console.WriteLine("****** d2Pi ******"); for (int i = 0; i < objPI.d2Pi.GetLength(1); i++) // column { z = i + 1; // item number for (int j = 0; j < objPI.d2Pi.GetLength(0); j++) // row { double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(d2Pi[pi_index]) ? d2Pi[pi_index] : 0; if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d2Pi[j, i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } pi_index++; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d2Pi are Matched!"); #endregion #region "d3Pi" z = 0; pi_index = 0; resultFlag = true; Console.WriteLine("****** d3Pi ******"); for (int i = 0; i < objPI.d3Pi.GetLength(1); i++) // column { z = i + 1; // item number for (int j = 0; j < objPI.d3Pi.GetLength(0); j++) // row { double tempPiVal = !CatRcs.Utils.CheckNaValues.IsNaNvalue(d3Pi[pi_index]) ? d3Pi[pi_index] : 0; if (decimal.Round(Convert.ToDecimal(tempPiVal), decimalPoint) != decimal.Round(Convert.ToDecimal(objPI.d3Pi[j, i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } pi_index++; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of d3Pi are Matched!"); #endregion }
// Pi method for Polytomous Items public static PiListPoly Pi_Poly_Calc(double th, CATItems it, string model, double D) { PiListPoly objPi = null; double[,] prov = null, prov1 = null, prov2 = null, prov3 = null; try { if (!String.IsNullOrEmpty(model)) { ModelNames.Models model_Name = ModelNames.StringToEnum(model); if (model_Name == ModelNames.Models.GRM || model_Name == ModelNames.Models.MGRM) { #region "Variable Declarations" if (model_Name == ModelNames.Models.GRM) { prov = new double[it.NumOfItems, it.colSize]; prov1 = new double[it.NumOfItems, it.colSize]; prov2 = new double[it.NumOfItems, it.colSize]; prov3 = new double[it.NumOfItems, it.colSize]; objPi = new PiListPoly(it.NumOfItems, it.colSize); } if (model_Name == ModelNames.Models.MGRM) { prov = new double[it.NumOfItems, it.colSize - 1]; prov1 = new double[it.NumOfItems, it.colSize - 1]; prov2 = new double[it.NumOfItems, it.colSize - 1]; prov3 = new double[it.NumOfItems, it.colSize - 1]; objPi = new PiListPoly(it.NumOfItems, it.colSize - 1); } #endregion #region "Functional Logic" for (int i = 0; i < it.NumOfItems; i++) // Traversing through items { // "alphaj", 1st column double aj = it.all_items_poly[0][i]; // Calculation of "betaj" vector values double[] bj = null; // "GRM" if (model_Name == ModelNames.Models.GRM) { bj = new double[it.colSize - 1]; // Values starting from 2nd column int b_Index = 1; for (int k = 0; k < bj.Length; k++) { bj[k] = it.all_items_poly[b_Index][i];; b_Index++; } } else // "MGRM" { // "betaj", 2nd column double bj0 = it.all_items_poly[1][i]; bj = new double[it.colSize - 2]; // Values starting from 3rd column int c_Index = 2; for (int k = 0; k < bj.Length; k++) { bj[k] = bj0 - it.all_items_poly[c_Index][i]; c_Index++; } } // Final Array must be NaN value free bj = CheckNaValues.GetArrayWithoutNaN(bj); double[] ej = new double[bj.Length]; double[] Pjs = new double[bj.Length + 2]; double[] dPjs = new double[Pjs.Length]; double[] d2Pjs = new double[Pjs.Length]; double[] d3Pjs = new double[Pjs.Length]; Pjs[0] = 1.000; // 1st Column Pjs[Pjs.Length - 1] = 0.000; // last column for (int m = 0; m < bj.Length; m++) { // Calculation of "ej" ej[m] = Math.Exp(D * aj * (th - bj[m])); // Calculation of "Pjs" Pjs[m + 1] = ej[m] / (1 + ej[m]); } // Calculation of "dPj", "d2Pj", "d3Pj" for (int j = 0; j < Pjs.Length; j++) { dPjs[j] = D * aj * Pjs[j] * (1 - Pjs[j]); d2Pjs[j] = D * aj * (dPjs[j] - 2 * Pjs[j] * dPjs[j]); d3Pjs[j] = D * aj * (d2Pjs[j] - 2 * Math.Pow(dPjs[j], 2) - 2 * Pjs[j] * d2Pjs[j]); } for (int index = 0; index < Pjs.Length - 1; index++) { prov[i, index] = Pjs[index] - Pjs[index + 1]; prov1[i, index] = dPjs[index] - dPjs[index + 1]; prov2[i, index] = d2Pjs[index] - d2Pjs[index + 1]; prov3[i, index] = d3Pjs[index] - d3Pjs[index + 1]; } } #endregion } else if (model_Name == ModelNames.Models.PCM || model_Name == ModelNames.Models.GPCM || model_Name == ModelNames.Models.RSM || model_Name == ModelNames.Models.NRM) { double[] dj = null; double[] v = null; // Common column for the following models if (model_Name == ModelNames.Models.PCM) { #region "Variable Declarations" prov = new double[it.NumOfItems, it.colSize + 1]; prov1 = new double[it.NumOfItems, it.colSize + 1]; prov2 = new double[it.NumOfItems, it.colSize + 1]; prov3 = new double[it.NumOfItems, it.colSize + 1]; objPi = new PiListPoly(it.NumOfItems, it.colSize + 1); #endregion #region "Functional Logic" for (int i = 0; i < it.NumOfItems; i++) { dj = new double[it.colSize + 1]; v = new double[it.colSize + 1]; dj[0] = 0.00; v[0] = 0.00; for (int k = 1; k < dj.Length; k++) { dj[k] = dj[k - 1] + D * (th - it.all_items_poly[k - 1][i]); v[k] = k; } double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v); dj = temp_dj_v[0]; v = temp_dj_v[1]; calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i); } #endregion } if (model_Name == ModelNames.Models.GPCM) { #region "Variable Declarations" prov = new double[it.NumOfItems, it.colSize]; prov1 = new double[it.NumOfItems, it.colSize]; prov2 = new double[it.NumOfItems, it.colSize]; prov3 = new double[it.NumOfItems, it.colSize]; objPi = new PiListPoly(it.NumOfItems, it.colSize); #endregion #region "Functional Logic" for (int i = 0; i < it.NumOfItems; i++) { // "alphaj", 1st column double aj = it.all_items_poly[0][i]; dj = new double[it.colSize]; v = new double[it.colSize]; dj[0] = 0.00; v[0] = 0.00; for (int k = 1; k < dj.Length; k++) { dj[k] = dj[k - 1] + aj * D * (th - it.all_items_poly[k][i]); v[k] = aj * k; } double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v); dj = temp_dj_v[0]; v = temp_dj_v[1]; calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i); } #endregion } if (model_Name == ModelNames.Models.RSM) { #region "Variable Declarations" prov = new double[it.NumOfItems, it.colSize]; prov1 = new double[it.NumOfItems, it.colSize]; prov2 = new double[it.NumOfItems, it.colSize]; prov3 = new double[it.NumOfItems, it.colSize]; objPi = new PiListPoly(it.NumOfItems, it.colSize); #endregion #region "Functional Logic" for (int i = 0; i < it.NumOfItems; i++) { // "lambdaj", 1st column double lambdaj = it.all_items_poly[0][i]; dj = new double[it.colSize]; v = new double[it.colSize]; dj[0] = 0.00; v[0] = 0.00; for (int k = 1; k < dj.Length; k++) { dj[k] = dj[k - 1] + D * (th - (lambdaj + it.all_items_poly[k][i])); v[k] = k; } double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v); dj = temp_dj_v[0]; v = temp_dj_v[1]; calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i); } #endregion } if (model_Name == ModelNames.Models.NRM) { #region "Variable Declarations" int nc = (it.colSize / 2) + 1; prov = new double[it.NumOfItems, nc]; prov1 = new double[it.NumOfItems, nc]; prov2 = new double[it.NumOfItems, nc]; prov3 = new double[it.NumOfItems, nc]; objPi = new PiListPoly(it.NumOfItems, nc); #endregion #region "Functional Logic" for (int i = 0; i < it.NumOfItems; i++) // Row Iteration { dj = new double[nc]; v = new double[nc]; dj[0] = 0.00; v[0] = 0.00; for (int k = 1; k < dj.Length; k++) { dj[k] = it.all_items_poly[2 * (k - 2) + 2][i] * th + it.all_items_poly[2 * (k - 2) + 3][i]; v[k] = it.all_items_poly[2 * (k - 2) + 2][i]; } double[][] temp_dj_v = CheckNaValues.GetArrayWithoutNaN(dj, v); dj = temp_dj_v[0]; v = temp_dj_v[1]; calculatePolyPi(dj, prov, prov1, prov2, prov3, v, i); } #endregion } } else { objPi.Exception = "Polytomous items model not matched!"; } } else { objPi.Exception = "Polytomous model not provided!"; } objPi.Add(prov, prov1, prov2, prov3); //objPi.catDictionaryList = new Dictionary<string, Dictionary<string, catList>>(); //GetCATList(objPi.Pi, objPi.catDictionaryList, "Pi"); //GetCATList(objPi.dPi, objPi.catDictionaryList, "dPi"); //GetCATList(objPi.d2Pi, objPi.catDictionaryList, "d2Pi"); //GetCATList(objPi.d3Pi, objPi.catDictionaryList, "d3Pi"); } catch (Exception ex) { if (ex != null) { if (objPi == null) { objPi = new PiListPoly(); } objPi.Exception = ex.Message; } return(objPi); } return(objPi); }
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 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 }
public void testJi_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(100, 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()); } #region "Test block for Ability Values (th)" JiList objJi = null; int decimalPoint = 6; double[] th_Values = CatRcs.Utils.CommonHelper.Sequence(-6, 6, 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 "Ji" function from R GenericVector result_Ji = engineObj.Evaluate("result_Ji <- Ji(th = th_val, PolyItems, model = modelName, D = 1)").AsList(); // Getting the function result NumericVector Ji = result_Ji[0].AsNumeric(); NumericVector dJi = result_Ji[1].AsNumeric(); Console.WriteLine("Value of Theta: " + th_Values[k]); objJi = CatRLib.Ji(th_Values[k], itemBank, paramModel.EnumToString(), 1); #region "Ji" int z = 0; resultFlag = true; Console.WriteLine("****** Ji ******"); for (int i = 0; i < objJi.Ji.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(Ji[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objJi.Ji[i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); Console.WriteLine("Value from R: " + decimal.Round(Convert.ToDecimal(Ji[i]), decimalPoint)); Console.WriteLine("Value from CS: " + decimal.Round(Convert.ToDecimal(objJi.Ji[i]), decimalPoint)); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of Ji result is Passed!"); #endregion #region "dJi" z = 0; resultFlag = true; Console.WriteLine("****** dJi ******"); for (int i = 0; i < objJi.dJi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(dJi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objJi.dJi[i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of dJi result is Passed!"); #endregion } #endregion #region "Test block for Metric values" objJi = 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 "Ji" function from R GenericVector result_Ji = engineObj.Evaluate("result_Ji <- Ji(th = 0, PolyItems, model = modelName, D = D_val)").AsList(); // Getting the function result NumericVector Ji = result_Ji[0].AsNumeric(); NumericVector dJi = result_Ji[1].AsNumeric(); Console.WriteLine("Value of Metric Constant: " + D_Values[k]); objJi = CatRLib.Ji(0, itemBank, paramModel.EnumToString(), D_Values[k]); #region "Ji" int z = 0; resultFlag = true; Console.WriteLine("****** Ji ******"); for (int i = 0; i < objJi.Ji.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(Ji[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objJi.Ji[i]), decimalPoint)) { /*Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); * Console.WriteLine("Value from R: " + decimal.Round(Convert.ToDecimal(Ji[i]), decimalPoint)); * Console.WriteLine("Value from CS: " + decimal.Round(Convert.ToDecimal(objJi.Ji[i]), decimalPoint));*/ resultFlag = false; } } //Assert.IsTrue(resultFlag); Console.WriteLine("Values of Ji result is Passed!"); #endregion #region "dJi" z = 0; resultFlag = true; Console.WriteLine("****** dJi ******"); for (int i = 0; i < objJi.dJi.Length; i++) { z = i + 1; // item number if (decimal.Round(Convert.ToDecimal(dJi[i]), decimalPoint) != decimal.Round(Convert.ToDecimal(objJi.dJi[i]), decimalPoint)) { Console.WriteLine("Test for Item No. # " + z + " is not Passed!"); resultFlag = false; } } Assert.IsTrue(resultFlag); Console.WriteLine("Values of dJi result is Passed!"); #endregion } #endregion }
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 testEPV_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()); } #region "Test block for Ability Values (th)" double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); for (int j = 0; j < th.Length; j++) { string temp = th[j].ToString(nfi); // Polytomous Items DataFrame it_given_R = engineObj.Evaluate("it_given_R <- PolyItems[c(4, 8),]").AsDataFrame(); engineObj.SetSymbol("it_given_R", it_given_R); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + th[j].ToString(nfi) + ", it_given_R, model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); CATItems it_given = itemBank.FindItem(new int[] { 4, 8 }); // Call "EPV" function from R NumericVector r_epv = engineObj.Evaluate("r_epv <- EPV(PolyItems, 1, x_val, " + th[j].ToString(nfi) + ", it_given_R, model = modelName)").AsNumeric(); // Call "EPV" function from CatRCS double cs_epv = CatRLib.EPV(itemBank, 1, x, th[j], it_given, paramModel.EnumToString()); if (r_epv[0] - cs_epv > testEpsilon) { resultFlag = false; } } Assert.IsTrue(resultFlag); #endregion }