Assigning a value to the global variable (and a argument of a function aswell) R -
i working on master thesis (model of evolutionary trust game). , have following problem. have several types of investors , trustees paired play trust game. investors , trustees defined vectors. code(trust game) takes (globally defined) investor , trustee. play iteration of trust game , globally defined variables updated inside function trust game. work investor\trustee used argument of function trustgame. know how code this? not sure aswell if post code aswell.
#### defining trustees #### # [1] honor\abuse # [2] information previous interaction - relevant investors buy 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 1 generation investor1 <- c(1,1,1,1,0,0) here code 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 } }
if inputs strings, can use get
, assign
change values in global environment this:
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
Comments
Post a Comment