Giter Club home page Giter Club logo

bmggum's Introduction

bmggum

CRAN_Status_Badge Total Downloads

The goal of bmggum is to estimate Multidimensional Generalized Graded Unfolding Model (MGGUM) using Bayesian method. Specifically,the R package rstan that utilizes the Hamiltonian Monte Carlo sampling algorithm was used for estimation. Below are some important features of the bmggum package:

  1. Allows users to incorporate person covariates (e.g., age, gender, education) into the estimation to improve estimation accuracy.
  2. Automatically deals with missing data in a way similar to how full information maximum likelihood handles missing data.
  3. Allows users to estimate the multidimensional version of three unfolding models that are available in the software GGUM2004 (Roberts, Fang, Cui, & Wang, 2006).
    • UM8: The Generalized Graded Unfolding Model (GGUM).
    • UM4: The Partial Credit Unfolding Model, which is the GGUM with all alphas constrained to 1.
    • UM7: The Generalized Rating Scale Unfolding Model, which is the GGUM with equal taus across items.
  4. Five functions (i.e., bmggum( ), extract( ), modfit( ), bayesplot( ), and itemplot( )) are provided for model estimation, results extraction, model fit examination (e.g.,waic, loo, chisq/df), and plottings, respectively.

Installation

You can install bmggum from CRAN:

install.packages("bmggum")

You can install the development version of bmggum from GitHub:

devtools::install_github("Naidantu/bmggum")

Example

This is a basic example which shows you how to prepare data, fit the model, extract and plot results.

library(bmggum)

## basic example code
## Step 1: Input data
# 1.1 Response data in wide format
GGUM.Data <- c(1,4,4,1,1,1,1,1,1,1,4,1,1,3,1,1,NA,2,NA,3,2,2,2,1,3,2,NA,2,1,1,2,1,NA,NA,NA,1,3,NA,1,2)
GGUM.Data <- matrix(GGUM.Data,nrow = 10)
GGUM.Data
#>       [,1] [,2] [,3] [,4]
#>  [1,]    1    4    2    2
#>  [2,]    4    1    2    1
#>  [3,]    4    1    2   NA
#>  [4,]    1    3    1   NA
#>  [5,]    1    1    3   NA
#>  [6,]    1    1    2    1
#>  [7,]    1   NA   NA    3
#>  [8,]    1    2    2   NA
#>  [9,]    1   NA    1    1
#> [10,]    1    3    1    2

# 1.2 A two-row data matrix: the first row is the item number (1,2,3,4...); the second row indicates the signs of delta for each item (-1,0,1,...). For items that have negative deltas for sure, "-1" should be assigned; for items that have positive deltas, "1" should be assigned; for items whose deltas may be either positive or negative (e.g., intermediate items), "0" should assigned. We recommend at least two positive and two negative items per trait for better estimation.
delindex <- c(1,-1,2,1,3,-1,4,1)
delindex <- matrix(delindex,nrow = 2)
delindex
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]   -1    1   -1    1

# 1.3 A row vector mapping each item to each trait. For example, c(1,1,1,2,2,2) means that the first 3 items belong to trait 1 and the last 3 items belong to trait 2.
ind <- c(1,1,2,2)
ind <- t(ind)
ind
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    2    2

# 1.4 A p*c person covariate matrix where p equals sample size and c equals the number of covariates. The default is NULL, meaning no person covariate.
covariate <- c(0.70, -1.25, 0.48, -0.47, 0.86, 1.25, 1.17, -1.35, -0.84, -0.55)

## Step 2: Fit the MGGUM model
mod <- bmggum(GGUM.Data=GGUM.Data, delindex=delindex, trait=2, ind=ind, option=4, model="UM8", covariate=covariate)
#> [1] "Case 9 was deleted because they endorse the same response option across all items"

## Step 3: Extract the estimated results 
# 3.1 Extract the theta estimates 
theta <- extract(x=mod, pars='theta')
# Turn the theta estimates into p*trait matrix where p equals sample size and trait equals the number of latent traits
theta <- theta[,1]
# nrow=trait
theta <- matrix(theta, nrow=2)  
theta <- t(theta)
# theta estimates in p*trait matrix format
theta
#>             [,1]       [,2]
#>  [1,]  1.0187912  0.1121540
#>  [2,] -1.5066328 -0.4671378
#>  [3,] -1.2348383 -0.5255688
#>  [4,]  0.7027544  0.9666617
#>  [5,]  0.0337327 -0.7254310
#>  [6,]  0.2418053 -0.6745725
#>  [7,]  0.8668120  0.3956210
#>  [8,]  0.1091474  0.3868123
#>  [9,]  0.6335485  0.8056360

