R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("BradleyTerry-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('BradleyTerry') Loading required package: brlr > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "BTabilities" > > ### * BTabilities > > flush(stderr()); flush(stdout()) > > ### Name: BTabilities > ### Title: estimated abilities in a Bradley-Terry model > ### Aliases: BTabilities > ### Keywords: models > > ### ** Examples > > data(citations) > origin <- factor(c("UK", "USA", "USA", "UK")) > citeModel2 <- BTm(citations ~ origin) > BTabilities(citeModel2) ability s.e. Biometrika 0.0000 0.00000000 Comm.Statist -1.2732 0.04999872 JASA -1.2732 0.04999872 JRSS.B 0.0000 0.00000000 > > > > cleanEx(); ..nameEx <- "BTm" > > ### * BTm > > flush(stderr()); flush(stdout()) > > ### Name: BTm > ### Title: Bradley-Terry model and extensions > ### Aliases: BTm drop1.BTm add1.BTm terms.BTm formula.BTm > ### Keywords: models > > ### ** Examples > > ## > ## Statistics journal citation data from Stigler (1994) > ## -- see also Agresti (2002, p448) > data(citations) > > ## First fit the "standard" Bradley-Terry model > print(citeModel <- BTm(citations ~ ..)) Call: BTm(formula = citations ~ ..) Coefficients: ..Comm.Statist ..JASA ..JRSS.B -2.9491 -0.4796 0.2690 Degrees of Freedom: 6 Total (i.e. Null); 3 Residual Null Deviance: 1925 Residual Deviance: 4.293 AIC: 46.39 > > ## Now the same thing with a different "reference" journal > update(citeModel, . ~ ., refcat = "JASA") Call: BTm(formula = citations ~ .., refcat = "JASA") Coefficients: ..Biometrika ..Comm.Statist ..JRSS.B 0.4796 -2.4695 0.7485 Degrees of Freedom: 6 Total (i.e. Null); 3 Residual Null Deviance: 1925 Residual Deviance: 4.293 AIC: 46.39 > > ## Is the "citeability" of a journal predicted by its country of origin? > origin <- factor(c("UK", "USA", "USA", "UK")) > print(citeModel2 <- BTm(citations ~ origin)) Call: BTm(formula = citations ~ origin) Coefficients: originUSA -1.273 Degrees of Freedom: 6 Total (i.e. Null); 5 Residual Null Deviance: 1925 Residual Deviance: 1139 AIC: 1177 > > ## Hmm... not so sure about the origin of "Comm Statist" ... > is.na(origin[2]) <- TRUE > citeModel2 <- update(citeModel2, . ~ .) > > ## Now an example with an order effect -- see Agresti (2002) p438 > data(baseball) > > ## Simple Bradley-Terry model as in Agresti p437 > print(baseballModel <- BTm(baseball ~ ..)) Call: BTm(formula = baseball ~ ..) Coefficients: ..Boston ..Cleveland ..Detroit ..Milwaukee ..New.York ..Toronto 1.1077 0.6839 1.4364 1.5814 1.2476 1.2945 Degrees of Freedom: 21 Total (i.e. Null); 15 Residual Null Deviance: 49.7 Residual Deviance: 15.74 AIC: 87.32 > > ## Introduce order effect as in Agresti p438 > update(baseballModel, order.effect = baseball$home.adv) Call: BTm(formula = baseball ~ .., order.effect = baseball$home.adv) Coefficients: ..Boston ..Cleveland ..Detroit ..Milwaukee ..New.York ..Toronto 1.1438 0.7047 1.4754 1.6196 1.2813 1.3271 .order 0.3023 Degrees of Freedom: 42 Total (i.e. Null); 35 Residual Null Deviance: 78.02 Residual Deviance: 38.64 AIC: 137.1 > > > > > cleanEx(); ..nameEx <- "BTresiduals" > > ### * BTresiduals > > flush(stderr()); flush(stdout()) > > ### Name: BTresiduals > ### Title: player-specific residuals from a Bradley-Terry model > ### Aliases: BTresiduals > ### Keywords: models > > ### ** Examples > > data(citations) > origin <- factor(c("UK", "USA", "USA", "UK")) > citeModel2 <- BTm(citations ~ origin) > BTresiduals(citeModel2) Biometrika Comm.Statist JASA JRSS.B -0.09767683 -1.38124450 1.25835736 0.15117998 attr(,"weights") Biometrika Comm.Statist JASA JRSS.B 396.4048 400.6950 439.8255 256.1157 > > > > cleanEx(); ..nameEx <- "baseball" > > ### * baseball > > flush(stderr()); flush(stdout()) > > ### Name: baseball > ### Title: Baseball data from Agresti (2002) > ### Aliases: baseball > ### Keywords: datasets > > ### ** Examples > > data(baseball) > ## The data in collapsed tabular form as on p438 of Agresti > xtabs(Freq ~ winner + loser, baseball) loser winner Milwaukee Detroit Toronto New York Boston Cleveland Baltimore Milwaukee 0 7 9 7 7 9 11 Detroit 6 0 7 5 11 9 9 Toronto 4 6 0 7 7 8 12 New York 6 8 6 0 6 7 10 Boston 6 2 6 7 0 7 12 Cleveland 4 4 5 6 6 0 6 Baltimore 2 4 1 3 1 7 0 > ## Simple Bradley-Terry model as in Agresti p437 > print(baseballModel <- BTm(baseball ~ ..)) Call: BTm(formula = baseball ~ ..) Coefficients: ..Boston ..Cleveland ..Detroit ..Milwaukee ..New.York ..Toronto 1.1077 0.6839 1.4364 1.5814 1.2476 1.2945 Degrees of Freedom: 21 Total (i.e. Null); 15 Residual Null Deviance: 49.7 Residual Deviance: 15.74 AIC: 87.32 > ## Introduce order effect as in Agresti p438 > update(baseballModel, order.effect = baseball$home.adv) Call: BTm(formula = baseball ~ .., order.effect = baseball$home.adv) Coefficients: ..Boston ..Cleveland ..Detroit ..Milwaukee ..New.York ..Toronto 1.1438 0.7047 1.4754 1.6196 1.2813 1.3271 .order 0.3023 Degrees of Freedom: 42 Total (i.e. Null); 35 Residual Null Deviance: 78.02 Residual Deviance: 38.64 AIC: 137.1 > > > > cleanEx(); ..nameEx <- "citations" > > ### * citations > > flush(stderr()); flush(stdout()) > > ### Name: citations > ### Title: Statistics journal citation data from Stigler (1994) > ### Aliases: citations > ### Keywords: datasets > > ### ** Examples > > data(citations) > ## Data as a square table, as in Agresti p448 > xtabs(Freq ~ ., citations) loser winner Biometrika Comm Statist JASA JRSS-B Biometrika 0 730 498 221 Comm Statist 33 0 68 17 JASA 320 813 0 142 JRSS-B 284 276 325 0 > ## Standard Bradley-Terry model fitted to these data > BTm(citations ~ ..) Call: BTm(formula = citations ~ ..) Coefficients: ..Comm.Statist ..JASA ..JRSS.B -2.9491 -0.4796 0.2690 Degrees of Freedom: 6 Total (i.e. Null); 3 Residual Null Deviance: 1925 Residual Deviance: 4.293 AIC: 46.39 > > > > ### *