Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to graph the log likelihood function

Tags:

r

I want to graph the log likelihood function between -pi and pi.

the log likelihood function

llh <- function (teta,x) {

  sum(log((1-cos(x-teta))/(2*pi)))
}

x=c(3.91,4.85,2.28,4.06,3.70,4.04,5.46,3.53,2.28,1.96,2.53,3.88,2.22,3.47,4.82,2.46,2.99,2.54,0.52,2.50)

teta=seq(-4,4, by=0.01)

y = llh(teta,x)

plot(teta, llh(teta,x), pch=16)

It failed to plot the function. Here's the error message:

Warning message:
In x - teta :
  longer object length is not a multiple of shorter object length
>   
> plot(teta, llh(teta,x), pch=16)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
In addition: Warning message:
In x - teta :
  longer object length is not a multiple of shorter object length
like image 608
bowshock Avatar asked Jun 16 '26 15:06

bowshock


2 Answers

As written your function will work for one value of teta and several x values, or several values of teta and one x values. Otherwise you get an incorrect value or a warning.

Example: llh for teta=1 and teta=2:

> llh(1,x)
[1] -34.88704>
> llh(2,x)
[1] -60.00497

is not the same as:

> llh(c(1,2),x)
[1] -49.50943

And if you try and do three:

> llh(c(1,2,3),x)
[1] -49.52109
Warning message:
In x - teta :
  longer object length is not a multiple of shorter object length

which fundamentally comes from:

> cos(x-c(1,2,3))
[...]
Warning message:
In x - c(1, 2, 3) :
  longer object length is not a multiple of shorter object length

because R is trying to subtract a length-3 vector from a length-20 vector. It repeats the length-3 vector over the length-20 vector until its done it six times, then it only has two elements left. Hmmmm... thinks R, I'll do this but it looks wrong so here's a warning.

You can either rewrite the function to work on vectors for both arguments, or vectorise the function by wrapping it.

> vllh = Vectorize(llh,"teta")
> vllh(c(1,2,3),x)
[1] -34.88704 -60.00497 -67.30765
> plot(teta, vllh(teta,x))

ll plot

like image 65
Spacedman Avatar answered Jun 18 '26 03:06

Spacedman


you'll need to use the function sapply (read ?sapply) as your code is not vectorised

plot(teta, sapply(X=teta, FUN=function(teta) llh(teta, x=x)), type="l")

OR

to vectorise your function as such:

llh2 <- function (teta,x) {
  sapply(X=teta, FUN=function(teta) sum(log((1-cos(x-teta))/(2*pi))) )
}

plot(teta, llh2(teta,x), type="l")
like image 34
RockScience Avatar answered Jun 18 '26 05:06

RockScience



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!