rp.nls: Interactive Fitting for Non Linear Regression Models

Description Usage Arguments Value Author(s) See Also Examples

View source: R/rp.nls.R

Description

This function opens a interface, builted with rpanel elements, designed for non linear regression models fitting. Sliders allows the user control values used as start values in the nls() call, checkbox allows select strata of the data to fit separed curves and more options are available.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
rp.nls(
  model,
  data,
  start,
  subset = NULL,
  xlab = NULL,
  ylab = NULL,
  ...,
  gpar = list(try = list(col = 2, lty = 2), fit = list(col = 2, lwd = 1.5)),
  extra = NULL
)

Arguments

model

a non linear model formula including variables and parameters to passed to nls() function.

data

a data frame with dependent and independent variables present in the model formula.

start

a named list with one item for each parameter. Each item must vector with 2 (from, to) or 3 values (from, to, init) for the init, from and to. When two values are provided, the third is the average of them. Values are sorted internally to match the initial, minimum and maximum to be used by rp.slider().

subset

an optional string indicating a grouping variable to fit separeted curves for each level. It must be a factor.

xlab

labels for the x axis.

ylab

labels for the y axis.

...

arguments passed to the plot function used to draw the lines function to draw the curve at the start values. The most used arguments are lty, col and lwd.

gpar

a named list with graphical parameters for curve. The "try" element is a list for the eye curve that is manipulated with sliders. The "fit" is a list for the fitted curve.

extra

a function of the model parameters that draw auxiliary graphical elements. For example, draw vertical and horizontal lines as references for model intercept and/or model asymptote.

Value

In fact none is returned by the function. There isn't a return, instead there is a invisible at the end. On the other hand, the model fit is assigned to the object with name passed to the assign argument.

Author(s)

Walmes Zeviani, walmes@ufpr.br.

See Also

nls(), lines(), rp.slider()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
## Not run: 

#--------------------------------------------
# A simple linear regression.

# The model below is a linear model. You should fit in lm().

library(rpanel)

cars.fit <- rp.nls(model = dist ~ b0 + b1 * speed, data = cars,
                   start = list(b0 = c(-20, 20),
                                b1 = c(0, 2, 10)),
                   xlab = "Speed", ylab = "Distance")

summary(cars.fit)
confint(cars.fit)

f <- formula(cars.fit)

plot(dist ~ speed, data = cars)
with(as.list(coef(cars.fit)), {
    eval(call("curve", f[[3]], add = TRUE,
              xname = intersect(all.vars(f[-2]), names(cars))))
})

#--------------------------------------------
# A non linear regression.

data(turk0, package = "alr3")
str(turk0)

plot(Gain ~ A, data = turk0,
     xlab = "Metionine", ylab = "Weight gain")

turk.fit <- rp.nls(model = Gain ~ Int + (Asy - Int) * A/(Half + A),
                   data = turk0,
                   start = list(Int = c(600, 650),
                                Asy = c(750, 850),
                                Half = c(0, 0.2)),
                   extra = function(Int, Asy, Half) {
                       abline(h = c(Asy, Int), v = Half,
                              col = "green")
                   },
                   xlab = "Metionine", ylab = "Weight gain")

summary(turk.fit)
confint(turk.fit)

f <- formula(turk.fit)

plot(Gain ~ A, data = turk0,
     xlab = "Metionine", ylab = "Weight gain")
with(as.list(coef(turk.fit)), {
    eval(call("curve", f[[3]], add = TRUE,
              xname = intersect(all.vars(f[-2]), names(turk0))))
})

#--------------------------------------------
# A more interesting example.

library(lattice)

xyplot(rate ~ conc, groups = state, data = Puromycin,
       type = c("p", "smooth"), auto.key = TRUE)

Puro.fit <- rp.nls(model = rate ~
                       Int + (Top - Int) * conc/(Half + conc),
                   data = Puromycin,
                   start = list(Int = c(20, 70),
                                Top = c(100, 200),
                                Half = c(0, 0.6)),
                   subset = "state",
                   gpar = list(try = list(col = 2, lty = 2),
                               fit = list(col = "blue", lwd = 1.5)),
#                  extra = function(Int, Top, Half) {
#                      abline(h = c(Int, Top), v = Half,
#                             col = 2, lty = 2)
#                  },
                   xlab = "Concentration", ylab = "Rate",
                   xlim = c(0, 1.2), ylim = c(40, 220))

length(Puro.fit)
sapply(Puro.fit, coef)
sapply(Puro.fit, logLik)
sapply(Puro.fit, deviance)


## End(Not run)

walmes/wzRfun documentation built on Aug. 10, 2021, 2:19 p.m.