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.)
r 3d
add a comment |
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.)
r 3d
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
One problem is that you've usedf
in yourpersp()
call where I think you meant to useZ
. Otherwise, you can just set a lower limit forZ
so it doesn't go all the way down to infinity, e.g.persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58
add a comment |
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.)
r 3d
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.)
r 3d
r 3d
edited Nov 14 '18 at 23:46
Dianafreedom
asked Nov 13 '18 at 0:53
DianafreedomDianafreedom
235
235
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
One problem is that you've usedf
in yourpersp()
call where I think you meant to useZ
. Otherwise, you can just set a lower limit forZ
so it doesn't go all the way down to infinity, e.g.persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58
add a comment |
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
One problem is that you've usedf
in yourpersp()
call where I think you meant to useZ
. Otherwise, you can just set a lower limit forZ
so it doesn't go all the way down to infinity, e.g.persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
One problem is that you've used
f
in your persp()
call where I think you meant to use Z
. Otherwise, you can just set a lower limit for Z
so it doesn't go all the way down to infinity, e.g. persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58
One problem is that you've used
f
in your persp()
call where I think you meant to use Z
. Otherwise, you can just set a lower limit for Z
so it doesn't go all the way down to infinity, e.g. persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58
add a comment |
1 Answer
1
active
oldest
votes
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()
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272226%2fhow-to-draw-a-3-d-plot-with-3-input-variables-as-well-as-somewhere-infinite-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
add a comment |
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()
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
add a comment |
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()
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()
answered Nov 13 '18 at 1:11
42-42-
212k15253399
212k15253399
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
add a comment |
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Thanks for your answer but after I cleared up the global environment and just copied your code to run, Rstudio still remind me that I produced NaNs.
– Dianafreedom
Nov 13 '18 at 5:38
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
Yep. I saw the warnings, too. The NA's are the reason that the high B + high A region is empty. NA's do not cause a plotting error, rather there will be nothing plotted where they occur. You are not explaining your current problem, despite the fact that you've been shown what was causing your error conditions. Two-thirds of the Z values are finite, one-third are NA.
– 42-
Nov 13 '18 at 6:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272226%2fhow-to-draw-a-3-d-plot-with-3-input-variables-as-well-as-somewhere-infinite-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you want tested code you will open up your questions using edit and improve it. You have not defineg G. In fact, you should start R with a blank session and test the code every time before posting a question.
– 42-
Nov 13 '18 at 0:58
One problem is that you've used
f
in yourpersp()
call where I think you meant to useZ
. Otherwise, you can just set a lower limit forZ
so it doesn't go all the way down to infinity, e.g.persp(A,B,Z, zlim = c(-2000, max(Z, na.rm = TRUE)))
– Marius
Nov 13 '18 at 0:58