Get predictions with standard errors into data frame for use with geom_smooth

# S3 method for nls
predictdf(model, xseq, se, level)



nls object




Display confidence interval around smooth?


Level of confidence interval to use


dataframe with x and y values, if se is TRUE dataframe also includes ymin and ymax


ggplot2::geom_smooth produces confidence intervals by silently calling functions of the form predictdf.method, where method is "loess", "lm", "glm" etc. depending on what method is specified in the call to geom_smooth. Currently ggplot2 does not define a predictdf.nls function for method of type "nls", and thus confidence intervals cannot be automatically generated by geom_smooth for method = "nls". Here we define predictdf.nls for calculating the confidence intervals of an object of type nls. geom_smooth will silently call this function whenever method = "nls", and produce the appropriate confidence intervals.

predictdf.nls calculates CI for a model fit of class nls based on the "delta-method"

CI = [ f(x0, beta) + qt_(alpha/2, n - d) * se(f(x0, beta)), f(x0, beta) + qt_(1 - alpha/2, n - d) * se(f(x0, beta))]

where: beta = vector of parameter estimates x = independent variable se(f(x0, beta)) = sqrt( delta(f)(x0, beta) * Var(beta) * (delta(f)(x0, beta))' ) delta(f) is the gradient of f