| Title: | Stan Models for the Paired Comparison Factor Model |
|---|---|
| Description: | Provides convenience functions and pre-programmed Stan models related to the paired comparison factor model. Its purpose is to make fitting paired comparison data using Stan easy. This package is described in Pritikin (2020) <doi:10.1016/j.heliyon.2020.e04821>. |
| Authors: | Joshua N. Pritikin [aut, cre]
|
| Maintainer: | Joshua N. Pritikin <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.5.4 |
| Built: | 2026-10-29 11:30:58 UTC |
| Source: | https://github.com/cran/pcFactorStan |
pcFactorStan makes it easy to fit the paired comparison factor model using rstan.
A user will generally want to use prepData and
pcStan to fit a model.
The package includes a number of Stan models (see
findModel for a list) and an example dataset
phyActFlowPropensity.
After gaining some experience with the pre-defined models, we
anticipate that users may write their own Stan models and fit them
with stan, for which pcStan is a
wrapper.
Data are passed through filterGraph and normalizeData.
Then the ‘unidim_adapt’ model is fit to each item individually.
A larger varCorrection will obtain a more accurate
scale, but is also more likely to produce an intractable
model. A good compromise is between 5.0 and 9.0.
calibrateItems( df, iter = 2000L, chains = 4L, varCorrection = 5, maxAttempts = 5L, ... )calibrateItems( df, iter = 2000L, chains = 4L, varCorrection = 5, maxAttempts = 5L, ... )
df |
a data frame with pairs of vertices given in columns |
iter |
A positive integer specifying the number of iterations for each chain (including warmup). |
chains |
A positive integer specifying the number of Markov chains. |
varCorrection |
A correction factor greater than or equal to 1.0 |
maxAttempts |
How many times to try re-running a model with more iterations. |
... |
Additional options passed to |
A data.frame (one row per item) with the following columns:
Name of the item
Number of iterations per chain
Number of divergent transitions observed after warmup
Number of times the treedepth was exceeded
Number of chains with low E-BFMI
Minimum effective number of samples across all parameters
Maximum Rhat across all parameters
Median marginal posterior of scale
Median variance of theta (latent scores)
Vehtari, A., Gelman, A., Simpson, D., Carpenter, B., & Bürkner, P. C. (2019). Rank-normalization, folding, and localization: An improved for assessing convergence of MCMC. arXiv preprint arXiv:1903.08008.
result <- calibrateItems(phyActFlowPropensity) # takes more than 5 seconds print(result)result <- calibrateItems(phyActFlowPropensity) # takes more than 5 seconds print(result)
Use itemModelExplorer to explore the item model. In
this shiny app, the discrimination parameter does what
is customary in item response models. However, it is not difficult
to show that discrimination is a function of thresholds and
scale. That is, discrimination is not an independent parameter. In
paired comparison models, discrimination and measurement error are
confounded.
cmp_probs(alpha, scale, pa1, pa2, thRaw)cmp_probs(alpha, scale, pa1, pa2, thRaw)
alpha |
discrimination parameter |
scale |
scale correction factor |
pa1 |
first latent worth |
pa2 |
second latent worth |
thRaw |
vector of positive thresholds |
The thresholds are parameterized as the difference
from the previous threshold. For example, thresholds c(0.5, 0.6)
are not at the same location but are at locations c(0.5,
1.1). Thresholds are symmetric. If there is one threshold then the
model admits three possible response outcomes (e.g. win, tie, and
lose). Responses are always stored centered with zero representing
a tie. Therefore, it is necessary to add one plus the number of
thresholds to response data to index into the vector returned by
cmp_probs. For example, if our response data is (-1, 0, 1)
and has one threshold then we would add 2 (1 + 1 threshold) to
obtain the indices (1, 2, 3).
A vector of probabilities of observing each outcome
Up until version 1.4, the item response model was based on the partial credit model (Masters, 1982). In version 1.5, the graded response model is used instead (Samejima, 1969). The advantage of the graded response model is greater independence among threshold parameters and the ability to compute only the parts of the model that are actually needed given particular observations. The curves predicted by both models are similar and should obtain similar results in data analyses.
Samejima, F. (1969). Estimation of latent ability using a response pattern of graded scores. Psychometrika Monograph Supplement, 34(4, Pt. 2), 100.
Masters, G. N. (1982). A Rasch model for partial credit scoring. Psychometrika, 47, 149–174. doi: 10.1007/BF02296272
# Returns probabilities of # c(pa1 > pa2, pa1 = pa2, pa1 < pa2) cmp_probs(1,1,0,1,.8) # Add another threshold for a symmtric 3 point Likert scale cmp_probs(1,1,0,.5,c(.8, 1.6))# Returns probabilities of # c(pa1 > pa2, pa1 = pa2, pa1 < pa2) cmp_probs(1,1,0,1,.8) # Add another threshold for a symmtric 3 point Likert scale cmp_probs(1,1,0,.5,c(.8, 1.6))
Vertices not part of the largest connected component are excluded (Hopcroft & Tarjan, 1973).
Vertices that have fewer than minAny edges and are not
connected to minDifferent or more different vertices are
excluded. For example, vertex ‘a’ connected to vertices
‘b’ and ‘c’ will be include so long as these vertices
are part of the largest connected component.
filterGraph(df, minAny = 11L, minDifferent = 2L)filterGraph(df, minAny = 11L, minDifferent = 2L)
df |
a data frame with pairs of vertices given in columns |
minAny |
the minimum number of edges |
minDifferent |
the minimum number of vertices |
Given that minDifferent defaults to 2,
if activity was compared to at least
two other activities, and , then is retained.
The rationale is that,
although little may be learned about ,
there may be a transitive relationship,
such as , by which the model can infer that .
Therefore, per-activity sample size is less of a concern
when the graph is densely connected.
A young novice asked the wise master, "Why is 11 the default minAny instead of 10?"
The master answered, "Because 11 is a prime number."
The same graph excluding some vertices.
Hopcroft, J., & Tarjan, R. (1973). Algorithm 447: Efficient algorithms for graph manipulation. Communications of the ACM, 16(6), 372–378. doi:10.1145/362248.362272
df <- filterGraph(phyActFlowPropensity[,c(paste0('pa',1:2),'predict')]) head(df)df <- filterGraph(phyActFlowPropensity[,c(paste0('pa',1:2),'predict')]) head(df)
This is a convenience function to help you look up the path to an appropriate model for your data.
findModel(model = NULL)findModel(model = NULL)
model |
the name of a model |
There are essentially three models: ‘unidim’, ‘covariance’, and ‘factor’. ‘unidim’ analyzes a single item. ‘covariance’ is suitable for two or more items. Once you have vetted your items with the ‘unidim’ and ‘covariance’ models, then you can try the ‘factor’ model. For each model, there is a ‘_ll’ variation. This model includes row-wise log likelihoods suitable for feeding to loo for efficient approximate leave-one-out cross-validation (Vehtari, Gelman, & Gabry, 2017).
There is also a special model ‘unidim_adapt’. Except for this model, the other models require a scaling constant. To find an appropriate scaling constant, we recommend fitting ‘unidim_adapt’ to each item separately and then take the median of median point estimates to set the scale. ‘unidim_adapt’ requires a varCorrection constant. In general, a varCorrection of 2.0 or 3.0 should provide optimal results.
Since version 1.1.0, the factor model permits an arbitrary number of factors and arbitrary factor-to-item paths. If you were using the old factor model, you'll need to update your code to call prepSingleFactorModel. Arbitrary factor model structure should be specified using prepFactorModel. The single factor model is called ‘factor1’ and the general factor model is called ‘factor’.
An instance of S4 class stanmodel that can be passed to pcStan.
Vehtari A, Gelman A, Gabry J (2017). "Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC." Statistics and Computing, 27, 1413-1432. doi: 10.1007/s11222-016-9696-4
findModel() # shows available models findModel('unidim')findModel() # shows available models findModel('unidim')
If you need access to the correlation matrix used to generate the absolute latent scores then you will need to generate them yourself. This is not difficult. See how in the example.
generateCovItems(df, numItems, th = 0.5, name, ..., scale = 1, alpha = 1)generateCovItems(df, numItems, th = 0.5, name, ..., scale = 1, alpha = 1)
df |
a data frame with pairs of vertices given in columns |
numItems |
how many items to create |
th |
a vector of thresholds |
name |
a vector of item names |
... |
Not used. Forces remaining arguments to be specified by name. |
scale |
a vector of scaling constants |
alpha |
a vector of item discriminations |
The given data.frame df with additional columns for each item.
In addition, you can obtain the correlation matrix used
to generate the latent worths from attr(df, "cor") and
and latent worths from attr(df, "worth").
See cmp_probs for details.
Other item generators:
generateFactorItems(),
generateItem(),
generateSingleFactorItems()
library(mvtnorm) df <- twoLevelGraph(letters[1:10], 100) df <- generateCovItems(df, 3) # generateCovItems essentially does the same thing as: numItems <- 3 palist <- letters[1:10] trueCor <- cov2cor(rWishart(1, numItems, diag(numItems))[,,1]) theta <- rmvnorm(length(palist), sigma=trueCor) dimnames(theta) <- list(palist, paste0('i', 3 + 1:numItems)) df <- generateItem(df, theta) attr(df, "cor")library(mvtnorm) df <- twoLevelGraph(letters[1:10], 100) df <- generateCovItems(df, 3) # generateCovItems essentially does the same thing as: numItems <- 3 palist <- letters[1:10] trueCor <- cov2cor(rWishart(1, numItems, diag(numItems))[,,1]) theta <- rmvnorm(length(palist), sigma=trueCor) dimnames(theta) <- list(palist, paste0('i', 3 + 1:numItems)) df <- generateItem(df, theta) attr(df, "cor")
Generate paired comparison data given a mapping from factors to items.
generateFactorItems( df, path, factorScalePrior = deprecated(), th = 0.5, name, ..., scale = 1, alpha = 1 )generateFactorItems( df, path, factorScalePrior = deprecated(), th = 0.5, name, ..., scale = 1, alpha = 1 )
df |
a data frame with pairs of vertices given in columns |
path |
a named list of item names |
factorScalePrior |
a named numeric vector (deprecated) |
th |
a vector of thresholds |
name |
a vector of item names |
... |
Not used. Forces remaining arguments to be specified by name. |
scale |
a vector of scaling constants |
alpha |
a vector of item discriminations |
For each factor, you need to specify its name and which items it predicts. The connections from factors to items is specified by the 'path' argument. Both factors and items are specified by name (not index).
Path proportions (factor-to-item loadings) are sampled from a logistic transformed normal distribution with scale 0.6. A few attempts are made to resample path proportions if any of the item proportions sum to more than 1.0. An exception will be raised if repeated attempts fail to produce viable proportion assignments.
The given data.frame df with additional columns for each item.
In addition, you can obtain path proportions (factor-to-item loadings) from attr(df, "pathProp"),
the factor scores from attr(df, "score"),
and latent worths from attr(df, "worth").
See cmp_probs for details.
The function generateFactorItems was renamed to
generateSingleFactorItems (version 1.1.0) to make space for a more
flexible factor model with an arbitrary number of factors and
arbitrary factor-to-item loading pattern. If you don't need this
flexibility, you can call the old function generateSingleFactorItems.
Silver, D., Hubert, T., Schrittwieser, J., Antonoglou, I., Lai, M., Guez, A., ... & Lillicrap, T. (2018). A general reinforcement learning algorithm that masters chess, shogi, and Go through self-play. Science, 362(6419), 1140-1144.
To fit a factor model: prepFactorModel
Other item generators:
generateCovItems(),
generateItem(),
generateSingleFactorItems()
df <- twoLevelGraph(letters[1:10], 100) df <- generateFactorItems(df, list(f1=paste0('i',1:4), f2=paste0('i',2:4)), c(f1=0.9, f2=0.5)) head(df) attr(df, "pathProp") attr(df, "score") attr(df, "worth")df <- twoLevelGraph(letters[1:10], 100) df <- generateFactorItems(df, list(f1=paste0('i',1:4), f2=paste0('i',2:4)), c(f1=0.9, f2=0.5)) head(df) attr(df, "pathProp") attr(df, "score") attr(df, "worth")
To add a single item, theta should be a vector of latent
scores. To add multiple items at a time, theta should be a
matrix with one item in each column. Item names can be given as
the colnames of theta.
The interpretation of theta depends on the context where the
data were generated. For example, in chess, theta represents
unobserved chess skill that is partially revealed by match
outcomes.
The graph can be regarded as undirected, but data are generated
relative to the order of vertices within each row. Vertices do not
commute. For example, a -1 for vertices ‘a’ and
‘b’ is the same as 1 for vertices ‘b’ and
‘a’.
generateItem(df, theta, th = 0.5, name, ..., scale = 1, alpha = 1)generateItem(df, theta, th = 0.5, name, ..., scale = 1, alpha = 1)
df |
a data frame with pairs of vertices given in columns |
theta |
a vector or matrix of absolute latent scores. See details below. |
th |
a vector of thresholds |
name |
a vector of item names |
... |
Not used. Forces remaining arguments to be specified by name. |
scale |
a vector of scaling constants |
alpha |
a vector of item discriminations |
The given data.frame df with additional columns for each item.
See cmp_probs for details.
Other item generators:
generateCovItems(),
generateFactorItems(),
generateSingleFactorItems()
df <- roundRobinGraph(letters[1:5], 40) df <- generateItem(df)df <- roundRobinGraph(letters[1:5], 40) df <- generateItem(df)
Imagine that there are people that play in tournaments of more than one board game. For example, the computer player AlphaZero (Silver et al. 2018) has trained to play chess, shogi, and Go. We can take the tournament match outcome data and find rankings among the players for each of these games. We may also suspect that there is a latent board game skill that accounts for some proportion of the variance in the per-board game rankings.
generateSingleFactorItems(df, prop, th = 0.5, name, ..., scale = 1, alpha = 1)generateSingleFactorItems(df, prop, th = 0.5, name, ..., scale = 1, alpha = 1)
df |
a data frame with pairs of vertices given in columns |
prop |
the number of items or a vector of signed proportions of variance |
th |
a vector of thresholds |
name |
a vector of item names |
... |
Not used. Forces remaining arguments to be specified by name. |
scale |
a vector of scaling constants |
alpha |
a vector of item discriminations |
The given data.frame df with additional columns for each item.
See cmp_probs for details.
The function generateFactorItems was renamed to
generateSingleFactorItems (version 1.1.0) to make space for a more
flexible factor model with an arbitrary number of factors and
arbitrary factor-to-item loading pattern. If you don't need this
flexibility, you can call the old function generateSingleFactorItems.
Silver, D., Hubert, T., Schrittwieser, J., Antonoglou, I., Lai, M., Guez, A., ... & Lillicrap, T. (2018). A general reinforcement learning algorithm that masters chess, shogi, and Go through self-play. Science, 362(6419), 1140-1144.
Other item generators:
generateCovItems(),
generateFactorItems(),
generateItem()
df <- twoLevelGraph(letters[1:10], 100) df <- generateSingleFactorItems(df, 3)df <- twoLevelGraph(letters[1:10], 100) df <- generateSingleFactorItems(df, 3)
When data dl and fitted model fit are provided, the
item parameters associated with item are loaded for
inspection.
itemModelExplorer(dl = NULL, fit = NULL, item = NULL)itemModelExplorer(dl = NULL, fit = NULL, item = NULL)
dl |
a data list prepared by |
fit |
a |
item |
name of the item to visualize |
See cmp_probs for details.
itemModelExplorer() # will launch a browser in RStudioitemModelExplorer() # will launch a browser in RStudio
Pairwise comparison data are not commutative.
Alice beating Bob in chess is equivalent to Bob losing to
Alice. normalizeData assigns an arbitrary order to all
vertices and reorders vertices column-wise to match,
flipping signs as needed.
normalizeData(df, ..., .palist = NULL, .sortRows = TRUE)normalizeData(df, ..., .palist = NULL, .sortRows = TRUE)
df |
a data frame with pairs of vertices given in columns |
... |
Not used. Forces remaining arguments to be specified by name. |
.palist |
a character vector giving an order to use instead of the default |
.sortRows |
logical. Using the same order, sort rows in addition to vertex pairs. |
df <- data.frame(pa1=NA, pa2=NA, i1=c(1, -1)) df[1,paste0('pa',1:2)] <- c('a','b') df[2,paste0('pa',1:2)] <- c('b','a') normalizeData(df)df <- data.frame(pa1=NA, pa2=NA, i1=c(1, -1)) df[1,paste0('pa',1:2)] <- c('a','b') df[2,paste0('pa',1:2)] <- c('b','a') normalizeData(df)
The function prepCleanData compresses observations
into the most efficient format for evaluation by Stan. This function
maps indices of observations back to the actual observations,
filtering by the largest Pareto k values. It is assumed that
data was processed by normalizeData or is in
the same order as seen by prepCleanData.
outlierTable(data, x, threshold = 0.5)outlierTable(data, x, threshold = 0.5)
data |
a data list prepared for processing by Stan |
x |
An object created by |
threshold |
threshold is the minimum k value to include |
A data.frame (one row per observation) with the following columns:
Name of object 1
Name of object 2
Name of item
Observed response
Associated Pareto k value
palist <- letters[1:10] df <- twoLevelGraph(palist, 300) theta <- rnorm(length(palist)) names(theta) <- palist df <- generateItem(df, theta, th=rep(0.5, 4)) df <- filterGraph(df) df <- normalizeData(df) dl <- prepCleanData(df) dl$scale <- 1.5 m1 <- pcStan("unidim_ll", dl) loo1 <- toLoo(m1, cores=1) ot <- outlierTable(dl, loo1, threshold=.2) df[df$pa1==ot[1,'pa1'] & df$pa2==ot[1,'pa2'], 'i1']palist <- letters[1:10] df <- twoLevelGraph(palist, 300) theta <- rnorm(length(palist)) names(theta) <- palist df <- generateItem(df, theta, th=rep(0.5, 4)) df <- filterGraph(df) df <- normalizeData(df) dl <- prepCleanData(df) dl$scale <- 1.5 m1 <- pcStan("unidim_ll", dl) loo1 <- toLoo(m1, cores=1) ot <- outlierTable(dl, loo1, threshold=.2) df[df$pa1==ot[1,'pa1'] & df$pa2==ot[1,'pa2'], 'i1']
Produce data suitable for plotting parameter distributions
parDistributionCustom( fit, pars, nameVec, label = withoutIndex(pars[1]), samples = 500 ) parDistributionFor(fit, pi, samples = 500)parDistributionCustom( fit, pars, nameVec, label = withoutIndex(pars[1]), samples = 500 ) parDistributionFor(fit, pi, samples = 500)
fit |
a |
pars |
a vector of parameter names |
nameVec |
a vector of explanatory parameters names |
label |
column name for |
samples |
number of posterior samples |
pi |
a data.frame returned by |
A data.frame with the following columns:
Sample index
A name from nameVec
A single sample of the associated parameter
Other data extractor:
parInterval(),
responseCurve()
vignette('manual', 'pcFactorStan')vignette('manual', 'pcFactorStan')
Produce data suitable for plotting parameter estimates
parInterval(fit, pars, nameVec, label = withoutIndex(pars[1]), width = 0.8)parInterval(fit, pars, nameVec, label = withoutIndex(pars[1]), width = 0.8)
fit |
a |
pars |
a vector of parameter names |
nameVec |
a vector of explanatory parameters names |
label |
column name for |
width |
a width in probability units for the uncertainty interval |
A data.frame with the following columns:
Lower quantile
Median
Upper quantile
nameVec
Other data extractor:
parDistributionCustom(),
responseCurve()
vignette('manual', 'pcFactorStan')vignette('manual', 'pcFactorStan')
Uses findModel to find the appropriate
model and then invokes sampling.
pcStan(model, data, ...)pcStan(model, data, ...)
model |
the name of a model |
data |
a data list prepared for processing by Stan |
... |
Additional options passed to |
A stanfit object.
An object of S4 class stanfit.
See sampling, for which this function is
a wrapper, for additional options. See prepData to
create a suitable data list. See
print.stanfit for ways of getting tables
summarizing parameter posteriors.
dl <- prepData(phyActFlowPropensity[,c(1,2,3)]) dl$varCorrection <- 5.0 pcStan('unidim_adapt', data=dl) # takes more than 5 secondsdl <- prepData(phyActFlowPropensity[,c(1,2,3)]) dl$varCorrection <- 5.0 pcStan('unidim_adapt', data=dl) # takes more than 5 seconds
A dataset containing paired comparisons of 87 physical activities on 16 flow-related facets. Participants submitted two activities using free-form input. These activities were substitute into item templates. For example, the ‘predict’ item asked, “How predictable is the action?” with response options:
A1 is much more predictable than A2.
A1 is somewhat more predictable than A2.
Both offer roughly equal predictability.
A2 is somewhat more predictable than A1.
A2 is much more predictable than A1.
Most items were adapted from Jackson & Eklund (2002).
phyActFlowPropensityphyActFlowPropensity
A data.frame with one row per activity comparison and items in the columns. All item responses are between -2 and 2. Zero indicates that both activities were judged equal on the trait.
A manuscript fully describing the study is in preparation. Data are made available under the Community Data License Agreement - Sharing - Version 1.0
Jackson, S. A., & Eklund, R. C. (2002). Assessing flow in physical activity: The flow state scale-2 and dispositional flow scale-2. Journal of Sport and Exercise Psychology, 24(2), 133-150. doi:10.1123/jsep.24.2.133
Vertex names, if not already factors, are converted to factors. The number of thresholds per item is determined by the largest absolute response value. Missing responses are filtered out. Responses on the same pair of vertices on the same item are grouped together. Within a vertex pair and item, responses are ordered from negative to positive.
prepCleanData(df)prepCleanData(df)
df |
a data frame with pairs of vertices given in columns |
Note: Reordering of responses is likely unless something like
normalizeData has been used with .sortRows=TRUE.
a data list suitable for passing as the data
argument to pcStan or stan
Other data preppers:
prepData(),
prepFactorModel(),
prepSingleFactorModel()
df <- prepCleanData(phyActFlowPropensity) str(df)df <- prepCleanData(phyActFlowPropensity) str(df)
Invokes filterGraph and normalizeData.
Vertex names, if not already factors, are converted to
factors. The number of thresholds per item is determined by the
largest absolute response value. Missing responses are filtered
out. Responses on the same pair of vertices on the same item are
grouped together. Within a vertex pair and item, responses
are ordered from negative to positive.
prepData(df)prepData(df)
df |
a data frame with pairs of vertices given in columns |
a data list suitable for passing as the data
argument to pcStan or stan
Other data preppers:
prepCleanData(),
prepFactorModel(),
prepSingleFactorModel()
df <- prepData(phyActFlowPropensity) str(df)df <- prepData(phyActFlowPropensity) str(df)
Specify a factor model with an arbitrary number of factors and arbitrary factor-to-item structure.
prepFactorModel( data, path, factorScalePrior = deprecated(), psiScalePrior = deprecated() )prepFactorModel( data, path, factorScalePrior = deprecated(), psiScalePrior = deprecated() )
data |
a data list prepared for processing by Stan |
path |
a named list of item names |
factorScalePrior |
a named numeric vector (deprecated) |
psiScalePrior |
matrix of priors for factor correlations (deprecated) |
For each factor, you need to specify its name and which items it predicts. The connections from factors to items is specified by the 'path' argument. Both factors and items are specified by name (not index).
a data list suitable for passing as the data
argument to pcStan or stan
To simulate data from a factor model: generateFactorItems
Other factor model:
prepSingleFactorModel()
Other data preppers:
prepCleanData(),
prepData(),
prepSingleFactorModel()
pa <- phyActFlowPropensity[,setdiff(colnames(phyActFlowPropensity), c('goal1','feedback1'))] dl <- prepData(pa) dl <- prepFactorModel(dl, list(flow=c('complex','skill','predict', 'creative', 'novelty', 'stakes', 'present', 'reward', 'chatter', 'body'), f2=c('waiting','control','evaluated','spont'), rc=c('novelty', 'waiting'))) str(dl)pa <- phyActFlowPropensity[,setdiff(colnames(phyActFlowPropensity), c('goal1','feedback1'))] dl <- prepData(pa) dl <- prepFactorModel(dl, list(flow=c('complex','skill','predict', 'creative', 'novelty', 'stakes', 'present', 'reward', 'chatter', 'body'), f2=c('waiting','control','evaluated','spont'), rc=c('novelty', 'waiting'))) str(dl)
Specify a single latent factor with a path to each item.
prepSingleFactorModel(data, factorScalePrior = deprecated())prepSingleFactorModel(data, factorScalePrior = deprecated())
data |
a data list prepared for processing by Stan |
factorScalePrior |
a named numeric vector (deprecated) |
a data list suitable for passing as the data
argument to pcStan or stan
Other factor model:
prepFactorModel()
Other data preppers:
prepCleanData(),
prepData(),
prepFactorModel()
dl <- prepData(phyActFlowPropensity) dl <- prepSingleFactorModel(dl) str(dl)dl <- prepData(phyActFlowPropensity) dl <- prepSingleFactorModel(dl) str(dl)
Selects samples random draws from the posterior and evaluates the item
response curve on the grid given by seq(from,to,by).
All items use the same responseNames. If you have some items
with a different number of thresholds or different response names
then you can call responseCurve for each item separately
and rbind the results together.
responseCurve( dl, fit, responseNames, item = dl$nameInfo$item, samples = 100, from = qnorm(0.1), to = -from, by = 0.02 )responseCurve( dl, fit, responseNames, item = dl$nameInfo$item, samples = 100, from = qnorm(0.1), to = -from, by = 0.02 )
dl |
a data list prepared by |
fit |
a |
responseNames |
a vector of labels for the possible responses |
item |
a vector of item names |
samples |
number of posterior samples |
from |
the starting latent difference value |
to |
the ending latent difference value |
by |
the grid increment |
A data.frame with the following columns:
Which response
Difference in worth
Which item
Which sample
Associated probability
A grouping index for independent item response samples
See cmp_probs for details.
Other data extractor:
parDistributionCustom(),
parInterval()
vignette('manual', 'pcFactorStan')vignette('manual', 'pcFactorStan')
Create an edge list with round-robin connectivity
roundRobinGraph(name, N)roundRobinGraph(name, N)
name |
vector of vertex names |
N |
number of comparisons |
An undirected graph represented as a data frame with each row describing an edge.
Other graph generators:
twoLevelGraph()
roundRobinGraph(letters[1:5], 10)roundRobinGraph(letters[1:5], 10)
You must use an ‘_ll’ model variation (see findModel).
toLoo(fit, ...)toLoo(fit, ...)
fit |
a |
... |
Additional options passed to |
a loo object
palist <- letters[1:10] df <- twoLevelGraph(palist, 300) theta <- rnorm(length(palist)) names(theta) <- palist df <- generateItem(df, theta, th=rep(0.5, 4)) df <- filterGraph(df) df <- normalizeData(df) dl <- prepCleanData(df) dl$scale <- 1.5 m1 <- pcStan("unidim_ll", dl) loo1 <- toLoo(m1, cores=1) print(loo1)palist <- letters[1:10] df <- twoLevelGraph(palist, 300) theta <- rnorm(length(palist)) names(theta) <- palist df <- generateItem(df, theta, th=rep(0.5, 4)) df <- filterGraph(df) df <- normalizeData(df) dl <- prepCleanData(df) dl$scale <- 1.5 m1 <- pcStan("unidim_ll", dl) loo1 <- toLoo(m1, cores=1) print(loo1)
Initially, edges are added from the first vertex to all the other
vertices. Thereafter, the first vertex is drawn from a Beta(shape1,
1.0) distribution and the second vertex is drawn from a
Beta(shape2, 1.0) distribution. The idea is that the edges will
tend to connect a small subset of vertices from the top of the tree
to leaf vertices. These vertex connections are similar to the pairs
that you might observe in an elimination tournament. The selected
vertices are sorted so it doesn't matter whether shape1 >
shape2 or shape1 < shape2.
twoLevelGraph(name, N, shape1 = 0.8, shape2 = 0.5)twoLevelGraph(name, N, shape1 = 0.8, shape2 = 0.5)
name |
vector of vertex names |
N |
number of comparisons |
shape1 |
beta distribution parameter for first edge |
shape2 |
beta distribution parameter for second edge |
An undirected graph represented as a data frame with each row describing an edge.
Other graph generators:
roundRobinGraph()
twoLevelGraph(letters[1:5], 20)twoLevelGraph(letters[1:5], 20)
Factors store values as integers and use a 'levels' attribute to map the integers to labels. This function removes the 'factor' class and levels attribute, leaving the vector of integers.
unfactor(f)unfactor(f)
f |
a factor |
f <- factor(letters[1:3]) print(f) print(unfactor(f))f <- factor(letters[1:3]) print(f) print(unfactor(f))
Remove the array indexing from a parameter name
withoutIndex(name)withoutIndex(name)
name |
a parameter name |
the name without the square bracket parameter indexing
withoutIndex("foo[1,2]")withoutIndex("foo[1,2]")