C:/programs/etirm/src/swig_etirm.h

Go to the documentation of this file.
00001 /*! \file swig_etirm.h
00002   
00003   \brief
00004   Declarations of functions and classes to generate scripting
00005   language wrapper functions for ETIRM using SWIG.
00006 
00007   See swig_etirm.cpp for documentation.
00008 
00009   Estimation Toolkit for Item Response Models (ETIRM)
00010   http://www.smallwaters.com/software/cpp/etirm.html
00011 
00012   Author(s): 
00013   Werner Wothke, maintenance (http://www.smallwaters.com)
00014   Brad Hanson (http://www.b-a-h.com/)
00015   See the file LICENSE for information on usage and redistribution.
00016 
00017   Copyright (C) 2008, Werner Wothke
00018   Copyright (c) 2000-2002, Bradley A. Hanson
00019  */
00020 
00021 #ifndef SWIGETIRM_H_
00022 #define SWIGETIRM_H_
00023 
00024 // This must be provided - declares types unique to application 
00025 // (see comments at the top of swig_etirm.cpp).
00026 // It is not provided in ETIRM.
00027 #include "swig_etirm_types.h"
00028 
00029 #ifdef ETIRM_NO_DIR_PREFIX
00030 #include "EStepDiscrete.h"
00031 #else
00032 #include "etirm/EStepDiscrete.h"
00033 #endif
00034 
00035 #include "Uncmin.h"
00036 
00037 #include <vector>
00038 #include <string>
00039 
00040 // Use uniform random number generator from boost library
00041 // (http://www.boost.org).
00042 #include <boost/random/uniform_01.hpp>
00043 
00044 #ifndef SWIG
00045 namespace etirm
00046 {
00047 #endif
00048 
00049   // Declarations not wrapped with SWIG
00050 #ifndef SWIG
00051   //! Vector type for double precision values.
00052   typedef RealVector double_vector;
00053 
00054   //! Vector type for integer values.
00055   typedef SCPPNT::Vector<int> int_vector;
00056 
00057   //! Vector type for item pointers
00058   typedef std::vector<item_type *> ItemVector;
00059 
00060   //! Class used in E-step calculation
00061   typedef EStepDiscrete<examinee_type, item_type, ItemVector::iterator, lvdist_type> estep_type;
00062 
00063   //! Container to hold Uncmin optimization objects
00064   typedef std::vector<Uncmin<RealVector, RealMatrix, item_type> *> UncminVector;
00065 
00066   //! Container holding examinee object pointers
00067   typedef std::vector<examinee_type *> ExamineeVector;
00068 
00069   /*!
00070     \brief
00071     Class to hold counts of number of examinees who responded in each response 
00072     category of an item.
00073    */
00074   class ItemRespCounts
00075   {
00076 public:
00077 
00078     /*! 
00079       \brief
00080       Class constructor.
00081       
00082       \section function_args Function Parameters
00083    
00084       \param[in]  ncat Number of response categories for the item.
00085       \param[in]  ngroup  Number of examinee groups.
00086      */
00087     ItemRespCounts(int ncat, int ngroup) :
00088       catCounts(ngroup+1, ncat, 0.0), totalCount(ngroup+1, 0.0)
00089     {
00090     }
00091 
00092     //! Deconstructor
00093     virtual ~ItemRespCounts()
00094     {
00095     }
00096 
00097     // Increment counts for a particular item response (1-offset)
00098     void AddResponse(int respIndex, Real count, int group);
00099 
00100 
00101     /*!
00102       \brief
00103       Returns total number of responses to item.
00104       
00105       \section function_args Function Parameters
00106    
00107       \param[in]  group Group(1, 2, ...) for which counts are to be returned.
00108       
00109       Note: group = 0 returns counts across all groups.
00110      */
00111     Real RespCount(int group)
00112     {
00113       return totalCount[group];
00114     }
00115 
00116     /*!
00117       \brief
00118       Returns number of responses in each response category.
00119       
00120       \section function_args Function Parameters
00121    
00122       \param[in]  group Group(1, 2, ...) for which counts are to be returned.
00123       
00124       Note: group = 0 returns counts across all groups.
00125      */      
00126     RealVector CategoryCounts(int group)
00127     {
00128       return RealVector(catCounts.begin_row(group+1), catCounts.begin_row(group+1)
00129           +catCounts.num_columns());
00130     } // ww: Edit "num_cols" -> "num_columns", 1/8/2008.
00131 
00132 
00133 protected:
00134     /*!
00135       \brief
00136       Total number of examinees who have responses for the item.
00137       
00138       totalCount[0] is total count across all groups
00139       totalCount[i], i>0, is count for group i
00140      */
00141     RealVector totalCount;
00142 
00143     /*!
00144       \brief
00145       Number of responses in each response category.
00146       
00147       Columns give counts for response categories
00148       Row 1 gives counts across all groups.
00149       Row i, i>1,  gives counts for group i-1.
00150      */
00151     RealMatrix catCounts;
00152   };
00153 
00154   //! Container holding pointers to objects containing item statistics for each item
00155   typedef std::vector<ItemRespCounts *> ItemStatsVector;
00156 
00157   //! Class holding information used for one modeling problem.
00158   class SwigEtirmRun
00159   {
00160 
00161 public:
00162 
00163     SwigEtirmRun(int nitems, int nlatentcat, int ngroups, Real minTheta = -4.0,
00164         Real maxTheta = 4.0, bool uniquePoints = false);
00165 
00166     virtual ~SwigEtirmRun();
00167 
00168     ExamineeVector examinees;
00169     //!< Vector holding pointers to examinee objects.
00170     
00171     ItemVector items;
00172     //!< Vector holding pointers to item objects.
00173     
00174     ItemStatsVector itemStats;
00175     //!< Vector holding pointers to item count objects.
00176     
00177     UncminVector minProc;
00178     //!< Vector holding pointers to minimization objects.
00179     
00180     lvdist_type latentDist;
00181     //!< Latent distribution object.
00182 
00183     random_type *rand_boot;
00184     //!< Base random number generator object for bootstrap.
00185 
00186     random_type *base_rand_simulate;
00187     //!< Base random number generator object for simulating item responses.
00188 
00189     boost::uniform_01<random_type> *rand_simulate;
00190     //!< Uniform random number generator object for simulating item responses.
00191 
00192     std::string returnString;
00193     //!< Temporary space to hold strings returned by functions.
00194 
00195     int numItems;
00196     //!< Number of operational/field test items to model.
00197     
00198     int numLatentCat;
00199     //!< Number of discrete categories to approximate the latent ability distribution.
00200     
00201     int numGroups;
00202     //!< Number of non-equivalents groups of examinees.
00203     
00204     RealVector examineeCounts;
00205     //!< Examinee counts in each group: examineeCounts[0] contains count across all groups; examineeCounts[i], i>0, contains count for group i.
00206 
00207     double mstepMaxDiff;
00208     //!< Maximum relative difference between item parameters computed in last M-Step call.
00209 
00210     /*!
00211       \brief
00212       Checks that data for examinees exist.
00213       
00214       \section function_args Function Parameters
00215    
00216       \param[in] *funcname  Pointer to name of calling routine.
00217      */
00218     void CheckExaminees(const char *funcname);
00219 
00220     /*!
00221       \brief
00222       Check that the examinee number is within its valid range.
00223       
00224       \section function_args Function Parameters
00225    
00226       \param[in] examno Examinee number.
00227       \param[in] *funcname  Pointer to name of calling routine.
00228      */
00229     void CheckExamineeNumber(int examno, const char *funcname);
00230 
00231     /*!
00232       \brief
00233       Checks for valid item number.
00234       
00235       \section function_args Function Parameters
00236    
00237       \param[in] itemno Item number.
00238       \param[in] *funcname  Pointer to name of calling routine.
00239      */
00240     void CheckItemNumber(int itemno, const char *funcname);
00241 
00242     /*!
00243       \brief
00244       Checks for valid group.
00245       
00246       \section function_args Function Parameters
00247       
00248       \param[in]  group Group number.
00249       \param[in] *funcname  Pointer to name of calling routine.
00250      */
00251     void CheckGroup(int group, const char *funcname);
00252   };
00253 
00254   /*!
00255     \brief
00256     Check that new_etirm has been called
00257   
00258     Note: This function is not defined in swig_etirm.cpp, it must be defined 
00259     somewhere in the application.
00260    */
00261   void CheckRunInit();
00262 
00263   /*! 
00264     \brief
00265     Tests whether item parameter index is valid for the item.
00266    
00267     \section function_args Function Parameters
00268     
00269     \param[in]  *item   Pointer to item object for which index is checked.
00270     \param[in]  index   Zero-offset index of item parameter.
00271     \param[in]  *funcname pointer to name of calling function (used in error message).
00272    */
00273   void CheckItemParam(item_type *item, int index, const char *funcname);
00274 
00275   //Create a prior distribution object and return a pointer to it.
00276   ItemParamPrior *CreatePrior(const std::string &pstr, const double_vector &priorparam,
00277       const char *funcname);
00278 
00279   /*!
00280     \brief 
00281     Creates a container holding the elements corresponding to a subset of items.
00282 
00283     ItemSubset is based on a container holding elements corresponding to all items. 
00284     This container is typically used to create subsets of etirmrun->items (which 
00285     is an ItemVector) and etirmrun->minProc (which is a UncminVector). 
00286     
00287     \section template_args Template Parameters
00288    
00289     \param V ItemVector type.
00290    
00291     \section function_args Function Parameters
00292    
00293     \param[in]  *itemnums   Pointer to vector containing numbers of items to include in subset.
00294     \param[in]  &all  Address of container containing all items.
00295     \param[in]  *funcname   Pointer to name of calling function (used in error messages)
00296    */
00297   template <class V> V ItemSubset(int_vector *itemnums, V &all, const char *funcname)
00298   {
00299     int i;
00300     int nsub = itemnums->size();
00301     int nall = all.size();
00302 
00303     if (nsub < 1 || nsub> nall)
00304     {
00305       throw RuntimeError("Invalid number of items", funcname);
00306     }
00307 
00308     V sub(nsub);
00309     int *flag = new int[nall]; // flags indicating which items are in subset
00310     for (i = 0; i < nall; ++i) flag[i] = 0;
00311 
00312     typename V::iterator iall = all.begin();
00313     typename V::iterator isub = sub.begin();
00314     int_vector::iterator ino = itemnums->begin();
00315     for(i = 0; i < nsub; ++i, ++isub, ++ino)
00316     {
00317       int index = *ino - 1; // elements of itemnums are 1-offset indices
00318       if (index < 0 || index >= nall)
00319       {
00320         delete [] flag;
00321         throw RuntimeError("Invalid item number", funcname);
00322       }
00323       if (flag[index] == 1)
00324       {
00325         delete [] flag;
00326         throw RuntimeError("Duplicate item number", funcname);
00327       }
00328       flag[index] = 1;
00329       *isub = iall[index];
00330     }
00331     delete [] flag;
00332 
00333     return sub;
00334   }
00335 
00336   // Convert an item response to a character, where '0'
00337   // corresponds to the first response category
00338   char Resp2Char(Response r, const item_type *item);
00339 
00340 #endif // SWIG
00341   /*** Classes and functions to be wrapped by SWIG ***/
00342 
00343   //! SWIG wrapper for EStepDiscrete object
00344   class estep
00345   {
00346 
00347   public:
00348 
00349     estep(int_vector *ilist1 = 0);
00350     ~estep();
00351 
00352     double compute(bool compute_prior = TRUE, bool store_prior = FALSE, int_vector *ilist4 = 0);
00353     // calls mEStep->DoEStep  // Retyped compute_prior and store_prior as "bool", ww, 2-24-2008.
00354 
00355 #ifndef SWIG
00356     /*! 
00357       \brief
00358       Returns EStepDiscrete object. 
00359       
00360       Do not create SWIG wrapper for this member function!
00361      */
00362     estep_type *GetEStep()
00363     { return mEStep;}
00364 #endif
00365 
00366   private:
00367 
00368     estep_type *mEStep; //!< EStepDiscrete object.
00369     ItemVector *mItems; //!< List of items used to initialize mEStep
00370   };
00371 
00372   // Set response which indicates an examinee did
00373   // not respond to an item
00374   void set_missing_resp(char nr);
00375 
00376   // Return number of items, number of theta points, and number of groups
00377   // specified in new_etirm
00378   int num_items();
00379   int num_latent_dist_points();
00380   int num_groups();
00381 
00382   // Return number of examinees added by calling add_examinee
00383   int num_examinees();
00384 
00385   /*** item functions ***/
00386 
00387   // Return name of model used for item
00388   const char * item_get_model(int itemno);
00389 
00390   // Assign a value to one parameter for one item
00391   void item_set_param(int paramno, int itemno, double paramvalue);
00392 
00393   // Assign values to all estimated parameters for one item
00394   void item_set_params(int itemno, double_vector *params);
00395 
00396   // Assign values to all fixed and estimated parameters for one item
00397   void item_set_all_params(int itemno, double_vector *params);
00398 
00399   // Get one parameter for one item
00400   double item_get_param(int paramno, int itemno);
00401 
00402   // Get all estimated parameters for one item
00403   double_vector *item_get_params(int itemno);
00404 
00405   // Get all estimated and fixed parameters for one item
00406   double_vector *item_get_all_params(int itemno);
00407 
00408   // Return number of parameters for one item
00409   int item_num_params(int itemno);
00410 
00411   // Return number of response categories for an item.
00412   int item_num_resp_cat(int itemno);
00413 
00414   // Set prior for one item
00415   void item_set_prior(int paramno, int itemno, char *priortype, double_vector *dlist4 = 0);
00416 
00417   // Get prior distribution for one parameter and one item
00418   const char *item_get_prior_type(int paramno, int itemno);
00419 
00420   // Get parameters of prior
00421   double_vector *item_get_prior_param(int paramno, int item);
00422 
00423   // Get probability of an item response
00424   double item_prob_resp(int itemno, int response, double theta);
00425 
00426   // Transform item parameters to different IRT scale
00427   int item_scale_params(int itemno, double slope, double intercept, bool ignorePriorError = 0); 
00428   // Retyped ignorePriorError from "int" to "bool", ww, 2-26-2008.
00429 
00430   // Return vector of counts of responses in each response category for an item
00431   // in an examinee group (0=all groups)
00432   double_vector *item_cat_counts(int itemno, int group = 0);
00433 
00434   // Return counts of number of examinees responding to an item
00435   // in an examinee group (0=all groups)
00436   double item_resp_count(int itemno, int group = 0);
00437 
00438   // Compute M-step for indicated items
00439   double_vector *test_characteristic_curve(double_vector *thetas, int_vector *ilist2 = 0);
00440 
00441   /** Latent distribution functions ***/
00442 
00443   // Set point value for one category of discrete latent variable distribution
00444   void dist_set_point(int index, double p, int group = 1);
00445 
00446   // Set points of latent discrete distribution
00447   void dist_set_points(double_vector *dlist, int group = 1);
00448 
00449   // Return value of point for one category of latent variable distribution
00450   double dist_get_point(int index, int group = 1);
00451 
00452   // Get points of latent discrete distribution
00453   double_vector *dist_get_points(int group = 1);
00454 
00455   // Set value of the probability for one category of discrete latent variable distribution for one group
00456   void dist_set_prob(int index, double w, int group = 1);
00457 
00458   // Set probabilities for latent discrete distribution for one group
00459   void dist_set_probs(double_vector *dlist, int group = 1);
00460 
00461   // Return value of probability for one category of latent variable distribution for one group
00462   double dist_get_prob(int index, int group = 1);
00463 
00464   // Get probabilities for latent discrete distribution for one group
00465   double_vector *dist_get_probs(int group = 1);
00466 
00467   // Transform points of latent variable distribution to a new scale
00468   void dist_transform(double slope, double intercept);
00469 
00470   // Scale points to give specific mean and sd in one group
00471   // Returns slope and intercept of linear scale transformation
00472   double_vector *dist_scale(double mean, double sd, int group = 1);
00473 
00474   // Return mean and sd of distribution in one group
00475   double_vector *dist_mean_sd(int group = 1);
00476 
00477   // Returns 1 if unique discrete latent distribution points are used for
00478   // different examinee groups and number of examinee groups is greater than 1, otherwise returns 0
00479   int dist_unique_points();
00480 
00481   // Return vector of probabilities for discrete distribution to approximate a normal distribution
00482   double_vector *normal_dist_prob(int npoints, double minPoint, double maxPoint,
00483   double mean = 0.0, double sd = 1.0);
00484 
00485   // Return vector of points for discrete distribution to approximate a normal distribution
00486   double_vector *normal_dist_points(int npoints, double minPoint, double maxPoint, double mean = 0.0,
00487   double sd = 1.0);
00488 
00489   /*** M-step functions ***/
00490 
00491   // Compute M-step for indicated items
00492   int mstep_items(bool ignore_max_iter = FALSE, int_vector *ilist2 = 0);
00493   // Retyped ignore_max_iter from "int" to "bool", ww, 2-25-2008.
00494   
00495   // Return maximum relative difference between parameters in current and previous
00496   // EM iteration computed in last call to mstep_items
00497   double mstep_max_diff();
00498 
00499   // Return message from optimization procedure generated in
00500   // last call to mstep_items for an item
00501   int mstep_message(int itemno);
00502 
00503   // Set maximum number of iterations used by optimization procedure
00504   // in mstep_items for one item.
00505   void mstep_max_iter(int itemno, int maxiter);
00506 
00507   // M-step for latent distribution in one group
00508   // Returns maximum relative difference between new and old probabilities
00509   double mstep_dist(estep *e, int group);
00510 
00511   /*** Examinee functions ***/
00512 
00513   // add one examinee
00514   int add_examinee(int_vector *responses, int group = 1, double count = 1.0);
00515 
00516   // Return vector of all item responses for an examinee
00517   int_vector *examinee_responses(int examineeno);
00518 
00519   // Returns string containing examinee responses to all items.
00520   // A response in the first response category is '0'.
00521   const char *examinee_response_str(int examineeno);
00522 
00523   // Return group examinee belongs to
00524   int examinee_get_group(int examineeno);
00525 
00526   // Sets group examinee belongs to
00527   void examinee_set_group(int examineeno, int group);
00528 
00529   // Set count for an examinee
00530   void examinee_set_count(int examineeno, double count);
00531 
00532   // Return count for an examinee
00533   double examinee_get_count(int examineeno);
00534 
00535   // Return total examinee counts in an examinee group (1, 2, ...).
00536   // If group==0 then return total count across all groups.
00537   double examinees_count(int group = 0);
00538 
00539   // Set posterior distribution for an examinee
00540   void examinee_set_posterior(int examineeno, double_vector *posterior);
00541 
00542   // Return copy of posterior distribution for an examinee
00543   double_vector* examinee_get_posterior(int examineeno);
00544 
00545   // Return mean of posterior distribution for an examinee
00546   double examinee_posterior_mean(int examineeno);
00547 
00548   // Return MLE theta estimate for an examinee
00549   double examinee_theta_MLE(int examineeno, double minTheta, double maxTheta,
00550   double precision = 0.001, int_vector *ilist5 = 0);
00551 
00552   // Set seed of random number generator used for bootstrap samples
00553   void bootstrap_seed(unsigned long seed);
00554 
00555   // Generate bootstrap sample of examinees
00556   void bootstrap_sample();
00557 
00558   // Set seed of random number generator used for simulating item responses
00559   void simulate_seed(unsigned long seed);
00560 
00561   // Simulate item responses corresponding to latent variable value theta
00562   int_vector *simulate_responses(double theta, int_vector *ilist2 = 0);
00563 
00564   // Return a string containing simulated item responses
00565   const char *simulate_response_str(double theta, int_vector *ilist2 = 0);
00566 
00567   // Read item responses from a string
00568   int_vector *get_responses(char *line, int_vector *offset, int_vector *len);
00569 
00570   // Read item responses for a subset of items from a string
00571   int_vector *get_responses_missing(char *line, int_vector *offset, int_vector *len,
00572   int_vector *items);
00573 
00574 #ifndef SWIG
00575 } // namespace etirm
00576 #endif // SWIG
00577 
00578 #endif

Generated on Sat Mar 1 21:40:16 2008 for ETIRM by  doxygen 1.5.4