我目前正在写我的硕士论文(进化信任博弈模型(。我有一个以下问题。我有几种类型的投资者和受托人配对玩信托游戏。投资者和受托人被定义为载体。我希望我的代码(信任游戏(做的是它需要(全球定义的(投资者和受托人。它们一起玩信任博弈的迭代,然后在函数信任博弈中更新全局定义的变量。我希望它适用于任何用作函数信任游戏参数的投资者\受托人。你知道我怎么编码吗?我也不确定它是否会帮助你,但我也发布了我的代码。
#### defining trustees ####
# [1] honorabuse
# [2] information about previous interaction - relevant to investors who buy the information
# [3] payoff
# [4] number of interactions
trustee1 <- c(1,0,0,0)
#### defining investors ####
# [1] buynot buy
# [2-4] investment decision if buying information in case 1(TH), case 2(TA), case 3(NT)
# [5] aggregated payoff
# [6] number of interactions in one generation
investor1 <- c(1,1,1,1,0,0)
here is the code for the trust game
trustgame <- function(investor,trustee)
{ investordecision <- NULL
trusteedecision <- trustee[1]
investor[6] <- investor[6]+1
trustee[4] <- trustee[4]+1
if (investor[1]==0) investordecision <- investor[2]
if (investor[1]==1)
{ if (trustee[2]==1) investordecision <- investor[2]
if (trustee[2]==2) investordecision <- investor[3]
if (trustee[2]==3) investordecision <- investor[4]
if (trustee[2]==0) investordecision <- rbinom(1,1,0.5)
}
if (investordecision==1 && trustee[2]==1) trustee[2] <- 1
if (investordecision==1 && trustee[2]==0) trustee[2] <- 2
if (investordecision==0) trustee[2] <- 3
if (investordecision==1 && trusteedecision==1)
{trustee[3] <- trustee[3] +3
investor[5] <- investor[5] + 3 }
if (investordecision==1 && trusteedecision==0)
{trustee[3] <- trustee[3] +5
investor[5] <- investor[5] + 0 }
if (investordecision==0 && trusteedecision==0)
{trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1 }
if (investordecision==0 && trusteedecision==1)
{trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1 }
}
如果输入是字符串,则可以使用 get
和 assign
更改全局环境中的值,如下所示:
trustee1 <- c(1,0,0,0)
investor1 <- c(1,1,1,1,0,0)
trustgame <- function(investor_string,trustee_string){
investor <- get(investor_string, envir = globalenv())
trustee <- get(trustee_string, envir = globalenv())
investordecision <- NULL
trusteedecision <- trustee[1]
investor[6] <- investor[6]+1
trustee[4] <- trustee[4]+1
if (investor[1]==0) investordecision <- investor[2]
if (investor[1]==1){
if (trustee[2]==1) investordecision <- investor[2]
if (trustee[2]==2) investordecision <- investor[3]
if (trustee[2]==3) investordecision <- investor[4]
if (trustee[2]==0) investordecision <- rbinom(1,1,0.5)
}
if (investordecision==1 && trustee[2]==1) trustee[2] <- 1
if (investordecision==1 && trustee[2]==0) trustee[2] <- 2
if (investordecision==0) trustee[2] <- 3
if (investordecision==1 && trusteedecision==1){
trustee[3] <- trustee[3] +3
investor[5] <- investor[5] + 3
}
if (investordecision==1 && trusteedecision==0){
trustee[3] <- trustee[3] +5
investor[5] <- investor[5] + 0
}
if (investordecision==0 && trusteedecision==0){
trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1
}
if (investordecision==0 && trusteedecision==1){
trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1
}
assign(trustee_string, trustee, envir = globalenv())
assign(investor_string, investor, envir = globalenv())
}
trustgame("investor1", "trustee1")
> investor1
[1] 1 1 1 1 1 1
> trustee1
[1] 1 3 1 1