I generate a sample of size 1000 consisting of realizations of independent N(4, 1) distributed random variables:
n <- 1000
x <- rnorm(n, mean = 4, sd = 1)
I give the density for this sample depending on the parametres (\$\mu\$, \$\sigma\$) \$\in\$ \$\mathbb{R}\$ x \$\mathbb{R}\$+ standing for the mean and standard deviation of the normal density.
generate <- function(mu, sigma,n)
{
x <- rnorm(n,mu,sigma)
plot(hist(x),breaks=30)
}
I apply a numerical maximization procedure to determine a maximum likelihood estimator for the unknown parameters:
estimateparameters <- function(mu, sigma,n)
{
a = dnorm(x, mu, sigma)
-sum(log(a))
mle(likelihood, start = list(mu = 1, sigma=1), method = "L-BFGS-B", lower =c(-Inf, 0),upper = c(Inf, Inf))
}
What do you think about what I have done?