# 3.2 Extract the tau estimates 
tau <- extract(x=mod, pars='tau')
# Turn the tau estimates into I*(option-1) matrix where I equals the number of items and option equals the number of response options
tau <- tau[,1]
# nrow=option-1
tau <- matrix(tau, nrow=3)  
tau <- t(tau)
# tau estimates in I*(option-1) matrix format
tau
#>           [,1]       [,2]       [,3]
#> [1,] -0.871650 -1.5754448 -2.3666246
#> [2,] -1.533934 -1.8805259 -1.1326023
#> [3,] -3.238270 -0.8797071 -0.8817295
#> [4,] -2.392070 -1.3460863 -1.0581735

# 3.3 Extract the lambda estimates 
lambda <- extract(x=mod, pars='lambda')
# lambda[1,1] is the coefficient linking person covariate 1 to latent trait 1
# lambda[1,2] is the coefficient linking person covariate 1 to latent trait 2
lambda
#>                   mean    se_mean        sd       2.5%        50%    97.5%
#> lambda[1,1]  0.2810566 0.02251794 0.4872688 -0.6503625  0.2678305 1.365346
#> lambda[1,2] -0.2285741 0.04068253 0.6538357 -1.6925588 -0.1881410 1.062749
#>                n_eff     Rhat
#> lambda[1,1] 468.2522 1.003816
#> lambda[1,2] 258.2981 1.006084

## Step 4: Obtain model fit statistics 
waic <- modfit(x=mod, index='waic')
loo <- modfit(mod)

## Step 5: Plottings
# 5.1 Obtain the density plots for alpha
bayesplot(x=mod, pars='alpha', plot='density', inc_warmup=FALSE)

# 5.2 Obtain the trace plots for alpha
bayesplot(x=mod, pars='alpha', plot='trace', inc_warmup=FALSE)

## Step 6: Plotting observable response categories (ORCs) for items
# 6.1 Obtain item plots with ORCs for item 1, 2, 3
itemplot(x=mod, items = 1:3)

Citation

Tu, N., Zhang, B., Angrave, L., & Sun, T. (2021). bmggum: An R Package for Bayesian Estimation of the Multidimensional Generalized Graded Unfolding Model With Covariates. Applied Psychological Measurement, 01466216211040488.

bmggum's People

Contributors

naidantu avatar

Stargazers

Rodrigo Martins avatar Cengiz Zopluoglu avatar Josué M avatar Sebastian Castro-Alvarez avatar Tianjun Sun avatar

Watchers

Rodrigo Martins avatar Tianjun Sun avatar  avatar

Forkers

andrjohns

bmggum's Issues

y[i_0__] is more than 6

Hi Naidan,

I'm fitting a UM8 model and got this warning:

delindex <- c(1,1,2,1,3,0,4,0,5,1,6,1,7,-1,8,-1)
delindex <- matrix(delindex,nrow=2)

ind <- c(1,1,1,1,2,2,2,2)
ind <- t(ind)

