Dianafreedom
Dianafreedom

Reputation: 411

How to draw a 3-D plot with 3 input variables as well as somewhere infinite in R?

I am writing a log-likelihood surface for the function:

ln[Pr(Y_A=186,Y_B=38,Y_{AB}=13,Y_O=284)]

= ln(G+186*ln(A^2+2*A*O)+38*ln(B^2+2*B*O)+13*ln(2*A*B)+284*ln(O^2))

Thanks to one answerer, I have changed my code to the following but facing new problems:

A = seq(0.0001, .9999,length=50)
B = A
O = A
G = 1.129675e-06   
f = function(A,B,O){F = ifelse(A+B+O==1,
                    G+186*log(A*A+2*A*O)+38*log(B*B+2*B*O)+13*log(2*A*B)+284*log(O*O), O)}
Z <- outer(A, B, O, f)
png()
persp(A,B,Z, theta=60, phi=30 )
dev.off()

The error told me that there isn't object "O".

Error in get(as.character(FUN), mode = "function", envir = envir)

What I mean to do is to input A, B and O under the constraint that A+B+O=1, and then to plot the log-likelihood surface letting A:x-axis, B:y-axis, log-likelihood:z-axis.

I cannot get rid of "O" cause the instruction commands that the parameter of the function should be a 3-dimensional vector: A,B,O.

So what should I do to improve my current code? If I need to change a function, can anyone suggest a function to use? (I think maybe I can use barycentric coordinates but I consider it as the last thing I want to do.)

Upvotes: 1

Views: 55

Answers (1)

IRTFM
IRTFM

Reputation: 263411

It might be better to avoid regions of A and B where you know you will get into trouble. And use Z rather than f for the z-argument:

A = seq(0.0001, .9999,length=50)
B = A
G=1   # throws an error if not foundf
f = function(A,B){O <- 1-A-B; O <- ifelse(O==0, 0.00000001, O)
G+186*log(A*A+2*A*O)+38*log(B*B+2*B*O)+13*log(2*A*B)+284*log(O*O)}
Z <- outer(A, B, f)
png(); persp(A,B,Z, theta=60, phi=30 ); dev.off()

enter image description here

Upvotes: 1

Related Questions