BradleyTerry2包中的Predictor变量问题

时间:2016-08-19 09:00:09

标签: r

我正在尝试使用R.3.3.1中的BradleyTerry2软件包在我的数据分析中包含特定于竞争的变量(我还尝试使用R.2.11.1与旧版本的BradleyTerry2进行比较)。我面临的问题是没有正确考虑我的预测变量。下面的例子显示了我的问题,使用CEMS数据来说明我的观点。

    CEMS.BTmodel_01 <- BTm(outcome = cbind(win1.adj, win2.adj),
        player1 = school1, 
        player2 = school2, 
        formula = ~ .. + WOR[student] * LAT[..], 
        refcat = "Stockholm", 
        data = CEMS)
    summary(CEMS.BTmodel_01)

使用这个模型,我们得到一个AIC = 5837.4,估计到LAT的相互作用[..] * WOR [学生] = 0.85771

现在,如果我在列表顶部添加一所新学校(图卢兹,LAT = 1)

    Toulouse <- c(1,0,0,0,0,0,0)
    Barcelona <- c(0,1,0,0,0,0,0)
    London <- c(0,0,1,0,0,0,0)
    Milano <- c(0,0,0,1,0,0,0)
    Paris <- c(0,0,0,0,1,0,0)
    St.Gallen <- c(0,0,0,0,0,1,0)
    Stockholm <- c(0,0,0,0,0,0,1)
    LAT <- c(1,1,0,1,1,0,0)
    schools <- data.frame(Toulouse, Barcelona, London, Milano, Paris, St.Gallen, Stockholm, LAT)
    rownames(schools) <- c("Toulouse", "Barcelona", "London", "Milano", "Paris", "St.Gallen", "Stockholm")
    CEMS$schools <- schools

我希望从分析中得到相同的结果,因为新学校没有出现在数据集中。但我实际得到AIC = 5855.8,互动LAT [..] * WOR [学生] = 0.13199

使用数据,看起来我的预测变量的名称(这里是学校的名称)没有被正确考虑并与我的比较数据(这里是欧洲学生的成对比较)相匹配。相反,重要的是他们的订单。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

CEMS$schools行应符合school1school2因素的级别(CEMS$schools的rownames实际上未在代码中使用;第一行应该匹配第一级等)。因此,您需要更新school1school2

的级别
CEMS$preferences <-
within(CEMS$preferences, {
    school1 <- factor(school1, rownames(CEMS$schools))
    school2 <- factor(school2, rownames(CEMS$schools))
    })

CEMS.BTmodel_02 <- BTm(outcome = cbind(win1.adj, win2.adj),
                   player1 = school1, 
                   player2 = school2, 
                   formula = ~ .. + WOR[student] * LAT[..], 
                   refcat = "Stockholm", 
                   data = CEMS)

现在模型与预期相同:

> CEMS.BTmodel_01
Bradley Terry model fit by glm.fit 

Call:  BTm(outcome = cbind(win1.adj, win2.adj), player1 = school1, player2 = school2, 
    formula = ~.. + WOR[student] * LAT[..], refcat = "Stockholm", 
    data = CEMS)

Coefficients  [contrasts:  ..=contr.treatment ]:
        ..Barcelona                 ..London                 ..Milano  
             0.5044                   1.6037                   0.3538  
            ..Paris              ..St.Gallen          WOR[student]yes  
             0.8741                   0.5268                       NA  
            LAT[..]  WOR[student]yes:LAT[..]  
                 NA                   0.8577  
Degrees of Freedom: 4454 Total (i.e. Null);  4448 Residual
  (91 observations deleted due to missingness)
Null Deviance:      5499 
Residual Deviance: 4912     AIC: 5837

> CEMS.BTmodel_02
Bradley Terry model fit by glm.fit 

Call:  BTm(outcome = cbind(win1.adj, win2.adj), player1 = school1, player2 = school2, 
    formula = ~.. + WOR[student] * LAT[..], refcat = "Stockholm", 
    data = CEMS)

Coefficients  [contrasts:  ..=contr.treatment ]:
         ..Toulouse              ..Barcelona                 ..London  
                 NA                   0.5044                   1.6037  
           ..Milano                  ..Paris              ..St.Gallen  
             0.3538                   0.8741                   0.5268  
    WOR[student]yes                  LAT[..]  WOR[student]yes:LAT[..]  
                 NA                       NA                   0.8577 
Degrees of Freedom: 4454 Total (i.e. Null);  4448 Residual
  (91 observations deleted due to missingness)
Null Deviance:      5499 
Residual Deviance: 4912     AIC: 5837