R 费希尔';s和皮尔逊';s独立性试验
在R中,我有两个数据集:R 费希尔';s和皮尔逊';s独立性试验,r,statistics,R,Statistics,在R中,我有两个数据集:group1和group2 对于第1组我有10个游戏id,这是一个游戏的id,我们有数字,这是该游戏在第1组中玩过的次数 所以如果我们打字 group1 我们得到这个输出 game_id number 1 758565 2 235289 ... 10 87084 对于group2 game_id number 1 79310 2 28564 ... 10 9048 如果我想测试gro
group1
和group2
对于第1组
我有10个游戏id
,这是一个游戏的id,我们有数字
,这是该游戏在第1组
中玩过的次数
所以如果我们打字
group1
我们得到这个输出
game_id number
1 758565
2 235289
...
10 87084
对于group2
game_id number
1 79310
2 28564
...
10 9048
如果我想测试group1
和group2
在前2个game\u id
中是否存在统计差异,我可以使用Pearson卡方检验
在R中,我只是创建矩阵
# The first 2 'numbers' in group1
a <- c( group1[1,2] , group1[2,2] )
# The first 2 'numbers' in group2
b <- c( group2[1,2], group2[2,2] )
# Creating it on matrix-form
m <- rbind(a,b)
这里我可以测试H:“a独立于b”,这意味着group1
中的用户玩game\u id
1比group2
多2个
在R中,我们输入chisq.test(m),得到一个非常低的p值,这意味着我们可以拒绝H,这意味着a和b不是独立的
如何找到在
组1
中比在组2
中玩得更多的游戏id
。我用卡方检验和比例比较检验。就我个人而言,我更喜欢第二个,因为它让你知道你在比较什么百分比。运行脚本并确保您理解该过程
# dataset of group 1
dt_group1 = data.frame(game_id = 1:3,
number_games = c(758565,235289,87084))
dt_group1
# game_id number_games
# 1 1 758565
# 2 2 235289
# 3 3 87084
# add extra variables
dt_group1$number_rest_games = sum(dt_group1$number_games) - dt_group1$number_games # needed for chisq.test
dt_group1$number_all_games = sum(dt_group1$number_games) # needed for prop.test
dt_group1$Prc = dt_group1$number_games / dt_group1$number_all_games # just to get an idea about the percentages
dt_group1
# game_id number_games number_rest_games number_all_games Prc
# 1 1 758565 322373 1080938 0.70176550
# 2 2 235289 845649 1080938 0.21767113
# 3 3 87084 993854 1080938 0.08056336
# dataset of group 2
dt_group2 = data.frame(game_id = 1:3,
number_games = c(79310,28564,9048))
# add extra variables
dt_group2$number_rest_games = sum(dt_group2$number_games) - dt_group2$number_games
dt_group2$number_all_games = sum(dt_group2$number_games)
dt_group2$Prc = dt_group2$number_games / dt_group2$number_all_games
# input the game id you want to investigate
input_game_id = 1
# create a table of successes (games played) and failures (games not played)
dt_test = rbind(c(dt_group1$number_games[dt_group1$game_id==input_game_id], dt_group1$number_rest_games[dt_group1$game_id==input_game_id]),
c(dt_group2$number_games[dt_group2$game_id==input_game_id], dt_group2$number_rest_games[dt_group2$game_id==input_game_id]))
# perform chi sq test
chisq.test(dt_test)
# Pearson's Chi-squared test with Yates' continuity correction
#
# data: dt_test
# X-squared = 275.9, df = 1, p-value < 2.2e-16
# create a vector of successes (games played) and vector of total games
x = c(dt_group1$number_games[dt_group1$game_id==input_game_id], dt_group2$number_games[dt_group2$game_id==input_game_id])
y = c(dt_group1$number_all_games[dt_group1$game_id==input_game_id], dt_group2$number_all_games[dt_group2$game_id==input_game_id])
# perform test of proportions
prop.test(x,y)
# 2-sample test for equality of proportions with continuity correction
#
# data: x out of y
# X-squared = 275.9, df = 1, p-value < 2.2e-16
# alternative hypothesis: two.sided
# 95 percent confidence interval:
# 0.02063233 0.02626776
# sample estimates:
# prop 1 prop 2
# 0.7017655 0.6783155
另一个是使用数据。表
和扫帚
包:
library(dplyr)
library(broom)
# dataset of group 1
dt_group1 = data.frame(game_id = 1:3,
number_games = c(758565,235289,87084),
group_id = 1) ## adding the id of the group
# dataset of group 2
dt_group2 = data.frame(game_id = 1:3,
number_games = c(79310,28564,9048),
group_id = 2) ## adding the id of the group
# combine datasets
dt = rbind(dt_group1, dt_group2)
dt %>%
group_by(group_id) %>% # for each group id
mutate(number_all_games = sum(number_games), # create new columns
number_rest_games = number_all_games - number_games,
Prc = number_games / number_all_games) %>%
group_by(game_id) %>% # for each game
do(tidy(prop.test(.$number_games, .$number_all_games))) %>% # perform the test
ungroup()
# game_id estimate1 estimate2 statistic p.value parameter conf.low conf.high
# (int) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
# 1 1 0.70176550 0.67831546 275.89973 5.876772e-62 1 0.020632330 0.026267761
# 2 2 0.21767113 0.24429962 435.44091 1.063385e-96 1 -0.029216006 -0.024040964
# 3 3 0.08056336 0.07738492 14.39768 1.479844e-04 1 0.001558471 0.004798407
library(data.table)
library(broom)
# dataset of group 1
dt_group1 = data.frame(game_id = 1:3,
number_games = c(758565,235289,87084),
group_id = 1) ## adding the id of the group
# dataset of group 2
dt_group2 = data.frame(game_id = 1:3,
number_games = c(79310,28564,9048),
group_id = 2) ## adding the id of the group
# combine datasets
dt = data.table(rbind(dt_group1, dt_group2))
# create new columns for each group
dt[, number_all_games := sum(number_games), by=group_id]
dt[, `:=`(number_rest_games = number_all_games - number_games,
Prc = number_games / number_all_games) , by=group_id]
# for each game id compare percentages
dt[, tidy(prop.test(.SD$number_games, .SD$number_all_games)) , by=game_id]
# game_id estimate1 estimate2 statistic p.value parameter conf.low conf.high
# 1: 1 0.70176550 0.67831546 275.89973 5.876772e-62 1 0.020632330 0.026267761
# 2: 2 0.21767113 0.24429962 435.44091 1.063385e-96 1 -0.029216006 -0.024040964
# 3: 3 0.08056336 0.07738492 14.39768 1.479844e-04 1 0.001558471 0.004798407
您可以看到,每一行代表一场比赛,比较结果在第1组和第2组之间。您可以从相应的列中获得p值,但也可以获得测试/比较的其他信息。您的卡方检验在统计上无效为什么无效?因为它违反了Pearson卡方检验的假设。列联表中的事件必须是互斥的,总和为1,你只考虑一个部分表和你应该的10 x 10表(假设玩家只能玩1-10个游戏,并且这些不仅仅是排名前10位的数量)。我不知道你如何将用户分为好/坏,但对于每个游戏,你需要知道a)有多少好用户玩过,有多少好用户没玩过,b)有多少坏用户玩过,有多少坏用户没玩过。然后你可以比较每场比赛的百分比。那么我应该如何处理这个问题呢。如果我的列联表应该加起来运行,我可以创建一个新的列,显示一个固定游戏id在group1中玩过的百分比。例如,对于game_id 1,我们得到758565/和(group1[,2])=9%。我做了所有的游戏,总共1。谢谢。这一切都有道理。我在我所有的10个游戏id上都试过了,但都得到了一个低p值。这意味着我们在第一组和第二组之间没有独立性。我觉得这有点奇怪。别忘了,当你处理统计显著性和p值时,如果你有大量(足够)的观察结果,任何差异,无论多么小,都可以在统计上显著。这就是为什么当你想要进行这些比较时,会有“有效样本量”的概念。看看这一点,以及如何为百分比比较设计实验。我有很多数据,所以功率或样本大小应该不是问题。我是这样看的:对于每一场比赛,两组之间都有一种依赖关系。所以坏用户有一些“流行”游戏,好用户有他们的流行游戏。没有一款游戏对这两个群体都同样受欢迎。正是因为你有大量的数据,你应该期望即使是很小的差异也能被捕获/归类为具有统计意义的差异。我也计划更新我的答案(添加一种更有效的方法),现在你对结果很好。重点是你必须事先决定你认为是什么样的影响,并且你尝试收集你需要的多个观察(有效样本大小),以便捕获这种影响/差异,具有统计学意义。你应该搜索“实验设计”、“AB测试设计”等,看看它是如何工作的。在分析之后,如果没有实验设计,你能做的就是报告你的发现,如上所述,让公司决定如何处理统计上的显著差异。
library(data.table)
library(broom)
# dataset of group 1
dt_group1 = data.frame(game_id = 1:3,
number_games = c(758565,235289,87084),
group_id = 1) ## adding the id of the group
# dataset of group 2
dt_group2 = data.frame(game_id = 1:3,
number_games = c(79310,28564,9048),
group_id = 2) ## adding the id of the group
# combine datasets
dt = data.table(rbind(dt_group1, dt_group2))
# create new columns for each group
dt[, number_all_games := sum(number_games), by=group_id]
dt[, `:=`(number_rest_games = number_all_games - number_games,
Prc = number_games / number_all_games) , by=group_id]
# for each game id compare percentages
dt[, tidy(prop.test(.SD$number_games, .SD$number_all_games)) , by=game_id]
# game_id estimate1 estimate2 statistic p.value parameter conf.low conf.high
# 1: 1 0.70176550 0.67831546 275.89973 5.876772e-62 1 0.020632330 0.026267761
# 2: 2 0.21767113 0.24429962 435.44091 1.063385e-96 1 -0.029216006 -0.024040964
# 3: 3 0.08056336 0.07738492 14.39768 1.479844e-04 1 0.001558471 0.004798407