bmg.model <- bmggum(GGUM.Data = comp.matrix, delindex=delindex, trait=2, ind=ind, option=11, model="UM8")
#> [1] "Case 6 was deleted because they endorse the same response option across all items"
#> [1] "Case 25 was deleted because they endorse the same response option across all items"
#> [1] "Case 39 was deleted because they endorse the same response option across all items"
#> [1] "Case 52 was deleted because they endorse the same response option across all items"
#> [1] "Case 53 was deleted because they endorse the same response option across all items"
#> [1] "Case 54 was deleted because they endorse the same response option across all items"
#> [1] "Case 55 was deleted because they endorse the same response option across all items"
#> [1] "Case 56 was deleted because they endorse the same response option across all items"
#> [1] "Case 60 was deleted because they endorse the same response option across all items"
#> [1] "Case 64 was deleted because they endorse the same response option across all items"
#> [1] "Case 81 was deleted because they endorse the same response option across all items"
#> [1] "Case 86 was deleted because they endorse the same response option across all items"
#> [1] "Case 109 was deleted because they endorse the same response option across all items"
#> [1] "Case 111 was deleted because they endorse the same response option across all items"
#> [1] "Case 129 was deleted because they endorse the same response option across all items"
#> [1] "Case 130 was deleted because they endorse the same response option across all items"
#> [1] "Case 134 was deleted because they endorse the same response option across all items"
#> [1] "Case 139 was deleted because they endorse the same response option across all items"
#> [1] "Case 143 was deleted because they endorse the same response option across all items"
#> [1] "Case 151 was deleted because they endorse the same response option across all items"
#> [1] "Case 159 was deleted because they endorse the same response option across all items"
#> [1] "Case 173 was deleted because they endorse the same response option across all items"
#> [1] "Case 184 was deleted because they endorse the same response option across all items"
#> [1] "Case 185 was deleted because they endorse the same response option across all items"
#> [1] "Case 198 was deleted because they endorse the same response option across all items"
#> [1] "Case 246 was deleted because they endorse the same response option across all items"
#> [1] "Case 247 was deleted because they endorse the same response option across all items"
#> [1] "Case 281 was deleted because they endorse the same response option across all items"
#> [1] "Case 284 was deleted because they endorse the same response option across all items"
#> [1] "Case 288 was deleted because they endorse the same response option across all items"
#> [1] "Case 297 was deleted because they endorse the same response option across all items"
#> [1] "Case 302 was deleted because they endorse the same response option across all items"
#> [1] "Case 303 was deleted because they endorse the same response option across all items"
#> [1] "Case 306 was deleted because they endorse the same response option across all items"
#> [1] "Case 313 was deleted because they endorse the same response option across all items"
#> [1] "Case 314 was deleted because they endorse the same response option across all items"
#> [1] "Case 325 was deleted because they endorse the same response option across all items"
#> [1] "Case 327 was deleted because they endorse the same response option across all items"
#> [1] "Case 361 was deleted because they endorse the same response option across all items"
#> [1] "Case 374 was deleted because they endorse the same response option across all items"
#> [1] "Case 379 was deleted because they endorse the same response option across all items"
#> [1] "Case 380 was deleted because they endorse the same response option across all items"
#> [1] "Case 385 was deleted because they endorse the same response option across all items"
#> [1] "Case 387 was deleted because they endorse the same response option across all items"
#> [1] "Case 392 was deleted because they endorse the same response option across all items"
#> [1] "Case 393 was deleted because they endorse the same response option across all items"
#> [1] "Case 394 was deleted because they endorse the same response option across all items"
#> [1] "Case 396 was deleted because they endorse the same response option across all items"
#> [1] "Case 408 was deleted because they endorse the same response option across all items"
#> [1] "Case 415 was deleted because they endorse the same response option across all items"
#> [1] "Case 417 was deleted because they endorse the same response option across all items"
#> [1] "Case 422 was deleted because they endorse the same response option across all items"
#> [1] "Case 424 was deleted because they endorse the same response option across all items"
#> [1] "Case 431 was deleted because they endorse the same response option across all items"
#> [1] "Case 433 was deleted because they endorse the same response option across all items"
#> [1] "Case 443 was deleted because they endorse the same response option across all items"
#> [1] "Case 464 was deleted because they endorse the same response option across all items"
#> [1] "Case 504 was deleted because they endorse the same response option across all items"
#> [1] "Case 512 was deleted because they endorse the same response option across all items"
#> [1] "Case 521 was deleted because they endorse the same response option across all items"
#> [1] "Case 528 was deleted because they endorse the same response option across all items"
#> [1] "Case 530 was deleted because they endorse the same response option across all items"
#> [1] "Case 533 was deleted because they endorse the same response option across all items"
#> [1] "Case 540 was deleted because they endorse the same response option across all items"
#> [1] "Case 542 was deleted because they endorse the same response option across all items"
#> [1] "Case 543 was deleted because they endorse the same response option across all items"
#> Error : Exception: model_UM8nocov_namespace::model_UM8nocov: y[i_0__] is 9, but must be less than or equal to 6  (in 'model_UM8nocov' at line 37)

#> failed to create the sampler; sampling not done
#> Stan model 'UM8nocov' does not contain samples.
#> Stan model 'UM8nocov' does not contain samples.
#> Stan model 'UM8nocov' does not contain samples.
#> Stan model 'UM8nocov' does not contain samples.
#> Stan model 'UM8nocov' does not contain samples.
#> Error in estimates[, 8 + 8 + 1] : subscript out of bounds

Created on 2023-08-19 with reprex v2.0.2

The scale has eight items, two traits (four items each), and 11 response options (0-10). Any advice on what went wrong?

Many thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.