How graph credibility log function

I want a likelihood logarithm plot between -pi and pi.

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)

Could not build function. Here is 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
+3
source share
3 answers

As it is written, your function will work for one value tetaand several values xor several values tetaand one value x. Otherwise, you will receive an incorrect value or warning.

Example: llhfor teta=1and teta=2:

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

- this is not the same as:

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

And if you try to 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 mainly 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

R -3 -20. -3 -20, , . ... , R, , , .

, , .

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

ll plot

+6

sapply (read? sapply),

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

:

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")
+2

Another solution using external:

llh <- function (teta,x) {
  X <- outer(x, teta, "-")
  Y <- log((1-cos(X))/(2*pi))
  apply(Y, 2, sum)
}

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)  

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

I agree with a spaced person that Vectorize is interesting!

0
source

All Articles