R library(BradleyTerry2). Error in Diff(player1, player2… must be factors with the same levels
up vote
0
down vote
favorite
I have a problem using BradleyTerry2 package in R.
My data looks like as below. I created the data from another code, and I think the format is same to the "citations.sf" in BradleyTerry exmple (https://cran.r-project.org/web/packages/BradleyTerry2/vignettes/BradleyTerry.pdf)
player1 player2 win1 win2
1 1 2 10 0
2 1 3 10 0
3 1 4 5 5
4 1 5 10 0
5 1 6 9 1
6 2 3 6 4
7 2 4 4 6
8 2 5 5 5
9 2 6 8 2
10 3 4 2 8
11 3 5 7 3
12 3 6 6 4
13 4 5 10 0
14 4 6 9 1
15 5 6 4 6
However, when I run speedModel <- BTm(cbind(win1, win2), player1, player2, data = dat)
, It shows an error message as below.
Error in Diff(player1, player2, formula, id, data, separate.ability, refcat, :
'player1$..' and 'player2$..' must be factors with the same levels
I looked at another page on StackOverflow (Updated with data: Error in Diff...must be factors with the same levels), and I tried the below code. (I don't understand what it does, though.)
levels(dat[,1]) <- dat(c(dat[,1], dat[,2]))
levels(dat[,2]) <- dat(c(dat[,1], dat[,2]))
However, the BTm() function throws the same message. Can anyone tell me what I can do?
Here is the result of dput(dat)
structure(list(player1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = 1:6), player2 = structure(c(2L,
3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 4L, 5L, 6L, 5L, 6L, 6L), .Label = 1:6),
win1 = c(10L, 10L, 5L, 10L, 9L, 6L, 4L, 5L, 8L, 2L, 7L, 6L,
10L, 9L, 4L), win2 = c(0L, 0L, 5L, 0L, 1L, 4L, 6L, 5L, 2L,
8L, 3L, 4L, 0L, 1L, 6L)), row.names = c(NA, -15L), class = "data.frame")
r
add a comment |
up vote
0
down vote
favorite
I have a problem using BradleyTerry2 package in R.
My data looks like as below. I created the data from another code, and I think the format is same to the "citations.sf" in BradleyTerry exmple (https://cran.r-project.org/web/packages/BradleyTerry2/vignettes/BradleyTerry.pdf)
player1 player2 win1 win2
1 1 2 10 0
2 1 3 10 0
3 1 4 5 5
4 1 5 10 0
5 1 6 9 1
6 2 3 6 4
7 2 4 4 6
8 2 5 5 5
9 2 6 8 2
10 3 4 2 8
11 3 5 7 3
12 3 6 6 4
13 4 5 10 0
14 4 6 9 1
15 5 6 4 6
However, when I run speedModel <- BTm(cbind(win1, win2), player1, player2, data = dat)
, It shows an error message as below.
Error in Diff(player1, player2, formula, id, data, separate.ability, refcat, :
'player1$..' and 'player2$..' must be factors with the same levels
I looked at another page on StackOverflow (Updated with data: Error in Diff...must be factors with the same levels), and I tried the below code. (I don't understand what it does, though.)
levels(dat[,1]) <- dat(c(dat[,1], dat[,2]))
levels(dat[,2]) <- dat(c(dat[,1], dat[,2]))
However, the BTm() function throws the same message. Can anyone tell me what I can do?
Here is the result of dput(dat)
structure(list(player1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = 1:6), player2 = structure(c(2L,
3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 4L, 5L, 6L, 5L, 6L, 6L), .Label = 1:6),
win1 = c(10L, 10L, 5L, 10L, 9L, 6L, 4L, 5L, 8L, 2L, 7L, 6L,
10L, 9L, 4L), win2 = c(0L, 0L, 5L, 0L, 1L, 4L, 6L, 5L, 2L,
8L, 3L, 4L, 0L, 1L, 6L)), row.names = c(NA, -15L), class = "data.frame")
r
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a problem using BradleyTerry2 package in R.
My data looks like as below. I created the data from another code, and I think the format is same to the "citations.sf" in BradleyTerry exmple (https://cran.r-project.org/web/packages/BradleyTerry2/vignettes/BradleyTerry.pdf)
player1 player2 win1 win2
1 1 2 10 0
2 1 3 10 0
3 1 4 5 5
4 1 5 10 0
5 1 6 9 1
6 2 3 6 4
7 2 4 4 6
8 2 5 5 5
9 2 6 8 2
10 3 4 2 8
11 3 5 7 3
12 3 6 6 4
13 4 5 10 0
14 4 6 9 1
15 5 6 4 6
However, when I run speedModel <- BTm(cbind(win1, win2), player1, player2, data = dat)
, It shows an error message as below.
Error in Diff(player1, player2, formula, id, data, separate.ability, refcat, :
'player1$..' and 'player2$..' must be factors with the same levels
I looked at another page on StackOverflow (Updated with data: Error in Diff...must be factors with the same levels), and I tried the below code. (I don't understand what it does, though.)
levels(dat[,1]) <- dat(c(dat[,1], dat[,2]))
levels(dat[,2]) <- dat(c(dat[,1], dat[,2]))
However, the BTm() function throws the same message. Can anyone tell me what I can do?
Here is the result of dput(dat)
structure(list(player1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = 1:6), player2 = structure(c(2L,
3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 4L, 5L, 6L, 5L, 6L, 6L), .Label = 1:6),
win1 = c(10L, 10L, 5L, 10L, 9L, 6L, 4L, 5L, 8L, 2L, 7L, 6L,
10L, 9L, 4L), win2 = c(0L, 0L, 5L, 0L, 1L, 4L, 6L, 5L, 2L,
8L, 3L, 4L, 0L, 1L, 6L)), row.names = c(NA, -15L), class = "data.frame")
r
I have a problem using BradleyTerry2 package in R.
My data looks like as below. I created the data from another code, and I think the format is same to the "citations.sf" in BradleyTerry exmple (https://cran.r-project.org/web/packages/BradleyTerry2/vignettes/BradleyTerry.pdf)
player1 player2 win1 win2
1 1 2 10 0
2 1 3 10 0
3 1 4 5 5
4 1 5 10 0
5 1 6 9 1
6 2 3 6 4
7 2 4 4 6
8 2 5 5 5
9 2 6 8 2
10 3 4 2 8
11 3 5 7 3
12 3 6 6 4
13 4 5 10 0
14 4 6 9 1
15 5 6 4 6
However, when I run speedModel <- BTm(cbind(win1, win2), player1, player2, data = dat)
, It shows an error message as below.
Error in Diff(player1, player2, formula, id, data, separate.ability, refcat, :
'player1$..' and 'player2$..' must be factors with the same levels
I looked at another page on StackOverflow (Updated with data: Error in Diff...must be factors with the same levels), and I tried the below code. (I don't understand what it does, though.)
levels(dat[,1]) <- dat(c(dat[,1], dat[,2]))
levels(dat[,2]) <- dat(c(dat[,1], dat[,2]))
However, the BTm() function throws the same message. Can anyone tell me what I can do?
Here is the result of dput(dat)
structure(list(player1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = 1:6), player2 = structure(c(2L,
3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 4L, 5L, 6L, 5L, 6L, 6L), .Label = 1:6),
win1 = c(10L, 10L, 5L, 10L, 9L, 6L, 4L, 5L, 8L, 2L, 7L, 6L,
10L, 9L, 4L), win2 = c(0L, 0L, 5L, 0L, 1L, 4L, 6L, 5L, 2L,
8L, 3L, 4L, 0L, 1L, 6L)), row.names = c(NA, -15L), class = "data.frame")
r
r
asked Nov 9 at 16:06
pico123
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Your problem is with the definition of the player factors in your data frame. They need to have the exact same levels but because player1
only contains the values 1, ..., 5, while player2
contains the values 2, ..., 6 then they will have different levels.
You need to force the levels to be identical for the two factors by providing the exact levels. Here's one way to do it by creating two new variables with the right factor levels.
dat$p1 <- factor(dat$player1, levels=unique(c(dat$player1, dat$player2)))
dat$p2 <- factor(dat$player2, levels=unique(c(dat$player1, dat$player2)))
Then we can run
> speedModel <- BTm(cbind(win1, win2), p1, p2, data = dat)
> speedModel
Bradley Terry model fit by glm.fit
Call: BTm(outcome = cbind(win1, win2), player1 = p1, player2 = p2,
data = indata)
Coefficients:
..2 ..3 ..4 ..5 ..6
-2.1433 -2.4885 -0.7286 -3.1201 -2.9323
Degrees of Freedom: 15 Total (i.e. Null); 10 Residual
Null Deviance: 81.14
Residual Deviance: 13.71 AIC: 51.6
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229310%2fr-librarybradleyterry2-error-in-diffplayer1-player2-must-be-factors-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Your problem is with the definition of the player factors in your data frame. They need to have the exact same levels but because player1
only contains the values 1, ..., 5, while player2
contains the values 2, ..., 6 then they will have different levels.
You need to force the levels to be identical for the two factors by providing the exact levels. Here's one way to do it by creating two new variables with the right factor levels.
dat$p1 <- factor(dat$player1, levels=unique(c(dat$player1, dat$player2)))
dat$p2 <- factor(dat$player2, levels=unique(c(dat$player1, dat$player2)))
Then we can run
> speedModel <- BTm(cbind(win1, win2), p1, p2, data = dat)
> speedModel
Bradley Terry model fit by glm.fit
Call: BTm(outcome = cbind(win1, win2), player1 = p1, player2 = p2,
data = indata)
Coefficients:
..2 ..3 ..4 ..5 ..6
-2.1433 -2.4885 -0.7286 -3.1201 -2.9323
Degrees of Freedom: 15 Total (i.e. Null); 10 Residual
Null Deviance: 81.14
Residual Deviance: 13.71 AIC: 51.6
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
add a comment |
up vote
0
down vote
accepted
Your problem is with the definition of the player factors in your data frame. They need to have the exact same levels but because player1
only contains the values 1, ..., 5, while player2
contains the values 2, ..., 6 then they will have different levels.
You need to force the levels to be identical for the two factors by providing the exact levels. Here's one way to do it by creating two new variables with the right factor levels.
dat$p1 <- factor(dat$player1, levels=unique(c(dat$player1, dat$player2)))
dat$p2 <- factor(dat$player2, levels=unique(c(dat$player1, dat$player2)))
Then we can run
> speedModel <- BTm(cbind(win1, win2), p1, p2, data = dat)
> speedModel
Bradley Terry model fit by glm.fit
Call: BTm(outcome = cbind(win1, win2), player1 = p1, player2 = p2,
data = indata)
Coefficients:
..2 ..3 ..4 ..5 ..6
-2.1433 -2.4885 -0.7286 -3.1201 -2.9323
Degrees of Freedom: 15 Total (i.e. Null); 10 Residual
Null Deviance: 81.14
Residual Deviance: 13.71 AIC: 51.6
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Your problem is with the definition of the player factors in your data frame. They need to have the exact same levels but because player1
only contains the values 1, ..., 5, while player2
contains the values 2, ..., 6 then they will have different levels.
You need to force the levels to be identical for the two factors by providing the exact levels. Here's one way to do it by creating two new variables with the right factor levels.
dat$p1 <- factor(dat$player1, levels=unique(c(dat$player1, dat$player2)))
dat$p2 <- factor(dat$player2, levels=unique(c(dat$player1, dat$player2)))
Then we can run
> speedModel <- BTm(cbind(win1, win2), p1, p2, data = dat)
> speedModel
Bradley Terry model fit by glm.fit
Call: BTm(outcome = cbind(win1, win2), player1 = p1, player2 = p2,
data = indata)
Coefficients:
..2 ..3 ..4 ..5 ..6
-2.1433 -2.4885 -0.7286 -3.1201 -2.9323
Degrees of Freedom: 15 Total (i.e. Null); 10 Residual
Null Deviance: 81.14
Residual Deviance: 13.71 AIC: 51.6
Your problem is with the definition of the player factors in your data frame. They need to have the exact same levels but because player1
only contains the values 1, ..., 5, while player2
contains the values 2, ..., 6 then they will have different levels.
You need to force the levels to be identical for the two factors by providing the exact levels. Here's one way to do it by creating two new variables with the right factor levels.
dat$p1 <- factor(dat$player1, levels=unique(c(dat$player1, dat$player2)))
dat$p2 <- factor(dat$player2, levels=unique(c(dat$player1, dat$player2)))
Then we can run
> speedModel <- BTm(cbind(win1, win2), p1, p2, data = dat)
> speedModel
Bradley Terry model fit by glm.fit
Call: BTm(outcome = cbind(win1, win2), player1 = p1, player2 = p2,
data = indata)
Coefficients:
..2 ..3 ..4 ..5 ..6
-2.1433 -2.4885 -0.7286 -3.1201 -2.9323
Degrees of Freedom: 15 Total (i.e. Null); 10 Residual
Null Deviance: 81.14
Residual Deviance: 13.71 AIC: 51.6
answered Nov 9 at 19:49
ekstroem
3,52021230
3,52021230
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
add a comment |
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
Thank you ekstroem. It works well for me! I still don't understand what is happening, because p1 and p2 look same to player1 and player2 on the matrix. ...R is fascinating!
– pico123
Nov 14 at 12:03
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
You should read up on R factors and their factor levels - that's where the problem was
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
If it works then please accept the answer.
– ekstroem
Nov 14 at 19:06
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
Sorry, I didn't know the function. I just did and thank you again!
– pico123
Nov 16 at 9:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229310%2fr-librarybradleyterry2-error-in-diffplayer1-player2-must-be-factors-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown