在 R 中创建 "Prisoner's Dilemma Table"
Creating a "Prisoner's Dilemma Table" in R
我正在使用 R 编程语言。我在 R 中创建了以下“游戏”,其中涉及两个玩家掷硬币(每个硬币的结果都与“分数”相关联)以查看谁获得最高分数:
score_coin_1 = c(-1,1)
score_coin_2 = c(-3, 4)
results <- list()
for (i in 1:100)
{
iteration = i
player_1_coin_choice_i = sample(2, 1, replace = TRUE)
player_2_coin_choice_i = sample(2, 1, replace = TRUE)
player_1_result_i = ifelse(player_1_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)) )
player_2_result_i = ifelse(player_2_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)))
winner_i = ifelse(player_1_result_i > player_2_result_i, "PLAYER_1", ifelse(player_1_result_i == player_2_result_i, "TIE", "PLAYER_2"))
my_data_i = data.frame(iteration, player_1_coin_choice_i, player_2_coin_choice_i, player_1_result_i, player_2_result_i , winner_i )
results[[i]] <- my_data_i
}
results_df <- data.frame(do.call(rbind.data.frame, results))
head(results_df)
iteration player_1_coin_choice_i player_2_coin_choice_i player_1_result_i player_2_result_i winner_i
1 1 1 1 -1 1 PLAYER_2
2 2 1 2 -1 -3 PLAYER_1
3 3 2 2 4 -3 PLAYER_1
4 4 1 2 1 -3 PLAYER_1
5 5 2 1 4 1 PLAYER_1
6 6 2 2 4 -3 PLAYER_1
然后我提取了所有可能结果的分数:
one_one <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 1), ]
one_two <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 2), ]
two_one <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 1), ]
two_two <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 2), ]
library(dplyr)
one_one_sum = data.frame(one_one %>%
group_by(winner_i) %>%
summarise(n = n()))
one_two_sum = data.frame(one_two %>%
group_by(winner_i) %>%
summarise(n = n()))
two_one_sum = data.frame(two_one %>%
group_by(winner_i) %>%
summarise(n = n()))
two_two_sum = data.frame(two_two %>%
group_by(winner_i) %>%
summarise(n = n()))
从这里开始,我想做这样的table(《囚徒困境》:https://www.greenflux.com/wp-content/uploads/knowledge-sharing-the-prisoners-dilemma-1-1.jpg)
这里,“硬币1,硬币1”表示玩家1选择了硬币1,玩家2也选择了硬币1 - “硬币2,硬币1”表示玩家1选择了硬币2,玩家1选择了硬币1,依此类推。
我的问题: 是否可以在 R 中“直接”制作这个 table?目前,我正在 Microsoft Excel 和 Powerpoint 中创建此 table - 但可以直接在 R 中创建此 table 吗?
谢谢!
不确定您希望如何将计算的收益转化为收益矩阵,但您可以使用 ggplot2
来实现。请注意,输入是一个包含元素 ll
、lr
、ul
、ur
的列表,用于在 lower-left、lower-right、upper-left 和 upper-right 象限。如果收益向量只有两个值,则不会打印 Tie 标签。这是一个例子:
library(ggplot2)
payoffs <- list(ul=c(1,2,3), ll = c(3,2,1), ur = c(4,5,6), lr = c(6,5,4))
pd_game <- function(payoffs, ...){
## payoffs should have elements ll, lr, ul, ur for lower-left
## lower-right, upper-left and upper-right quadrant payoffs
require(ggplot2)
g <- ggplot() +
geom_segment(aes(y=0, yend=0, x=.15, xend=1)) +
geom_segment(aes(y=.4, yend=.4, x=.2, xend=1)) +
geom_segment(aes(y=.8, yend=.8, x=.15, xend=1)) +
geom_segment(aes(y=0, yend=1, x=.3, xend=.3)) +
geom_segment(aes(y=0, yend=.95, x=.65, xend=.65)) +
geom_segment(aes(y=0, yend=1, x=1, xend=1)) +
geom_text(aes(x=.15, y=.2, label="Coin 1, Coin 2"), hjust=0) +
geom_text(aes(x=.15, y=.6, label="Coin 1, Coin 1"), hjust=0) +
geom_text(aes(x=.475, y=.9, label="Coin 2, Coin 1"), hjust=0.5) +
geom_text(aes(x=.825, y=.9, label="Coin 2, Coin 2"), hjust=0.5) +
geom_text(aes(x=.4, y=.275, label=paste0("Win: ", payoffs$ll[1])), hjust=0) +
geom_text(aes(x=.4, y=.2, label=paste0("Lose: ", payoffs$ll[2])), hjust=0) +
geom_text(aes(x=.75, y=.275, label=paste0("Win: ", payoffs$lr[1])), hjust=0) +
geom_text(aes(x=.75, y=.2, label=paste0("Lose: ", payoffs$lr[2])), hjust=0) +
geom_text(aes(x=.4, y=.675, label=paste0("Win: ", payoffs$ul[1])), hjust=0) +
geom_text(aes(x=.4, y=.6, label=paste0("Lose: ", payoffs$ul[2])), hjust=0) +
geom_text(aes(x=.75, y=.675, label=paste0("Win: ", payoffs$ur[1])), hjust=0) +
geom_text(aes(x=.75, y=.6, label=paste0("Lose: ", payoffs$ur[2])), hjust=0) +
geom_text(aes(x=.1, y=.4, label="Player 1"), hjust=0) +
geom_text(aes(x=.65, y=1, label="Player 2"), hjust=0.5) +
theme_void()
if(length(payoffs$ul) == 3){
g <- g + geom_text(aes(x=.4, y=.525, label=paste0("Tie: ", payoffs$ul[3])), hjust=0)
}
if(length(payoffs$lr) == 3){
g <- g + geom_text(aes(x=.75, y=.125, label=paste0("Tie: ", payoffs$lr[3])), hjust=0)
}
if(length(payoffs$ll) == 3){
g <- g + geom_text(aes(x=.4, y=.125, label=paste0("Tie: ", payoffs$ll[3])), hjust=0)
}
if(length(payoffs$ur) == 3){
g <- g+geom_text(aes(x=.75, y=.525, label=paste0("Tie: ", payoffs$ur[3])), hjust=0)
}
g
}
pd_game(payoffs)
我正在使用 R 编程语言。我在 R 中创建了以下“游戏”,其中涉及两个玩家掷硬币(每个硬币的结果都与“分数”相关联)以查看谁获得最高分数:
score_coin_1 = c(-1,1)
score_coin_2 = c(-3, 4)
results <- list()
for (i in 1:100)
{
iteration = i
player_1_coin_choice_i = sample(2, 1, replace = TRUE)
player_2_coin_choice_i = sample(2, 1, replace = TRUE)
player_1_result_i = ifelse(player_1_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)) )
player_2_result_i = ifelse(player_2_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)))
winner_i = ifelse(player_1_result_i > player_2_result_i, "PLAYER_1", ifelse(player_1_result_i == player_2_result_i, "TIE", "PLAYER_2"))
my_data_i = data.frame(iteration, player_1_coin_choice_i, player_2_coin_choice_i, player_1_result_i, player_2_result_i , winner_i )
results[[i]] <- my_data_i
}
results_df <- data.frame(do.call(rbind.data.frame, results))
head(results_df)
iteration player_1_coin_choice_i player_2_coin_choice_i player_1_result_i player_2_result_i winner_i
1 1 1 1 -1 1 PLAYER_2
2 2 1 2 -1 -3 PLAYER_1
3 3 2 2 4 -3 PLAYER_1
4 4 1 2 1 -3 PLAYER_1
5 5 2 1 4 1 PLAYER_1
6 6 2 2 4 -3 PLAYER_1
然后我提取了所有可能结果的分数:
one_one <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 1), ]
one_two <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 2), ]
two_one <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 1), ]
two_two <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 2), ]
library(dplyr)
one_one_sum = data.frame(one_one %>%
group_by(winner_i) %>%
summarise(n = n()))
one_two_sum = data.frame(one_two %>%
group_by(winner_i) %>%
summarise(n = n()))
two_one_sum = data.frame(two_one %>%
group_by(winner_i) %>%
summarise(n = n()))
two_two_sum = data.frame(two_two %>%
group_by(winner_i) %>%
summarise(n = n()))
从这里开始,我想做这样的table(《囚徒困境》:https://www.greenflux.com/wp-content/uploads/knowledge-sharing-the-prisoners-dilemma-1-1.jpg)
这里,“硬币1,硬币1”表示玩家1选择了硬币1,玩家2也选择了硬币1 - “硬币2,硬币1”表示玩家1选择了硬币2,玩家1选择了硬币1,依此类推。
我的问题: 是否可以在 R 中“直接”制作这个 table?目前,我正在 Microsoft Excel 和 Powerpoint 中创建此 table - 但可以直接在 R 中创建此 table 吗?
谢谢!
不确定您希望如何将计算的收益转化为收益矩阵,但您可以使用 ggplot2
来实现。请注意,输入是一个包含元素 ll
、lr
、ul
、ur
的列表,用于在 lower-left、lower-right、upper-left 和 upper-right 象限。如果收益向量只有两个值,则不会打印 Tie 标签。这是一个例子:
library(ggplot2)
payoffs <- list(ul=c(1,2,3), ll = c(3,2,1), ur = c(4,5,6), lr = c(6,5,4))
pd_game <- function(payoffs, ...){
## payoffs should have elements ll, lr, ul, ur for lower-left
## lower-right, upper-left and upper-right quadrant payoffs
require(ggplot2)
g <- ggplot() +
geom_segment(aes(y=0, yend=0, x=.15, xend=1)) +
geom_segment(aes(y=.4, yend=.4, x=.2, xend=1)) +
geom_segment(aes(y=.8, yend=.8, x=.15, xend=1)) +
geom_segment(aes(y=0, yend=1, x=.3, xend=.3)) +
geom_segment(aes(y=0, yend=.95, x=.65, xend=.65)) +
geom_segment(aes(y=0, yend=1, x=1, xend=1)) +
geom_text(aes(x=.15, y=.2, label="Coin 1, Coin 2"), hjust=0) +
geom_text(aes(x=.15, y=.6, label="Coin 1, Coin 1"), hjust=0) +
geom_text(aes(x=.475, y=.9, label="Coin 2, Coin 1"), hjust=0.5) +
geom_text(aes(x=.825, y=.9, label="Coin 2, Coin 2"), hjust=0.5) +
geom_text(aes(x=.4, y=.275, label=paste0("Win: ", payoffs$ll[1])), hjust=0) +
geom_text(aes(x=.4, y=.2, label=paste0("Lose: ", payoffs$ll[2])), hjust=0) +
geom_text(aes(x=.75, y=.275, label=paste0("Win: ", payoffs$lr[1])), hjust=0) +
geom_text(aes(x=.75, y=.2, label=paste0("Lose: ", payoffs$lr[2])), hjust=0) +
geom_text(aes(x=.4, y=.675, label=paste0("Win: ", payoffs$ul[1])), hjust=0) +
geom_text(aes(x=.4, y=.6, label=paste0("Lose: ", payoffs$ul[2])), hjust=0) +
geom_text(aes(x=.75, y=.675, label=paste0("Win: ", payoffs$ur[1])), hjust=0) +
geom_text(aes(x=.75, y=.6, label=paste0("Lose: ", payoffs$ur[2])), hjust=0) +
geom_text(aes(x=.1, y=.4, label="Player 1"), hjust=0) +
geom_text(aes(x=.65, y=1, label="Player 2"), hjust=0.5) +
theme_void()
if(length(payoffs$ul) == 3){
g <- g + geom_text(aes(x=.4, y=.525, label=paste0("Tie: ", payoffs$ul[3])), hjust=0)
}
if(length(payoffs$lr) == 3){
g <- g + geom_text(aes(x=.75, y=.125, label=paste0("Tie: ", payoffs$lr[3])), hjust=0)
}
if(length(payoffs$ll) == 3){
g <- g + geom_text(aes(x=.4, y=.125, label=paste0("Tie: ", payoffs$ll[3])), hjust=0)
}
if(length(payoffs$ur) == 3){
g <- g+geom_text(aes(x=.75, y=.525, label=paste0("Tie: ", payoffs$ur[3])), hjust=0)
}
g
}
pd_game(payoffs)