# [R] Functions in formulae ??

Jeremy Clark jeremyclarkmel at gmail.com
Thu Nov 21 11:57:29 CET 2013

```Dear All,

In the following simple case I can't seem to get an improved fit,
despite trying all of the control possibilities. As there seem to be
no examples anywhere which show use of functions such as "dnorm"
within a formula, and as I am not confident at all that my formula is
correctly configured, I would appreciate some feedback ! Many thanks

library(minpack.lm)

gg<-c(170,        171,      172,      173,      174,      175,
176,      177,      178,      179,      180,      181,      182,
183,      184,            185,      186,      187,      188,      189,
190,      191,      192,      193,      194,      195,      196,
197,      198,      199,            200,      201,      202,
203,      204,      205,      206,      207,      208,      209)

dd<-c(1673,      1659,    1738,    1687,    1882,    2010,    2202,
2248,    2409,    2417,    2215,    2279,    2539,    2479,    2341,
2395,    2314,    2404,    2369,    2254,    2048,    1899,
1892,    1744,    1333,    1183,    982,      772,      526,
451,            335,      253,      157,      108,      67,        44,
26,        12,        10,        0)

dd100000 <- dd * 0.00001

dd100000gg <- data.frame(dd100000, gg)

## modLM4:

modLM4 <- nlsLM(dd100000 ~ dnorm(gg, mu,sigma, log = FALSE),  data =
dd100000gg, start = c(mu = 190, sigma = 10), trace = TRUE)

windows()
## plot data
plot(gg, dd100000, main = "modLM4")
## plot fitted values
lines(gg, fitted(modLM4), col = 2, lwd = 2)

```