R Psychologist’s guide to multilevel models in R is stupendous.
seen from United States

seen from Poland

seen from Ireland

seen from United Kingdom
seen from Qatar
seen from Russia

seen from Germany
seen from Malaysia
seen from Ireland
seen from United States
seen from United States
seen from Australia
seen from T1

seen from Ireland

seen from United Kingdom

seen from United Kingdom
seen from China
seen from United States
seen from Türkiye

seen from United States
R Psychologist’s guide to multilevel models in R is stupendous.
Looking for information on Generalized Linear Mixed Models (GLMMs)?? I have spent the last few days working with with GLMMs and like most people find model selection to be a challenge. This FAQ sheet provides a banana truck load of great information about model selection, p-value calculation, degrees of freedom, model syntax and tons more... Make sure to check it out!
Tutz Coding: Coding a polytomous variable for a continuation ratio model
Analyzing polytomous variables in Item Response Modeling relies in the recoding of those variables into a set of dichotomous variables in order to run a multinomial logistic regression model. This dichotomization can be of four types: (1) baseline-category, (2) adjacent-category, (3) cumulative and, (4) continuation-ratio.
Producing the recoding for the first three kinds is fairly straightforward, but I had a hard time finding a function to create the last one; for that reason I worked on a function (with the help of Ronli Diakow) to make this conversion easier.
The function:
This function takes a dataframe in long format, the name of the column that contains the item names and finally, the name of the column that contains the responses.
From this information, the function will expand the data in order to make the polytomous variables into sets of dichotomous answers.
A couple of caveats about this function:
The function requires the reshape package.
Notice that the function requires three parameters:
The name of the data frame.
The name of the column that contains the item names.
The name of the column that contains the responses.
IMPORTANT: The name of the columns (second and third parameters) must be in quotes.
The code:
TutzCoding <- function( dataMat, itemColumn, respColumn){ require(reshape) cats <- length( unique( dataMat[,respColumn])) TutzMat <- matrix(rep(0, cats*cats), ncol = cats) diag(TutzMat) <- 1 TutzMat[lower.tri(TutzMat)] <- NA TutzMat <- as.data.frame(TutzMat) names(TutzMat) <- rev(paste(rep('C',cats),letters[1:cats],sep='')) TutzMat <- TutzMat[,-1] TutzVars <- names(TutzMat) TutzMat <- t(apply(TutzMat,1,rev)) print('Tutz Coding Matrix') print(TutzMat) TempMat <- with(dataMat,TutzMat[dataMat[,respColumn],]) TempMat2 <- cbind(dataMat,TempMat) Output <- melt(TempMat2, measure.vars = TutzVars) Output$newitem <- interaction(Output[,itemColumn], Output[,"variable"]) colnames(Output)[which(colnames(Output) == 'variable')] <- 'category' colnames(Output)[which(colnames(Output) == 'value')] <- 'tutz' return(Output) }
An Example:
This example uses the verbal aggression dataset available in the lme4 package. You can use this coding function to run polytomous models using the lmer function provided in this package. This first example creates dichotomous variables from a 3 category polytomous variable:
library(lme4) NewData <- TutzCoding(VerbAgg, "item", "resp") str(NewData) head(NewData)
And here is another example illustrating that it also works for 5 categories:
sample <- cbind(c('a','b','c','d','e'),c('i1','i2','i3','i4','i5'),c(1,2,3,4,5)) sample[,3] <- as.numeric(sample[,3]) sample <- as.data.frame(sample) sample[,3] <- as.numeric(as.character(sample[,3])) names(sample) <- c('idvar','item','respvar') FiveCat <- TutzCoding(sample, "item", "respvar") str(FiveCat) head(FiveCat)
Fully Bayesian mixed effects models with the familiar lme4 syntax.
Possibly slightly better text analysis with lme4
lme4 and its cousin arm are extremely useful for a huge variety of modeling applications (see Gelman and Hill's book), but today we're going to do something a little frivolous with them. Namely, we're going to extend our Denver Debate analysis to include some sense of error.
Instead of the term-frequency scatter plot seen in the previous post, this code fits the most basic possible partially-pooled model predicting which of the two candidates, Obama or Romney, spoke a given term. This allows us to get a slightly better idea of which candidate "owned" a term on the night, and simultaneously accounts for volume of usage (evidenced by narrower confidence intervals).
Anyway, we will almost certainly return to lmer() at some point in the future, but this code offers some ideas as to how best translate a model object into a data frame amenable to plotting.
https://gist.github.com/4260706