
require(nlraa)
require(ggplot2)
##library(agridat)

if(FALSE){
  
  data("hernandez.nitrogen")

  hn <- hernandez.nitrogen
  
  ggplot(data = hn, aes(x = nitro, y = yield)) + 
    facet_wrap(~loc) + 
    geom_point()
  
  fm1 <- lm(yield ~ nitro + I(nitro^2), data = hn)
  fm2 <- nls(yield ~ SSlinp(nitro, a, b, xs), data = hn)
  fm3 <- nls(yield ~ SSquadp3xs(nitro, a, b, xs), data = hn)
  fm4 <- nls(yield ~ SSquadp(nitro, a, b, c, xs), data = hn)
  fm5 <- nls(yield ~ SSquadpq(nitro, a, b, xs, ldxs = 4.2),
             start = list(a = 7, b = 0.05, xs = 200),
             data = hn)
  
  ## This does not work
  ## fm6 <- minpack.lm::nlsLM(yield ~ SSquadpq(nitro, a, b, xs, ldxs), data = hn)
  
  ## Interestingly they all fit better than the quadratic
  IC_tab(fm1, fm2, fm3, fm4, fm5)
  
  ggplot(data = hn, aes(x = nitro, y = yield)) + 
    geom_point() + 
    geom_line(aes(y = fitted(fm1), color = "Q")) + 
    geom_line(aes(y = fitted(fm2), color = "LP")) + 
    geom_line(aes(y = fitted(fm3), color = "QP3-xs")) + 
    geom_line(aes(y = fitted(fm4), color = "QP")) + 
    geom_line(aes(y = fitted(fm5), color = "QPQ"))
  
}

