1

Hi I have received an error of x and y length differ in my code. datasim is a simulated sample of size 1000. Please help me with this.

x <- datasim
loglik <- function(theta){
  k<- theta[1]
  lambda<- theta[2]
  out <- sum(dweibull(x,shape = k, scale=lambda, log = TRUE) )
  return(out)
}

theta<- c(0.5,1.5)
plot(theta, loglik(theta), type="l", lwd=3, main="logliklihood_Weibull, n=1000")

siegfried
  • 461
  • 1
  • 5
  • 20
  • In order to make your question reproducible and thus answerable, we need minimal, self-contained code and data so that we are able to reproduce your problem on our machine, please follow these simple guidelines: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610. – jay.sf May 23 '20 at 08:45
  • In your shown case, your function takes a vector of length 2 and returns a vector of length 1 since the summation gives you just one value. Therefore by using `plot(theta, loglik(theta), type="l", lwd=3, main="logliklihood_Weibull, n=1000")` you are trying to plot x of length 2 (`theta`) against y of length 1 (`loglik(theta)`). – Martin Gal May 23 '20 at 09:06
  • `theta` is a vector of length 2, so it's better to draw a 3-D plot of a surface over the x-y plane, which x-axis is the shape parameter and y-axis is the scale parameter of Weibull distribution. In this way, you can easily observe the MLE. – Darren Tsai May 23 '20 at 09:13
  • @MartinGal oh thx for the tip. I had a similar example on using optim() to find mles of a 2-parameter dsn with loglik. So I was not aware of the dimension discrepancy. – siegfried May 23 '20 at 11:37

1 Answers1

2

You can use a package since it's weibull you are fitting:

library(fitdistrplus)
x = rweibull(1000,20,10)
fit <- fitdist(x, "weibull")
llplot(fitg, expand=5)

enter image description here

Or using your loglik function:

library(lattice)

da = expand.grid(k=seq(8,22,length.out=50),
lambda = seq(9,12,length.out=50))
da$LL = apply(da,1,loglik)

wireframe(LL ~ k * lambda, data = da,
scales = list(arrows = FALSE),drape = TRUE, colorkey = TRUE)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72