1

I'm writing codes for Prisoners' Dilemma in R, and am blocked in creating the payoff matrix. Wonder if there is a way to create a python dictionary like array or a matrix whose elements are tuples. Thus this matrix should represent the gains of both players:

Here is the image:

enter image description here

This image is only to show you what I'm up to, and the values are different from the matrix I want to create. I try to create lists and then assign them to the matrix but it only assigns the first element of lists to the matrix. This is my code snippet:

T=2
R=1
P=0
S=-1


act1 <- as.vector(list(T,S), mode="pairlist")
act2 <- as.vector(list(P,P), mode="pairlist")
act3 <- as.vector(list(R,R), mode="pairlist")
act4 <- as.vector(list(S,T), mode="pairlist")
actions <- c(act3,act4,act1,act2)
payoff.mat <- as.matrix(actions,nrow=2,ncol=2)
dimnames(payoff.mat)[[1]] <- c("Gift","NoGift")
dimnames(payoff.mat)[[2]] = dimnames(payoff.mat)[[1]]

Any Idea of how to do this?

989
  • 12,579
  • 5
  • 31
  • 53
Taabi
  • 27
  • 1
  • 7
  • you can use a payoff matrix for **each** player or a 3-dimensional array or a list with two matrices. – jogo Nov 13 '15 at 07:24
  • Even with `matrix(...)` it gives out the first element of the list – Taabi Nov 13 '15 at 07:25
  • You have the payoffs. You are looking for pretty output. You could use LateX or another visualization. – Pierre L Nov 13 '15 at 07:28
  • @jogo I tried your suggestion and it gives both matrices in the output . But guess it is a sufficient way to do the operations anyway. Thanx – Taabi Nov 13 '15 at 07:33

1 Answers1

2

Your work is done already. You are just attempting to clean up the output. I would combine the payoffs in couplets like Python tuples and create a table from there:

T=2
R=1
P=0
S=-1

pair <- function(x,y) sprintf("(%d,%d)", x,y)
all_pairs <- c(pair(T,S), pair(P,P), pair(R,R), pair(S,T))
payoff.mat <- matrix(all_pairs, nrow=2)
dimnames(payoff.mat) <- c(rep(list(c("Gift","NoGift")), 2))

With Rmarkdown and RStudio you can print the matrix in a nice format by entering the above code into an rmarkdown document and adding:

library(knitr)
kable(payoff.mat, format="latex")

enter image description here

A full explanation of markdown documents would be beyond the scope of this question. For a quick overview see http://kbroman.org/knitr_knutshell/pages/figs_tables.html

Pierre L
  • 28,203
  • 6
  • 47
  • 69