r - Calculate BradleyTerry Ability rankings -
i'm trying output estimates of ability bradley terry model using bradleyterry2 package in r. keep getting cryptic error. of examples documentation work , others return same error using data. code uses 1 of example analysis documentation. if load library "chameleon" data should there
install.packages("bradleyterry2") library (bradleyterry2) summary(chameleon.model <- btm(player1 = winner, player2 = loser,formula = ~ prev.wins.2 + ch.res[id] + prop.main[id] + (1|id), id = "id",data = chameleons)) btabilities(chameleon.model)
and error
error in x[, est, drop = false] : (subscript) logical subscript long
anyone know how this?
i maintain bradleyterry2
. bug occurs when have contest-specific predictor in ability formula. should work documented in ?btabilities
:
... abilities computed terms of fitted model involve player covariates (those indexed ‘model$id’ in model formula). parameters in other terms assumed zero.
we weren't aware wasn't working, bug report. until fixed, can work out abilities , standard errors directly:
## names of relevant coefficients > nm <- grep("[id]", names(coef(chameleon.model)), > fixed = true, value = true) > nm [1] "ch.res[id]" "prop.main[id]" ## names of corresponding predictors > idvar <- gsub("[id]", "", nm, fixed = true) > idvar [1] "ch.res" "prop.main" ## create coefficient vector , design matrix > cf <- coef(chameleon.model)[nm] > x <- as.matrix(chameleons$predictors[, idvar]) ## compute abilities > abilities <- x %*% cf > colnames(abilities) <- "abilities" ## compute standard errors > v <- vcov(chameleon.model)[nm, nm] > res <- cbind(abilities = abilities, > se = sqrt(diag(x %*% v %*% t(x)))) > head(res) abilities se c01 3.757229 1.655205 c02 2.404778 1.017782 c03 2.319346 1.133959 c04 1.892671 1.399391 c05 2.253472 1.101628 c06 2.015840 1.075806
this give na
individuals missing values in predictors, ability modelled separately , returned in model summary. above code assumes continuous covariates, if of predictors factors, need create model matrix more formally, e.g.
x <- model.matrix(reformulate(idvar, intercept = false), c(chameleons$predictors))
Comments
Post a Comment