### Examples from 'nls' doc ###
DNase1 <- subset(DNase, Run == 1)
## using logistic formula
fm2DNase1 <- nls_tac(density ~ Asym/(1 + exp((xmid - log(conc))/scal)),
data = DNase1,
nlparam = list(xmid = c(1e-7,10), scal = c(1e-7,3)))
## some generics are applicable
coefficients(fm2DNase1)
summary(fm2DNase1)
## obtaining extra information
fm2DNase1$resid # residuals
fm2DNase1$formula # formula used
fm2DNase1$df # degrees of freedom
fm2DNase1$convInfo # Convergence information (n. iterations, tolerance attained)
fm2DNase1$SSR # SSR
fm2DNase1$data$density - fm2DNase1$resid # fitted values
## Synthetic examples
## Double exponential
x <- seq(from = 0, to = 20, length.out = 1000)
y <- 3*exp(-0.12*x) + 0.6*exp(-3.05*x) + 5 + 0.1*rnorm(length(x))
df <- data.frame(time = x, Temp = y)
# The nonlinear parameter list (with lower and upper values)
nlparam <- list(b1 = c(0,2), b2 = c(0,8))
fittac <- nls_tac('Temp ~ a1*exp(-b1*time) + a2*exp(-b2*time) + a3',
data = df,
nlparam = nlparam,
N = 5)
summary(fittac)
plot(Temp ~ time, data = df)
lines(x, predict(fittac), col = "red", lwd = 2)
##
N <- 100
x <- seq(from = 0, to = 3, length.out = N)
y <- 3*sin(5*x)^2 + 2 + 0.2*rnorm(N)
df <- data.frame(x = x, y = y)
form <- y ~ a1*sin(b1*x)^2 + a2
nlbnds <- list(b1 = c(0.5,10)) # rough bouds for tac
tac_model <- nls_tac(formula = form,
data = df,
nlparam = nlbnds,
N = 10,
tol = 1e-5)
yhat <- predict(tac_model)
plot(x,y)
lines(x,yhat, col = "blue")
Run the code above in your browser using DataLab