Hi!
I wonder why I cant create curve from given data from normal distribution and created function. I could make mistake calculating hes and grad, but the outcome is very close to mean. I use "maxLik" package.
Thanks for help,
regards
x = c(0.5677523, 0.8236657, 0.4568736, 0.5125185, 0.3192458)
N = length(x)
sigma2 = 1
sd = sqrt(1)
options(scipen=999)
lnL = function(mu){
l = -(N/2)*log(2*pi)-N*log(sd)-sum(x-mu)^2/2*sigma2
return(l)
}
grad = function(mu){
g = 1/sigma2*sum(x)-N*mu
return(g)
}
hes = function(mu){
h = N
}
solution= maxNR(fn=lnL, grad=grad, hess=hes, start = 0)
summary(solution)
curve(lnL(x), from=0, to= 0.8)
abline(v=solution$estimate, col="red")
(mu = sum(x)/N)
?curve
expr
The name of a function, or a call or an expression written as a function of x which will evaluate to an object of the same length as x.
your lnL function, sums over the mu vector you pass it, so reduces a length (n) input vector to a single solution, therefore its not suitable to be used by curve, which requires the function calculate a return value for each element of input vector x.
Now you could just duplicate the answer length n times and return that vector, though I would think this would probably not give you the curves you want, (even though I have little idea what curve you want)