0

enter image description here

I try to plot the profile of function f_n(theta) for 1<theta<15 . My code is as follows. I first got 100 samples at random. I solved lambda with uniroot, and then I wrote the function for f again

# I first get 100 samples 
set.seed(201111)
x=rlnorm(100,0,2)

theta_seq = seq(from = 1, to = 15, by = 0.01)
f_n = rep(NA, length(theta_seq))

# I define function h, and use unifoot function to find lambda
h=function(lam, n)
{
  sum((x-theta)/(1+lam*(x-theta)))
}

f_n = rep(NA, length(theta_seq))
i = 1

for (theta in theta_seq)
{
  x = rnorm(100, theta, 2)
  n=100
  xmax=max(x)
  xmin=min(x)
  
  lambda = uniroot(h, c((1/n-1)/(xmax-theta),(1/n-1)/(xmin-theta)),n=n)$root
  
  f_n[i] = -sum(log(1+lambda*(x-theta)))
  i = i+1
}
plot(theta_seq, f_n)



Is my function h right? But how to use ggplot to draw the profile of f_n(theta) like the following picture enter image description here?

I got a point plot of f_n(theta) like the following picture.

enter image description here

Hermi
  • 343
  • 1
  • 10
  • I think this thread might help you: https://stackoverflow.com/questions/26091323/how-to-plot-a-function-curve-in-r – tjebo Mar 21 '22 at 18:06

0 Answers0