bgreenwell / investr

Inverse estimation in R
22 stars 6 forks source link

plotFit with `log = "x"` can give weird results #27

Closed bgreenwell closed 8 years ago

bgreenwell commented 8 years ago

Example:

# Grab data
data(Puromycin, package = "datasets")
Puromycin2 <- Puromycin[Puromycin$state == "treated", ][, 1:2]

# Fit NLS model
Puro.nls <- nls(rate ~ Vm * conc/(K + conc), data = Puromycin2,
                start = c(Vm = 200, K = 0.05))

# Plot fitted mean response with 0.95 confidence/prediction bands
plotFit(Puro.nls, interval = "both", pch = 19, shade = TRUE, 
        extend.range = TRUE,
        log = "x",
        col.conf = "skyblue4", col.pred = "lightskyblue2")

Should be fixed by taking the log of x before extending the range.

bgreenwell commented 8 years ago
  # Check if logged x-axis is required
  dots <- list(...)
  logx <- FALSE
  if ("log" %in% names(dots)) {
    if (grepl("x", dots$log)) {
      logx <- TRUE
    }
  } 

  # Plot limits, labels, etc.
  if (missing(xlim)) {
    xlim <- range(xvals) # default limits for x-axis
  }
  ulim <- if (logx) {
    log(xlim) 
  } else {
    xlim
  }
  if (extend.range) {
    ulim <- extendrange(ulim)
  }
  xgrid <- if (logx) {
    list(exp(seq(from = ulim[1L], to = ulim[2L], length = n)))
  } else {
    list(seq(from = ulim[1L], to = ulim[2L], length = n))
  }