For a function l(p;x)=log p 3log(1-p) as the following picture,
how can I add mark a value for l(p;x) and mark its p value as in the red color line. For example, I want to mark l(p;x)=-3.2 and its corresponding interval for p.
I can plot the function in R:
l=function(p){log(p) 3*log(1-p)}
plot(l, 0, 1)
CodePudding user response:
How about this Base R solution:
l=function(p){log(p) 3*log(1-p)}
plot(l, 0, 1)
#draw line at -3.2
abline(h=-3.2, col="red")
#find the upper and lower solutions
lower <-uniroot(function(x) {l(x) 3.2}, lower=0.01, upper=0.4)
upper <-uniroot(function(x) {l(x) 3.2}, lower=0.4, upper=1)
#plot
segments(lower$root, -20, y1=-3.2, col="blue")
segments(upper$root, -20, y1=-3.2, col="blue")
#or
abline(v=lower$root, col="red")
abline(v=upper$root, col="red")
CodePudding user response:
y_int <- -3.2
x_int <- rootSolve::uniroot.all(function(x) l(x) - y_int, c(0, 1))
library(ggplot2)
ggplot(data.frame(x = c(0, 1)))
stat_function(fun = l)
geom_hline(yintercept = y_int, color = 'red')
lapply(x_int, function(x)
geom_segment(y = -Inf, yend = y_int, x = x, xend = x, color = 'red')
)
theme_minimal()