1
$\begingroup$

I have 2 groups (data.frame) in R called good and bad which contains good users and bad users respectively.

The group good contains game_id which is the id for a computergame and number which is how many times this game has been played.

For example good$game_id we get 1 2 3 ... 20. We have 20 games. Similar good$number we get 45214 1254 23 ... 8914 which is the number the game has been played. For example has game_id==1 been played 45214 times in group good.

Similar for bad.
We also have the same number of users in the two groups.

For

head(good)

we get

game_id number

1   45214
2   1254
...
20  8914

I want to investigate if there is dependence between the number of times a fixed computergame has been played.

For game_id==1 I would try to use Pearson's Chi test for 'Independence'. In R I type chisq.test(good[1,2], bad[1,2]) to see if there is indepence between good and bad for game_id==1 but I get an error message: x and y must have same levels.

How can this problem be solved ?

$\endgroup$
2
  • $\begingroup$ Can you post the result of head(your_data.frame)? $\endgroup$ Commented Sep 28, 2015 at 13:24
  • $\begingroup$ The error message suggests that you are feeding the function with factors instead of numbers/counts. str(good) and str(bad) will tell if that is so. If yes, this part of the question is about R programming and not on topic for this forum. In that case you should go back to how the data entered R and what went wrong there. $\endgroup$
    – Bernhard
    Commented Feb 15, 2021 at 13:21

1 Answer 1

1
$\begingroup$

So a game has been played 1554 times and you wonder what share of these uses happened by a good and how many by a bad. This can be done via a binomial test. Let's say we observed the game being played by good 1254 times and by bad 300 times. binom.test says

> binom.test(c(1254, 300))

    Exact binomial test

data:  c(1254, 300)
number of successes = 1254, number of trials = 1554, p-value < 2.2e-16
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.7864318 0.8263009
sample estimates:
probability of success 
             0.8069498 

So the confidence interval for the true share of good players is 78% to 82% and we reject the null hypothesis of each kind of player being equally likely.

Now for your strange data format (corresponding data should really be in one dataframe) it is probably adviseable that you make a reproducible example. This seems to work:

good <- data.frame(game_id = 1:3,
                  number = sample(3000:5000, 3))
bad  <- data.frame(game_id = 1:3,
                  number = sample(300:500, 3))
for(i in 1:3){
    print(i)
    print(c(good[i, "number"], bad[i, "number"]))
    print(binom.test(x = c(good[i, "number"], bad[i, "number"])))
}

Better programming style yet: Merge good and bad into a common dataframe first:

good <- data.frame(game_id = 1:3,
                  number = sample(3000:5000, 3))
bad  <- data.frame(game_id = 1:3,
                  number = sample(300:500, 3))

common <- merge(good, bad, by = "game_id")

apply(common, MARGIN = 1, function(line) binom.test(line[2:3]))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.