2016-05-09 4 views
0

現在、私の卒業論文(進化論的信頼ゲームのモデル)に取り組んでいます。私には次のような問題があります。私にはいくつかのタイプの投資家と信託者がいます。投資家と受託者はベクトルとして定義されます。私のコード(トラストゲーム)がしたいのは、(グローバルに定義された)投資家とトラスティーが必要だということです。彼らはトラストゲームの反復を一緒にプレイし、グローバルに定義された変数が関数トラストゲーム内で更新されます。私はそれが機能trustgameの引数として使用される任意の投資家\ trusteeのために働くことを望みます。私はこれをどのようにコード化できるか知っていますか?それがあなたを助けてくれるかどうかは確かではありませんが、コードも投稿しています。あなたの入力が文字列である場合グローバル変数への値の代入(および関数の引数も同様)R

#### defining trustees #### 
# [1] honor\abuse 
# [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] buy\not 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 } 

} 
+2

を '<< - '関数内でグローバル変数を変更します。注意してください。 – cory

+0

私は知っていますが、私はいくつかのタイプの受託者(trustee1、trustee2、...)と投資家(investor1、investor2、...)を持っています。後で大規模なシミュレーションを行うことになるので、私は手動ですべてを入力する必要はありません。 – hed0r

+0

'envir'引数で' get'と 'assign'を実行しますか? '<< - '、 '' get''と '' assigned''の使い方が大変で始めると、状況が間違っているという良い兆候です。 – cory

答えて

0

、あなたがこのようなグローバル環境での値を変更するgetassignを使用することができます。

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 
+0

ありがとう、あなたは私に大きな時間を助けた:) – hed0r

+0

私はもう一つ質問があります。私は、受託者と会う投資家の人口の進化をシミュレーションしたいので、私は投資家の人口をリストに入れなければならなかった。 Imは、集団が格納されているリストの要素(ベクトル)に対して関数trustgameを機能させるのに苦労しています。この問題にどう対処するかをヒントとして教えてください。 ありがとう – hed0r

+0

@ hed0r投資家と受託者をリストで使用するように変更しました... https://gist.github.com/corynissen/5526748a7f839a207335cc1db2dd8931 – cory

関連する問題