julia> using Random; Random.seed!(0); julia> expmodel(x, theta)= theta[1] * exp.(-x .* theta[2]); ## or use `@. expmodel(x, p) = p[1]*exp(-x*p[2])` julia> xdata= range(0; stop=10, length=20); trueparms= [1.0 2.0]; julia> ydata= expmodel(xdata, trueparms) + 0.1*randn(length(xdata)); ## the "noisy" data julia> # # # above was the data generation. now comes the loading and modification of LsqFit # # # julia> using Statistics, LsqFit; julia> import Base.summary ## we need a nicer summary function for the results julia> function summary( f::LsqFit.LsqFitResult{Float64,1}; sigdigits=3 ); if (!f.converged) println("optimizer did not converge!"); return; end (length(fit.wt)==0) || error("this summary does not yet work with weights") print("Degrees of Freedom: \$(f.dof). ") println("RMSE: \$(round(mean(f.resid.^2);sigdigits=sigdigits)). SDE: \$(round(std(f.resid);sigdigits=sigdigits)).") println("Parameter Estimates: \$(f.param)") end;##function## julia> rsq( f::LsqFit.LsqFitResult{Float64,1}, ydata::Vector{Float64}; sigdigits=3 )= 1.0 - mean( f.resid.^2 )/std(ydata); julia> # # # # now we fit the model and print the results of the fitting # # # # julia> fit= curve_fit( expmodel, xdata, ydata, [0.5, 0.5] ); julia> summary(fit) ## the parameter estimate is not [1,2] but [1.07,1.91] --- estimation error Degrees of Freedom: 18. RMSE: 0.00983. SDE: 0.102. Parameter Estimates: [1.07429, 1.90889] julia> rsq( fit, ydata ) 0.9638146161544147 julia> standard_error(fit) ## coefficient standard error 2-element Array{Float64,1}: 0.10357853033234828 0.406806155417925 julia> margin_error(fit, 1.0-0.95) ## coefficient 5% margin 2-element Array{Float64,1}: 0.2176104172710054 0.8546680180232399 julia> confidence_interval(fit, 0.05) ## plus or minus margin error 2-element Array{Tuple{Float64,Float64},1}: (0.8566787016414885, 1.2918995361834993) (1.0542196905114256, 2.7635557265579056) julia> yfitted= expmodel( xdata, fit.param ); julia> using Plots; julia> scatter( xdata, ydata, labels="actual" ) julia> plot!( xdata, yfitted, labels="fitted" ) julia> savefig("plotting/lsqfit.png")