I am trying to fit a normal curve to a series of x,y coordinates found in an R dataframe. My goal is to find the best-fitting normal curve an record the mean and sd. I am trying to replicate the results from a paper, so it needs to be done using the BFGS algorithm with the mle2 function:
https://www.rdocumentation.org/packages/bbmle/versions/1.0.23.1/topics/mle2
sample data:
x <- seq(0, 10, by = .1)
y <- dnorm(x, mean = 5, sd = 1.5)
df <- cbind(as.data.frame(x), as.data.frame(y))
My understanding is I need to enter in the function for a normal distribution and then optimize the parameters:
bell_curve <- function(amp, mean, sd) {
y = amp*(1/(sd*(sqrt(2*pi))))*(exp((-1.0/2.0)*(((x-mean)/sd)**2)))
}
I am getting confused on how to actually construct the mle2() command. In the past I have only used it to optimize two parameters over one data series, and I am not sure how to include both the x and the y data. I have messed around with it for a few days and searched the forums but I cannot find any answers that I can relate to my problem.
I have tried:
results <- mle2(bell_curve, start = c(amp=8, mean=5, sd = 2.5), method = 'BFGS', data = df)
but I get:
Error in mle2(bell_curve, start = c(amp = 8, mean = 5, sd = 2.5), method = "BFGS", :
some named arguments in 'start' are not arguments to the specified log-likelihood function
I am pretty lost in this depth of statistical analysis, any help would be appreciated.
Thanks