R function to find suitable values for fitting constants
问题 library(ggplot2) set.seed(1) dataset <- data.frame(X = rnorm(1000)) dfun <- function(x, a, b) 1/(sqrt(2*pi)*b)*exp(-0.5*((x-a)^2/(2*b^2))) ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..), binwidth = 0.5)+ stat_function(fun = dfun, args = list(a = , b = )) How can I calculate suitable values of a and b in case like this? 回答1: You can compute values for the arguments a and b with nls . Something like the following. dens <- density(dataset$X, n = nrow(dataset)) df_dens <- data