It is entirely for the purpose of plotting fits and intervals on a scatterplot (or histogram). It is a generic function to predict models for loon smooth layer (a wrap of the function predict). However, the output is unified.

l_predict(model, ...)

# S3 method for default
l_predict(model, ...)

# S3 method for lm
l_predict(
  model,
  newdata = NULL,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  ...
)

# S3 method for nls
l_predict(
  model,
  newdata = NULL,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  ...
)

# S3 method for glm
l_predict(
  model,
  newdata = NULL,
  interval = c("none", "confidence"),
  level = 0.95,
  ...
)

# S3 method for loess
l_predict(
  model,
  newdata = NULL,
  interval = c("none", "confidence", "prediction"),
  level = 0.95,
  ...
)

Arguments

model

a model object for which prediction is desired

...

arguments passed in predict

newdata

optionally, a data frame in which to look for variables with which to predict. If omitted, the fitted linear predictors are used.

interval

type of interval, could be "none", "confidence" or "prediction" (not for glm)

level

confidence level

Value

A data frame is returned with x (if newdata is given) and y. If the interval is not none, two more columns, lower (lower interval) and upper (upper interval) will be returned.

Examples

y <- rnorm(10) x <- rnorm(10) model1 <- lm(y ~ x) # formal output pre <- l_predict(model1, newdata = data.frame(x = sort(x)), interval = "conf") head(pre)
#> x y lower upper #> 1 -1.47615111 -0.193835067 -1.7666154 1.3789453 #> 2 -1.26047622 -0.159577084 -1.5549521 1.2357979 #> 3 -0.82054972 -0.089698785 -1.1650372 0.9856396 #> 4 -0.22545194 0.004827053 -0.8269545 0.8366086 #> 5 -0.11465603 0.022425969 -0.8035391 0.8483910 #> 6 -0.08441358 0.027229706 -0.7996783 0.8541377
if(interactive()) { p <- with(cars, l_plot(speed, dist)) # Example taken from # https://stackoverflow.com/questions/23852505/how-to-get-confidence-interval-for-smooth-spline # l_predict.smooth.spline <- function(model, interval = c("confidence", "none"), level = 0.95, ...) { # confidence interval of `smooth.spline` interval <- match.arg(interval) res <- (model$yin - model$y)/(1 - model$lev) # jackknife residuals sigma <- sqrt(var(res)) # estimate sd std <- stats::qnorm(level / 2 + 0.5) upper <- model$y + std * sigma * sqrt(model$lev) # upper 95% conf. band lower <- model$y - std * sigma * sqrt(model$lev) # lower 95% conf. band data.frame(y = model$yin, lower = lower, upper = upper) } l <- l_layer_smooth(p, method = "smooth.spline", interval = "confidence") }