# NOT RUN {
## Example from ?nls.
DNase1 <- subset(DNase, Run == 1)
fm3DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)),
data = DNase1, start = list(Asym = 3, xmid = 0, scal = 1))
## Using a single predictor value without error.
PROP1 <- predictNLS(fm3DNase1, newdata = data.frame(conc = 2))
PRED1 <- predict(fm3DNase1, newdata = data.frame(conc = 2))
PROP1$summary
PRED1
## => Prop.Mean.1 equal to PRED1
# }
# NOT RUN {
## Using a sequence of predictor values without error.
CONC <- seq(1, 12, by = 1)
PROP2 <- predictNLS(fm3DNase1, newdata = data.frame(conc = CONC))
PRED2 <- predict(fm3DNase1, newdata = data.frame(conc = CONC))
PROP2$summary
PRED2
## => Prop.Mean.1 equal to PRED2
## Using a sequence of predictor values with error.
DAT <- data.frame(conc = CONC, error = rnorm(12, 0, 0.1))
PROP3 <- predictNLS(fm3DNase1, newdata = DAT)
PRED3 <- predict(fm3DNase1, newdata = DAT)
PROP3$summary
PRED3
## => Prop.Mean.1 equal to PRED3
## Plot predicted and confidence values from
## first-/second-order Taylor expansion
## and Monte Carlo simulation.
plot(DNase1$conc, DNase1$density)
lines(DNase1$conc, fitted(fm3DNase1), lwd = 2, col = 1)
points(CONC, PROP2$summary[, 1], col = 2, pch = 16)
lines(CONC, PROP2$summary[, 5], col = 2)
lines(CONC, PROP2$summary[, 6], col = 2)
# }
# NOT RUN {
## Using multiple predictor values
## 1: Setup of response values
## with gaussian error of 10%.
x <- seq(1, 10, by = 0.01)
y <- seq(10, 1, by = -0.01)
a <- 2
b <- 5
c <- 10
z <- a * exp(b * x)^sin(y/c)
z <- z + sapply(z, function(x) rnorm(1, x, 0.10 * x))
## 2: Fit 'nls' model.
MOD <- nls(z ~ a * exp(b * x)^sin(y/c),
start = list(a = 2, b = 5, c = 10))
## 3: newdata without errors and prediction.
DAT1 <- data.frame(x = 4, y = 3)
PROP4 <- predictNLS(MOD, newdata = DAT1)
PROP4$summary
## 4: newdata with errors and prediction.
DAT2 <- data.frame(x = 4, y = 3, error.x = 0.2, error.y = 0.1)
PROP5 <- predictNLS(MOD, newdata = DAT2)
PROP5$summary
# }
Run the code above in your browser using DataLab